Why Python?
>>> print("Hello World!")
Hello world!
>>>
>>> def hello_world():
... print("Hello World!")
...
>>> hello_world()
Hello World!
REPL
$ python3
Python 3.6.0 (default, Dec 24 2016, 08:01:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Demo time
#!/usr/bin/python3
def hello():
print("Hello world")
hello()
$ ./test.py
$ chmod u+x test.py
$ python3 test.py
create test.py
>>> for i in range(5):
... x = i * 10
... print(x)
...
0
10
20
30
40
Python code in muzi-scanner
>>> import module_name
>>>
>>> module_name.method()
>>>
>>> from module import function
Various types of imports
>>> a = 10
>>> type(a)
<class 'int'>
>>> b = 15.5
>>> type(b)
<class 'float'>
>>> c = int(b)
>>> type(c)
<class 'int'>
>>> print(c)
15
>>> type(True)
<class 'bool'>
>>> True
True
>>> False
False
>>> bool(True)
True
>>> bool(None)
False
>>> bool(13123)
True
Only `None`, `False`, 0 and empty collections evaluate to `False`
>> a = None
>> a is None
True
>>> 5 > 10
False
>>> 5 > 1
True
>>> 5 >= 5
True
>>> 5 != 5
False
"This is a string"
'This is also a string'
"You can't handle mah swag"
'You can\'t handle mah swag'
# Multi line strings delimited by 3 quotes
>>>"""This is a multi-
... -line string"""
>>> print('Try to print \n this string')
Try to print
this string
>>> print("try to print \\n this string")
try to print \n this string
# If you get tired used raw strings
>>> print(r'C:\path\to\directory')
C:\path\to\directory
>>> s = ' hello'
>>> r = 'world!'
>>> r.capitalize()
>>> s + r
>>> l = s + r
>>> l
>>> l.split()
>>> l.split('w')
>>> l.lstrip()
>>> l.rstrip()
>>> l.strip()
>>> l.strip('!')
Try: `help(str)`
This lists all the methods supported by str.
>>> a = 'hello world! '
>>> a.capitalize().strip().strip('!')
Methods can be chained
>>> b'Some bytes'
b'Some bytes'
Text
Encoding Decoding
>>> swag = "ϟÐϟℒα♭s"
>>> swag.encode('utf-8')
b'\xcf\x9f\xc3\x90\xcf\x9f\xe2\x84\x92\xce\xb1\xe2\x99\xads'
>>> bytes = swag.encode('utf-8')
>>> bytes.decode('utf-8')
'ϟÐϟℒα♭s'
>>> [1, 2, 3, 4]
ProTip: To see the attrs/methods available for an object
run `dir(obj)` in the interpreter
>>> [1,2,3]
[1,2,3]
>>> a = ["palai", "mangal", "nightfury"]
>>> a[1]
mangal
>>> a[1] = 7
>>> a
["palai", "mangal", "nightfury"]
>>> a.append("kd")
>>> a[-1]
kd
>>> a = ["palai", "mangal", "nightfury"]
>>> a.pop()
nightfury
>>> a
["palai", "mangal"]
>>> a.pop(0)
>>> a.remove("mangal")
>>> list("SDSLabs")
>>> date = { 'day': 6,
... 'month': 'March',
... 'year': 2017 }
>>> date['day']
6
>>> date
{'day': 6, 'month': 'March', 'year': 2017}
>>> date['day'] = 7
>>> date
{'day': 7, 'month': 'March', 'year': 2017}
>>> date.keys()
dict_keys(['day', 'month', 'year'])
>>> date.values()
dict_values([7, 'March', 2017])
If - Else
# Else is optional
if expression: # expression is converted to bool as if by bool() operator
print("It is true!")
b = 40
if b>50:
print("B is greater than 50")
else:
print("B is less than or equal to 50")
# Multiple if-else
if b>50:
print("B is greater than 50")
elif b<20:
print("B is less than 20")
else:
print("B is between 20 and 50")
While
while expression: # expression is converted to bool as if by bool() operator
print("Infinite loop")
# break out by Ctrl + C
b = 40
while b!=0:
print(b)
b = b - 10
while True:
num = Input("Enter a number:")
if num%7 == 0:
break
else:
continue
>>> for item in iterable:
..... body ....
>>> SDSLabs = { 'palai': 'pro',
'mangal': 'panda'}
>>> for member in SDSLabs:
print("{} is {}".format(member, SDSLabs[member]))
palai is pro
mangal is panda
# return is Optional
def myFunc(x):
x = x * 5
print(x)
# Use return
def myFunc(x):
x = x * 5
return x
# Can return multiple values :)
def myFunc(x,y):
return x*x, y*y
# default args
def myFunc(x = 5):
return x*5
>>> def myFunc(*args):
for arg in args:
print('this is our arg: {}'.format(arg))
>>> myFunc(10)
>>> myFunc(10, 'lol', 'yolo')
For sending arbitrary number of arguments
To send named args, use **kwargs (gets passed as a dict)
>>> def myFunc(**kwargs):
print(kwargs)
>>> myFunc(day = 6, month = 'March')
def launch_missiles():
print("Missiles launched!")
print("Lets see if we can import this")
def launch_missiles():
print("Missiles launched!")
if __name__ == '__main__':
print("Lets see if we can import this")
""" This is the module doc string"""
def launch_missiles():
""" This is the function doc string. Explain briefly
Args:
None
Returns:
None
"""
print("Missiles launched!")
if __name__ == '__main__':
# This is just a comment
print("Lets see if we can import this")
"""This is our sample class"""
class MyClass:
def test(self):
return('this is only a test')
def test2(self):
print(self.test)
"""This is our sample class"""
class MyClass:
def __init__(self):
self.__myName = 'gautham'
def test(self):
print('this is only a test')
return self.__myName
>>> my_class = MyClass()
>>> my_class.test()
"""This is our sample class"""
class MyClass:
def __init__(self, name = 'gautham'):
self.__myName = name
def test(self):
print('this is only a test')
return self.__myName
>>> my_class = MyClass('xz0r')
>>> my_class.test()
>>> my_class2 = MyClass()
""" Module for demonstrating exceptions"""
def convert(s):
"""Convert to integer"""
x = int(s)
return x
>>> import test
>>> print(test.convert("5"))
>>> print(test.convert("TryMe"))
""" Module for demonstrating exceptions"""
def convert(s):
"""Convert to integer"""
try:
x = int(s)
print("Conversion succeeded")
except ValueError:
print("Conversion Failed")
x = -1
return x
>>> import test
>>> print(test.convert("5"))
>>> print(test.convert("TryMe"))