Unit 1 - Introduction To Python-Part-2
Unit 1 - Introduction To Python-Part-2
Unit 1 - Introduction To Python-Part-2
Output:
1- Got a true expression value
10
Python- Decision-making cont..
IF...ELIF...ELSE Statement
• An else statement can be combined with an if statement.
• An else statement contains a block of code that executes if the conditional expression
in the if statement resolves to 0 or a FALSE value.
• The else statement is an optional statement and there could be at the most only one
else statement following if.
Syntax
The syntax of the if...else statement is
if
expression:
statement(s)
else:
statement(s)
Python- Decision-making cont..
Flow Diagram #!/usr/bin/python3
amount=int(input("Enter amount: "))
if amount<1000:
discount=amount*0.05
print ("Discount",discount)
else:
discount=amount*0.10
print ("Discount",discount)
print ("Net payable:",amount-discount)
Output:
Enter amount: 600
Discount 30.0
Net payable: 570.0
Enter amount: 1200
Discount 120.0
Python- Decision-making cont..
The elif Statement
• The elif statement allows you to check multiple expressions for TRUE and execute a block of code
as soon as one of the conditions evaluates to TRUE.
• Similar to the else, the elif statement is optional.
• However, unlike else, for which there can be at the most one statement, there can be an arbitrary
number of elif statements following an if.
Syntax
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Python- Decision-making cont..
Example:
Output:
#!/usr/bin/python3
Enter amount: 600
amount=int(input("Enter amount: ")) Discount 30.0
if amount<1000: Net payable: 570.0
discount=amount*0.05
print ("Discount",discount) Enter amount: 3000
elif amount<5000: Discount 300.0
discount=amount*0.10 Net payable: 2700.0
print ("Discount",discount)
else: Enter amount: 6000
discount=amount*0.15 Discount 900.0
print ("Discount",discount) Net payable: 5100.0
print ("Net payable:",amount-discount)
Python- Decision-making cont..
Nested IF Statements
• There may be a situation when you want to check for another condition after a condition
resolves to true. In such a situation, you can use the nested if construct.
• In a nested if construct, you can have an if...elif...else construct inside another if...elif...else
construct.
• Syntax
if expression1: elif expression4:
statement(s) statement(s)
if expression2: else:
statement(s) statement(s)
elif expression3:
statement(s)
else
statement(s)
Python- Decision-making cont..
Example:
# !/usr/bin/python3 Output:
num=int(input(“Enter number "))
if num%2==0: Enter number 10
divisible by 2 not divisible by 3
if num%3==0:
print ("Divisible by 3 and 2") Enter number 15
else: divisible by 3 not divisible by 2
print ("divisible by 2 not divisible by 3")
Enter number 12
else:
Divisible by 3 and 2
if num%3==0:
print ("divisible by 3 not divisible by 2") Enter number 7
else: not Divisible by 2 not divisible by 3
print ("not Divisible by 2 not divisible by 3")
Python- Decision-making cont..
Single Statement Suites
• If the suite of an if clause consists only of a single line, it may go on the same line as the
header statement.
• Example of a one-line if clause-
#!/usr/bin/python3
var = 100
if ( var == 100 ) : print ("Value of expression is 100")
print ("Single Statement Suite Example")
Output:
Value of expression is 100
Single Statement Suite Example
Python-Loops
• In general, statements are executed sequentially-
• The first statement in a function is executed first,
followed by the second, and so on.
• There may be a situation when you need to
execute a block of code several number of times.
• Programming languages provide various control
structures that allow more complicated execution
paths.
• A loop statement allows us to execute a statement
or group of statements multiple times.
Python-Loops
Python programming language provides the following types of loops to handle looping
requirements.
Python-Loops
Python programming language provides the following types of loops to handle looping
requirements.
while Loop Statements
A while loop statement in Python programming language repeatedly executes a target
statement as long as a given condition is true.
Syntax
The syntax of a while loop in Python programming language is
while expression:
statement(s)
In Python, all the statements indented by the same number of character spaces after a
programming construct are considered to be part of a single block of code.
Python uses indentation as its method of grouping statements.
Python-Loops
while Loop Statements Flow Diagram
Example:
#!/usr/bin/python3
flag = 1
while (flag): print ('Given flag is really true!')
print (“Single Suite Example!")
The above example goes into an infinite loop and you need to press CTRL+C keys to exit.
Python-Loops
for Loop Statements
The for statement in Python has the ability to iterate over the items of any sequence, such as a
list or a string.
Syntax
for iterating_var in sequence:
statements(s)
The len() built-in function provides the total number of elements in the tuple as well as
the range() built-in function to give the actual sequence to iterate over.
Python- Loops
Using else Statement with Loops
Python supports having an else statement associated with a loop statement.
• If the else statement is used with a for loop, the else block is executed only if for loops
terminates normally (and not by encountering break statement).
• If the else statement is used with a while loop, the else statement is executed when the condition
becomes false.
• Example
#!/usr/bin/python3
numbers=[15,31,51,39,55,75,37,21,23,41,13]
for num in numbers:
if num%2==0: Output:
print (‘The list contains an even number') The list does not contain even number
break
else:
print (‘The list does not contain even number')
Python- Loops
Nested loops
• Python programming language allows the use of one loop inside another loop.
• The following section shows a few examples to illustrate the concept.
• Syntax
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)
The syntax for a nested while loop statement in Python programming language is as follows
while expression:
while expression: Any type of loop can be embedded inside any other type of loop.
statement(s) For example a for loop can be inside a while loop or vice versa.
statement(s)
Python- Loops
Nested loops
Output:
Example:
#!/usr/bin/python3 1 2 3 4 5 6 7 8 9 10
import sys 2 4 6 8 10 12 14 16 18 20
for i in range(1,11): 3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
for j in range(1,11): 5 10 15 20 25 30 35 40 45 50
k=i*j 6 12 18 24 30 36 42 48 54 60
print (k, end=' ') 7 14 21 28 35 42 49 56 63 70
print() 8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
• The print() function inner loop has end=' ' which appends a space instead of default
newline. Hence, the numbers will appear in one row.
Python- Loops
Loop Control Statements
• The Loop control statements change the execution from its normal sequence.
• When the execution leaves a scope, all automatic objects that were created in that scope are
destroyed.
• Python supports the following control statements.
Python- Loops
Loop Control Statements
break statement
• The break statement is used for premature termination of the current loop.
• After abandoning the loop, execution at the next statement is resumed, just like the
traditional break statement in C.
• The most common use of break is when some external condition is triggered requiring a
hasty exit from a loop.
• The break statement can be used in both while and for loops.
• If you are using nested loops, the break statement stops the execution of the innermost loop
and starts executing the next line of the code after the block.
Python- Loops
Loop Control Statements
break statement
Syntax
The syntax for a break statement in Python is as follows:
break
Example
#!/usr/bin/python3
for letter in 'Python': # First Example
Output:
if letter == 'h':
Current Letter : P
break Current Letter : y
print ('Current Letter :', letter) Current Letter : t
Python- Loops
Loop Control Statements
break statement
Example
#!/usr/bin/python3
Output:
var = 10 # Second Example
Current variable value : 10
while var > 0: Current variable value : 9
print ('Current variable value :', var) Current variable value : 8
Current variable value : 7
var = var -1 Current variable value : 6
if var == 5: break statement!
break
print (“break statement!")
Python- Loops
Loop Control Statements
break statement
Output:
Example
#!/usr/bin/python3 Any number: 33
no=int(input('any number: ')) number found in list
numbers=[11,33,55,39,55,75,37,21,23,41,13]
any number: 5
for num in numbers:
number not found in list
if num==no:
print ('number found in list')
break
else:
print ('number not found in list')
Python- Loops
Loop Control Statements
continue statement
• The continue statement in Python returns the control
to the beginning of the current loop.
• When encountered, the loop starts next iteration
without executing the remaining statements in the
current iteration.
• The continue statement can be used in both while
and for loops.
Syntax
continue
Python- Loops
#!/usr/bin/python3
for letter in 'Python': # First Example Output:
if letter == 'h': Current Letter : P
continue
Current Letter : y
print ('Current Letter :', letter)
Current Letter : t
var = 5 # Second Example Current Letter : o
while var > 0: Current Letter : n
var = var -1 Current variable value : 4
if var == 3: Current variable value : 2
continue Current variable value : 1
print ('Current variable value :', var)
Current variable value : 0
print ("continue statement")
continue statement
Python- Loops
Loop Control Statements
pass statement
• It is used when a statement is required syntactically but you do not want any command
or code to execute.
• The pass statement is a null operation; nothing happens when it executes.
• The pass statement is also useful in places where your code will eventually go, but has
not been written yet i.e. in stubs).
Syntax Example: Output:
pass #!/usr/bin/python3 Current Letter : P
for letter in 'Python': Current Letter : y
if letter == 'h': Current Letter : t
pass This is pass block
print ('This is pass block') Current Letter : h
print ('Current Letter :', letter) Current Letter : o
print ("Good bye!") Current Letter : n
Python- Loops
Loop Control Statements
pass statement
• It is used when a statement is required syntactically but you do not want any command
or code to execute.
• The pass statement is a null operation; nothing happens when it executes.
• The pass statement is also useful in places where your code will eventually go, but has
not been written yet i.e. in stubs).
Syntax Example: Output:
pass #!/usr/bin/python3 Current Letter : P
for letter in 'Python': Current Letter : y
if letter == 'h': Current Letter : t
pass This is pass block
print ('This is pass block') Current Letter : h
print ('Current Letter :', letter) Current Letter : o
print ("Good bye!") Current Letter : n