Programming exercises in Python: In this guide, we will solve some necessary programming exercises om python which is important for every beginner to learn and practice by solving.
We are going to cover these topics:
List
Strings
Loops
Input-Output
Functions (built-in)
Flow control statements
Exercise 1: Write a program in Python to calculate the volume of a sphere.
a = int(input("Enter value of a: "))
b = int(input("Enter value of b: "))
print(f"\na is {a} b is {b}")
a = a + b
b = a - b
a = a - b
print("\nafter swapping")
print(f"a is {a} b is {b}")
Program Explanation:
Here we have used some logic and opearators to achieve our goal.
Exercise 5: Write a program to check wheteher the given word is vowel or consonent.
a = int(input("Enter value of a: "))
b = int(input("Enter value of b: "))
print(f"\na is {a} b is {b}")
a = a + b
b = a - b
a = a - b
print("\nafter swapping")
print(f"a is {a} b is {b}")
Program Explanation:
Here we have used some logic and opearators to achieve our goal.
Exercise 7: Write a program to count the occurence of even number and odd number between the range 10 – 55.
Hint Range 10,55
Expected Output Even number counts is 23
Odd number counts is 22
num1 = int(input("Enter starting range: "))
num2 = int(input("Enter ending range: "))
eve_count = 0
odd_count = 0
for i in range(num1, num2):
if i%2 == 0:
eve_count += 1
else:
odd_count += 1
print(f"Even number counts is {eve_count}")
print(f"Odd number counts is {odd_count}")
Program Explanation:
Here we are getting two inputs from the user which we will use as a start and end range for our range function.
The modulus operator (%) returns the remainder after division.
Hence if the number returning 0 after division by 2 means that number is the multiple of 2 and that is an even number.
This way we are incrementing the eve_counter by one each time the condition goes true.
And else if not, we are entering to else control statement and incrementing the odd_ counter by 1.
Exercise 8: Write a program to perform subtract and addition operation using switch.
Hint 1:
Press 1 for Add & 2 for Subtract: 1
Input:
a = 6
b = 4
Expected Output:
Result is 10
Hint 2:
Press 1 for Add & 2 for Subtract: 2
Input:
a = 6
b = 4
Expected Output:
Result is 2
a = int(input("Enter value 1: "))
b = int(input("Enter value 2: "))
choice = {
'1': a+b,
'2': a-b
}
z = input("Press 1 for Add & 2 for Subtract")
print(choice.get(z))
Program Explanation:
Like other programming languages, there is no specific switch method in python.
We can achieve this by using dictionary mapping.
The approach we used is dictionary mapping.
Dictionary is the same as an associative array of php, js etc.
Here get function is used to get the value at a specific choice.
Exercise 9: Write a program in Python to calculates acceleration given initial velocity v1, final velocity v2, start time t1, and end time t2.
Formula: a = (V2 – V1 / t2 – t1)
To test your solution, call the function by inputting values 0, 10, 0, 20 for v1, v2, t1, and t2 respectively, and you should get the expected output.
Hint Formula : a = (V2 – V1 / t2 – t1)
Formula inside function would look like a = (v2 – v1) / (t2 – t1)
Expected Output 0.5
def acceleration(v1, v2, t1, t2):
a = (v2 - v1) / (t2 - t1)
return a
print(acceleration(0,10,0,20))
# 0.5
Program Explanation:
The first three lines are where we create the function with three parameters
A function definition is like a blueprint.
Then in the last line, we are printing out the function output.
The output is whatever is returned by the return statement.
Exercise 10: Write a program in Python to sort 3 numbers without using loops or conditional statements.
for i in range(10,25):
if i%5 == 0:
pass
else:
print(i, end=" ")
# 11 12 13 14 16 17 18 19 21 22 23 24
Program Explanation:
Here we used the range() function to iterate to a finite specific range from 10, 25.
Next, we checked the condition ( if i%5 == 0) if the condition satisfies then it enters to if condition and in the if condition we used the pass keyword to skip that value.
It means the number which is the multiple of 5 is skipped.
Next, in else: part only those numbers will enter which are not multiples of 5. Hence we printed the values.
Here end = ” “ used for blank spaces instead of next line.
Exercise 12: Write a program in Python to count the occurrence of a specific value in a list.
x = int(input("Enter number to check: ")) # 3
a = [1,3,3,4,3,2,3]
count = 0
for i in range(len(a)):
if a[i] == x:
count = count+1
print(f"{x} occurs {count} time")
# 3 occurs 4 time
Program Explanation:
First, we took input from the user & stored that input value in x variable (suppose user entered 3).
In the very next line, we created a list a[] with some demo elements.
For loop is used for iteration. Each time loop executes the value of I increments by 1 and the max time loop iterates are the length of list a[].
If the number 3 occurs in the list, the count variable increment by 1.
Hence each time for loop executes and during its execution if number 3 occurs then count increments by 1.
Hence we got the solution.
Exercise 13: Write a program in Python to count the occurrence of a specific word in a string.
Hint string_ = “adam is a boy and adam loves to play cricket.” Input: “adam”
Expected Output adam occurs 2 time
string_ = "adam is a boy and adam loves to play cricket."
x = input("Enter the word you want to check: ")
main = string_.split(" ")
count = 0
for i in range(len(main)):
if main[i] == x:
count += 1
print(count)
# adam occurs 2 time
Program Explanation:
We split the string from empty space using the split() function of string.
It returns the list as output.
I.e. string “adam is a boy and adam loves to play cricket.” now converted to [‘adam’, ‘is’, ‘a’, ‘boy’, ‘and’, ‘adam’, ‘loves’, ‘to’, ‘play’, ‘cricket.’].
Next, we iterated over each element of the list using for loop.
We used a counter to increment the value by one each time value occurs in the list during iteration.
Exercise 14: Write a program in Python to check whether the given number is even or odd.
# Procedural approach
num1 = int(input("Enter number one ")) # 20
num2 = int(input("Enter number two ")) # 30
if(num1 * num2 == 500):
print("The result is ",num1*num2)
else:
print("The result is ",num1+num2)
# 50
# Functional approach
def exec(x,y):
if(x*y < 500):
print(x*y)
else:
print("The result is ",x+y)
exec(20,20)
# 400
Exercise 16: Write a program to print the greatest of three number.