#python |
- Python list exercises
- Python string exercises
- Python tuple exercises
- Python dictionary exercises
- Python matrix exercises
- Python function exercises
- Python pattern printing exercises
- Python DateTime exercises
Python list exercises for you to practice:
Exercise 1: Sum of List Write a Python function that takes in a list of numbers and returns the sum of all the numbers in the list.Exercise 4: Reverse List Write a Python function that takes in a list and returns a new list with the elements in reverse order.
Exercise 5: Even Numbers Write a Python function that takes in a list of numbers and returns a new list containing only the even numbers from the original list.
Exercise 6: Odd Numbers Write a Python function that takes in a list of numbers and returns a new list containing only the odd numbers from the original list.
Exercise 7: Common Elements Write a Python function that takes in two lists and returns a new list containing the common elements between the two lists.
Exercise 8: List Uniqueness Write a Python function that takes in a list and returns a new list with only the unique elements from the original list (removing any duplicates).
Exercise 9: List Manipulation Write a Python function that takes in a list of numbers and performs the following operations:
Removes the first and last elements from the list.
Doubles the remaining elements in the list.
Returns the modified list.
Exercise 10: List Sorting Write a Python function that takes in a list of numbers and returns a new list with the numbers sorted in ascending order.
These exercises should provide a good starting point for practicing list manipulation in Python.
Python string exercises for you to practice:
Exercise 1: String Length Write a Python function that takes in a string and returns the length of the string.
Exercise 2: Count Characters Write a Python function that takes in a string and a character, and returns the number of times the character appears in the string.
Exercise 3: Reverse String Write a Python function that takes in a string and returns a new string with the characters in reverse order.
Exercise 4: Uppercase and Lowercase Write a Python function that takes in a string and returns a new string with all the letters converted to uppercase.
Exercise 5: Capitalize Words Write a Python function that takes in a string and returns a new string with the first letter of each word capitalized.
Exercise 6: Count Words Write a Python function that takes in a string and returns the number of words in the string.
Exercise 7: Palindrome Check Write a Python function that takes in a string and returns True if the string is a palindrome (reads the same forwards and backwards), and False otherwise.
Exercise 8: Remove Vowels Write a Python function that takes in a string and returns a new string with all the vowels removed.
Exercise 9: Replace Characters Write a Python function that takes in a string, a character to be replaced, and a replacement character, and returns a new string with all occurrences of the character replaced with the replacement character.
Exercise 10: Split and Join Write a Python function that takes in a string and returns a list of words in the string. Then, write another function that takes in a list of words and returns a string by joining the words with a space between them.
These exercises should help you practice various string operations in Python.
Python tuple exercises for you to practice:
Exercise 1: Tuple Length Write a Python function that takes in a tuple and returns the length of the tuple.
Exercise 2: Access Tuple Elements Write a Python function that takes in a tuple and an index, and returns the element at the given index in the tuple.
Exercise 3: Concatenate Tuples Write a Python function that takes in two tuples and returns a new tuple that is the concatenation of the two input tuples.
Exercise 4: Count Occurrences Write a Python function that takes in a tuple and an element, and returns the number of times the element appears in the tuple.
Exercise 5: Convert Tuple to List Write a Python function that takes in a tuple and converts it into a list.
Exercise 6: Check Membership Write a Python function that takes in a tuple and an element, and returns True if the element is present in the tuple, and False otherwise.
Exercise 7: Tuple Unpacking Write a Python function that takes in a tuple of two elements and unpacks it into two separate variables.
Exercise 8: Swap Values Write a Python function that takes in two variables and swaps their values using tuples.
Exercise 9: Count Unique Elements Write a Python function that takes in a tuple and returns the number of unique elements in the tuple.
Exercise 10: Sort Tuples Write a Python function that takes in a list of tuples and sorts them based on a specific element in each tuple.
These exercises should give you a good opportunity to practice working with tuples in Python.
Python dictionary exercises for you to practice:
Exercise 1: Access Dictionary Values Write a Python function that takes in a dictionary and a key, and returns the value associated with that key in the dictionary.
Exercise 2: Add Key-Value Pair Write a Python function that takes in a dictionary, a key, and a value, and adds the key-value pair to the dictionary.
Exercise 3: Remove Key-Value Pair Write a Python function that takes in a dictionary and a key, and removes the key-value pair from the dictionary.
Exercise 4: Dictionary Length Write a Python function that takes in a dictionary and returns the number of key-value pairs in the dictionary.
Exercise 5: Dictionary Keys Write a Python function that takes in a dictionary and returns a list of all the keys in the dictionary.
Exercise 6: Dictionary Values Write a Python function that takes in a dictionary and returns a list of all the values in the dictionary.
Exercise 7: Merge Dictionaries Write a Python function that takes in two dictionaries and merges them into a new dictionary.
Exercise 8: Count Occurrences Write a Python function that takes in a string and returns a dictionary where the keys are the unique characters in the string and the values are the number of occurrences of each character.
Exercise 9: Check Key Existence Write a Python function that takes in a dictionary and a key, and returns True if the key exists in the dictionary, and False otherwise.
Exercise 10: Sort Dictionary by Values Write a Python function that takes in a dictionary and returns a new dictionary with the same keys, but the values sorted in ascending order.
Python set exercises for practice:
Exercise 1: Set Length Write a Python function that takes in a set and returns the number of elements in the set.
Exercise 2: Add Elements to Set Write a Python function that takes in a set and an element, and adds the element to the set.
Exercise 3: Remove Element from Set Write a Python function that takes in a set and an element, and removes the element from the set if it exists.
Exercise 4: Union of Sets Write a Python function that takes in two sets and returns a new set that contains all the unique elements from both sets.
Exercise 5: Intersection of Sets Write a Python function that takes in two sets and returns a new set that contains only the common elements between the two sets.
Exercise 6: Difference of Sets Write a Python function that takes in two sets and returns a new set that contains the elements from the first set that are not present in the second set.
Exercise 7: Subset Check Write a Python function that takes in two sets and returns True if the first set is a subset of the second set, and False otherwise.
Exercise 8: Set Conversion Write a Python function that takes in a list and converts it into a set.
Exercise 9: Set Operations Write a Python function that takes in two sets and performs the following operations:
Calculates the union of the two sets.
Calculates the intersection of the two sets.
Calculates the difference of the two sets.
Calculates the symmetric difference of the two sets.
Returns the results as a tuple.
Python matrix exercises for practice:
Exercise 1: Matrix Dimensions Write a Python function that takes in a matrix (a list of lists) and returns the dimensions of the matrix as a tuple (number of rows, number of columns).Exercise 2: Access Matrix Elements Write a Python function that takes in a matrix and two indices (row and column), and returns the element at the specified indices in the matrix.
Exercise 3: Transpose Matrix Write a Python function that takes in a matrix and returns a new matrix that is the transpose of the input matrix (rows become columns and columns become rows).
Exercise 4: Add Matrices Write a Python function that takes in two matrices of the same dimensions and returns a new matrix that is the result of adding the corresponding elements of the input matrices.
Exercise 5: Multiply Matrix by Scalar Write a Python function that takes in a matrix and a scalar (single value) and returns a new matrix where each element is multiplied by the scalar.
Exercise 6: Multiply Matrices Write a Python function that takes in two matrices and returns a new matrix that is the result of matrix multiplication.
Exercise 7: Check Square Matrix Write a Python function that takes in a matrix and returns True if it is a square matrix (number of rows equals number of columns), and False otherwise.
Exercise 8: Diagonal Elements Write a Python function that takes in a matrix and returns a list of the diagonal elements (elements on the main diagonal) of the matrix.
Exercise 9: Matrix Sum Write a Python function that takes in a matrix and returns the sum of all the elements in the matrix.
Exercise 10: Identity Matrix Write a Python function that takes in a number n and returns an n x n identity matrix (a square matrix where all diagonal elements are 1 and all other elements are 0).
Python function exercises for practice:
Exercise 1: Sum of Two Numbers Write a Python function that takes in two numbers and returns their sum.
Exercise 2: Maximum of Three Numbers Write a Python function that takes in three numbers and returns the maximum of the three.
Exercise 3: Factorial Write a Python function that takes in a number and returns its factorial (the product of all positive integers from 1 to the number).
Exercise 4: Fibonacci Sequence Write a Python function that takes in a number n and returns the nth number in the Fibonacci sequence (where each number is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, ...).
Exercise 5: Check Prime Number Write a Python function that takes in a number and returns True if it is a prime number (divisible only by 1 and itself), and False otherwise.
Exercise 6: Palindrome Check Write a Python function that takes in a string and returns True if it is a palindrome (reads the same forwards and backwards), and False otherwise.
Exercise 7: Reverse String Write a Python function that takes in a string and returns a new string with the characters in reverse order.
Exercise 8: List Average Write a Python function that takes in a list of numbers and returns the average (mean) of the numbers.
Exercise 9: Count Vowels Write a Python function that takes in a string and returns the number of vowels (a, e, i, o, u) in the string.
Exercise 10: Generate Prime Numbers Write a Python function that takes in a number n and returns a list of the first n prime numbers.
These exercises should provide a good starting point for practicing Python functions.
Python pattern printing exercises for practice:
Exercise 1:
Right Triangle Pattern Write a Python program that takes in a number n and prints a right triangle pattern of asterisks (*) of height n.
Example:* ** *** **** *****
Exercise 2:**** **** **** ****
Exercise 3: Diamond Pattern Write a Python program that takes in a number n and prints a diamond pattern of asterisks (*) of side length n.
Example: Input: 5 Output:
* *** ***** *** *
Exercise 4: Number Pyramid Write a Python program that takes in a number n and prints a number pyramid pattern from 1 to n.
Example: Input: 5 Output:
1 232 34543 4567654 567898765
Exercise 5: Pascal's Triangle Write a Python program that takes in a number n and prints Pascal's triangle of height n.
Example: Input: 5 Output:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Python DateTime exercises to practice:
Exercise 4: Leap Year Check
No comments:
Post a Comment