Python Exercises for Beginners – 15 Solutions

So, for what purpose are you learning python? Although, We all know python is a famous programming language used for web development, machine learning, software development, data science, etc.

If you are a python beginner or have learned some basic syntax, data types, functions, and looking for some beginner exercises questions to work on. Then, below I have put some amazing python exercises for beginners with solutions.

If you know a little bit about data structures and algorithms, you will be able to solve these questions very easily. Why, because, with the help of an algorithm, you will know how to process the data in a meaningful way.

Note:- if you want to know deeply about python data structure and algorithms, then this amazing book Dive Into Algorithms: A Pythonic Adventure for the Intrepid Beginner” will help you a lot.

Now, let’s solve these python beginner’s exercises.

Python Exercises


Exercise 1: Finding Even or Odd number from list

Write a program that will print the 10 randomly generated numbers from 1 digit to 3 digits. Also, print which number is even and which is odd.

--> Expected Input
[249, 222, 157, 229, 102, 71, 7, 165, 30, 274]
--> Expected Output
Odd and Even number from the above list are: 
249 is odd number.
222 is even number.
157 is odd number.
229 is odd number.
102 is even number.
71 is odd number.
7 is odd number.
165 is odd number.
30 is even number.
274 is even number.
See Solution
import random
odd_or_even = random.sample(range(1, 299), 10)
print(odd_or_even)
print("Odd and Even number from the above list are: ")
for check in odd_or_even:
  if check % 2 == 0:
    print(check, "is even number.")
  else:
    print(check, "is odd number.") 

Exercise 2: Program to display the multiplication table of any number.

Write a program that will ask the user any number, then print the multiplication table of that number.

--> Expected Input
What is your number for multiplication: 11  #(You may choose any other number)
--> Expected Output
11 * 1 = 11
11 * 2 = 22
11 * 3 = 33
11 * 4 = 44
11 * 5 = 55
11 * 6 = 66
11 * 7 = 77
11 * 8 = 88
11 * 9 = 99
11 * 10 = 110
See Solution
get_table_number = int(input("What is your number for multiplication: "))
for multiplication in range(1, 11):
  print(get_table_number, "*", multiplication, "=", get_table_number*multiplication)

Exercise 3: Display the sum of the square root from a randomly generated list of 20 numbers between 100 to 250.

Write a program that will generate a list of 20 random numbers between 100 to 250. Find the square root of all the numbers of a list and print the sum of the square root.

--> Expected Input
Random numbers between 100 to 250 are: [157, 248, 201, 163, 113, 111, 134, 106, 146, 226, 156, 179, 236, 208, 224, 120, 135, 214, 155, 124]
--> Expected Output
Square root of random numbers between 100 to 250 are: [12.529964086141668, 15.748015748023622, 14.177446878757825, 12.767145334803704, 10.63014581273465, 10.535653752852738, 11.575836902790225, 10.295630140987, 12.083045973594572, 15.033296378372908, 12.489995996796797, 13.379088160259652, 15.362291495737216, 14.422205101855956, 14.966629547095765, 10.954451150103322, 11.61895003862225, 14.628738838327793, 12.449899597988733, 11.135528725660043]

Total sum of square roots are: 256.78395966150646
See Solution
import math
import random
random_numbers = random.sample(range(100, 250), 20)
print("Random numbers between 100 to 250 are:", random_numbers)
result_square_root = []
for square_root in random_numbers:
  sqrt = math.sqrt(square_root)
  result_square_root.append(sqrt)
print("Square root of random numbers between 100 to 250 are:", result_square_root)
total_square_root_sum = sum(result_square_root)
print("Total sum of square root are:", total_square_root_sum)

Exercise 4: Program to find the greatest number.

You have three number, (a = 1000, b = 240, c = 40), you may assume another number. Now, write a program to find which is greatest number.

--> Expected Input
a = 1000
b = 240
c = 40
--> Expected Output
"a" having value 1000 is the greatest number.
See Solution
a = 1000
b = 200
c = 30
if (a>b) & (a>c):
  print('"a" having value', a,'is the Greatest number.')
elif (b>a) & (b>c):
  print('"b" having value', b,'is the Greatest number.')
elif (c>a) & (c>b):
  print('"c" having value', c,'is the Greatest number.') 

Exercise 5: Program to find names of planets with length greater than 5.

You have a list of the names of all the planets in your solar system. Write a program that will print planet names with a length greater than 5.

--> Expected Input
["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
--> Expected Output
Mercury
Jupiter
Saturn
Uranus
Neptune
See Solution
planet_names = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
print("Planet names are: ", planet_names)
for planet in planet_names:
  if len(planet) > 5:
    print(planet)

Exercise 6: A Program that will print price including tax.

People from some states of America have come to see the show going on in London, and for this, they have to pay $ 12 and tax separately.

Now, write a program that will ask the users the name of their state. If they’re from “New York, Los Angeles, Chicago, Houston, Phoenix, San Antonio, San Diego, Dallas, San Jose, Austin” then include the tax of $1.21 in their price and print it.

And if they are from “Fort Worth, Columbus, San Francisco, Denver, Washington, El Paso, Las Vegas, Detroit, Louisville, Albuquerque” then include the tax of $0.41 in their price and print it.

And if they are from any other states then include the tax of $0.39 in their price and print it.

Hint:- User may input their state name in capital lettel. So, use .lower()

--> Expected Input
#Input
Please enter your state name: 
--> Expected Output
# if Los Angeles
#Output
Including tax 1.21 dollar, you will have to pay 13.21 dollar.
#if Denver
#Output
Including tax 0.41 dollar, you will have to pay 12.41 dollar.
#if Augusta
#Output
Including tax 0.39 dollar, you will have to pay 12.39 dollar.
See Solution
ticket = 12
places = input("Please enter your country name: ")
if places.lower() in ("new york", "los angeles", "chicago", "houston", "phoenix", "san antonio", "san diego", "dallas", "san jose", "austin"):
  tax = 1.21
elif places.lower() in ("fort worth", "columbus", "san francisco", "denver", "washington", "el paso", "las vegas", "detroit", "louisville", "albuquerque"):
  tax = 0.41
else:
  tax = 0.52      
print("Including tax", tax, "dollar, you will have to pay", ticket + tax, "dollar.")

Exercise 7: Write a program to print today’s date and 365 days ago date.

NOTE:- Use datetime and timedelta module to print today’s date and 365 days ago date.

As you are solving this python exercises for beginners. Check out this article, if you are to start python projects.

--> Expected Output
Today is: 2022-02-26 15:14:56.709946
365 days ago date was: 2021-02-26 15:14:56.709946
See Solution
#Using datetime and timedelta in Datetime module
from datetime import datetime, timedelta
today_date = datetime.now()
print("Today is: " + str(today_date))

year_ago_date = timedelta(days = 365)
old_day = today - year_ago_date
print("365 days ago date was: " + str(old_day))

Exercise 8: Remove numbers greater than 65 from a list while iterating.

Write a program to remove numbers greater than 65 out of 30 randomly generated numbers between 18 and 82 from a list while iterating.

--> Expected Input
 30 Random Numbers are:  [14, 36, 46, 21, 49, 47, 48, 63, 18, 55, 15, 38, 45, 35, 79, 23, 68, 62, 13, 64, 44, 42, 30, 33, 31, 32, 77, 78, 28, 60]
--> Expected Output
Numbers less than 65 are : [14, 36, 46, 21, 49, 47, 48, 63, 18, 55, 15, 38, 45, 35, 23, 62, 13, 64, 44, 42, 30, 33, 31, 32, 28, 60]


See Solution
Solution no.1

import random
random_numbers = random.sample(range(12, 82), 30)
print("30 Random Numbers are: ",random_numbers)
for new_list in list(random_numbers):
  if new_list > 65:
    random_numbers.remove(new_list)
print("Numbers less than 65 are :", random_numbers)

Solution no.2

import random
random_numbers = random.sample(range(12, 82), 30)
print("30 Random Numbers are: ",random_numbers)
final_list = list(filter(lambda value: value < 65, random_numbers))
print("Numbers less than 65 are :",final_list)

Exercise 9: Double each Element from a given list.

You have a list of numbers 1 to 10 and you have to double each element of that list. e.g. [1, 2, 3] will be [1, 4, 9].

--> Expected Input
 list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
--> Expected Output
Doubling list is:  [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
See Solution
Solution 1

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_list = []
for double in list:
  final_list = double * double
  new_list.append(final_list)
print("Doubling list is: ",new_list)

Solution 2

l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list_final_new = list(map(lambda x: x * x, l1))
print("Doubling list is: ",list_final_new)

Exercise 10: A dictionary with even numbers as keys and their squares as values.

Suppose we want to create an output dictionary that contains only even numbers which are present in the input list as keys and their squares as values. Ex – {2: 4, 3: 9}. So write a program to create this dictionary.

--> Expected Input
 input_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
--> Expected Output
{2: 4, 4: 16, 6: 36, 8: 64, 10: 100}
See Solution
input_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
output_dict = {}
for even_values in input_list:
  if even_values % 2 == 0:
    output_dict[even_values] = even_values ** 2
print(output_dict)

Exercise 11: Write a program to return the factorial of a number using a function.

The factorial of a number is defined as the product of an integer and all the integers below it. It means, the factorial of 4 is 1×2×3×4, which is equal to 24. It is represented using the symbol ‘!’ So, 24 is the value of 4!

--> Expected Input
 factorial(6)
 factorial(9)
--> Expected Output
Factorial of 6 is 720
Factorial of 9 is 362880
See Solution
def factorial(n):
    result = 1
    for i in range(1, n + 1):
        result = result * i
    return result

print("Factorial of 6 is",factorial(6)) # should return 720
print("Factorial of 9 is",factorial(9)) # should return 362880

Exercise 12: Write a program to return the initials of the word contained in the given phrase, in upper case.

Understand this example, “australian space agency” should return “ASA” or “danish National Space center” should return “DNSC”.

--> Expected Input
National Space Science Agency  
INDIAN SPACE RESEARCH ORGANISATION
korea AEROSPACE Research instiTUTe
nATIONAL aerospace DEVELOPMENT Administration
--> Expected Output
NSSA
ISRO
KARI
NADA
See Solution
def initial_word(phrase):
  words = phrase.split()
  initial_alphabet = ""
  for word in words:
    initial_alphabet = initial_alphabet + word[0].upper()
  return initial_alphabet  


print(initial_word("National Space Science Agency"))
print(initial_word("INDIAN SPACE RESEARCH ORGANISATION"))
print(initial_word("korea AEROSPACE Research instiTUTe"))
print(initial_word("nATIONAL aerospace DEVELOPMENT Administration"))

Exercise 13: Write a python program to check if given phrases are palindrome are not.

Palindrome:- A palindrome is a word, phrase, verse or sentence that reads the same forward or backward. For Example:- “Redder” is a palindrome. “Borrow or bob” is also a palindrome. “Nurse is Nurse” is not a palindrome.

--> Expected Input
"Never Odd Or Even"
"kayak"
"Step on no pets"
"Stella won no wallets"
"Legal Mangale"
"Victory Story kiv"
"Tenet"
--> Expected Output
True
True
True
True
False
False
True
See Solution
def is_palindrome(input_string):
  new_string = input_string.split()
  new_string = "".join(new_string).lower()
  reverse_string = new_string[::-1]
  
  if new_string == reverse_string:
    return True
  return False  

print(is_palindrome("Never Odd Or Even"))  
print(is_palindrome("Kayak"))
print(is_palindrome("Step on no pets"))
print(is_palindrome("Stella won no wallets"))
print(is_palindrome("Legal Mangale"))
print(is_palindrome("Victory Story kiv"))
print(is_palindrome("Tenet"))

Exercise 14: Write a program to convert the fahrenheit degree value into celcius.

Ask the user, the temperature degree in Fahrenheit. Now your task is to convert the value to the Celcius scale in 1 float point number(28.9) and print it.

--> Expected Input
-20
--> Expected Output
-20 degree fahrenheit is equal to -28.9 degree celcius
See Solution
fahrenheit = int(input("Enter a Fahrenheit Temperature: "))
celcius = 5/9 * (fahrenheit - 32)
print("%.0f degree fahrenheit is equal to %.1f degree celcius"%(fahrenheit, celcius))

Exercise 15: Write a function to return a new string from a given string after removing a specific word.

Remove letter "o" from this given string "pneumonoultramicroscopicsilicovolcanoconiosis" and print it.

--> Expected Input
"pneumonoultramicroscopicsilicovolcanoconiosis"
--> Expected Output
pneumnultramicrscpicsilicvlcancnisis
See Solution
def remove_letter(n):
  split_it = n.split("o")
  result = "".join(split_it)
  return result
print(remove_letter("pneumonoultramicroscopicsilicovolcanoconiosis"))

So, these are the 15 python exercises for beginners. I hope you liked this article and you all would have completed these all beginner questions in your own way. If so, please put your code in the comment box, so that others could get benefit from it.

If you have any questions, please let me know in the comment section.

Thank You

This post may contain affiliate links. TechnologyNous.com may earn money from the companies that I have mentioned here.

2 Comments

  1. Hi Vivek,
    there is an error in the second exercise.
    The eleven should not be hardcoded in the solution but the variable should be used instead.
    >> for multiplication in range(1, 11):
    >> for multiplication in range(1, get_table_number):

Comments are closed.