Python is one of the most popular programming languages in the world with a vast community of developers, thanks to its simple syntax. Whether you are just starting or looking to improve your Programming skills, practicing hands-on programs is one of the best ways to improve your proficiency and confidence in programming. In this article, I’ve listed 20 Python programs to practice building your skills and knowledge.
Analyze each of these programs and try to implement them by yourself. These Python practice problems will give you the knowledge and confidence to easily create your own python programs. So let’s dive in and start practicing.
1. Python program to convert Celsius to Fahrenheit
celsius = float(input("Enter temperature in Celsius: ")) fah = (celsius * 9/5) + 32 print("Temperature in Fahrenheit: ",fah)
Temperature in Fahrenheit: 104.0
2. Python program to count vowels in a string
def count_vowel(string): vowels = "aeiouAEIOU" count = 0 for letter in string: if letter in vowels: count += 1 return count string = "I Love learning python" print("Total vowels =",count_vowel(string))
3. Python program to replace old domain with new in any outdated email address
def replace_domain(email, old_domain, new_domain): if "@"+old_domain in email: i = email.index("@"+old_domain) new_email = email[:i] + "@"+new_domain return new_email return email print("New Email: ",replace_domain('info@olddomain.com',"olddomain.com", "newdomain.com"))
4. Python program to calculate the area of a circle given the radius
from math import pi def calc_area(radius): area = pi * (radius ** 2) return area radius = float(input("Enter radius of circle: ")) print("Area of circle =", calc_area(radius))
Area of circle = 78.53981633974483
5. Python program to find the factorial of a given number
def find_fact(num): factorial = 1 if num < 1: print("Factorial of negative number does not exist!") elif num == 0: print("Factorial of 0 is 1!") else: for i in range(1,num+1): factorial = factorial * i print("Factorial of", num, "is = ", factorial) num = int(input("Enter a number: ")) find_fact(num)
Factorial of 6 is = 720
6. Python program to reverse a string
def reverse_string(string): return string[::-1] string = input("Enter a string: ") print("Reverse: ", reverse_string(string))
Reverse: nohtyP
7. Python program to check if a string is a palindrome or not
def check_palindrome(string): reverse = string[::-1] if string == reverse: print(string + " is a palindrome!") else: print(string + " is not a palindrome!") string = input("Enter a string: ") check_palindrome(string)
noon is a palindrome!
8. Python program to find the largest element in an array
def find_largest(array): largest = -1 for num in array: if num > largest: largest = num print("Largest =",largest) total = int(input("Enter total numbers: ")) array = [] for i in range(total): array.append(int(input(f"Enter number {i+1}: "))) find_largest(array)
Enter number 1: 65
Enter number 2: 23
Enter number 3: 12
Enter number 4: 67
Enter number 5: 34
Largest = 67
9. Python program to sort an array in ascending order
total = int(input("Enter total elements: ")) array = [] for i in range(total): num = int(input(f"Enter element {i+1}: ")) array.append(num) array.sort() print(array)
Enter element 1: 1
Enter element 2: 5
Enter element 3: 2
Enter element 4: 7
Enter element 5: 4
[1, 2, 4, 5, 7]
10. Python program to print the Fibonacci Series
def fibonacci_seq(num): a = 0 # start with 0 (first number) b = 1 # second number which is 1 if num == 1: print(a) elif num == 2: print(a, b, end=" ") else: print(a, b, end=" ") for i in range(num-2): c = a + b # sum a + b a = b # store the value of b in a b = c # store the value of c in b and then print b print(b, end=" ") num = int(input("Enter a number: ")) print("The Fibonacci Sequence =", end=" ") fibonacci_seq(num)
The Fibonacci Sequence = 0 1 1 2 3 5 8 13 21 34
11. Python program to find the circumference of a circle
from math import pi def find_circumference(radius): circumference = 2 * pi * radius return circumference radius = float(input("Enter the radius of the circle: ")) print(f"The circumference of the circle is: {find_circumference(radius):.2f}")
The circumference of the circle is: 25.13
12. Python program to check if a number is perfect or not
def find_is_perfect(num): sum_of_divisors = 0 for i in range(1, num): if num % i == 0: sum_of_divisors += i if sum_of_divisors == num: print(f"{num} is a perfect number!") else: print(f"{num} is not a perfect number!") num = int(input("Enter a number: ")) find_is_perfect(num)
6 is a perfect number!
13. Python program to find the sum of all odd numbers in a list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] odd_sum = 0 for number in numbers: if number % 2 != 0: odd_sum += number print(f"Sum = {odd_sum}")
14. Python program to separate even and odd numbers from a list
t = int(input("Enter total numbers: ")) my_list = [] for i in range(t): n = int(input(f"Enter number {i+1}: ")) my_list.append(n) def even_numbers(l): even_list = [] for i in my_list: if i % 2 == 0: even_list.append(i) return even_list def odd_numbers(l): odd_list = [] for i in my_list: if i % 2 != 0: odd_list.append(i) return odd_list print("Even numbers =",even_numbers(my_list),"Odd numbers =",odd_numbers(my_list))
Enter number 1: 8
Enter number 2: 2
Enter number 3: 1
Enter number 4: 5
Enter number 5: 9
Enter number 6: 3
Enter number 7: 4
Enter number 8: 6
Enter number 9: 7
Enter number 10: 2
Even numbers = [8, 2, 4, 6, 2] Odd numbers = [1, 5, 9, 3, 7]
15. Python program to reverse list characters
def reverse_list(l): rev = [] for i in l: rev.append(i[::-1]) return rev my_list = ['cba', 'fed', 'ihg', 'lkj'] print(reverse_list(my_list))
16. Python program to reverse list items
numbers = ["Word1","Word2","Word3"] rev = [] for i in range(len(numbers)): rev.append(numbers.pop()) print("Reversed: ",rev)
import math total = int(input("Enter total numbers: ")) numbers = list() for i in range(total): n = int(input(f"Enter list item {i+1}: ")) numbers.append(n) squares = list() for i in numbers: sq = int(math.pow(i,2)) squares.append(sq) print("Squares of",numbers," are =",squares)
Enter list item 1: 2
Enter list item 2: 3
Enter list item 3: 4
Enter list item 4: 5
Enter list item 5: 6
Squares of [2, 3, 4, 5, 6] are = [4, 9, 16, 25, 36]
17. Python program to copy a file
#copy file import shutil source_file = input("Enter source file: ") copy_file = input("Enter copy file: ") if shutil.copyfile(source_file,copy_file): print("File copied successfully!") else: print("Cannot copied the file!")
Enter copy file: copy_file.txt
File copied successfully!
18. Python program to search a number in a list
numbers = [4,2,8,2,1,2,3] num = int(input("Enter a number to search: ")) found_count = 0 for i in numbers: check = False if i == num: found_count += 1 check = True print(check, i) print("\n",num, "founded", found_count, "times in", numbers)
False 4
True 2
False 8
True 2
False 1
True 2
False 3
19. Python program for a number guessing game
import random computer = random.randint(1,100) user = int(input("Guess a number between 1 - 100 (in 5 tries): ")) tries = 5 steps = 0 tries = tries - 1 while True: if tries > 0: if user > computer: steps = steps+1 print("Too big, Guess smaller than",user,"(tries remaining = ",tries,")") user = int(input("Guess again: ")) elif user < computer: steps = steps+1 print("Too small, Guess bigger than",user,"(tries remaining = ",tries,")") user = int(input("Guess again: ")) elif user == computer: steps = steps + 1 print("Congratulations! you guessed the number in",steps,"times","(tries remaining = ",tries,")") break tries = tries - 1 else: print("You lost!",tries,"tries left, the number was",computer) break
Too small, Guess bigger than 67 (tries remaining = 4 )
Guess again: 87
Too small, Guess bigger than 87 (tries remaining = 3 )
Guess again: 99
Too big, Guess smaller than 99 (tries remaining = 2 )
Guess again: 92
Too big, Guess smaller than 92 (tries remaining = 1 )
Guess again: 90
You lost! 0 tries left, the number was 89
20. Python program to implement rock paper scissors game
import random choices = ["rock","paper","scissors"] again = None while again != 'n': computer = random.choice(choices) player = None while player not in choices: player = input("rock, paper, or scissors: ").lower() if player == computer: print("Computer = ",computer) print("Player =",player) print("Tie!") # Player = Rock elif player == "rock": if computer == "paper": print("Computer =",computer) print("Player =",player) print("You Lose!") if computer == "scissors": print("Computer =",computer) print("Player =",player) print("You Won!") # Player = Scissors elif player == "scissors": if computer == "rock": print("Computer =",computer) print("Player =",player) print("You Lose!") if computer == "paper": print("Computer =",computer) print("Player =",player) print("You Won!") # Player = Paper elif player == "paper": if computer == "scissors": print("Computer =",computer) print("Player =",player) print("You Lose!") if computer == "rock": print("Computer =",computer) print("Player =",player) print("You Won!") again = input("Play again (y/n): ").lower() print("Game Over!")
Computer = scissors
Player = rock
You Won!
Play again (y/n): y
rock, paper or scissors: paper
Computer = rock
Player = paper
You Won!
Play again (y/n): n
Game Over!
Conclusion
These were some of the Python programs to practice to brush up on your programming skills. These basic programs will not only help you develop a better understanding of the language, but they’ll also give you the practice you need to gain proficiency in Python programming. With a few of these basic python programs under your belt, you’ll be ready to move on to more complex projects and problems.
If you’ve any doubts or want to add something, feel free to let me know in the comments below
Happy coding!