1 Flashcards
Task
Answer
Create a variable with your name and print it.
{‘name = “Your Name”’, ‘description’: ‘Declare a string variable and use the print() function.’}
Write a function that returns the square of a number.
{‘def square(x): return x**2’, ‘description’: ‘Define a function with one parameter and return the squared value.’}
Create a list of your favorite fruits and print the third item.
{‘fruits = [“apple”, “banana”, “cherry”]\nprint(fruits[2])’, ‘description’: ‘Initialize a list with at least three items and print the item at index 2.’}
Write a program to check if a number is even or odd.
{‘number = 10\nif number % 2 == 0:\n print(“Even”)\nelse:\n print(“Odd”)’, ‘description’: ‘Use an if-else statement to determine if a number is divisible by 2.’}
Create a dictionary with keys as fruits and values as their colors.
{‘fruit_colors = {“apple”: “red”, “banana”: “yellow”, “cherry”: “red”}’, ‘description’: ‘Create a dictionary and print it.’}
Write a program that reads a file and prints its contents.
{‘with open(“file.txt”, “r”) as file:\n print(file.read())’, ‘description’: ‘Use open() to read a file and print its content.’}
Create a class called ‘Animal’ with a method that prints ‘I am an animal’.
{‘class Animal:\n def speak(self):\n print(“I am an animal”)\n\nanimal = Animal()\nanimal.speak()’, ‘description’: ‘Define a class with a method and create an instance to call the method.’}
Write a function that takes a list and returns a new list with unique elements.
{‘def unique_elements(lst):\n return list(set(lst))’, ‘description’: ‘Remove duplicates from a list and return the unique items.’}
Write a program to handle division by zero using try/except.
{‘try:\n result = 10 / 0\nexcept ZeroDivisionError:\n print(“Cannot divide by zero”)’, ‘description’: ‘Use try/except to handle potential division by zero errors.’}
Create a virtual environment and install a package in it.
{‘python -m venv myenv\nsource myenv/bin/activate\npip install package_name’, ‘description’: ‘Set up a virtual environment and use pip to install a package.’}
Write a unit test for a function that adds two numbers.
{‘import unittest\n\ndef add(a, b):\n return a + b\n\nclass TestAdd(unittest.TestCase):\n def test_add(self):\n self.assertEqual(add(2, 3), 5)\n\nif __name__ == “__main__”:\n unittest.main()’, ‘description’: ‘Write a test case using the unittest module.’}
Format a string to include your name and age using f-strings.
{‘name = “John”\nage = 30\nprint(f”My name is {name} and I am {age} years old”)’, ‘description’: ‘Use f-strings to embed variables in a string.’}
Write a program that counts the occurrences of each word in a given text.
{‘text = “this is a test. this is only a test.”\nwords = text.split()\nword_count = {}\nfor word in words:\n if word in word_count:\n word_count[word] += 1\n else:\n word_count[word] = 1\nprint(word_count)’, ‘description’: ‘Read text, split into words, and count each word's occurrences.’}
Use a set to remove duplicates from a list.
{‘my_list = [1, 2, 2, 3, 4, 4, 5]\nunique_list = list(set(my_list))\nprint(unique_list)’, ‘description’: ‘Convert a list to a set to remove duplicates.’}