Introduction of Python, Operators, Conditional Statements
Introduction of Python, Operators, Conditional Statements
name="Devansh"
age=6
print("Name:",name)
print("Age:",age)
• All statements with the same distance to the right belong to the
same block of code.
• If a block has to be more deeply nested, it is simply indented
further to the right.
• Leading whitespace (spaces and tabs) at the beginning of a logical
line is used to compute the indentation level of the line, which in
turn is used to determine the grouping of statements.
• Ex:
a=input("enter a")
b=input("enter b")
if a>b:
print('a is big')
print("hi")
else:
print("b is big")
x=5
y=2
print('Addition=',x+y)
print('subtraction =',x-y)
print('multiplication =',x*y)
print('division =',x/y) Output:
Addition= 7
print('Floor dvision =',x//y) subtraction = 3
multiplication = 10
print('Remainder =',x%y) division = 2.5
print('Exponentiation =',x**y) Floor dvision = 2
Remainder = 1
Exponentiation = 25
x = 10
y = 12
print('x > y is',x>y)
print('x < y is',x<y)
print('x == y is',x==y)
print('x != y is',x!=y)
print('x >= y is',x>=y)
print('x <= y is',x<=y)
Ex:
x=5
Output:
False
True
False
T Seshu Chakravarthy Department Of Computer Science and Engineering
Membership operators
x = 'Hello world'
print('H' in x)
print('S' in x)
print('Hello' not in x)
Output:
True
False
False
X<<n=x*2n
X=10;
X<<2=10x22
=10x4= 40
Ex:
a1 = 3
b1 = 3
print(a1 is b1)
print(a1 is not b1)
• Operators have different levels of precedence, which determine the order in which
they are evaluated.
• When multiple operators are present in an expression, the ones with higher
precedence are evaluated first.
• In the case of operators with the same precedence, their associativity comes into play,
determining the order of evaluation.
=(100 / 10) * 10
=10*10
value=100
1.Multiplication: 3 * 2 = 6
2.Addition: 4 + 6 = 10
3.Bitwise AND: 10 & 4 = 0 (In binary, 10 is 1010 and 4 is 0100. The result of a
bitwise AND is 0 where there are differing bits.)
4.Bitwise OR: 0 | 6 = 6 (In binary, 0 is 0000 and 6 is 0110. The result of a bitwise OR
is 1 where there is at least one 1.)
So, the final result of the expression is 6.
• Here, the program evaluates the test expression and will execute
statement(s) only if the text expression is True.
• If the text expression is False, the statement(s) is not executed.
• In Python, the body of the if statement is indicated by the indentation.
• Body starts with an indentation and the first unindented line marks
the end.
T Seshu Chakravarthy Department Of Computer Science and Engineering
if statement
num = 3
if num>0:
print(num, "is a positive number.")
print("This is always printed.")
• if...elif...else Statement allows you to execute different blocks of code based on specified
conditions.
print(season)