Cheatsheet Content
1. Prime Number Checker A function to determine if a number is prime. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. def is_prime(number): if number <= 1: return False for i in range(2, int(number**0.5) + 1): if number % i == 0: return False return True # Example Usage print(is_prime(11)) # Output: True print(is_prime(4)) # Output: False 2. List of Squares (1 to 30) Function to generate a list containing the squares of numbers from 1 to 30, inclusive. def generate_squares_list(): squares = [] for i in range(1, 31): squares.append(i**2) return squares # Example Usage my_squares = generate_squares_list() print(my_squares[:5]) # Output: [1, 4, 9, 16, 25] print(my_squares[-5:]) # Output: [676, 729, 784, 841, 900] 3. Extract Unique Elements from a List Function to take a list and return a new list containing only the unique elements from the original list, preserving order of first appearance (if using set and then converting to list, order is not guaranteed). def get_unique_list(input_list): unique_elements = [] seen = set() for item in input_list: if item not in seen: unique_elements.append(item) seen.add(item) return unique_elements # Example Usage sample_list = [1, 2, 5, 3, 4, 3, 4, 5] unique_list = get_unique_list(sample_list) print(unique_list) # Output: [1, 2, 5, 3, 4] # Alternative using set (order not guaranteed) def get_unique_list_set_method(input_list): return list(set(input_list)) print(get_unique_list_set_method(sample_list)) # Output: [1, 2, 3, 4, 5] (order may vary) 4. Find Maximum of Three Numbers A function to find the largest among three given numbers. def find_max_of_three(num1, num2, num3): return max(num1, num2, num3) # Example Usage print(find_max_of_three(10, 25, 15)) # Output: 25 print(find_max_of_three(-5, -1, -10)) # Output: -1 5. Count Upper and Lower Case Characters Function to analyze a string and count the occurrences of uppercase and lowercase letters. def count_case_characters(input_string): upper_count = 0 lower_count = 0 for char in input_string: if 'A'