2 Flashcards
(28 cards)
Task
Answer and Description
Sort a list of integers in descending order.
{‘sorted_list = my_list.sort(reverse=True)’, description:’Use the sort() method with the reverse parameter.’}
Write a program to find the largest number in a list.
{‘largest = max(my_list)’, description:’Iterate through a list to find the largest element.’}
Create a named tuple to store information about a book (title, author, year).
{‘from collections import namedtuple\nBook = namedtuple(“Book”, [“title”, “author”, “year”])’, description:’Use collections.namedtuple to define a simple class-like structure.’}
Write a program to find the common elements between two lists.
{‘common_elements = list(set(list1) & set(list2))’, description:’Find and print the common elements in two lists.’}
Use the ‘with’ statement to write ‘Hello, World!’ to a file.
{‘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.’}
Create a class with a private attribute and a method to access it.
{‘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.’}
Write a decorator that prints ‘Function called’ every time a function is called.
{‘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.’}
Write a function that takes a string and returns it in reverse.
{‘def reverse_string(s):\n return s[::-1]’, description:’Reverse a string using slicing or a loop.’}
Create a list of numbers from 1 to 10 using range() and print it.
{‘numbers = list(range(1, 11))’, description:’Generate a list using range() and convert it to a list.’}
Write a program that converts a list of Celsius temperatures to Fahrenheit.
{‘fahrenheit = [((9/5)*temp + 32) for temp in celsius]’, description:’Apply a formula to convert Celsius to Fahrenheit for each list item.’}
Create a class that inherits from another class and overrides a method.
{‘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.’}
Write a program that prints the Fibonacci sequence up to n terms.
{‘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.’}
Use the timeit module to measure the execution time of a function.
{‘import timeit\ntimeit.timeit(“my_function()”, setup=”from __main__ import my_function”)’, description:’Use the timeit module to time a simple function’s execution.’}
Create a function that takes any number of arguments and prints them.
{‘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.’}
Write a program to calculate the sum of all even numbers in a list.
{‘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.’}
Create a function that returns the maximum of three numbers.
{‘def max_of_three(a, b, c):\n return max(a, b, c)’, description:’Compare three numbers and return the largest one.’}
Write a program that removes all vowels from a given string.
{‘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.’}
Create a dictionary from two lists using zip().
{‘my_dict = dict(zip(keys, values))’, description:’Combine two lists into a dictionary using the zip() function.’}
Write a function that checks if a number is prime.
{‘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.’}
Create a class with a constructor that initializes an attribute.
{‘class MyClass:\n def __init__(self, attribute):\n self.attribute = attribute’, description:’Use __init__ to initialize an object’s attributes.’}
Write a program that finds the second largest number in a list.
{‘second_largest = sorted(my_list)[-2]’, description:’Sort a list and find the second-to-last item.’}
Use the collections.Counter to count the occurrences of elements in a list.
{‘from collections import Counter\nelement_counts = Counter(my_list)’, description:’Use collections.Counter to count how often each item appears.’}
Write a program that finds the longest word in a sentence.
{‘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.’}