Lecture 2

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

Cybersecurity Programming

Crash Course In Python Programming Language—Lesson 2

Dr. Mustafa Ababneh


Indentation & Control Flow: while

while i < 10:


tot += i
tot, i = 0, 1
i += 1
print(tot)
while i < 10:
tot += i
while i < 10:
i += 1
tot += i
print(tot)
i += 1 # IndentationError
print(tot)
while i < 10:
tot += i
while i < 10:
i += 1
tot += i
print(tot)
i += 1 # IndentationError
print(tot)

1 / 11
if Statement

#!/usr/bin/python3.6

x = int(input("Please enter an integer: "))

if x < 0:
a = -1 * x
elif x
== 2:
a = 2 * x
elif x == 3:
a = x
else:
a = x / 2
print(a)

2 / 11
for Statement

for i in range(10):
print(i ** 2) # outputs squares 0 to 81

for i in range(5, 10):


print(i ** 2) # outputs squares 25 to 81

for i in range(1, 10, 4):


print(i ** 2) # outputs 1,
25, 81

words = ['A', 'AB', 'ABC', 'ABCD']


for w in words:
print(len(w)) # outputs 1 to
4

for i in range(len(words)):
print(len(words[i])) # outputs 1 to 4

for i in 'abcd':
print(i) # outputs a
to d
3 / 11
Functions
(1)
def mul(a, b):
""" This is the function doc string """
return a * 2, b + 2, a * b

def add(list_arg):
s = list_arg[0]
for i in range(1, len(list_arg)):
s += list_arg[i]

return s

print(mul(9, 7)) # outputs '(18, 9, 63)'

list1 = [1, 2, 3, 4]
print (add(list1)) # outputs 10

list2 = ['a', 'b', 'c']


print (add(list2)) # outputs 'abc'

Myadd = add
print(Myadd(list2)) # outputs 'abc'
4 / 11
Functions
(2)

Functions can be used just like any other data


They can be
Arguments to function
Return values of functions
Assigned to variables (as in previous slide)
Parts of tuples, lists, etc.

def fun2(x):
return x * 3

def fun1(q,
x):
return q(x)
print(fun1(fun2, 7)) # outputs 21

5 / 11
Functions (3): Parameter Passing

def f1(a, b):


print(a, b)

f1(1, 2) # outputs 1 2

f1(b = 1, a = 2) # outputs 2 1

f1(1, b = 2) # outputs 1 2

# f1(b = 1, 2) # SyntaxError: positional argument follows


# keyword argument

6 / 11
More on Lists
a = [66.25, 333, 333, 1, 1234.5]
print(a.count(333), a.count(66.25), a.count('x')) # outputs 2 1 0

a.insert(2, -1)
a.append(333)
print(a) # outputs [66.25, 333, -1, 333, 1, 1234.5, 333]

print(a.index(333)) # outputs 1

a.remove(333)
print(a) # outputs [66.25, -1, 333, 1, 1234.5, 333]

a.reverse()
print(a) # outputs [333, 1234.5, 1, 333, -1, 66.25]

a.sort()
print(a) # outputs [-1, 1, 66.25, 333, 333, 1234.5]

print(a.pop()) # outputs 1234.5

print(a) # outputs [-1, 1, 66.25, 333, 333]


7 / 11
List Comprehensions

A concise way to create lists

a1 = []

for x in [1, 2, 3]:

for y in [3, 1, 4]:

if x != y:

a1.append((x,
y))

# a1 = [(1, 3), (1, 4), (2,


3), (2, 1), (2, 4), (3, 1),
(3, 4)]

a2 = [(x, y) for x in
[1,2,3] for y in [3,1,4] if
x != y]
8 / 11
More on Data Structures

Not to be covered
Tuples
Sets
Dictionaries
In other programming languages, called maps
Indexed by keys (strings or numbers)
• Where as, lists are indexed by a range of numbers

Un-ordered set of ‘key:value’ pairs


Keys must be unique in a given dictionary

9 / 11
Dictionaries
(1)
student = {'name' : 'Mohammad', 'StudentID' : 111111}

student['gender'] = 'male'

print(student)
# outputs {'StudentID': 111111, 'gender': 'male', 'name': 'Mohammad'}

print(student['name']) # outputs Mohammad

print(list(student.items()))
# outputs [('StudentID', 111111), ('gender', 'male'), \
# ('name', 'Mohammad')]

print(list(student.keys())) # outputs ['StudentID', 'gender', 'name']

print(list(student.values())) # outputs [111111, 'male', 'Mohammad']

student['name'] = 'Ahmad'
print(student)
# outputs {'StudentID': 111111, 'gender': 'male', 'name': 'Ahmad'}

10 /
Dictionaries (2)

student = {'name': 'Ahmad', 'StudentID': 111111, 'gender': 'male'}

k, v = list(student.items())[0]
print(k, v) # outputs StudentID 111111

for k, v in student.items():
print(k, v)

# outputs
# StudentID 111111
# gender male
# name Ahmad

MyList = {10 : 'klmn', 20 : 134, 'k1' : 56}


print(MyList[10], MyList[20], MyList['k1']) # outputs klmn 134 56

11 /

You might also like