2 Flashcards

1
Q

Task

A

Answer and Description

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Sort a list of integers in descending order.

A

{‘sorted_list = my_list.sort(reverse=True)’, description:’Use the sort() method with the reverse parameter.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a program to find the largest number in a list.

A

{‘largest = max(my_list)’, description:’Iterate through a list to find the largest element.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Create a named tuple to store information about a book (title, author, year).

A

{‘from collections import namedtuple\nBook = namedtuple(“Book”, [“title”, “author”, “year”])’, description:’Use collections.namedtuple to define a simple class-like structure.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a program to find the common elements between two lists.

A

{‘common_elements = list(set(list1) & set(list2))’, description:’Find and print the common elements in two lists.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Use the ‘with’ statement to write ‘Hello, World!’ to a file.

A

{‘with open(“hello.txt”, “w”) as file:\n file.write(“Hello, World!”)’, description:’Write to a file using the “with” statement to manage the file context.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Create a class with a private attribute and a method to access it.

A

{‘class MyClass:\n def __init__(self):\n self.__private_attr = “private”\n def get_private_attr(self):\n return self.__private_attr’, description:’Define a private attribute with double underscores and provide a public method.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a decorator that prints ‘Function called’ every time a function is called.

A

{‘def my_decorator(func):\n def wrapper(args, **kwargs):\n print(“Function called”)\n return func(args, **kwargs)\n return wrapper’, description:’Define a decorator function that wraps another function.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a function that takes a string and returns it in reverse.

A

{‘def reverse_string(s):\n return s[::-1]’, description:’Reverse a string using slicing or a loop.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Create a list of numbers from 1 to 10 using range() and print it.

A

{‘numbers = list(range(1, 11))’, description:’Generate a list using range() and convert it to a list.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a program that converts a list of Celsius temperatures to Fahrenheit.

A

{‘fahrenheit = [((9/5)*temp + 32) for temp in celsius]’, description:’Apply a formula to convert Celsius to Fahrenheit for each list item.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Create a class that inherits from another class and overrides a method.

A

{‘class BaseClass:\n def my_method(self):\n return “Base method”\nclass SubClass(BaseClass):\n def my_method(self):\n return “Overridden method”’, description:’Create a subclass and override one of its methods.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write a program that prints the Fibonacci sequence up to n terms.

A

{‘def fibonacci(n):\n a, b = 0, 1\n for _ in range(n):\n print(a)\n a, b = b, a + b’, description:’Generate Fibonacci numbers up to a specified count.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Use the timeit module to measure the execution time of a function.

A

{‘import timeit\ntimeit.timeit(“my_function()”, setup=”from __main__ import my_function”)’, description:’Use the timeit module to time a simple function’s execution.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Create a function that takes any number of arguments and prints them.

A

{‘def print_args(*args):\n for arg in args:\n print(arg)’, description:’Use *args to accept a variable number of arguments in a function.’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write a program to calculate the sum of all even numbers in a list.

A

{‘sum_even = sum([num for num in my_list if num % 2 == 0])’, description:’Sum all even numbers in a list using a loop or comprehension.’}

17
Q

Create a function that returns the maximum of three numbers.

A

{‘def max_of_three(a, b, c):\n return max(a, b, c)’, description:’Compare three numbers and return the largest one.’}

18
Q

Write a program that removes all vowels from a given string.

A

{‘def remove_vowels(s):\n return ‘‘.join([char for char in s if char not in “aeiouAEIOU”])’, description:’Remove vowels from a string using a loop or comprehension.’}

19
Q

Create a dictionary from two lists using zip().

A

{‘my_dict = dict(zip(keys, values))’, description:’Combine two lists into a dictionary using the zip() function.’}

20
Q

Write a function that checks if a number is prime.

A

{‘def is_prime(n):\n if n <= 1:\n return False\n for i in range(2, int(n**0.5) + 1):\n if n % i == 0:\n return False\n return True’, description:’Check divisibility from 2 to sqrt(n) to determine primality.’}

21
Q

Create a class with a constructor that initializes an attribute.

A

{‘class MyClass:\n def __init__(self, attribute):\n self.attribute = attribute’, description:’Use __init__ to initialize an object’s attributes.’}

22
Q

Write a program that finds the second largest number in a list.

A

{‘second_largest = sorted(my_list)[-2]’, description:’Sort a list and find the second-to-last item.’}

23
Q

Use the collections.Counter to count the occurrences of elements in a list.

A

{‘from collections import Counter\nelement_counts = Counter(my_list)’, description:’Use collections.Counter to count how often each item appears.’}

24
Q

Write a program that finds the longest word in a sentence.

A

{‘def find_longest_word(sentence):\n words = sentence.split()\n return max(words, key=len)’, description:’Split a sentence into words and find the longest one.’}

25
Q

Create a function that returns the sum of digits of a number.

A

{‘def sum_of_digits(n):\n return sum(int(digit) for digit in str(n))’, description:’Sum the digits of a number using a loop or recursion.’}

26
Q

Write a program that sorts a list of tuples by the second element.

A

{‘sorted_tuples = sorted(my_list, key=lambda x: x[1])’, description:’Sort a list of tuples by the second value of each tuple.’}

27
Q

Create a function that finds the greatest common divisor (GCD) of two numbers.

A

{‘def gcd(a, b):\n while b:\n a, b = b, a % b\n return a’, description:’Use the Euclidean algorithm to find the GCD of two numbers.’}

28
Q

Write a program that checks if two strings are anagrams.

A

{‘def are_anagrams(str1, str2):\n return sorted(str1) == sorted(str2)’, description:’Sort and compare characters of both strings.’}