0% found this document useful (0 votes)
5 views26 pages

Unit 1 Control Statements in Python

The document provides an overview of control statements in Python, including indentation, conditional statements (if, if-else, if-elif-else), loop statements (while and for), and jump statements (break and continue). It explains the syntax and usage of each statement type with examples. Additionally, it introduces lambda functions for creating small anonymous functions.

Uploaded by

monica.patil
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 views26 pages

Unit 1 Control Statements in Python

The document provides an overview of control statements in Python, including indentation, conditional statements (if, if-else, if-elif-else), loop statements (while and for), and jump statements (break and continue). It explains the syntax and usage of each statement type with examples. Additionally, it introduces lambda functions for creating small anonymous functions.

Uploaded by

monica.patil
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

Python Control Statements

Indentation in Python
Indentation in Python
• In Python, indentation is used to declare a block. If two
statements are at the same indentation level, then they are
the part of the same block.

• For the ease of programming and to achieve simplicity, python


doesn't allow the use of curly braces or parentheses for the
block level code.

• Indentation is the most used part of the python programming


language.

• Generally, a tab space or four spaces are given to indent the


statements in python.
Conditional Statements in Python
Conditional Statements in Python
• Conditional Statements performs different computations or actions depending
on conditions.
• In python, the following are conditional statements
 if
 if –else
 if – elif –else
If statement:
• The if statement is used to test a specific condition. If the
condition is true, a block of code (if-block) will be executed.
Syntax:
if condition:
statement1
statement2
Conditional Statements in Python Cont..

Example: [Link] Output:


a = 33 python [Link]
b = 200 b is greater than a
done…
if b > a:
print ("b is greater than a") print
("done…")

Remember:
input () function is used to get input from user.
Example:
a=input (“Enter a value”)
Conditional Statements in Python Cont..

Example: [Link]
If-else statement:
• The if else statement age = int(input("Enter your age : "))
provides an else if age>=18:
block combined with print("You are eligible to vote !!")
else:
the if statement which is print("Sorry! you have to wait !!"))
executed in the false case
of the condition.
Syntax:
Output:
if condition:
python [Link]
#block of statements Enter your age: 19
else: You are eligible to vote!!
#another block of statements
(else-block)
Write a Python program to check if a number is even or odd.
Conditional Statements in Python Cont..

If-elif-else statement:
• The elif statement enables us to check multiple conditions and execute
the specific block of statements depending upon the true condition among them.
Syntax:
if condition1:
# block of statements
elif condition2:
# block of statements
elif condition3:
# block of statements
else:
# block of statements
Conditional Statements in Python Cont..
Example: [Link]
a=int(input("Enter a value : "))
b=int(input("Enter b value : "))
c=int(input("Enter c value : "))
if (a>b) and (a>c):
print("Maximum value is :",a)
elif (b>a) and (b>c):
Output:
print("Maximum value is :",b)
python [Link]
else: Enter a value: 10
print("Maximum value is :",c) Enter b value: 14
Enter c value: 9
Maximum value is:
14
Write a Python program that checks whether a given number is positive, negative, or
zero.
Loop Statements in Python
Loop Statements in Python
• Sometimes we may need to alter the flow of the program. If the
execution of a specific code may need to be repeated several
numbers of times then we can go for loop statements.
• In python, the following are loop statements
 while loop
 for loop
while loop:
• With the while loop we can execute a set of statements as long as
a condition is true. The while loop is mostly used in the case where
the number of iterations is not known in advance.
Syntax:
while condition:
Statement(s)
Loop Statements in Python Cont..
Example: [Link] Output:
i=1; python [Link]
while i<=3: 1
print(i); 2
i=i+1; 3

Using else with while loop


• Python enables us to use the while loop with the else block also. The else
block is executed when the condition given in the while statement becomes
false.
Example: [Link] Output:
i=1; python [Link]
while i<=3: 1
print(i); 2
i=i+1; 3
else:
while loop terminated
print("while loop terminated")
 Write a python program to print “hellooooo world” 5 times
using while loop.

 Write a python program to print 1 to 20 numbers using


while loop.
Loop Statements in Python Cont..

for loop:
A for loop in Python is used to iterate over a sequence (like a list,
string, tuple, dictionary, or a range of numbers) and execute a
block of code for each item in the sequence

Syntax:
for iterating_var in sequence:
statement(s)
variable: A temporary name for the current item in the sequence.

sequence: A collection like range(), list, string, etc.


Example1 : Print elements of a list
fruits = ["apple", "banana", "cherry"] Output:
for fruit in fruits: apple
banana
print(fruit)
cherry

Example 2: Print each character in a word


word = "HELLO"
for letter in word: Output:
H
print(letter) E
L
L
O
range() Function in Python range(stop):
Generates numbers from 0 to stop - 1.
The range() function in Python is
commonly used to generate a sequence of for i in range(5):
print(i)
numbers.
It is especially useful in for loops when range(start, stop)
you want to repeat an action a certain Starts from start, ends at stop - 1.
number of times.
Syntax of range(): for i in range(2, 6):
range(stop) print(i)
range(start, stop) 3. range(start, stop, step)
range(start, stop, step) Starts at start, ends before stop, and steps by step.

for i in range(1, 10, 2):


print(i)
 Write a python program to print even 1 to 20 numbers.

 Write a python program to print 100 to 1 numbers.

 Write a python program to print even numbers between 1 to

50.
Loop Statements in Python Cont..

Using else with for loop


• Python allows us to use the else statement with the for loop which
can be executed only when all the iterations are exhausted.
• Here, we must notice that if the loop contains any of the break
statement then the else statement will not be executed.
Example: [Link]
for i in range(1,5):
print(i,end=' ')
else:
print("for loop completely exhausted");

Output:
python [Link] 1 2 3 4
for loop completely exhausted
Jump Statements in Python
Jump Statements in Python
• Jump statements in python are used to alter the flow of a loop
like you want to skip a part of a loop or terminate a loop.
• In python, the following are jump statements
obreak
ocontinue
break:
• The break is a keyword in python which is used to bring the
program control out of the loop.
• The break statement breaks the loops one by one, i.e., in the
case of nested loops, it breaks the inner loop first and then
proceeds to outer loops.
• The break is commonly used in the cases where we need to
break the loop for a given condition.
Syntax: break
Jump Statements in Python Cont..

Example: [Link]
i = 1 Output:
while i < 6: python [Link]
print(i)
if i == 3:
1
break 2
i += 1 3

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
Jump Statements in Python Cont..

Example: [Link]
str =input("Enter any String : ")
for i in str:
if i == 'h':
continue;
print(i,end=" ");

Output:
python [Link]
Enter any String : python
pyton
Lambda Function:
 A lambda function is a small anonymous function (without a name)
defined using the lambda keyword.
 It's typically used for short, simple functions.
 Lambda functions can take any number of arguments evaluate only one
expression and return a value.
Syntax
lambda arguments : expression
Example:
1. x = lambda a : a + 10
print(x(5))

2. x = lambda a, b : a * b
print(x(5, 6))
Example:
square = lambda x: x * x
print(square(5))

Output:
25

Write a lambda function in Python to calculate the cube of a number.

Write a lambda function in Python to calculate the area of rectangle.

You might also like