ELEC423 - Tutorial 05
ELEC423 - Tutorial 05
ELEC423 - Tutorial 05
[email protected]
Shell scripting
Strings
Example:
1 192.168.43.1
2 169.254.252.10
??
3 172.16.220.1
4 172.16.220.10
5 216.58.211.174
?
Datatypes, operations and operators
Basic datatypes
- (15/4) + 7
'b' * 5
Datatypes, operations and operators
Basic operators
Operator Example
Assign y to x x = y
x is equal to y x == y
x is not equal to y x != y
x is greater than y x > y
x is less than y x < y
x is greater than or equal to y x >= y
x is less than or equal to y x <= y
and
Local operators or
not
Check if x and y point to the same object, True if yes x is y
Datatypes, operations and operators
Basic operators
Working with basic operators:
• Example 1: assign a value to a variable
a = 3
• The variable ‘a’ will contain the value 3 of type int
• Example 2: assign a value to a variable given by
evaluating an expression
a = 3 + int(7.5)
• The variable ‘a’ will contain the value 10 of type int
• Example 3: evaluating a Boolean expression (1)
3 == 4 ⟹ False
Datatypes, operations and operators
Basic operators
"""
Create an iterable range object from the
start value to the stop value with an
optional step value
"""
r = range(start, stop, [step])
Control Structure
Sequential control:
• Implicit form of control in which instructions are
executed as they are written
Instruction 1
Instruction 2
Instruction 3
Instruction 4
Instruction 5
Instruction 6
Instruction 7
…
Control Structure
Selection control:
• Selectively executes instructions by using a control
statement
Instruction 1
Instruction 2
Instruction 3
Control statement
True False
Instruction 4 Instruction 7
Instruction 5 Instruction 8
Instruction 6 Instruction 9
… …
Control Structure
if statement
Nested if statements
if condition: if condition:
... ...
else: elif condition:
... ...
else:
...
Control Structure
Iterative control:
• Repeatedly executes instructions by using an
iterative control statement
Instruction 1
Instruction 2
Instruction 3
Control statement
True False
Instruction 4 Instruction 7
Instruction 5 Instruction 8
Instruction 6 Instruction 9
… …
Control Structure
for loop
for var in iterable:
...
Classic examples in Python
# for loop based on a range object for counting
for i in range(start, stop, [step]):
...
# for loop to retrieve each item i in a list
for i in list:
...
# for loop to retrieve each item in a list by
# using index i (list[i])
for i in range(len(list)):
...
Control Structure
while loop and statements
while condition:
...
Other statements