4 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

Create a variable with your name and print it.

A

name = “Your Name”\nprint(name) @@@ Use meaningful variable names and print to display the output.

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(number):\n return number**2 @@@ 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]) @@@ Use list indexing to access elements.

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 = 5\nif number % 2 == 0:\n print(“Even”)\nelse:\n print(“Odd”) @@@ Use if-else statement to check conditions.

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”} @@@ Create and use dictionaries for key-value pairs.

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 content = file.read()\n print(content) @@@ Use with statement for file handling.

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”) @@@ Define a class and a 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)) @@@ Convert list to set and back to list to remove duplicates.

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”) @@@ Use try/except to handle exceptions.

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 @@@ Use virtual environments to manage dependencies.

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\ndef add(a, b):\n return a + b\nclass TestAddFunction(unittest.TestCase):\n def test_add(self):\n self.assertEqual(add(2, 3), 5) @@@ Write unit tests to verify code correctness.

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.”) @@@ Use f-strings for string formatting.

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 = “hello world”\nword_counts = {}\nfor word in text.split():\n word_counts[word] = word_counts.get(word, 0) + 1 @@@ Use dictionary to count word 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

numbers = [1, 2, 2, 3, 4, 4]\nunique_numbers = list(set(numbers)) @@@ Use set to remove duplicates from a list.

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] @@@ Use list comprehension to generate a list.

17
Q

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

A

my_tuple = (1, 2, 3)\nfor item in my_tuple:\n print(item) @@@ Iterate through a tuple using a for loop.

18
Q

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

A

import csv\nwith open(“file.csv”, “r”) as file:\n reader = csv.reader(file)\n for row in reader:\n print(row) @@@ Use csv module to read files.

19
Q

Create a generator that yields even numbers up to 10.

A

def even_numbers():\n for i in range(11):\n if i % 2 == 0:\n yield i @@@ Use yield to create a generator.

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) @@@ Use recursion to calculate factorial.

21
Q

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

A

squares = [x**2 for x in range(1, 11)] @@@ Use list comprehension for concise loops.

22
Q

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

A

def is_palindrome(s):\n return s == s[::-1] @@@ 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) @@@ Use defaultdict for handling missing keys.

24
Q

Write a program that merges two dictionaries.

A

dict1 = {“a”: 1, “b”: 2}\ndict2 = {“b”: 3, “c”: 4}\nmerged_dict = {**dict1, **dict2} @@@ Merge dictionaries using unpacking operator.

25
Q

Sort a list of integers in descending order.

A

my_list.sort(reverse=True) @@@ Use the sort() method with the reverse parameter.

26
Q

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

A

largest = max(my_list) @@@ Use max() to find the largest element.

27
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”]) @@@ Use collections.namedtuple to define a simple class-like structure.

28
Q

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

A

common_elements = list(set(list1) & set(list2)) @@@ Use set intersection to find common elements.

29
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!”) @@@ Use with statement for file handling.

30
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 @@@ Define a private attribute with double underscores and provide a public method.

31
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 @@@ Define a decorator function that wraps another function.