2 Flashcards
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.’}