Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

2. Python Operators and Control Flow Statements

The document provides an overview of Python operators and control flow statements, including arithmetic, comparison, logical, and membership operators. It explains conditional statements like if, if-else, and nested if statements, as well as looping constructs such as for and while loops. Additionally, it covers loop manipulation techniques like continue, break, and pass, along with example programs demonstrating these concepts.

Uploaded by

folekiv959
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)
5 views

2. Python Operators and Control Flow Statements

The document provides an overview of Python operators and control flow statements, including arithmetic, comparison, logical, and membership operators. It explains conditional statements like if, if-else, and nested if statements, as well as looping constructs such as for and while loops. Additionally, it covers loop manipulation techniques like continue, break, and pass, along with example programs demonstrating these concepts.

Uploaded by

folekiv959
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/ 8

2.

Python Operators and Control Flow Statements

Operators

1. Arithmetic Operators:

2. Comparison/Relational Operators:
3. Assignment Operator:

4. Logical Operators:
5. Bitwise Operators:

6. Membership Operators:
Python membership operators are used to check the membership of value
inside a Python data structure. If the value is present in the data structure, then
the resulting value is true otherwise it returns false.

7. Identity Operators:
Conditional Statements / Decision Making Statements

1. if Statement:
The if statement is used to test a particular condition and if the condition is true, it
executes a block of code known as if-block. The condition of if statement can be
any valid logical expression which can be either evaluated to true or false.
Syntax:
if expression:
statement

2. if-else Statement:
The if-else statement provides an else block combined with the if statement which
is executed in the false case of the condition.
If the condition is true, then the if-block is executed. Otherwise, the else-block is
executed.
Syntax:
if condition:
#block of statements
else:
#Another block of statements (else-block)

3. elif Statement:
The elif statement enables us to check multiple conditions and execute the specific
block of statements depending upon the true condition among them.
However, using elif is optional.
Syntax:
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements

4. Nested if Statements:
When a programmer writes one if statements inside another if statement then it is
called a nested if statement.
Syntax:
if condition1:
if condition2:
statement1
else:
statemen3
Looping in Python

1. for Loop:
The for loop in Python is used to iterate the statements or a part of the program
several times. It is frequently used to traverse the data structures like list, tuple, or
dictionary.
Syntax:
for iterating_var in sequence:
statement(s)

2. Nested for Loop:


Python allows us to nest any number of for loops inside a for loop. The inner loop
is executed n number of times for every iteration of the outer loop.
Syntax:
for iterating_var1 in sequence:
for iterating_var2 in sequence:
#block of statements
#Other statements

3. while Loop:
It is a most basic looping statement in Python programming. It execute a sequence
of statements repeatedly as long as a condition is true.
Syntax:
X = 0 #initialize counter
while condition:
# statements
# increment / decrement
Loop Manipulation

1. continue
The continue statement in python is used to bring the program control to the
beginning of the loop.
The continue statement skips the remaining lines of code inside the loop and start
with the next iteration.
It is mainly used for a particular condition inside the loop so that we can skip
some specific code for a particular condition.
Syntax:
continue

2. pass
In Python, pass keyword is used to execute nothing.
It means, when we don't want to execute code, the pass can be used to execute
empty.
Syntax:
pass

3. break
break is used to abort the current execution of the program and the control goes to
the next line after the loop.
The break is commonly used in the cases where we need to break the loop for a
given condition.
Syntax:
break

4. else
The else block is executed when the condition given in the loop statement
becomes false.
Syntax:
Else
❖ Write a program to print following
1
12
123
1234

for i in range(1,5):
for j in range(1,i+1):
print(j,end=' ')
print()

❖ Write python program to illustrate if else ladder.


i = 20
if (i == 10):
print ("i is 10")
elif (i == 15):
print ("i is 15")
elif (i == 20):
print ("i is 20")
else:
print ("i is not present")

output:
i is 20

❖ Write python program to display output like.


2
4 6 8
10 12 14 16 18

a=2
for i in range(1,5):
for j in range(i):
print(a,end="\t")
a+=2
print()

❖ Print the following pattern using loop:


1010101
10101
101
1
p = '1'
q = '0'
j=0
k=4
while k >= 1:
print(" "*j + (k-1)*(p+q) +p+ " "*j)
k = k-1
j=j+1

You might also like