0% found this document useful (0 votes)
2 views11 pages

Python Notes for Class VII - Annual Exam (1)

This document provides Python notes for Class 7, covering operators, control structures, and programming examples. It explains various types of operators, including arithmetic, string, assignment, and their precedence, along with control structures like sequential and conditional statements. Additionally, it includes programming exercises to practice these concepts.

Uploaded by

mairagrover3
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)
2 views11 pages

Python Notes for Class VII - Annual Exam (1)

This document provides Python notes for Class 7, covering operators, control structures, and programming examples. It explains various types of operators, including arithmetic, string, assignment, and their precedence, along with control structures like sequential and conditional statements. Additionally, it includes programming exercises to practice these concepts.

Uploaded by

mairagrover3
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/ 11

Python Notes for Class 7

Operators
Operators are symbols that perform arithmetic and logical operations on the operands and
provide a meaningful result. An operator needs one or more operands to perform operations.
The valid combination of both operands and operators makes an expression that returns a
computed result.

Types of Operators
The following are the different types of operators that can be used while programming:
Arithmetic Operators
The arithmetic operators are used to perform basic mathematical calculations, i.e., Addition
(+), Subtraction (-), Multiplication (*), Division (/), etc.

Operator Symbol Usage Example


Addition + To obtain the sum of the values Value of 13+3.5 is 16.5
Subtraction - To subtract the values Value of 45 – 35 is 10
Multiplication * To find the product of the data Value of 6.2*6 is 37.2
Division / To divide the numbers and give an Value of 5/2 is 2.5
output in the decimal form
Program: Write a program to find the area and circumference of a circle whose radius is
entered by the user.

String Operators
While working with the string values, Python allows only two types of operators on the string
data types. You can either join two strings or replicate a string multiple times. For these two
operations, you use '+' and '*' operators on strings, respectively. The '+' operator is termed
the concatenation operator when you use it with the strings. It is used to concatenate or join
two or more strings.
Program:

The ‘*’ operator is used to replicate a given string specified times. It is also known as the
replication operator.
Program:
Here, the ‘*’ operator is used to print the string, 'Welcome' three times as you can see in the
output above.
Practice This!
Predict the output of the following statements:

S. No. Statement Output


1. Print(52*7)
2. Print(“52*7”)
3. Print(“52*7=”, 52*7)
4. Print(“52”*7)

Program: Write a program to print the string entered by the user five times.

Assignment Operator
The Assignment operator (=) is used to assign a value to a variable. It assigns the value on its
right side to the variable written on the left of it. You have used this operator previously in
the programs.
Program:

In the above example, you have assigned the value 10 to the variable 'a' and then the product
of 'a' and 10 is assigned to the variable 'b'.
Operator Precedence
An expression in Python may have more than one operator involved in it. For example :
a=5+2–3
In this statement, what should be done first? Addition or Subtraction?
When more than one operator is to be evaluated in an expression, the Python interpreter
decides which operator should be evaluated first at run time. This decision is based on the
precedence and associativity of the operators.
The order is shown below:
If there are 2 operators which have the same order then they are calculated from left to right.

Operator Description
() Parenthesis
** Exponentiation
+ a, -a + Unary, - Unary
*,/,//,% Multiplication, Division, Floor Division, Modulus (Remainder)
+, - Binary addition and subtraction
<, <=, >, >=, = =, != Relational operators
NOT
AND Boolean/Logical operators
OR
Operator Precedence
Higher precedence operators are operated before the lower precedence operators. When an
expression contains operators with the same precedence (like and /), then whichever
operator comes first is evaluated.
For example: Evaluate the following expressions:

12 + 3 * 4 – 6/2 (12 + 3) * 4 – 6/2 12 + 3 * (4 – 6)/2


Solution Solution Solution
= 12 + 12 – 6/2 = 15*4 – 6/2 = 12 + 3* (-2)/2
= 12 + 12 – 3.0 = 60 – 6/2 = 12 + (-6)/2
= 24 – 3.0 = 60 – 3.0 = 12 – 3.0
= 21.0 = 57.0 = 9.0

Types of Control Structures


The following are the different types of control structures that govern the flow of execution
in programs:
Sequential
In a Sequential construct, the statements in a program are executed in a sequential manner,
where one statement is followed by the other.
Program
A program to calculate the area of a rectangle; first, enter the length and breadth of the
rectangle, then calculate its area and print the result.

Conditional
In programming languages, conditional statements (also known as decision-making
statements) cause the program control to transfer to a specific location depending on the
outcome of the conditional expression. Every decision involves a choice between two
alternatives 'Yes' and 'No'. If a conditional statement is true, then one set of statements is
executed, otherwise, the other set of statements is executed.
Let us take another example. You need to have a valid driving license for driving any vehicle.
To apply for a driving license, your age should be greater than or equal to 18.

Condition Plan of Action


If your age is greater than or equal to 18 Then you are eligible to apply for a driving
license
If your age is less than 18 Then you are not eligible to apply for a driving
license

Conditional Statements
Before you start writing programs based on conditions, it is important to understand how
conditional programming works. In conditional programming, the most important element is
the condition on which the decision is based. The conditional statements check the condition
and execute the statements accordingly. These statements can be represented in many
forms:
The if Statement
The if statement is used when you have to evaluate only one condition. It performs a course
of action if the condition evaluates to true, otherwise, it skips the statements.
For example, your parents allow you to go out for playing only if you complete your
homework. Syntax:
if <condition>:
Statement 1
Statement 2
Remember that after the if condition, there is a colon and the condition body starts with an
indentation of the tab space. It is mandatory in Python to indent the statements in the
condition body, else it will display an error.
Let us understand this through a Python program:
Program 8:

So, in the above example, if you enter a number less than 200, it shows you the output "The
number is less than 200".
But, what will happen if you enter a number greater than 200? No output will be displayed
because you have not given any statement or instruction to be followed if the given condition
is false.
This is demonstrated in the following example:
Program 9:

Here, nothing is displayed when you enter a number greater than 200.
The if...else Statement
The if... else control structure is used when either of the two different actions is to be
performed depending upon the result of the conditional expression. It contains two blocks of
statements. If the conditional expression evaluates to true, the statements in the 'if' block get
executed, and if the result is false, then the statements in the 'else' block get executed.
For example, you can go out to play if it does not rain, else you have to play indoor games.
Syntax:
if <condition>:
Statements set 1
else:
Statements set 2
Program 10:

In this example, when you enter a number less than 200, the first part, i.e., the 'if' part, is
executed and you get the output, "The number is less than 200".
When you enter a number greater than 200, in this case, the statement following the 'else'
part gets executed and displays the output, "The number is greater than 200"
PYTHON PROGRAMS
*WAP stands for Write a Program
PROGRAMS USING VARIABLES AND INPUT STATEMENT
(1) WAP to find the sum of any two numbers

(2) WAP to convert hours given by the user into minutes and seconds

(3) WAP to convert temperature from Celsius to Fahrenheit

(4) WAP to find the area and circumference of a circle

(5) WAP to accept the year and print the age.

PROGRAMS USING CONDITIONAL STATEMENTS (IF-ELSE)


(1) WAP to find whether a person can vote or not.

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


if(age>=18):
print("You can vote")
else:
print ("You cannot vote")
(2) WAP to find whether water is boiling or not.

temp = int(input("Enter the temperature of water: "))


if(temp >= 100):
print ("Water is boiling")
else:
print ("Water is not boiling")

(3) WAP to find the greater of 2 numbers.

a = int (input( "enter a number: "))


b = int (input( "enter another number: "))
if (a==b):
print ("both numbers are the same")
if (a>b):
print ("the first number is greater")
if (b>a):
print ("the second number is greater")

(4) WAP to find whether a person is a baby, child, teenager, adult or old.

age = int(input("enter your age: "))


if (age <= 5):
print ("you are a baby")
if ((age > 5) & (age <= 12)):
print ("you are a child")
if ((age>12) & (age <=19)):
print ("you are a teenager")
if ((age>19) & (age<60)):
print ("you are an adult")
if (age >= 60):
print ("you are old")
(5) WAP to input marks of a student in all subjects. Calculate and display the total
marks and percentage. Also print whether the student has passed or failed.

name= input ("what's your name? ")


print ("\n")
a = int(input("enter marks in eng: "))
b = int(input("enter marks in hin: "))
c = int(input("enter marks in pun: "))
d = int(input("enter marks in mat: "))
e = int(input("enter marks in isc: "))
f = int(input("enter marks in sst: "))
total = a+b+c+d+e+f
perc = total/6
print ("**********report card**********")
print ("\n")
print ("Name: : ", name)
print ("\n")
print ("English : ", a)
print ("Hindi : ", b)
print ("Punjabi : ", c)
print ("Maths : ", d)
print ("Science : ", e)
print ("Social Science : ", f)
print ("\n")
print ("TOTAL MARKS : ", total)
print ("PERCENTAGE : ", perc)
print ("RESULT : ")
if(perc >= 33):
print (name, ", Congratulations! You have passed")
else:
print (name, ", Sorry. You have failed.")
PYTHON WORKSHEET
1. In the given program, circle the three mistakes and rewrite the program correctly.
a = int (input( enter a number: ))
b = int (input "enter another number: ")
if (a==b):
print ("both numbers are the same")
if (a>b)
print ("the first number is greater")
if (b>a):
print ("the second number is greater")

2. In the given program, fill in the missing lines to find the greater of two numbers.
a = int (input( "enter a number: "))
b = int (input( "enter another number: "))
__________________________________
print ("both numbers are the same")
__________________________________
print ("the first number is greater")
__________________________________
print ("the second number is greater")

3. In the given program, write the output if the value of temp is 100.
temp = int(input("Enter the temperature of water: "))
if(temp >= 100):
print ("Water is boiling")
else:
print ("Water is not boiling")

4. WAP to accept 2 numbers from the user. Print the sum and product of these
numbers.

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________

You might also like