5 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

Write a function to calculate the length of a string.

A

def string_length(s):\n return len(s) @@@ Use the built-in len() function to get the length of a string.

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

Write a program to check if a number is prime.

A

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.

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

Create a list of numbers and find the sum of all numbers.

A

numbers = [1, 2, 3, 4, 5]\nsum_numbers = sum(numbers) @@@ Use the built-in sum() function to calculate the sum of a list.

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

Write a function to find the minimum value in a list.

A

def find_min(lst):\n return min(lst) @@@ Use the built-in min() function to find the minimum value in a list.

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

Write a program to reverse a list.

A

lst = [1, 2, 3, 4, 5]\nreversed_lst = lst[::-1] @@@ Use slicing to reverse a list.

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

Create a dictionary and iterate over its keys and values.

A

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.

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

Write a function to concatenate two lists.

A

def concatenate_lists(lst1, lst2):\n return lst1 + lst2 @@@ Use the + operator to concatenate two lists.

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

Write a program to print the first 10 natural numbers using a loop.

A

for i in range(1, 11):\n print(i) @@@ Use a for loop to print numbers in a range.

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

Create a function to check if a number is in a given range.

A

def is_in_range(num, start, end):\n return start <= num <= end @@@ Use comparison operators to check if a number is within a range.

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

Write a program to find the GCD of two numbers.

A

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.

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

Create a class with a method that calculates the area of a rectangle.

A

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.

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

Write a function that returns the largest of three numbers.

A

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.

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

Write a program to convert a list of integers into a single integer.

A

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.

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

Create a function to check if a string is a pangram.

A

def is_pangram(s):\n alphabet = set(‘abcdefghijklmnopqrstuvwxyz’)\n return alphabet <= set(s.lower()) @@@ Check if all alphabet letters are present in the string.

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

Write a program to merge two sorted lists into a single sorted list.

A

def merge_sorted_lists(lst1, lst2):\n return sorted(lst1 + lst2) @@@ Use the sorted() function to merge and sort two lists.

17
Q

Create a function to remove all whitespace from a string.

A

def remove_whitespace(s):\n return s.replace(‘ ‘, ‘’) @@@ Use the replace() method to remove whitespace from a string.

18
Q

Write a program to find the second smallest number in a list.

A

def second_smallest(lst):\n return sorted(lst)[1] @@@ Sort the list and return the second element.

19
Q

Create a function to count the number of vowels in a string.

A

def count_vowels(s):\n return sum(1 for char in s if char in ‘aeiouAEIOU’) @@@ Use a generator expression to count vowels in a string.

20
Q

Write a program to find the common elements in three lists.

A

def common_elements(lst1, lst2, lst3):\n return list(set(lst1) & set(lst2) & set(lst3)) @@@ Use set intersection to find common elements.

21
Q

Create a function to convert a binary number to decimal.

A

def binary_to_decimal(binary):\n return int(binary, 2) @@@ Use the int() function with base 2 to convert binary to decimal.

22
Q

Write a program to calculate the sum of digits of a number.

A

def sum_of_digits(n):\n return sum(int(digit) for digit in str(n)) @@@ Convert the number to a string and sum its digits.

23
Q

Create a function to capitalize the first and last letter of a string.

A

def capitalize_first_last(s):\n if len(s) < 2:\n return s.upper()\n return s[0].upper() + s[1:-1] + s[-1].upper() @@@ Capitalize the first and last letters of the string.

24
Q

Write a program to find the mode of a list of numbers.

A

from collections import Counter\ndef find_mode(lst):\n return Counter(lst).most_common(1)[0][0] @@@ Use collections.Counter to find the most common element.

25
Q

Create a function to flatten a nested list.

A

def flatten_list(nested_lst):\n return [item for sublist in nested_lst for item in sublist] @@@ Use a list comprehension to flatten a nested list.

26
Q

Write a program to find the intersection of two sets.

A

set1 = {1, 2, 3}\nset2 = {2, 3, 4}\nintersection = set1 & set2 @@@ Use the & operator to find the intersection of two sets.

27
Q

Create a function to check if a given year is a leap year.

A

def is_leap_year(year):\n return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) @@@ Use modulus operators to check leap year conditions.

28
Q

Write a program to count the occurrences of a substring in a string.

A

def count_substring(s, substring):\n return s.count(substring) @@@ Use the count() method to find the occurrences of a substring.

29
Q

Create a function to sort a dictionary by its values.

A

def sort_dict_by_value(d):\n return dict(sorted(d.items(), key=lambda item: item[1])) @@@ Use the sorted() function with a lambda to sort a dictionary by its values.

30
Q

Write a program to check if a list is a palindrome.

A

def is_palindrome(lst):\n return lst == lst[::-1] @@@ Compare the list with its reversed version to check if it’s a palindrome.

31
Q

Create a function to generate a random password.

A

import random\ndef generate_password(length):\n chars = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()’\n return ‘‘.join(random.choice(chars) for _ in range(length)) @@@ Use random.choice to generate a random password.