Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Python Control

Structures
Networking for Software Developers
Narendra Pershad
Objectives
 Explore how python expects code block

 To use the if statement


 Enables you to process or ignore blocks of code
 An app is require to complete more than one task

 To use the while and for loop (there is no do-while)


 Enables you to process a block of code 0 or more times
 Allows you to write less code

 To use the conditional statement


What are control structures?

 Control structure dictates how the lines of code is process

 Sequential or sequence:
 Each line is process exactly once with no skips

 Conditionals or branching:
 Blocks of code that may be processed optionally

 Looping or iteration or repetition:


 Blocks of code that may be processed zero or more times

 Each control structure brings some benefit(s) to the language


Code layout

 A colon is used to signal the start of a code block.

 All the statements that is to be part of that block must be indented by the same amount

 If a line is outdented, that this signal that the previous block has completed

 If you don’t have a statement for a block, then you must use the keyword pass

 Python uses indentation to identify blocks of code


 It does not use curly braces
 In this course we will use two spaces for an indent or a tab.
 Pick one and use int in all your program
The if statement

if test1: # if test
statement1 # associated block
elif test2: # optional elifs
statement2
else # optional else
statement3
>>> temp = 98
>>> if temp < 98:
...   print('Cold')
... elif temp > 98:
...   print('Hot')
... else:
...   print('Normal')
... 
Normal
The while loop

while condition: # condition


statement1 # associated block

 There are no curly braces in python, blocks are specified by indentation


 You may use tabs or spaces for indentation. This course we will use 4 spaces

 You may use the break and continue statements in both the while and the for
loop

 They behave exactly like the way they do in C#


>>> x = 1
>>> sum = 0
>>> while x < 100:
...   sum += x
...   x += 1
...  

>>> sum
4950 #sum of the integers up to 100
>>> x = 0
>>> while True:
...   print(f'{x}', end=' ')
...   x += 1
...   if x > 5:
...   break

0 1 2 3 4 5
The for loop

for condition: # condition


statement1 # associated block

 The for loop works on a range or a collection, so it is similar to a c# foreach


loop
>>> a = [1, 2, 3, 4, 5]
>>> sum = 0
>>> for x in a:
...   sum += x
...  

>>> sum
15 #sum of the number in the list a
 Using the range method

>>> a = range(5) # end=5 default:start=0, step=1


>>> a
range(0, 5)
>>> for x in a:
... print(x, end=' ') #the end parameter suppresses the carriage return
0 1 2 3 4

>>> b = range(2, 8) # start=2 end=8 default:step=1

>>> c = range(3, 11, 2) # start=3 end=11, step=2


>>> for x in c:
... print(x, end=' ')
3 5 7 9
>>> a = range(1, 6)
>>> lst = []
>>> for x in a:
...   lst = lst + [x]
...  

>>> lst
[1, 2, 3, 4, 5]
>>> for x in range(10) :
...   if x % 2 == 0:
...   continue
...   print(f'{x}', end=' ')
...
...
1 3 5 7 9
>>> a = 'Programming I'
>>> for x in a:
...   print(f'{x}', end=' ')
...  

P r o g r a m m i n g I
>>> distros = ['ubuntu', 'redhat', 'centos']
>>> arch = ['32-bit', '64-bit']
>>> for d in distros:
... for a in arch:
... print(d + ' ' + a)
... print('------------')
...
ubuntu 32-bit
ubuntu 64-bit
------------
redhat 32-bit
redhat 64-bit
------------
centos 32-bit
centos 64-bit
------------
The Ternary operator

Syntax:
«statement_if_true» if «assertion» else «statement_if_false»

>>> temp = 97
>>> print('Cold') if temp < 98 else print('Normal')

Cold
Summary
 As with all programming languages, python has the usual control structures

 Sequence
 Programs can complete a single task

 Conditional
 Programs can complete multiple tasks

 Looping
 Write less code to do the same thing

 Functions
 Developer more production, because code can be reuse

You might also like