Python Notes for Class VII - Annual Exam (1)
Python Notes for Class VII - Annual Exam (1)
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.
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:
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:
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.
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
(4) WAP to find whether a person is a baby, child, teenager, adult or old.
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.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________