5 Flashcards
Task
Answer and Description
Write a function to calculate the length of a string.
def string_length(s):\n return len(s) @@@ Use the built-in len() function to get the length of a string.
Write a program to check if a number is prime.
def is_prime(num):\n if num <= 1:\n return False\n for i in range(2, int(num**0.5) + 1):\n if num % i == 0:\n return False\n return True @@@ Use a loop to check divisibility and determine primality.
Create a list of numbers and find the sum of all numbers.
numbers = [1, 2, 3, 4, 5]\nsum_numbers = sum(numbers) @@@ Use the built-in sum() function to calculate the sum of a list.
Write a function to find the minimum value in a list.
def find_min(lst):\n return min(lst) @@@ Use the built-in min() function to find the minimum value in a list.
Write a program to reverse a list.
lst = [1, 2, 3, 4, 5]\nreversed_lst = lst[::-1] @@@ Use slicing to reverse a list.
Create a dictionary and iterate over its keys and values.
my_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3}\nfor key, value in my_dict.items():\n print(key, value) @@@ Iterate over dictionary keys and values using the items() method.
Write a function to concatenate two lists.
def concatenate_lists(lst1, lst2):\n return lst1 + lst2 @@@ Use the + operator to concatenate two lists.
Write a program to print the first 10 natural numbers using a loop.
for i in range(1, 11):\n print(i) @@@ Use a for loop to print numbers in a range.
Create a function to check if a number is in a given range.
def is_in_range(num, start, end):\n return start <= num <= end @@@ Use comparison operators to check if a number is within a range.
Write a program to find the GCD of two numbers.
def gcd(a, b):\n while b:\n a, b = b, a % b\n return a @@@ Use the Euclidean algorithm to find the greatest common divisor.
Create a class with a method that calculates the area of a rectangle.
class Rectangle:\n def __init__(self, width, height):\n self.width = width\n self.height = height\n def area(self):\n return self.width * self.height @@@ Define a class with an __init__ method and an area method.
Write a function that returns the largest of three numbers.
def largest_of_three(a, b, c):\n return max(a, b, c) @@@ Use the built-in max() function to find the largest of three numbers.
Write a program to convert a list of integers into a single integer.
lst = [1, 2, 3, 4]\nnumber = int(‘‘.join(map(str, lst))) @@@ Use join() and map() to convert a list of integers to a single integer.
Create a function to check if a string is a pangram.
def is_pangram(s):\n alphabet = set(‘abcdefghijklmnopqrstuvwxyz’)\n return alphabet <= set(s.lower()) @@@ Check if all alphabet letters are present in the string.