Python Practice Solutions
Python Practice Solutions
Python Exercises
import datetime
now = datetime.datetime.now()
print("Current date and time : ", now)
2- Write a Python func on that calculates and returns the area of a circle based on the radius.
area = pi * r ** 2
return area
3- Write a Python func on that accepts the user's first and last name and store them in a file with
the name “users.txt”.
filename = open(“users.txt”, a)
fullname = firstname + “ “ + lastname + “\n”
filename.write(fullname)
4- Write a Python program that accepts a sequence of comma-separated numbers from the user
and generates a list and a tuple of those numbers.
Sample data : 3, 5, 7, 23
numbers = “3,5,7,23”
numbers_list = numbers.split(",")
numbers_tuple = tuple(list)
5- Write a Python program to switches the posi on of colors “Red” and “Black” in the following list.
6- Write a Python program that accepts an integer (n) and computes the value of n+nn+nnn.
Solu on 1:
a = 5
n1 = int("%s" % a)
n2 = int("%s%s" % (a, a))
n3 = int("%s%s%s" % (a, a, a))
n = n1 + n2 + n3
a = 5
n = a*111 + a*11 + a
7- Write a Python program that will read the file name “data.csv” and store the second line in a list
called loaded_data’
Data.csv:
Output: loaded_data=[20,45,66,71,25,120]
12,5,21,87,48,36
20,45,66,71,25,120
Filename = open(“data.csv”, r)
55,53,41,85,114,25
for line in range (0,2):
170,221,52,4,89,15
Line = filename.readline()
loaded_data=line.split(“,”)
8- Write a Python program to calculate the number of days between two dates.
Sample dates : 10/20/2023 and 11/18/2023
print(delta.days)
9- Write a Python program that determines whether a given number is even or odd, and prints an
appropriate message to the user.
number = 5
mod = number % 2
if mod > 0:
print("This is an odd number.")
else:
print("This is an even number.")
10- Write a Python func on called “list_count” to count and return the number 4 in a given list.
Example: list1 = [4, 12, 6, 7 , 4]
Print(list_count(list1) Output : 2
def list_count(numbers):
count = 0
return count