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

PYTHON CONDITIONAL

Uploaded by

sareenniyati85
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 views

PYTHON CONDITIONAL

Uploaded by

sareenniyati85
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/ 4

CLASS XI PYTHON VARIABLES, OPERATORS, CONDITIONAL STATEMENTS

Q. Evaluate the following expressions:


(1) 12 + 3 * 4 - 6 / 2 (2) (12 + 3) * 4 – 6 / 2 (3) 12 + 3 * (4 – 6) / 2
(4) 12 + (3 ** 4 – 6)/ 2 (5) 12 * (3 % 4)// 2 + 6 (6) 12 % 3 ** 4//5 + 6
Variables
A variable is a named memory location which is used to store data. The contents of a variable may change during the
programs execution. A variable can store one and only one data value at a time. When a new value is stored in a
variable, its previous value gets overwritten.
The assignment operator (=)
Values are assigned to variables using assignment operator ( =). For example, the statement a=10 assigns value 10 to
variable a. The general rule of assignment is:
There should be one and only one variable on the left hand side of assignment operator. This variable is called
the Left value or the L-value. There can be any valid expression on the right hand side of assignment operator.
This expression is called the Right value or R-value. The statement:
L-value = R-value
is called the assignment statement. When the interpreter encounters an assignment statement, it evaluates the
right hand side expression (R-value) and assigns the value to the left hand side variable (L-value).
Eg. Amt2 = Price*Qty # Value 132 (22*6) is assigned to the variable Amt2 (Price=22 and Qty=6)

Python is case sensitive. Variable a and A are different.


Eg: a=10
Print (A) #will give NameError: name ‘A’ is not defined

Python keywords (these cannot be used as variable names):

and, as, assert, break, class, continue, def, del, elif, else,
except, False, finally, for, from, global, if, import, in, is,
lambda, None, nonlocal, not, or, pass, raise, return, True, try,
while, with, yield
Examples of some valid variable names are:
st_name, father_name, TotalValue, Age, Bal_Qty, A123, BillNo9
Following are examples of some invalid variable names:
Invalid Variable Name Reason
Roll Number Space is not allowed
9years Variable name cannot start with a digit
D.A.V. . is not allowed
No# Special characters are not allowed
while while is a keyword in Python, so it cannot be used as a variable name.
We can use id() function to know the address of memory location to which a variable is referring.
Following is an example to show this:

In Python, we can declare multiple variables in a single statement. There are two ways of doing this:
(i) var1, var2, var3, . . ., varn = value1, value2, value3,. . ., valuen
This statement will declare variables var1, var2, . . ., varn with values value1, value2,…,
valuen respectively.
(ii) var1=var2=var3= . . .=varn = value1
This statement will declare variables var1, var2, . . ., varn , all with value value1

1
Comments
Comments are statements in a script that are ignored by the Python interpreter, and, therefore, have no effect on the
actual output of the code. Comments make the code more readable and understandable for the human beings. This
makes the revision/modification of the code easier by the original author of the code and also by some new author who
has been assigned the responsibility to modify it. Comments are not necessary in a script, but these are highly
recommended if the script is too complex or if it is to be reviewed by multiple people over an interval of time.
Two types of comments can be added to a Python script. These are –
(i) Single line Comments (ii) Multi line comments.
 A single line comment starts with a # anywhere in a line and extends till the end of the line.
# This is a single line comment
 A multi-line comment starts and ends with three single quotes separated by single spaces (''').
'''
This is a multi-line comment
The comment ends here
'''
Assignment Operators: Assignment operators are used to assign a value to a variable. Different assignment
operators available in Python are:
Operator Description Example
x=5;
= Evaluates R-value and assigns the result to y = 2*x;
(Assign) L-value print("x=",x, "and y=",y)
gives the result: x= 5 and y= 10
x=5;
y = 10;
+= Evaluates R-value and adds it to L-value. The x += 2*y;
(Add and Assign) final result is assigned to L-value. print("x=",x, "and y=",y)
gives the result: x= 25 and y= 10
x=5;
-= y = 10;
Evaluates R-value and subtracts it from L- x -= 2*y;
(Subtract and
value. The final result is assigned to L-value. print("x=",x, "and y=",y)
Assign)
gives the result: x= -15 and y= 10
x=5;
*= y = 10;
Evaluates R-value and multiplies it with L- x -= 2*y;
(Multiply and
value. The final result is assigned to L-value. print("x=",x, "and y=",y)
Assign)
gives the result: x= 100 and y= 10
x=5;
/= Evaluates R-value and divides the L-value y = 10;
(Divide and with R-value. The quotient is assigned to L- x /= y;
Assign Quotient) value. print("x=",x, "and y=",y)
gives the result: x= 0.5 and y= 10
%= x=5;
Evaluates R-value and divides the L-value x %= 4;
(Divide and
with R-value. The remainder is assigned to L- print("x="+x)
Assign
value. gives the result: x= 1
Remainder)
x=5;
//= Evaluates R-value and divides (floor division) x //= 4;
(Floor division the L-value with R-value. The quotient is print("x="+x)
and Assign) assigned to L-value. gives the result: x= 1
x=5;
**= R-
Evaluates R-value and calculates (L-value) x %= 4;
(Exponent and value print("x="+x)
. The final result is assigned to L-value.
Assign) gives the result: x= 1
Q. Find output generated by the following code:
(1) x=2 (2) x=8 (3) a=5
y=3 y=2 b=10
x+=y x+=y a+=a+b
print(x,y) y-=x b*=a+b
print(x,y) print(a,b)

(4) p=10 (5) p=5/2 (6) p=2/5


q=20 q=p*4 q=p*4
p*=q//3 r=p+q r=p*q
q+=p+q**2 p+=p+q+r p+=p+q-r
print(p,q) r+=p+q+r r*=p-q+r
q-=p+q*r q+=p+q
print(p,q,r) print(p,q,r)

2
Comparison or Relational Operators: To form a condition we have to compare two values. Python provides
Relational operators (also called Comparison operators) to compare two values. These relational operators are:
Operator Description Example
(assuming x=6, y=2)
== Compares two values for equality. Returns true if (x == y) returns
(Equality) they are equal, otherwise returns false. false
!= (Inequality) Compares two values for inequality. Returns true (x != y) returns
if they are unequal, otherwise returns false. true
< Compares two values. Returns true if first value is (x < y) returns
(Less than) less than the second, otherwise returns false. false
<= Compares two values. Returns true if first value is (x <= y) returns
(Less than or equal to) less than or equal to the second, otherwise false
returns false.
> Compares two values. Returns true if first value is (x > y) returns
(Greater than) greater than the second, otherwise returns false. true
>= Compares two values. Returns true if first value is (x >= y) returns
(Greater than or equal greater than or equal to the second, otherwise true
to) returns false.
Logical Operators: A simple condition can be formed using a relational operator. Sometimes a condition contains
more than one comparisons. For example, to check if the marks are in the range 28 to 32. In this case we have to check
that marks are greater than or equal to (>=) 28 as well as marks are less than or equal to (<=) 32. Each relational
operator forms a simple condition. If a condition contains more than one relational operators then we can say that the
condition is complex and the complex condition is formed by joining simple conditions using Logical operators. A complex
condition can also be formed by negating a simple condition. For example, to check that a condition is not true. For
forming complex conditions, we have to use logical operators with simple conditions. Different logical operators available
in Python are:
Operator Description Example
(assuming x=6, y=2)
Negates a condition and returns true if the not(x > y) returns
not
condition is false, otherwise returns false. false
Combines two conditions and returns true if both (x >3 and y<2)
and
the conditions are true, otherwise returns false. returns false
Combines two conditions and returns true if at
(x>3 or y<2) returns
or least one of the conditions is true, otherwise
true
returns false

Conditional Statements
Python provides if statement which can be used in the following three formats as per the requirement of the script:
(i)
if condition:
Statement(s);
If the specified condition is true, then the given Statement(s) are executed, otherwise these are not
executed.

Example: To input and check the marks of a student. If the marks are in the range 28 to 32, then upgrade the
marks to 33. Then the marks (upgraded or otherwise) should be displayed.
marks = eval(input("Enter marks (out of 100): "));
if marks > 28 and marks < 33:
marks = 33;
print('Marks:',marks)
(i)
if condition:
First block of Statement(s);
else:
Second block of Statement(s);

If the specified condition is true, then the First block of Statement(s) is executed, otherwise
Second block of Statement(s) is executed.

3
Example: To input the marks of a student and calculate the result. If the marks are 33 or more then calculate
result as “Pass”, otherwise calculate result as “Fail”. Then display the result to the user.
marks = eval(input("Enter marks (out of 100): "));
if marks >= 33:
print("Pass")
else:
print("Fail")
(ii)
if condition1:
First block of Statement(s);
elif condition2
Second block of Statement(s);
elif condition3
Third block of Statement(s);
.
else
Last block of Statements(s);
If condition1 is true, then the First block of Statement(s) is executed, otherwise
condition2 is checked, if condition2 is true then Second block of statement(s) is
executed, otherwise condition3 is checked, if condition3 is true then Third block of
statement(s) is executed, and so on. If none of the specified conditions is true, then Last block of
statement(s) is executed.

Example: To input marks of a student and display the grade. Grade should be calculated as per the following criteria:
Marks range Grade
90 – 100 A
75 – 89 B
60 – 74 C
50 – 59 D
49 or less E

marks = eval(input("Enter marks: "))


if marks>=90:
grade = "A"
elif marks>=75 and marks<=89:
grade = "B"
elif marks>=60 and marks<=74:
grade = "C"
elif marks>=50 and marks<=59:
grade = "D"
elif marks<=49:
grade = "E"
print("Grade:",grade)
Indentation: Indentation is shifting of a statement to the right of another statement. In Python, indentation is used to
form blocks of statements. A block is a group of statements which is treated as a unit. If a block of statements (let us call
it B) is indented with respect to its above statement (let us call it A), then block B becomes sub-block of statement A. This
concept is used extensively in conditional statements and loops (covered later). The concept of indentation can be
understood better with the help of following figure:

You might also like