This repository contains a set of Python exercises divided into two many categories: Conditional Statements and Loops and many more. Each section is designed to reinforce key concepts in Python programming, ranging from basic control flow to more complex looping structures.
This section includes 20 exercises that involve using conditional statements in Python to solve different types of problems. Each exercise helps build an understanding of decision-making in programming.
- Positive, Negative, or Zero Check: Write a program to check if a given number is positive, negative, or zero.
- Age Classification: Determine if a user is a minor, adult, or senior citizen based on age input.
- Leap Year Checker: Check if a given year is a leap year.
- Even or Odd Checker: Determine if an integer is even or odd.
- Grade Conversion: Convert a percentage score into a letter grade (A, B, C, D, F).
- Largest of Two Numbers: Find the largest of two numbers.
- Largest of Three Numbers: Find the largest of three numbers.
- Palindrome Check: Check if a given string is a palindrome.
- Triangle Validity Checker: Determine if three given side lengths form a valid triangle.
- Vowel or Consonant: Identify if a character is a vowel or consonant.
- Multiple of 3 and 5: Check if a number is a multiple of both 3 and 5.
- Temperature Categorization: Classify temperature in Celsius as freezing, moderate, or hot.
- Basic Calculator: Perform a basic arithmetic operation based on user input.
- Century Year Check: Determine if a year is a century year.
- Range Check: Check if a number is within a specified range.
- Triangle Type Classification: Classify a triangle as equilateral, isosceles, or scalene.
- Divisibility Check: Check if a number is divisible by 2, 3, or both.
- Pass or Fail: Determine if a user’s score is a pass or fail (50 or above).
- String Case Check: Identify if a string is uppercase, lowercase, or a mix.
- Prime Number Check: Check if a given number is prime.
The following 20 exercises provide practice with different types of loops in Python, helping to strengthen skills in iteration and control flow.
- Print 1 to 20: Print numbers from 1 to 20 using a for loop.
- Even Numbers (1-50): Use a while loop to print even numbers from 1 to 50.
- Sum of Numbers (1-100): Calculate the sum of all numbers from 1 to 100.
- Multiplication Table: Print the multiplication table of a given number.
- Odd Numbers (1-100): Print all odd numbers from 1 to 100.
- String Character Printer: Use a for loop to print each character of a string.
- Factorial Calculator: Find the factorial of a number using a while loop.
- Countdown from 10: Print numbers from 10 down to 1.
- Fibonacci Series: Print the first 10 Fibonacci numbers.
- Digit Counter: Use a loop to count the number of digits in an integer.
- Number Reversal: Print the reverse of a given number.
- Prime Numbers (1-50): Print all prime numbers between 1 and 50.
- Pyramid Pattern: Use nested loops to print a pyramid pattern of * characters.
- Loop Break Example: Write a program that breaks out of a loop when a specific condition is met.
- Sum of Even and Odd Numbers: Calculate the sum of even and odd numbers up to a given number.
- Digit Sum Calculator: Calculate the sum of the digits of an inputted integer.
- Number Guessing Game: Keep asking for a number until the correct number is guessed.
- Reverse Order Printer: Print numbers in reverse order within a given range.
- Squares (1-10): Print the square of each number from 1 to 10.
- Countdown Timer: Simulate a countdown timer starting from a given number down to zero.
- Write a function to calculate the area of a circle given its radius.
- Create a function that takes two numbers and returns their sum.
- Write a function to find the factorial of a number using recursion.
- Write a function that takes a string and returns it reversed.
- Create a function to check if a given number is prime.
- Write a function to count the vowels in a given string.
Intermediate Function Questions
- Create a function that takes a list of numbers and returns the largest number.
- Write a function to find the nth Fibonacci number using recursion.
- Write a function to check whether a string is a palindrome.
- Create a function that takes a list of integers and returns the sum of all even numbers.
- Write a function to calculate the GCD (Greatest Common Divisor) of two numbers.
- Create a function that accepts a dictionary and returns the key with the highest value.
Advanced Function Questions
- Write a function that calculates the power of a number without using the ** operator.
- Create a function that converts a given temperature from Celsius to Fahrenheit and vice versa.
- Write a function to flatten a nested list.
- Create a function to check if two strings are anagrams.
- Write a function that takes a list and removes all duplicate elements.
- Create a function that takes a string and counts the frequency of each character.
- Write a function that takes a list of employee salaries and calculates the average salary.
- Create a function to generate a random password of given length, containing uppercase, lowercase, numbers, and special characters.
- Create a dictionary
student
with keys:name
,age
, andgrade
. Assign them appropriate values. - Access the value of the key
grade
in thestudent
dictionary. - Add a new key
city
to thestudent
dictionary and set its value to"New York"
. - Update the value of the
age
key in thestudent
dictionary to20
. - Remove the key
city
from thestudent
dictionary.
- Iterate through the dictionary
student
and print all keys. - Iterate through the dictionary
student
and print all values. - Iterate through the dictionary
student
and print all key-value pairs in the formatkey: value
. - Check if the key
grade
exists in thestudent
dictionary and printTrue
orFalse
. - Count the total number of keys in the
student
dictionary.
- Merge the following two dictionaries and print the result:
dict1 = {'a': 1, 'b': 2} dict2 = {'c': 3, 'd': 4}
- Create a dictionary from a list of tuples:
[('name', 'Alice'), ('age', 25), ('city', 'Paris')]
. - Sort the keys of the dictionary
{'z': 1, 'a': 2, 'c': 3}
in ascending order and print the sorted dictionary. - Reverse the dictionary
{'a': 1, 'b': 2, 'c': 3}
so that keys become values and values become keys. - Write a Python function to check if two dictionaries are identical (contain the same key-value pairs).
- Create a nested dictionary to represent the following data:
Person: Name: John Age: 30 Address: Street: 123 Elm St City: Boston
- Access the value of the
City
key in the nested dictionary from the previous question. - Add a new key
Phone
to the nested dictionary with the value"123-456-7890"
. - Delete the
Address
key from the nested dictionary. - Iterate through all the keys in the outermost level of the nested dictionary and print them.
- Use a dictionary to count the occurrences of each word in the string
"hello world hello python world"
. - Write a Python program to find the key with the maximum value in the dictionary
{'a': 10, 'b': 15, 'c': 7}
. - Create a dictionary to map numbers 1 to 5 to their squares (e.g.,
{1: 1, 2: 4, 3: 9, ...}
). - Write a Python program to remove duplicate values from the dictionary
{'a': 10, 'b': 15, 'c': 10, 'd': 15}
. - Write a Python function that accepts a dictionary and a key, and returns the value associated with the key. If the key doesn’t exist, return
"Key not found"
.
- Given two dictionaries
dict1 = {'a': 5, 'b': 10}
anddict2 = {'a': 3, 'b': 7}
, write a Python program to add the values of matching keys and print the result. - Write a Python program to create a dictionary where the keys are the first
n
positive integers, and the values are their cubes. Taken
as user input. - Flatten the following nested dictionary into a single-level dictionary:
{'a': {'b': 1, 'c': 2}, 'd': {'e': 3, 'f': 4}}
- Write a Python program to split a dictionary into two based on whether the values are odd or even.
- Create a dictionary comprehension to filter out all keys in
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
where the value is less than 3.