You are on page 1of 8

Q1: Write a program that check if the year is leap or not.

Hint: leap year is divided by 400

In [ ]: year = int(input("Please Enter the year: "))

if ((year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0))):


print("%d is a leap year" %year)
else:
print("%d is not a leap year" %year)
#2023

2023 is not a leap year

Q2: Given the following grading scale, you are asked to write a program that will allow a
student to enter a test score and then display the proper message for that score.

Percentage Message 90 and above A , 80-89 B , 70-79 C , 60-69 D , Below 60 Fail.

In [ ]: grade = int(input("Enter your grade: "))

if grade >= 90:


print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
elif grade >= 60:
print("D")
else:
print("You did not pass the exam")

#i entered 75

Q3: Write a python program that read number then find if this number odd or even.

In [ ]: x = int(input("Enter number: "))

if x % 2 == 0:
print("Even")
else:
print("Odd")

# 5

Odd

Q4: Write a python program that read two numbers and find if second is multiple of first.

In [ ]: x = int(input("Enter number: "))


y = int(input("Enter number: "))

if y % x == 0:
print("True")
else:
print("False")
#2 , 4

True

Q5: Write a python program that find root of quadratic equation.

In [ ]: #import math

# ax2 + bx + c = 0

a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))

x = b**2 - 4*a*c

if x < 0:
print("The roots are complex numbers.")
else:
sq1 = (-b + (x)**(1/2)) / (2 * a)
sq2 = (-b - (x)**(1/2)) / (2 * a)
print("Root1:", sq1)
print("Root2:", sq2)

# 6 , -17 , 12

Root1: 1.5
Root2: 1.3333333333333333

Q6: Write a python program that check if number is prime or not.

In [ ]: x=int(input("enter the num :"))


if x==1:
print(x,"is prime")
elif x > 1 :
for i in range(2,x):
if x%i==0:
print(x,"not prime")
break
else:
print(x,"is prime")

else:
print("error")

# 7

7 is prime

Q7: Using a while loop, ask the user how long their bus ride was this month until they enter
0. Print the sum of these numbers.

In [ ]: sum = 0
data = int(input("How long your bus ride? (enter 0 to end): "))
while data != 0:

sum += data
data = int(input("How long your bus ride? (enter 0 to end): "))

print("Sum:", sum)

#100 , 200 , 300 , 0 to end

Sum: 600

Q8: Using a while loop, ask the user for the length of their bus/car ride until the user enters
0. Print the average of these numbers.

In [ ]: sum = 0
counter = 0
data = int(input("How long your bus ride? (enter 0 to end): "))

while data != 0:

sum += data
counter += 1
data = int(input("How long your bus ride? (enter 0 to end): "))

average = sum / counter

print("Average:", average)

#20 , 40 , 60 , 0 to end

Average: 40.0

Q9: Request the user to type numbers, each time printing its triple, until the user enters -1.

In [ ]: x = int(input("Enter number: "))

while x != -1:

print(3 * x)
x = int(input("Enter number: "))

# 3 , 6 , 5 , -1 to end

9
18
15

Q10: Write a Program to find out no. of Even & Odd no. in each Data Series and terminate if
entered value is -1

In [ ]: x = int(input("Enter number: "))


even = 0
odd = 0
while x != -1:
if x % 2 == 0:
even += 1
else:
odd += 1
x = int(input("Enter number: "))

print("Odd:",odd)
print("Even:",even)
#1,2,3,4,5,6,7,8,9 , -1 to end

Odd: 5
Even: 4

Q11: Write a program that calculates and prints the product of the odd integers from 1 to
100.

In [ ]: product = 1

for i in range(1,101,2):

product *= i

print(product)

2725392139750729502980713245400918633290796330545803413734328823443106201171875

Q12: Suppose we have the following list: List=[[‘Ali’,50,10,80],[‘Noha’,100,50,30],


[‘Fadi’,10,70,10]] A. Print the average of the first grade for all students B. Print the name of
the student and his/her average grade

In [ ]: sum=0
count=0
List=[["Ali",50,10,80],["Noha",100,50,30],["Fadi",10,70,10]]
for i in range(len(List)):
#for j in range(len(List[i])):
sum+=List[i][1]
count+=1
print(sum/count)

List=[["Ali",50,10,80],
["Noha",100,50,30],
["Fadi",10,70,10]
]
for i in range(len(List)):
sum=0
count=0
for j in range(1,len(List[i])):
sum+=List[i][j]
count+=1
print(List[i][0],(sum/count))

53.333333333333336
Ali 46.666666666666664
Noha 60.0
Fadi 30.0
FILE 2
Q1: Write a python function that calculate the age by taking the year of birth

In [ ]: def year(x):
age = 2023 - x
#print("age is ",age)
return age
x = int(input("enter the year of birth"))
print("age is ",year(x))

age is 20

Q2: Write a program that read data from user then store in files.

In [ ]: # r read the file


# w create and write if the file have a data remove the old data and create new dat
# a add data
# x new file and if the file exist give error

my_file=open("the grades.txt","w")
x=int(input("enter num of std"))
for i in range(x):
id=str(input("enter id"))
std=str(input("enter name"))
grade=str(input("enter grade"))
my_file.write(id +" "+std +" "+ grade +"\n")

my_file.close()

my_file=open("the grades.txt","r")
print(my_file.read())
my_file.close()

1 abdo 3.8
2 bassel 3.0

Q3: Write a function divide () that used try command to prevent division of zero

In [ ]: try:
x=int(input("enter the "))
y=int(input("enter the "))
print(x/y)
except ZeroDivisionError :
print("your divide by 0")
# 10 , 0

your divide by 0
Q4: Assume a file containing a series of integers is named grades.txt and exists on the
computer’s disk. Write a program that calculates the average of all the numbers stored in the
file

In [ ]: new_file=open("grade.txt","w")
x=int(input("enter num of grades : "))
for i in range(x):
grade=str(input("enter the grade : "))

new_file.write(grade+"\n")
new_file.close()

new_file=open("grade.txt","r")
sum=0
count=0
for i in new_file:
sum+=int(i)
count+=1
print(sum/count)
new_file.close()

# 3 , 40 , 60 , 80

60.0

Q5: Write a Python program to read a file line by line and store it into a list.

In [ ]: new_file=open("grade.txt","r")
print(new_file.readlines())
new_file.close()
# we used the code above

['40\n', '60\n', '80\n']

Q6: write a class called coin it has:

1. One attribute sideup take two values Head or Tail.


2. Two method called toss and get_sideup.

In [ ]: import random
class coin :
def __init__(self,sideup="Head"):
self.sideup=sideup
def toss(self):
if random.randint(0,1)==0:
self.sideup="Tail"
else:
self.sideup="Head"
def get_sideup(self):
return self.sideup
coin1=coin()
coin1.toss()
print(coin1.get_sideup())

# run the code More than once

Head

Q7: Write a GUI program the converts from miles to kilometers


In [ ]: import tkinter as tk

def main():

window = tk.Tk()
window.title("Miles to kilometers Converter")
window.geometry("375x200")

label1 = tk.Label(window, text="Enter Miles:")


label2 = tk.Label(window, text="Kilometers:")

label1.place(x=50, y=30)
label2.place(x=50, y =100)

textbox1 = tk.Entry(window, width=12)


textbox1.place(x=200, y =35)

label3 = tk.Label(window, text=" ")


label3.place(x=180, y=100)
def btn_click():
kilom = (int(textbox1.get())) * (1.60934)
label3.configure(text = str(kilom) + ' Kilometers')
btn1 = tk.Button(window, text="Convert", command=btn_click)
btn1.place(x=90, y=150)
window.mainloop()
main()

In [ ]: import tkinter as tk

def convert():
miles = float(miles_entry.get())
kilometers = miles * 1.60934
result_label.config(text=f"{miles} miles is equal to {kilometers:.2f} kilometer

# Create the main window


window = tk.Tk()
window.title("Miles to Kilometers Converter")

# Create a label and an entry field for miles


miles_label = tk.Label(window, text="Miles:")
miles_label.pack()
miles_entry = tk.Entry(window)
miles_entry.pack()

# Create a button to perform the conversion


convert_button = tk.Button(window, text="Convert", command=convert)
convert_button.pack()

# Create a label to display the result


result_label = tk.Label(window)
result_label.pack()

# Start the main loop


window.mainloop()

You might also like