Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Python Practice Solutions

The document provides 10 Python exercises involving various programming concepts like functions, dates, lists, tuples, files, and conditionals. The exercises cover skills like defining functions, working with dates, splitting strings, reading/writing files, and determining even/odd numbers.

Uploaded by

blossomb8090s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Practice Solutions

The document provides 10 Python exercises involving various programming concepts like functions, dates, lists, tuples, files, and conditionals. The exercises cover skills like defining functions, working with dates, splitting strings, reading/writing files, and determining even/odd numbers.

Uploaded by

blossomb8090s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

BAI 3301

Python Exercises

1- Write a Python program to display the current date and me.

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.

from math import pi


Def CircleArea(r):

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”.

Def CircleArea(lastname, firstname):

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.

color_list = ["Red","Green","White" ,"Black"]

color_list = ["Red", "Green", "White", "Black"]


temp = color_list[0]
color_list[0] = color_list[-1]
color_list[-1] = temp

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

Solu on 2: (Hiba Msa a’s solu on)

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

from datetime import date


f_date = date(2023, 10, 10)
l_date = date(2023, 11, 18)
delta = l_date - f_date

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

for num in numbers:


if num == 4:
count = count + 1

return count

You might also like