1 Flashcards

1
Q

Task

A

Answer

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

Create a variable with your name and print it.

A

{‘name = “Your Name”’, ‘description’: ‘Declare a string variable and use the print() function.’}

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

Write a function that returns the square of a number.

A

{‘def square(x): return x**2’, ‘description’: ‘Define a function with one parameter and return the squared value.’}

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

Create a list of your favorite fruits and print the third item.

A

{‘fruits = [“apple”, “banana”, “cherry”]\nprint(fruits[2])’, ‘description’: ‘Initialize a list with at least three items and print the item at index 2.’}

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

Write a program to check if a number is even or odd.

A

{‘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.’}

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

Create a dictionary with keys as fruits and values as their colors.

A

{‘fruit_colors = {“apple”: “red”, “banana”: “yellow”, “cherry”: “red”}’, ‘description’: ‘Create a dictionary and print it.’}

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

Write a program that reads a file and prints its contents.

A

{‘with open(“file.txt”, “r”) as file:\n print(file.read())’, ‘description’: ‘Use open() to read a file and print its content.’}

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

Create a class called ‘Animal’ with a method that prints ‘I am an animal’.

A

{‘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.’}

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

Write a function that takes a list and returns a new list with unique elements.

A

{‘def unique_elements(lst):\n return list(set(lst))’, ‘description’: ‘Remove duplicates from a list and return the unique items.’}

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

Write a program to handle division by zero using try/except.

A

{‘try:\n result = 10 / 0\nexcept ZeroDivisionError:\n print(“Cannot divide by zero”)’, ‘description’: ‘Use try/except to handle potential division by zero errors.’}

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

Create a virtual environment and install a package in it.

A

{‘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.’}

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

Write a unit test for a function that adds two numbers.

A

{‘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.’}

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

Format a string to include your name and age using f-strings.

A

{‘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.’}

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

Write a program that counts the occurrences of each word in a given text.

A

{‘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.’}

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

Use a set to remove duplicates from a list.

A

{‘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.’}

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

Write a function that takes a list and returns a list of squares of the numbers.

A

{‘def square_list(lst):\n return [x**2 for x in lst]’, ‘description’: ‘Use a loop or list comprehension to square each number in a list.’}

17
Q

Create a tuple and print its elements using a for loop.

A

{‘my_tuple = (“apple”, “banana”, “cherry”)\nfor item in my_tuple:\n print(item)’, ‘description’: ‘Iterate over a tuple using a for loop.’}

18
Q

Write a program that reads a CSV file and prints each row.

A

{‘import csv\n\nwith open(“file.csv”, “r”) as file:\n reader = csv.reader(file)\n for row in reader:\n print(row)’, ‘description’: ‘Use the csv module to read and print rows from a CSV file.’}

19
Q

Create a generator that yields even numbers up to 10.

A

{‘def even_numbers():\n for number in range(2, 11, 2):\n yield number’, ‘description’: ‘Define a generator function with the yield keyword.’}

20
Q

Write a function that returns the factorial of a number.

A

{‘def factorial(n):\n if n == 0:\n return 1\n else:\n return n * factorial(n-1)’, ‘description’: ‘Calculate the factorial using a loop or recursion.’}

21
Q

Create a list comprehension to generate a list of squares of numbers from 1 to 10.

A

{‘[x**2 for x in range(1, 11)]’, ‘description’: ‘Use list comprehension to generate a new list.’}

22
Q

Write a program that checks if a string is a palindrome.

A

{‘def is_palindrome(s):\n return s == s[::-1]’, ‘description’: ‘Check if a string reads the same forwards and backwards.’}

23
Q

Create a dictionary with default values using defaultdict.

A

{‘from collections import defaultdict\nd = defaultdict(int)’, ‘description’: ‘Use collections.defaultdict to create a dictionary with default values.’}

24
Q

Write a program that merges two dictionaries.

A

{‘dict1 = {“a”: 1, “b”: 2}\ndict2 = {“b”: 3, “c”: 4}\ndict1.update(dict2)’, ‘description’: ‘Combine two dictionaries into one.’}

25
Q

Sort a list of integers in descending order.

A

{‘my_list = [4, 2, 9, 1]\nmy_list.sort(reverse=True)’, ‘description’: ‘Use the sort() method with the reverse parameter.’}

26
Q

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

A

{‘my_list = [10, 20, 4, 45, 99]\nmax_value = max(my_list)\nprint(max_value)’, ‘description’: ‘Iterate through a list to find the largest element.’}