0% found this document useful (0 votes)
63 views22 pages

Pthon Module 3.. 2

This document is a course module for CSE1021 at VIT Bhopal University, focusing on fundamental algorithms and Python programming concepts. It covers topics such as Boolean values, conditional statements, and loop control structures including break, continue, and pass. The module aims to provide students with essential programming skills and understanding of control flow in Python.
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)
63 views22 pages

Pthon Module 3.. 2

This document is a course module for CSE1021 at VIT Bhopal University, focusing on fundamental algorithms and Python programming concepts. It covers topics such as Boolean values, conditional statements, and loop control structures including break, continue, and pass. The module aims to provide students with essential programming skills and understanding of control flow in Python.
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

VIT Bhopal University

Bhopal-Indore Highway, Kothrikalan, Sehore,


Madhya Pradesh – 466114.

Course Code
CSE1021
Introduction to Problem Solving and
Programming

MODULE 3 Credits 4
Unit -2
(8-Hours)

VIT Bhopal
Unit -3
Fundamental Algorithms, Python Control
Flow, Functions
● Fundamental Algorithms: Introduction – Exchange the
values – Counting – Summation – Factorial Computation –
Fibonacci Sequence – Reverse – Base Conversion– Character
to Number Conversion
● Python Control Flow, Functions : Conditionals: Boolean
values and operators, conditional (if, if- else, if-elif-else) –
Iteration statements (state, while, for break, continue, pass);
BOOLEAN VALUES
● Any object can be tested for truth value, for use in an if or while
condition or as operand of the Boolean operations below. The
following values are considered false:
• None
• False
• zero of any numeric type, for example, 0, 0L, 0.0, 0j.
• any empty sequence, for example, '', (), [].
• any empty mapping, for example, {}.

All other values are considered true — so objects of many types are
always true.
Operations and built-in functions that have a Boolean result always
return 0 or False for false and 1 or True for true, unless otherwise
stated.
CONDITIONAL STATEMENTS
1. if statement
2. if-else statement
3. if-elif-else statement
4. Nested if
5. Chained conditional
if statement
if-else statement
if-elif-else statement
ITERATION
Nested while
for loop
range()
Nested for loops
Loop Control Structures

● Loop control statements change execution from its normal


sequence. When execution leaves a scope, all automatic
objects that were created in that scope are destroyed. Python
supports the following control statements.
● Break
● Continue
● Pass
Break
❖ Break statements can alter the flow of a loop.
❖ It terminates the current loop and executes the remaining statement
outside the loop.
❖ If the loop has else statement, that will also gets terminated and come
out of the loop completely.
Syntax: break
Break

Example: Output:
for i in range(5): 0
if i == 3: 1
break 2
print(i)
Continue
• It terminates the current iteration and transfer the control to the next iteration in the
loop.

• Syntax: continue
Continue
for i in range(5):
if i == 3:
continue
print(i)

Output: • Observe the output of the code, the value 3 is skipped


because we have provided the if condition using
0
1 with continue statement in while loop.
2 • When it matched with the given condition then control
4 transferred to the beginning of the while loop and it skipped
the value 3 from the code.
Pass

• The pass statement is a null operation since nothing happens when it is


executed. It is used in the cases where a statement is syntactically needed
but we don't want to use any executable statement at its place.
• For example, it can be used while overriding a parent class method in the
subclass but don't want to give its specific implementation in the subclass.
• Pass is also used where the code will be written somewhere but not yet
written in the program file. Consider the following example.
Pass
Example:
for letter in "python":
if letter=="h":
pass
print("pass block") print("letter=",letter)
Output:
letter= p
letter= y
letter= t
pass block letter= h
letter= o
letter= n
Difference: Break and Continue
break continue

It terminates the current loop and It terminates the current iteration and
executes the remaining statement outside transfer the control to the next iteration in
the loop. the loop.

syntax: syntax:
break continue

for i in "welcome": for i in "welcome":


if(i=="c"): if(i=="c"):
break continue
print(i) print(i)

w w
e e
l l
o
m
e
Thanks!

You might also like