Professional Documents
Culture Documents
Artificial Intelligence
Lab 2
Introduction to python / Input / Output
1. Introduction to python IDLE
2. Writing first program in python.
3. Write a program that inputs name, age and address from the user and then displays these
values on the screen.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
address = input("Enter your address: ")
print("Name:", name)
print("Age:", age)
print("Address:", address)
4. Write a program that input two numbers and display their sum on screen.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum = num1 + num2
print("Sum= ", sum)
5. Write a program that inputs radius from the user and calculate area and circumference of
circle using formula and.
radius = float(input("Enter the radius of circle: "))
area = 3.14 * radius * radius
circ = 2 * 3.14 * radius
area = round(area,2)
circ = round(circ,2)
print("Area of circle is ",area)
print("Circumference of a circle is ",circ)
6. Write a program that inputs two numbers, swap their values without using third variable
and display them.
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
x = x + y
y = x - y
x = x - y
print("Numbers after swapping")
print("First Number: ",x)
print("Second Number: ",y)
7. Write a program that inputs time in seconds and convert it into HH:MM:SS format.
sec = int(input("Enter time in seconds: "))
hour = sec//3600
sec = sec%3600
minute = sec//60
sec = sec%60
print("Hours: ",hour)
print("Minutes: ",minute)
print("Seconds: ",sec)
8. Write a program that inputs two numbers and apply mathematical operation like addition,
subtraction, multiplication, division and remainder. And display the result on screen.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum = num1 + num2
sub = num1 - num2
multi = num1 * num2
divide = num1 / num2
remainder = num1 % num2
print("Sum= ",sum)
print("Subtract= ",sub)
print("Multiply= ",multi)
print("Divide= ",divide)
print("Remainder= ",remainder)
9. Write a program that input a character and display its ASCII code.
c = input("Enter a character: ")
asci = ord(c)
print("ASCII code of",c, "is" ,asci)
10. Write a program that will prompt the user to enter number of hours. It computes and
displays the number of weeks, days and hours within the input number of hours.
hours = int(input("Enter the number of hours: "))
week = hours//168
hours = hours%168
days = hours//24
hours = hours%24
print("Weeks: ",week)
print("Days: ",days)
print("Hours: ",hours)
11. Write a program that will prompt the user to enter the current rates of electricity, gas and
petrol per unit. Give each item’s rate an increment of 10%. Computer and display the
new prices per unit of electricity, gas and petrol.
electricity = int(input("Enter the current rate of
electricity: "))
gas = int(input("Enter the current rate of gas: "))
petrol = int(input("Enter the current rate of petrol: "))
1. Write a program that inputs marks and displays “Congratulations! You have passed.” If the
marks are 40 or more.
marks = int(input("Enter marks: "))
if marks>=40:
print("Congratulations! You are passed ")
2. Write a program that inputs marks of three subjects. If the average of marks is more than 80,
it displays two messages. “You are above standard!” and “Admission granted”.
marks1= int(input("Enter frst marks: "))
marks2= int(input("Enter second marks: "))
marks3= int(input("Enter third marks: "))
avg = (marks1 + marks2 + marks3)/2
if avg>=80:
print("You are above standard!")
print("Admission granted.")
3. Write a program that inputs a number and finds whether it is even or odd using if-else
statement.
num= int(input("Enter the number: "))
if num %2==0:
print("This is even number")
else:
print("this is odd number")
4. Write a program that inputs salary and grade. It adds 50% bonus if grade is greater than 15. It
adds 25% bonus if the grade is 15 or less and then displays the total salary.
salary= int(input("Enter the salary: "))
grade= int(input("Enter the grade: "))
bonus = 0
total_salary = 0
if grade >15:
bonus = salary * 50/100
else:
bonus = salary * 25/100
total_salary = bonus + salary
print("Salary: ",salary)
print("Bonus: ",bonus)
print("Total_salary: ",total_salary)
5. Write a program that inputs test score of a student and displays his grade according to the
following criteria:
Test score Grade
>= 90 A
80 – 89 B
70 – 79 C
60 – 69 D
BELOW 60 F
9. Write a program that allows the user to enter any character through keyboard and determine
whether is capital letter, small letter, a digit number or a special symbol.
10. Write a program that inputs three digits and displays all possible combinations of these
digits.
Lab 4
Loop Introduction / For Loop
1. Write a program that displays “Pakistan” for five times using for loop.
for i in range(0,5):
print("Pakistan")
2. Write a program that displays counting from 1 to 10 using for loop.
for i in range(1,11):
print(i)
3. Write a program that displays first five numbers and their sum using for loop.
sum = 0
for i in range(1,6):
print(i)
sum = sum + i
print("Sum = ",sum)
4. Write a program that inputs a number from the user and displays a table of that number using
for loop.
table = int(input("Enter table: "))
for i in range(1,11):
print(table, " * ", i, "=", table * i)
5. Write a program that displays sum of the following series using for loop.
r=1
for c in range(2,101,2):
r = r + 1/c
print("Sum = ",round(r,2))
6. Write a program that inputs starting and ending number from the user and displays all even
numbers in the given range using for loop.
start = int(input("Enter starting value: "))
end=int(input("Enter ending value: "))
for i in range(start,end+1):
if i%2==0:
print(i)
7. Write a program that finds the sum of the squares of integers from 1 to n. where n is a
positive value entered by the user. ( )
n = int(input("Enter value: "))
sum =0
for i in range(1,n+1):
sum = sum + (i*i)
print("Sum of squares = ",sum)
8. Write a program that displays counting from 1 to 10 using while loop.
i=1
while i<=10:
print(i)
i = i+1
9. Write a program that displays first five numbers and their sum using while loop.
sum = 0
i=1
while i<=5:
print(i)
sum = sum + i
i=i+1
10. Write a program that inputs a number from the user and displays a table of that number using
while loop.
table = int(input("Enter table: "))
i=1
while i<=10:
print(table, " * ", i, "=",table*i)
i=i+1
Lab 5
While Loop
1. Write a program that uses a while loop to enter number from the user and then display it. The
loop is terminated when user enters -1.
num = int(input("Enter the number: "))
while num != -1:
print(num)
num = int(input("Enter the number: "))
2. Write a program that inputs a number from the user and displays a table of that number using
while loop.
table = int(input("Enter table: "))
i=1
while i<=10:
print(table, " * ", i, "=",table*i)
i=i+1
3. Write a program that inputs an integer and displays the sum of its digits. For example the
program should displays 9 if user enter 135.
num = (input("Enter a number: "))
sum = 0
for digit in str(num):
sum += int(digit)
print("Sum of",num,"is",sum)
4. Write a program that displays sum of the following series using while loop.
c=2
r=1
while c<=100:
r = r + 1/c
c+=2
print("Sum = ",round(r,2))
5. Write a program that inputs a number and check whether it is an Armstrong number or not. A
number is armstrong if the sum of the cubes of its digits is equal to the number itself. For
example, 371 is an armstrong number since
num = int(input("Enter a number: "))
n = num
sum = 0
r=0
while n!=0:
r = n%10
sum = sum + (r**3)
n = n//10
if sum == num:
print(num,"is an armstrong number")
else:
print(num,"is not an armstrong number")
6. Write a program that inputs numbers until the user enters a negative number. The program
calculates the average, maximum and minimum of all positive numbers.
n = int(input("Enter a number: "))
sum=0
minimum = n
maximum = n
avg=0
c=0
while n>0:
sum = sum + n
c+=1
if n>maximum:
maximum = n
elif n<minimum:
minimum = n
n = int(input("Enter a number: "))
if c == 0:
print("No Positive number entered!!")
else:
avg = sum/c
print("You entered",c,"numbers")
print("Average: ",avg)
print("Maximum: ", maximum)
print("Minimum: ", minimum)
7. Write a program that inputs a number and checks whether it is a palindrome or not. A
palindrome is a number that reads the same backwards as forwards such 62526 and 4994
num=int(input("Enter a number:"))
temp=num
rev=0
while(num>0):
dig=num%10
rev=rev*10+dig
num=num//10
if(temp==rev):
print("The number is palindrome!")
else:
print("Not a palindrome!")
k = rows - 2
def cb(n):
return n**3