Python Programs
Python Programs
VIII IGCSE
Date :
NAME: Prepared by : Ms. Maya
If statement in python:
Write a program to accept a number and print whether it is positive, zero or negative.
Counter=10
while Counter<21:
print(Counter)
Counter=Counter + 1
Arrays :
print(NumberArray)
NumberArray = []
print(NumberArray)
Textbook Programs
1. Tickets are sold for a concert at $20 each, if 10 tickets are bought then the discount
is 10%, if 20 tickets are bought the discount is 20%. No more than 25 tickets can be
bought in a single transaction. Write a python program to calculate ticket cost.
N=0
while N<1 or N>=26 :
print("How many tickets would you like to buy? ")
N=int(input())
if N<10:
D=0
elif N<20:
D=0.1
else:
D=0.2
cost=20*N*(1-D)
print("Your tickets cost ", cost)
2. Write a python program to accept age and if age >= 18 print adult otherwise print Child
# Page 266 IF
#page 268 if
PercentageMark= float(input("Please enter a mark "))
if PercentageMark<0 or PercentageMark>100:
print( "Invalid Mark")
elif PercentageMark>49:
print("pass")
else:
print("Fail")
4. Write a python program to add marks to a StudentMark array and calculate total marks
and average.
#Standard Methods of Solution page 274
Total=0
Marks=0
StudentMark=[]
while Marks != -1:
Marks = int(input("Enter Marks : "))
if Marks != -1:
StudentMark.append(Marks)
for Marks in StudentMark:
Total = Total + Marks
Average=Total/len(StudentMark)
Average=Total/len(StudentMark)
if not found :
print("Subject not Found")
6. Write a python program to add marks to a StudentMark array and sort it.
StudentMark=[0,0,0,0,0]
Total=0
for i in range (0, 5):
StudentMark[i]= int(input("Enter Marks : "))
print(StudentMark)
top=4
swap=True
while swap and top>0:
swap=False
for i in range (0, top) :
if StudentMark[i]>StudentMark[i+1]:
swap=True
temp=StudentMark[i]
StudentMark[i]=StudentMark[i+1]
StudentMark[i+1]=temp
top=top-1
print(StudentMark)
7. Write a python program to add marks to a StudentMark array. Use range check to
ensure marks input is between 0 and 100.
#validation checks
8. Write a python program to validate the length of a password to be greater than or equal
to 8.
9. Write a python program to validate a password which should have the first character as
alphabet and last should be a digit .
#validation checks - Format (first character should be letter and last should be digit)
if (not Password[0].isalpha()):
print("First letter is not an alphabet")
if (not Password[-1].isdigit()):
print("Last character should be digit ")
#Input a positive integer, use this value to set up how many other numbers are to be input
#input these numbers and validate if number is not less than zero.
# Calculate and output the total and the average of valid numbers.
#Count Invalid and Valid Numbers.
CountInvalid=0
CountValid=0
Total=0
Average=0
for Counter in range(1, N+1):
Num = int(input("Enter a number > 0 : "))
if Num < 0:
print(Num, " is an invalid number. ")
CountInvalid=CountInvalid+1
else:
print(Num, " is a valid number. ")
CountValid=CountValid + 1
Total=Total+Num
Average=Total/CountValid
print(Total,"\n", Average)