0% found this document useful (0 votes)
16 views

Python Programs

NJNDIS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Python Programs

NJNDIS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DPS INTERNATIONAL SCHOOL

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.

Num=int(input("Enter a number : "))


if Num>0:
print(Num, " is positive. ")
elif Num < 0 :
print(Num, " is negative. ")
else:
print(Num, " is zero. ")

for loop in python:

a. Write a program in python to print numbers from 10 to 20.

for Counter in range(10, 21):


print(Counter)

b. Write a program in python to print odd numbers from 50 to 100.

for Counter in range(51, 100, 2):


print(Counter)

c. Write a program in python to print prime numbers from 10 to 20.

for Counter in range(10, 21):


prime=True
for i in range(2,Counter):
if Counter%i ==0 :
prime=False
if prime:
print(Counter)
While loop in python

Write a program in python to print numbers from 10 to 20.

Counter=10
while Counter<21:
print(Counter)
Counter=Counter + 1

Arrays :

Write a program to accept 5 numbers and store in an array.

# Method 1 - NumberArray List initialized before adding elements


NumberArray = [0]*5

for Index in range(0,5):


NumberArray[Index]=int(input("Enter Number : "))

print(NumberArray)

#Method 2 - Empty NumberArray list

NumberArray = []

for Counter in range(0,5):


Num=int(input("Enter Number : "))
NumberArray.append(Num)

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.

# Flowchart page 264

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

age=int(input("Enter Age : "))


if age < 18:
print("Child")
else:
print("Adult")

3. Write a python program to accept PercentageMark and if PercentageMark is less than 0


or greater than 100 print Invalid else check if PercentageMark >49 print pass else print fail.

#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)

print("Average : ", Average,"\n","Total : ",Total)

5. Write a python program to add marks to a StudentMark array and subjects to


SubjectName array and calculate total marks and average. Also find marks scored in a subject
using Linear Search.

# Standard Methods of Solution

SubjectName=[" "," " ," "," "," "]


StudentMark=[0,0,0,0,0]
Total=0
for i in range (0, 5):
SubjectName[i] = input("Enter Subject Name : ")
StudentMark[i]= int(input("Enter Marks : "))

for i in range (0, 5) :


Total=Total + StudentMark[i]

Average=Total/len(StudentMark)

print("Average : ", Average, "\nTotal : ", Total)

sub=input("Type Subject marks to search ")


found=False
for i in range (0, 5) :
if sub==SubjectName[i]:
print("Subject = ", sub,"\n Marks = ", StudentMark[i])
found=True

if not found :
print("Subject not Found")

6. Write a python program to add marks to a StudentMark array and sort it.

# Standard Methods of Solution - Bubble sort

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

StudentMark=int(input("Enter Student Marks: "))

while StudentMark <0 or StudentMark>100:


StudentMark=int(input("Please enter marks between 0 and 100 : "))

8. Write a python program to validate the length of a password to be greater than or equal
to 8.

#validation checks - Length

Password=input("Enter Password : ")


while len(Password)<8 :
Password=input("Enter Password (Length>=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)

Password=input("Enter Password : ")


while len(Password)<8 :
Password=input("Enter Password (Length>=8) : ")

if (not Password[0].isalpha()):
print("First letter is not an alphabet")

if (not Password[-1].isdigit()):
print("Last character should be digit ")

10. Write a program to :


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

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

N=int(input("Enter a positive number : "))


while N<1:
N=int(input("Enter a positive number : "))

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)

You might also like