0% found this document useful (0 votes)
29 views9 pages

Python Programming - Control Structure

Uploaded by

Sanket
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)
29 views9 pages

Python Programming - Control Structure

Uploaded by

Sanket
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/ 9

SNBPIS MORWADI

Grade XI
Informatics Practices
Python Programming

Control Flow Statements


1 Simple if
2 if-else statement
3 Nested-if statement
4 elif ladder (Chained conditionals)
5 Single statement condition

Simple if
•Syntax:

if expression:
statement(s)
next statement
•Remember to indent the statements in a block equally.

•This is because we don’t use curly braces to delimit blocks. Also, use
a colon(:) after the condition.

•If the boolean expression evaluates to TRUE, then the block of


statement(s) inside the if statement is executed.

•If boolean expression evaluates to FALSE, then the first set of code
after the end of the if statement(s) next statement is executed.

Example:
a=3
if a > 2:
print(a, "is greater")
print("done")
a = -1
if a < 0:
print(a, "a is smaller")
print("Finish")

If-else statement
•Syntax:

if expression:
statement(s)
else:
statement(s)
•If the boolean expression evaluates to TRUE, then the block of
statement(s) inside the if statement is executed.

•If boolean expression evaluates to FALSE, then block of


statement(s) inside the else statement is executed.

Example-1:
if 2<1:
print("2")
else:
print("1")

Nested If statement
•Syntax:

if expression:
statement(s)
if expression:
statement(s)
if expression:
statement(s)

You can put an if statement in the block under another if statement.


This is to implement further checks.

a=1
b=2
if a==1:
if b==2:
print("a is 1 and b is 2")

El-If ladder (chained conditionals)


Syntax
if expression:
Body of if stmts
elif expression:
Body of elif stmts
else:
Body of else stmt

The elif statement allows us to check multiple expressions for TRUE


and execute a block of code as soon as one of the conditions
evaluates to TRUE.

if 2<1:
print("2")
elif 3<1:
print("3")
else:
print("1")

Functions

Input/ Datatype Mathema Other


Output Conversio tical Functions
n Functions
input() bool() abs() __import_
print() chr() divmod() _()
dict() max() len()
float() min() range()
int() pow() type()
list() sum()
ord()
set()
str()
tuple()

The range() Function


The range() is a built-in function in Python. Syntax of range() function
is:
range([start], stop[, step])
It is used to create a list containing a sequence of integers from the
given start value upto stop value (excluding stop value), with a
difference of the given step value. If start value is not specified, by
default the list starts from 0. If step is also not specified, by default
the value is incremented by 1 in each iteration. All parameters of
range() function must be integers. The step parameter can be a
positive or a negative integer excluding zero.

>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#start value is given as 2
>>> list(range(2, 10))
[2, 3, 4, 5, 6, 7, 8, 9]
#step value is 5 and start value is 0
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
#step value is -1. Hence, decreasing #sequence is generated
>>> list(range(0, -9, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8]
The function range() is often used in for loops for generating a
sequence of numbers.

Program to print the multiples of 10 for numbers in a given range.


#Program 3-5
#Print multiples of 10 for numbers in a range
for num in range(5):
if num > 0:
print(num * 10)

For Loop

Sometimes we need to repeat certain things for a particular number


of times. For example, a program has to display attendance for every
student of a class. Here the program has to execute the print
statement for every student. In programming, this kind of repetition
is called looping or iteration, and it is done using for statement. The
for statement is used to iterate over a range of values or a sequence.
The loop is executed for each item in the range. The values can be
numeric, string, list, or tuple.
When all the items in the range are exhausted, the statements within
loop are not executed and Python interpreter starts executing the
statements immediately following the for loop. While using for loop,
we should know in advance the number of times the loop will execute.
Syntax of the for Loop:
for <control-variable> in <sequence/items in range>:
<statements inside body of the loop>

# Program to find the sum of all numbers stored in a list


# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
sum = 0 # variable to store the sum
for val in numbers: # iterate over the list
sum = sum+val
print("The sum is", sum)

#Print even numbers in the given sequence


numbers = [1,2,3,4,5,6,7,8,9,10]
for num in numbers:
if (num % 2) == 0:
print(num,'is an even Number')

For loop: Iterating over a list


fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
# Program to iterate through a list using indexing
>>> list=['Romanian','Spanish','Gujarati']
>>> for i in range(len(list)):
print(list[i])

For loop: Iterating over a tuple


tuple = (2,3,5,7)
print ('These are the first four prime numbers ')
#Iterating over the tuple
for a in tuple:
print (a)

For loop: Iterating over a dictionary


#creating a dictionary
college = {"ces":"block1","it":"block2","ece":"block3"}
#Iterating over the dictionary to print keys print ('Keys are:')
for keys in college:
print (keys)

For loop: Iterating over a string


#declare a string to iterate over
class= ‘python’
#Iterating over the string
for name in class:
print (name)

Nested For loop


You can also nest a loop inside another. Or you can put a loop inside
a loop inside a loop. You can go as far as you want.
>>> for i in range(1,6):
for j in range(i):
print("*",end=' ')
print()
Output
*
**
***
****
*****

While Loop

Syntax:
Initialization
While(condition):
statement(s)
incr/decr
•When the program control reaches the while loop, the condition is
checked. If the condition is true, the block of code under it is
executed.

•This continues until the condition becomes false.

•Then, the first statement, if any, after the loop is executed.

Example:
a=3
while(a>0):
print(a)
a-=1
Output
3
2
1
This loop prints numbers from 3 to 1. In Python, a—wouldn’t work.
We use a-=1 for the same.

Syntax:
Initialization
While(condition): statement(s); incer/decr;

•Like an if statement, if we have only one statement in while’s body,


we can write it all in one line.

•>>> a=3

•>>> while a>0: print(a); a-=1;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy