Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views65 pages

Module 1 FlowControl SHS

Uploaded by

asmashariff2330
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views65 pages

Module 1 FlowControl SHS

Uploaded by

asmashariff2330
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 65

Flow Control

MODULE -1
Introduction and Flow control

Introduction, Python Basics: Entering Expressions into the Interactive Shell, The
Integer, Floating-Point, and String Data Types, String Concatenation and Replication,
Storing Values in Variables, Your First Program, Dissecting your Program

Flow control: Boolean Values, Comparison Operators, Boolean Operators, Mixing


Boolean and Comparison Operators, Elements of Flow Control, Program Execution, Flow
Control Statements, Importing Modules, Ending a Program Early with sys.exit()

Text Book: T1 Chapters: 1, 2


Flow control

• Flow control statements can decide which


Python instructions to execute under which
conditions

• The flow control statements control the


order of program execution
Contd ….
Boolean Values
Boolean Values

• The Boolean data type has only two values:


• True
• False

• The Boolean values True and False are not enclosed in quotes

• They always start with a capital T or F

• Boolean values are used in expressions and can be stored in variables

• True and False cannot be used as variable names


Comparison Operators
Comparison Operators
• Comparison operators, also
called relational operators

• They compare two values and


evaluate down to a single
Boolean value
• The == and != operators can
actually work with values of
any data type
• The <, >, <=, and >=
operators, on the other hand,
work properly only with
integer and floating-point
• values
THE DIFFERENCE BETWEEN
THE == AND = OPERATORS
• The == operator (equal to) asks whether two values are
the same as each other.
• The = operator (assignment) puts the value on the right
into the variable on the left.
Boolean Operators
Boolean Operators

• The Boolean operators


• and
• or
• not

• The and and or operators always take two Boolean values


(or expressions), so they’re considered binary operators

• The not operator operates on only one Boolean value (or


expression) and is called a unary operator
Boolean Operators

Fig: The Truth tables for and, or and not operators

Fig: Examples for and, or and not boolean operators


Mixing Boolean and Comparison
Operators
Mixing Boolean and Comparison Operators
• Comparison operators can
be used with Boolean
operators
• The expression on the left side
of Boolean operator will be
evaluated first
• Boolean operators are
evaluated in the following
order
• not
• and
• or
Elements of Flow Control
Elements of Flow Control

• Flow control statements often start with a part called the


condition

• Conditions are always followed by a block of code called


the clause

• Conditions always evaluate down to True or False based


on which the flow of execution is decided

• Lines of Python code can be grouped together in blocks


Elements of Flow Control

• Rules for blocks are as follows

• Blocks begin when the indentation increases

• Blocks can contain other blocks

• Blocks end when the indentation decreases to zero


or to a containing block’s indentation
Flow Control Statements
Flow Control Statements

• if statements
• else statements
• elif statements
• while loop
• for loop
• break
• continue
if statements
• The most common type of flow control statement
• An if statement’s clause will execute if the statement’s
condition is True
• An if statement consists of the following:
– if keyword
– A condition (an expression) that evaluates to True or
False
– A colon
– Starting on the next line, an indented block of code called
the if clause
Example of if statement
Simple if
else

• An else statement doesn’t have a condition, and in code,


an else statement always consists of the following:
• The else keyword
• A colon
• Starting on the next line, an indented block of code
(called the else clause)
Contd ….
if...else Statement
The if..else statement evaluates test expression and will execute body
of if only when test condition is True.
If the condition is False, body of else is executed. Indentation is used to
separate the blocks.
if...else
Program to check whether a
person is eligible to vote or
not.

age = int (input("Enter your age? "))


if age>=18:
print("You are eligible to vote !!")
else:
print("Sorry! you have to wait !!")
Program to check whether the
number is even or odd

num = int(input("enter the


number?\n"))
if num%2 == 0:
print("Number is Even")
else:
print("Number is Odd")
if...elif...else

 The elif is short for else if. It allows us


to check for multiple expressions.
 If the condition for if is False, it checks
the condition of the next elif block
and so on.
 If all the conditions are False, body of
else is executed.
 Only one block among the several
if...elif...else blocks is executed
according to the condition.
 The if block can have only
one else block. But it can
have multiple elif blocks.
Contd ….
Quick Cmpare
elif example1
number = int(input("Enter the number?"))
if number==10:
print("number is equals to 10")
elif number==50:
print("number is equal to 50")
elif number==100:
print("number is equal to 100")
else:
print("number is not equal to 10, 50 or 100")
elif example2
marks = int(input("Enter the marks? "))
if marks > 85 and marks <= 100:
print("Congrats ! you scored grade A ...")
elif marks > 60 and marks <= 85:
print("You scored grade B + ...")
elif marks > 40 and marks <= 60:
print("You scored grade B ...")
elif (marks > 30 and marks <= 40):
print("You scored grade C ...")
else:
print("Sorry you are fail ?")
The while loop
while loops
• The while keyword Syntax
• A condition
while condition:
• A colon
#indented code
• Indented block of code block

Output
What does the following code do?

Output 1
Output 2
Program to print 1 to 10 using
while loop

i=1
#The while loop will iterate until condition
becomes false.
while i<=10:
print(i)
i=i+1
break statements
break statements

• It is used to exit
from a loop

• The break keyword Output

is used
break statement with while
loop

i=0
while 1:
print(i)
i=i+1;
if i == 20:
break
print('came out of while loop')
continue statements

• It skips the current iteration

• The continue keyword is used


Example for continue
statement

i=0 Outpu
t:
1
while(i < 10):
2
3
i = i+1
4
if(i == 5): 6
continue 7
print(i) 8
9
10
continue statements
Output 1

Output 2
Values like 0, 0.0, ' '(empty string) are
considered False
Truthy and All other values are considered True
Falsey
Such values in other datatypes(int, float,
values string) are equivalent to True or False

Such values can be used in conditions


What is the output of the following code?

Output
You could have entered
not name != ' ' instead of not
name, and
numOfGuests != 0 instead of
numOfGuests
The for loops and the
range() function
for loops
• It is used to execute a block of code a
certain number of times
• This is done using for loop statement and a
range() function
• A for loop contains
– for keyword
– A variable
– The in keyword
– range() function with up to 3 integers
– A colon
– for code block with indentation
Example of for loop and range()

Output
What is the output of the following code?

Output
Example of for loop and range( )

total = 0
for num in range(10):
Outpu
total = total + num
print(total)
t:
45
The Starting, Stopping, and Stepping
Arguments to range()
Syntax
range(start, stop, step)

Output
Program to print table of given
number.

Output:
Enter the
n = int(input('Enter the number: 19
number: \n')) 19 * 1 = 19
for i in range(1,11): 19 * 2 = 38
19 * 3 = 57
c = n*i 19 * 4 = 76
print(n,'*',i,'=',c) 19 * 5 = 95
19 * 6 = 114
19 * 7 = 133
19 * 8 = 152
19 * 9 = 171
19 * 10 = 190
Program to print even number
using step size in range().

Output:
Enter the
number: 21
n = int(input('Enter the number: \ 2
n')) 4
6
for i in range(2,n,2): 8
print(i) 10
12
14
16
18
20
Negative arguments in range()

Output
Importing modules
Importing modules
• Python also comes with a set of modules called the standard
library

• Each module is a Python program that contains a related


group of functions

• These modules can be embedded in other programs

• Examples
– math module has mathematics-related functions
– random module has random number-related functions
import statement
• An import statement consists of the following:

– The import keyword

– The name of the module

– Optionally, more module names, as long as they are


separated by commas
Examples of import module
Run 1:

Run 2:

Run Run
1: 2:
Contd….
from import statements
• The from keyword is used to import only a specified section
from a module.

• composed of
– the from keyword
– the import keyword
– an asterisk

• E.g.
from random import *
sys
The sys module provides functions and variables used to
manipulate different parts of the Python runtime environment.

sys.argv
Contd ….
• sys.version

• sys.path

• sys.maxsize
Ending a Program Early with the
sys.exit() Function
sys.exit()
• Used to terminate a program
• The exit method belongs to sys module
• Therefore, sys module must be imported
before using this function
Output

You might also like