Lecture 1
Lecture 1
https://www.youtube.com/watch?v=nKIu9yen5nc1
2/
Features
3/
Features
Concise
No { } for blocks (indentation instead)
No ( ) for if/while conditions
Rich standard library of modules
Several third party modules & tools
Procedural vs. Object-oriented programming
Multi-threading
GUI, Web, Database connectivity, network programming, distributed
computing, cryptography libraries, security tools, etc., etc., etc.
Strong open-source community
4/
Informal Introduction To Python
5/
Python’s Advanced “Hello World!”
# Example1.py
print("Hello World !")
ahmad@ubuntu:~$ python3.6 Example1.py
print('Hello World !') Hello World !
print("Hello\nWorld!") Hello World !
print("""Hello Hello
World World!
!""") Hello
World
!
x = 23 # A comment 23
print(x) 5.7 foo2
foo1
Traceback
x, y, z = 'foo1', "foo2", 5.7
(most
print(z, y, x) recent
del x call
print(x) last):
File "Example1.py", line 15, in <module>
print(x)
NameError: name 'x' is not defined
6/
Making Python Source File Executable
#!/usr/bin/python3.6
7 / 13
Numbers
#!/usr/bin/python3.6
x = (50 - 5 * 6) / 4
print(x)
print ('The value of x is', x)
y = 17 / 3 # conventional division
z = 17 // 3 # floor division
w = 17 % 3 # reminder
a = 5 ** 3 # power operator
print(y, z, w, a)
ahmad@ubuntu:~$ ./Example2.py
5.0
The value of x is 5.0
5.666666666666667 5 2 125
8 / 13
Strings (1)
#!/usr/bin/python3.6
str1 = 'a-\t-b-\n-c'
str2 = r'a-\t-b-\n-c'
print(str1)
print(str2)
str3 = 7 * 'ab-' +
"--cde" # Concise
code
print(str3)
print(len(str3))
ahmad@ubuntu:~$ ./Example3.py
a- -b-
-c
a-\t-b-\n-c
ab-ab-ab-ab-ab-ab-ab---cde
26
9 / 13
Strings (2)
#!/usr/bin/python3.6
word = 'Python'
print(word[0])
print(word[5])
print(word[-1])
print(word[0:2] # characters from position 0 (included)
) # to 2 (excluded)
print(word[2:5])
print(word[2:])
print(word[:4])
ahmad@ubuntu:~$ ./Example4.py
P
n
n
Py
tho
thon
Pyth
10 /
Strings (3)
#!/usr/bin/python3.6
word = 'Python'
print(word[15]) # gives IndexError Exception
print(word[2:25]) # prints thon
print(word[12:]) # prints only new line
11 /
Compound Data Types: Lists (1)
#!/usr/bin/python3.6
print(list1[0])
print(list1[-3:])
list3 = list1 +
[36, 49, 64]
list3[0] = -9 # Lists are mutable
list3.append(90)
list3[2:5] = ['A', "B", 'C']
print(list3)
ahmad@ubuntu:~$ ./Example6.py
[1, 4, 9, 16, 25]
[1, 'a', 'qqe']
1
[9, 16, 25]
[-9, 4, 'A', 'B', 'C', 36, 12 /
Lists (2)
...
list1[:] = [] # Clearing the list
print(len(list3))
print(x)
print(x[0])
print(x[0][1])
...
9
[['a', 'b', 'c'], [1, 2, 3]]
['a', 'b', 'c']
b
13 /
Cybersecurity Programming
Crash Course In Python Programming Language—Lesson 2
1 / 11
if Statement
#!/usr/bin/python3.6
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(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
list1 = [1, 2, 3, 4]
print (add(list1)) # outputs 10
Myadd = add
print(Myadd(list2)) # outputs 'abc'
4 / 11
Functions
(2)
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
f1(1, 2) # outputs 1 2
f1(b = 1, a = 2) # outputs 2 1
f1(1, b = 2) # outputs 1 2
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]
a1 = []
if x != y:
a1.append((x,
y))
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
9 / 11
Dictionaries
(1)
student = {'name' : 'Mohammad', 'StudentID' : 111111}
student['gender'] = 'male'
print(student)
# outputs {'StudentID': 111111, 'gender': 'male', 'name': 'Mohammad'}
print(list(student.items()))
# outputs [('StudentID', 111111), ('gender', 'male'), \
# ('name', 'Mohammad')]
student['name'] = 'Ahmad'
print(student)
# outputs {'StudentID': 111111, 'gender': 'male', 'name': 'Ahmad'}
10 /
Dictionaries (2)
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
11 /