20 Python Programs to Practice to Get Better in Python

by Muhammad Talha
Python Programs to Practice

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)
Output

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))
Output

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"))
Output

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))
Output

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)
Output

6. Python program to reverse a string

def reverse_string(string):
    return string[::-1]

string = input("Enter a string: ")
print("Reverse: ", reverse_string(string))
Output

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)
Output

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)
Output

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)
Output

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)
Output

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}")
Output

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)
Output

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}")
Output

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))

Output

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))


Output

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)
Output
16. Python program to find the square of list items
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)

Output

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!")
Output

Python program to copy a file

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)
Output
2 founded 3 times in [4, 2, 8, 2, 1, 2, 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
        
    
Output

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!")
Output

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!

Related Posts

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

Ad Blocker Detected!

Refresh

5 Best ChatGPT Prompts for Programmers