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

Python-Basics (Chapter 6A & 6B) (1)

The document provides an introduction to Python programming for Grade 7, covering its history, features, and applications. It includes basic concepts such as data types, conditional statements, loops, and practical exercises for students to practice coding. Additionally, it outlines problem-solving techniques and examples for various programming scenarios.

Uploaded by

free fire legend
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python-Basics (Chapter 6A & 6B) (1)

The document provides an introduction to Python programming for Grade 7, covering its history, features, and applications. It includes basic concepts such as data types, conditional statements, loops, and practical exercises for students to practice coding. Additionally, it outlines problem-solving techniques and examples for various programming scenarios.

Uploaded by

free fire legend
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Python Basics

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

Algorithm Flowchart Code to machine language


Environment
Data Types

Pseudo Code Python


STRING, CHAR str
INTEGER int
REAL float
Conditional Statement
Example: Take a input number & find out the number is even or odd.

INPUT Taking from user Input taking from user in


Python
DECLARE num: INTEGER num =int(input("Enter your
INPUT num number: "))

Condition Condition in Python


IF MOD(num,2)=0 THEN if num%2==0:
OUTPUT "even" print ("even")
ELSE Else:
OUTPUT "odd" print ("odd")
ENDIF
Exercises:
Q1. Print “Hello world!”
Ans: print (“Hello World”)

Q2. Take two input from user & do addition.

Q3. Find out the largest number among 3 numbers.


Problem Solving (Q3)
Pseudo Code Python
DECLARE num1, num2, num3: REAL
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
INPUT num1,num2,num3
num3 = float(input("Enter third number: "))
IF num1> num2 AND num1>num3
if (num1 >= num2) and (num1 >= num3):
THEN
largest = num1
OUTPUT " num1 is largest"
elif (num2 >= num1) and (num2 >= num3):
ELSE
largest = num2
IF num2> num1 AND num2>num3
else:
THEN
largest = num3
OUTPUT "num2 is largest"
ELSE
print("The largest number is", largest)
OUTPUT "num3 is largest"
END IF
ENDIF
Practice Work:
4. Write a Python code that input amount in ATM Machine & print ‘accept’ if
amount is multiple of 500. Otherwise print ‘not accepted’.

5. Write a python code that input a character & print input character is
vowel or not vowel.
Q5 Solution

ch = input("Enter a character: ")

if(ch=='A' or ch=='a' or ch=='E' or ch =='e' or ch=='I' or


ch=='i' or ch=='O' or ch=='o' or ch=='U' or ch=='u’):
hhprint(ch, "is a Vowel")
else:
hh print(ch, "is a Consonant")
Loop
Life is full of routines. In programming we also do
lots of repetitive tasks. In order to handle repetitive task
programming languages use loops. Python programming
language also provides the following types of two loops:

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)

Print 1 to 10 in python code . initialValue= initialValue+1


7.Guessing Game

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

Print 1 to 10 in python code .


Practice Work:
9. Python program to print all the even numbers within the
given range (1 to 100).

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

buy_adult_ticket=int(input("enter the tickent quantity you want for adult:"))


buy_Children_ticket=int(input("enter the tickent quantity you want for child:"))
popcorn_combo=str(input("do you want popcorn combo? yes/no:"))

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 score >= 90:


print("You got an A.")
elif score >= 80:
print("You got a B.")
elif score >= 70:
print("You got a C.")
elif score >= 60:
print("You got a D.")
else:
print("You got an F.")

Find out the limitations of given code & solve it.


Age Group Classification
Description: Write a program that classifies a person based on their age.
This will teach you how to
implement conditions that categorize users into different age groups.
Task:
1.Prompt the user to enter their age.
2.Use if, elif, and else statements to classify the age into:
•Child (0 to 12)
•Teenager (13 to 19)
•Adult (20 to 64)
•Senior citizen (65 and above)
3.Print the corresponding age group.
age = int(input("Enter your age: "))

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

You might also like