Python-Basics (Chapter 6A & 6B) (1)
Python-Basics (Chapter 6A & 6B) (1)
Grade 7
INTRODUCTION
What is Python? History
▪ Invented by Guido Van
Python is interpreted, object-
Rossum in 1989
oriented, high level programming
▪ First Released at 20
language with dynamic semantics.
February,1991
Feature: Application:
▪ Easy ▪ Build a website & develop game
▪ Portable ▪ Implement machine learning
▪ GUI Supports ▪ Robotics & AI
▪ Free & Open Source ▪ Real Example: Intel, IBM,NASA,
▪ Large Python Library Netflix, Facebook
BACKGROUND
Step
1 Varia
Step ble 101111
2 If 101100
Step For .
Problem 3 Print .
Solve
. . .
. .
.
5. Write a python code that input a character & print input character is
vowel or not vowel.
Q5 Solution
1.while loop
2.for loop
While loop
Syntax Python
WHILE condition:
print (“------”)
Count=count+1
initialValue = 1
while intialValue<=10:
Example: 06 print(initialValue)
Algorithm:
1. System must have a secret number.
2. System will give chance to user for 3 times for
guessing the secret number.
3.System must count guess from 1
4. System will allow user to give input when it will
be in between (3)guess limit.
5. If user’s guess & secret number is same, system
will show you a massage “you win” or after 3
chances it will show you “you lose… ”
Guessing Game
Pseudo Code:
DECLARE guessNum= INTEGER
SecretNumber =5 secret_num= 3
guessLimit=3 guess_limit =3
guessCount=1 guess_count = 1
OUTPUT “Enter your guessNumber:”
while guess_count <=guess_limit:
guess_num=int(input("guess:
WHILE guessCount<=guessLimit "))
INPUT guessNum guess_count=guess_count+1
guessCount= guessCount+1 if guess_num==secret_num:
print("you win")
IF guessNum==SecretNumber else:
print("you lose... :( )")
THEN
OUTPUT “you win”
ELSE
OUTPUT “you lose ”
ENDIF
ENDWHILE
for loop
Syntax Python
for item in ___________
print (item)
for I in range(1,11)
Print (i)
Example: 08
10. Python program to calculate the sum of all numbers from 1 to a 50.
11. Python program to calculate the sum of all the odd numbers
within 50 to 150.
Practice Work:
9. for i in range (1,101):
11. sum_odd=0
if i%2==0:
for i in range(51,151,2):
print (i)
if i%2!=0:
sum_odd += i
10. given_number=50
print(sum_odd)
sum=0
for i in range (1,given_number+1):
sum+=i
print(sum)
Practice Work:1
A cinema hall sells tickets at the following prices:
•Adults (above 12 years): 250 BDT per ticket
•Children (12 years or below): 150 BDT per ticket
Write a Python program that:
1.Asks the user to enter the number of adult tickets they want to buy.
2.Asks the user to enter the number of child tickets they want to buy.
3.Calculates the total cost of the tickets.
4.Asks the user if they want a popcorn combo (Yes/No).
5.If yes, add 200 BDT to the total cost.
6.Displays the final total bill.
Solution:
Adult_ticket= 250
Children_ticket=150
popcorn_price=200
adult_ticket_price= buy_adult_ticket*Adult_ticket
child_ticket_price= buy_Children_ticket*Children_ticket
total_ticket_price=adult_ticket_price+child_ticket_price
if (popcorn_combo=="yes" or popcorn_combo=="YES") :
total_ticket_price+=popcorn_price
print(total_ticket_price)
Practice Work:2
The late fee system works as follows:
• 0 to 5 days late: No late fee.
• 6 to 10 days late: $1 per day.
• 11 to 15 days late: $2 per day.
• 16 or more days late: $5 per day.
Write a Python program that will:
• Ask the user how many days they are late in returning the book.
• Use if, elif, and else statements to calculate the late fee based on the number
of days late.
• Print out the total late fee and a message. For example, "Your late fee is $10.
Please return the book on time next time!" or "You don't have any late fee.
Keep up the good work!"
Days_late= int(input("Enter the days you late:")
if days_late <= 5:
Print ("You don't have any late fee. Keep up the good work!")
elif days_late <= 10:
fee = days_late * 1
return fee, f"Your late fee is ${fee}. Please return the book on time next
time!"
elif days_late <= 15:
fee = days_late * 2
return fee, f"Your late fee is ${fee}. Please return the book on time next
time!"
else: # days_late > 15
fee = days_late * 5
return fee, f"Your late fee is ${fee}. Please return the book on time next
time!"
Exercise 2: Grade Evaluation
Description: Create a program that assigns a letter grade
based on a numeric score (ranging from 0 to 100). This
exercise will enhance your ability to categorize data using
conditional statements.
Task:
1.Ask the user to input their score.
2.Use conditional statements to evaluate the score:
1.A for scores 90 and above
2.B for scores 80 to 89
3.C for scores 70 to 79
4.D for scores 60 to 69
5.F for scores below 60
3.Print the letter grade.
score = float(input("Enter your score: "))
if age < 0:
print("Invalid age.")
elif age <= 12:
print("You are a child.")
elif age <= 19:
print("You are a teenager.")
elif age <= 64:
print("You are an adult.")
else:
print("You are a senior citizen.")
Find out the limitations of given code & solve it.
THANK YOU