6edited Flashcards

1
Q

How do you calculate the length of a string in Python?

A

def string_length(s):
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
2
Q

How can you check if a number is prime in Python?

A

def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
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
3
Q

How do you find the sum of all numbers in a list?

A

numbers = [1, 2, 3, 4, 5]
sum_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
4
Q

How do you find the minimum value in a list?

A

def find_min(lst):
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
5
Q

How do you reverse a list in Python?

A

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

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

How do you iterate over keys and values in a dictionary?

A

my_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3}
for key, value in my_dict.items():
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
7
Q

How do you concatenate two lists in Python?

A

def concatenate_lists(lst1, lst2):
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
8
Q

How do you print the first 10 natural numbers using a loop?

A

for i in range(1, 11):
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
9
Q

How do you check if a number is in a given range?

A

def is_in_range(num, start, end):
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
10
Q

How do you find the GCD of two numbers in Python?

A

def gcd(a, b):
while b:
a, b = b, a % b
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
11
Q

How do you calculate the area of a rectangle using a class?

A

class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
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
12
Q

How do you find the largest of three numbers?

A

def largest_of_three(a, b, c):
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
13
Q

How do you convert a list of integers into a single integer?

A

lst = [1, 2, 3, 4]
number = 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
14
Q

How do you check if a string is a pangram?

A

def is_pangram(s):
alphabet = set(‘abcdefghijklmnopqrstuvwxyz’)
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
15
Q

How do you merge two sorted lists into a single sorted list?

A

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

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

How do you remove all whitespace from a string?

A

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

17
Q

How do you find the second smallest number in a list?

A

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

18
Q

How do you count the number of vowels in a string?

A

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

19
Q

How do you find the common elements in three lists?

A

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

20
Q

How do you convert a binary number to decimal in Python?

A

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

21
Q

How do you calculate the sum of digits of a number?

A

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

22
Q

How do you capitalize the first and last letter of a string?

A

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

23
Q

How do you find the mode of a list of numbers?

A

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

24
Q

How do you flatten a nested list in Python?

A

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

25
Q

How do you find the intersection of two sets?

A

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

26
Q

How do you check if a given year is a leap year?

A

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

27
Q

How do you count the occurrences of a substring in a string?

A

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

28
Q

How do you sort a dictionary by its values?

A

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

29
Q

How do you check if a list is a palindrome?

A

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

30
Q

How do you generate a random password in Python?

A

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