The slides for introducing Python to beginner. This is a complimentary class made in Wekanta's Soft Launch in Universities within Malaysia.
Slides Overview:
• Python Background
• Syntax
• Variables
• Data Structure
• Comparison
• Control Flow
• Function
Breaking the Kubernetes Kill Chain: Host Path Mount
Introduction to Python by Wekanta
1. Introduction to Python
a readable, dynamic, pleasant,
flexible, fast and powerful language.
Murtadha Bazli Tukimat
Developer/Founder at Wekanta
bazli@wekanta.com
Wekanta
3. What is Python?
• Python is a dynamic, interpreted (bytecode-compiled) and
multi-platform language
• No type declarations of variables, parameters, functions,
or methods in source code
• Code become short and fast to compile
Wekanta
4. Why Python?
• Machine learning, NLP and data scientific tools packages
• From embedded system to the web applications
(Web, GUI, Scripting, etc.)
• A general purpose language
Read it more → https://goo.gl/ZwtjJV
Wekanta
7. • Indentation affects code's meaning
• End of the line means end of the statement
• Inconsistent indentation == Syntax Error
• Tabs vs Spaces?
Spaces are the preferred indentation method.
Indentation
Wekanta
10. # This is single line comment
' This is also single line comment '
' This is a single line comment too '
'''
This is example of
multi-line comment.
'''
Comment
Wekanta
12. String
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# declaration
s = 'hello python'
# methods
s.lower() # return string lowercase version
s.upper() # return string uppercase version
s.strip() # remove trailing & leading whitespaces
s.isalpha() # alphanumeric test
s.isdigit() # digit/number test
s.isspace() # spaces test
s.startswith('hello') # check if string start with 'foo'
s.endswith('python') # check if string ending with 'bar'
s.find('hello') # return 'foo' index
s.replace('hello', 'hi') # replace 'foo' with 'bar' string
s.split('hello') # return list of substrings
s.join(list) # opposite of split
Wekanta
13. String Cont'd
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# example
s = 'Hello, this is Python '
s.lower() # 'hello, this is python '
s.upper() # 'HELLO, THIS IS PYTHON '
s.strip() # 'Hello, this is Python'
s.lower().find('hello') # 0
s.replace('this', 'that') # 'Hello, that is Python '
s = s.split(',') # ['Hello', ' this is Python ']
','.join(s) # 'Hello, this is Python '
Wekanta
14. Number
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# integer number
year = 2010
year = int('2010')
# floating point number
temperature = 27.6
temperature = float('27.6')
# fixed point number
price = Decimal('12.50')
Wekanta
15. Boolean
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# example
is_python = True
# cast to any variables
is_python = bool(0)
is_python = bool('check')
Wekanta
16. Null
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
optional_data = None
Wekanta
18. List
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# declaration
list = []
list = list()
# methods
list.append(element)
list.extend(list2)
list.insert(index, element)
list.remove(element)
list.pop(index)
list.clear()
list.index(element)
list.count(element)
list.sort()
list.reverse()
list.copy()
Wekanta
19. List Cont'd
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# example
list = ['bazli', 'hidayah', 'nabila']
list.append('others') # append 'others' at end
list.extend(['yyy', 'zzz']) # add list of element at end
list.insert(0, 'xxx') # insert 'xxx' at index 0
print(list)
# ['xxx', 'bazli', 'hidayah', 'nabila', 'others', 'yyy', 'zzz']
print(list.index('bazli')) # 1
list.remove('bazli') # search and remove that element
list.pop(1) # removes and returns 'larry'
print(list.count('xxx')) # 1
print(list)
# ['xxx', 'nabila', 'others', 'yyy', 'zzz']
Wekanta
20. List Slices
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# example
list = ['a', 'b', 'c', 'd']
print(list[1:-1]) # ['b', 'c']
print(list[:-1]) # ['a', 'b', 'c']
list[0:2] = 'z' # replace ['a', 'b'] with 'z'
print(list) # ['z', 'c', 'd']
Wekanta
21. Dictionary
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# declaration
dict = {}
dict = dict()
# methods
dict.clear()
dict.copy()
dict.get()
dict.items()
dict.keys()
dict.pop()
dict.update()
dict.values()
Wekanta
22. Dictionary Cont'd
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# example
product = {'name': 'sensor', 'price': 25.0}
product['name'] = 'device' # set 'name' to 'device'
print(product['name']) # temperature sensor
product.get('name') # sensor
product.keys() # ['name', 'price']
product.pop('name') # remove key 'name'
product.values() # ['sensor', 25.0]
product.update({'price': 20.0}) # update price value
product.items() # [ ['name', 'sensor'], ['price', 25.0] ]
Wekanta
23. Dictionary Hash Table
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# example
dict = {}
dict['a'] = 'alpha'
dict['o'] = 'omega'
dict['g'] = 'gamma'
print(dict[‘a']) # 'alpha'
print(dict)
# {'a': 'alpha', 'o': 'omega', 'g': 'gamma'}
Wekanta
25. Operator
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
== # Equal
!= # Not equal
<> # Not equal
>= # More than or equal
<= # Less than or equal
> # More than
< # Less than
Wekanta
26. Logical Comparison
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# Logical AND
a and b
# Logical OR
a or b
# Logical Negation
not a
# Compound
(a and not (a or b)
Wekanta
27. Identity Comparison
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# identity
1 is 1 == True
# non identity
1 is not '1' == True
Wekanta
29. Conditional
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# example
price = 25.0
if price >= 20:
print('Expensive')
elif 20 > price >= 10:
print('Cheap')
else:
print('Super Cheap')
Wekanta
30. For Loop
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# example
docs = { 'name': 'sensor', 'price': 25.0 }
# iterate by range
for x in range(10):
print(x)
# 0, 1, 2 ….
# iterate by element / dictionary keys
for doc in docs:
print(doc)
# name, price
# expanded for-loop
for k, v in docs:
print(k, v)
Wekanta
31. While Loop
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# infinite
while True:
print(‘running…’)
# conditional
while x < 10:
print(‘running…’)
x += 1
Wekanta
32. List Comprehension
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# example
docs = [{'name': 'Arduino', 'price': 180.0},
{'name': 'Raspberry Pi', 'price': 200.0}]
# common method
new_list = []
for d in docs:
if d['price'] < 200:
new_list.append(d['name'])
# list comprehension method
new_list = [d['name'] for d in docs if d[‘price'] < 200]
# ['Arduino']
Wekanta
34. Function Argument
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# basic function
def my_function():
''' Function Documentation '''
pass
# keyword argument
def price_check(price, currency='MYR'):
''' Check Price '''
pass
Wekanta
35. Arbitrary Argument
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
*args is non-keyworded arguments
# my_function(1, 2, 3)
**kwargs is keyworded arguments
# my_function(price=42)
Wekanta
36. Arbitrary Argument Cont’d
# This is single line comment
'This is also single line comment'
'''
This is example of
multi-line comment.
'''
# example
def my_function(*args, **kwargs):
''' Function Documentation '''
for arg in args:
print(arg)
for key, value in kwargs.items():
print(key, value)
# usage
my_function(1, 2, currency='MYR')
# results
1
2
currency MYR
Wekanta