Table of content
Overview of Python
Basic syntax
Datatypes
Control statements
Comprehension
Function
Overview of Python
Python is suitable for science, data analysis & Finance
Some famous services using Python:
Recently, Python is also used in trading
Lesson from Quora
2.1 Your first program in Python
print('Hello world')
Hello world
2.2 Indentation
a = 3
if a > 0:
print('Positive')
else:
print('Negative')
Positive
def sum(a, b):
result = a + b
return result
print(sum(5, 8))
13
2.3 Semi-colon is not required at the end of each command
a = 5
b = 9
c = a * b
print(c)
45
2.4 snack_case should be use for naming
width_of_rect = 5
height_of_rect = 4
area_of_rect = width_of_rect * height_of_rect
print(area_of_rect)
20
def get_area(width, height):
return width * height
print(get_area(5, 4))
20
2.5 Comments
# Calculate area of a rectangle
area_of_rect = width_of_rect * height_of_rect
p = (width_of_rect + height_of_rect) * 2 # perimeter of the rectangle
'''
Calculate area of a rectangle.
It is the multiplication of the width & the height
'''
area_of_rect = width_of_rect * height_of_rect
"""
Calculate perimeter of a rectangle.
It is twice of the sum of width & height
"""
perimeter_of_rect = width_of_rect * height_of_rect
2.6 Operators
a = 5
b = 3
a + b # 8 (Addition)
a - b # 2 (Subtraction)
a * b # 15 (Multiplication)
a / b # 1 (Division)
4.3 / 2 # 2.15
a // b # 1 (floor division)
4.3 // 2 # 2.0
a % b # 2 (Modulus)
a ** b # 125 (Exponentiation)
125
a > b # True
a <= b # False
a < b # False
a >= b # True
a == b # False
a != b # True
True
a = 27 #11011
b = 14 #01110
a & b #01010 = 10(Bitwise AND)
a | b #11111 = 31 (Bitwise OR)
a ^ b #10101 = 21 (Bitwise XOR)
~a #111..11100100 = -28 (Bitwise inversion)
-28
a = 27 #0011011
a >> 2 #0000110 = 6 (Shift right)
a << 2 #1101100 = 108 (Shift left)
108
3.1 Bool
True
and False
a = (5 == 6)
a
False
b = (5 == 5)
b
True
type(a)
bool
a and b
False
a or b
True
not a
True
3.2 Numeric
int
a = 100
type(a)
int
a = 10 ** 10
a
10000000000
type(a)
int
b = 10 ** 100
type(b)
long
import sys
sys.maxint
9223372036854775807
long
Python works hard to support unlimited integer values through long
datatype
unbound_value = 10 ** 100000
type(unbound_value)
long
another_unbound_value = 2 ** 10000000
# not enough space & time to print out this huge value
type(another_unbound_value)
long
float
a = 3.432
type(a)
float
b = a ** 334.32
b
1.1070478028957259e+179
type(b)
float
sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
10 / 3 # integer division
3
To retrieve float value from integer division
10.0 / 3
3.3333333333333335
10 / float(3)
3.3333333333333335
Infinity value
infinity = float("inf")
infinity / 3
inf
infinity * 100
inf
3.3 List
Creating list
list_of_ints = [1,2,3,4,5,6,7,8]
list_of_strings = ['one', 'two', 'three']
mixed_list = ['a', 1, 2.323, ['sublist', 3, ['nested_list', 2.5]]]
Accessing list
my_list = [1, 3, 7, 2, 10] # List in Python is 0-based
len(my_list)
5
my_list[0] # the first item
my_list[4] # the last item
10
my_list[-1]
10
A sublist can be accessed through a slice operator :
my_list[i:j]
will extract a sublist with items from # [1, 3, 7, 2, 10]
my_list[1:3] # Get items 1th -> 2th
[3, 7]
[:j]
returns a sublist starting at the beginning to my_list[:3] # Get items 0th -> 2th
[1, 3, 7]
[i:]
returns a sublist starting from # [1, 3, 7, 2, 10]
my_list[2:] # Get items 2th -> 4th (end of the llst)
[7, 2, 10]
# [1, 3, 7, 2, 10]
my_list[:]
[1, 3, 7, 2, 10]
my_list[:-1]
[1, 3, 7, 2]
Advanced slice operator:
[i:j:k]: slice from
Reverse an array
# [1, 3, 7, 2, 10]
my_list[::-1]
[10, 2, 7, 3, 1]
Check the existence of items in list
x in list
: check the existence (boolean)x not in list
: check the non-existence (boolean)# [1, 3, 7, 2, 10]
10 in my_list
True
15 in my_list
False
Iterating list
# [1, 3, 7, 2, 10]
for item in my_list:
print(item)
1 3 7 2 10
sum = 0
for i in range(len(my_list)):
sum += my_list[i]
sum
23
parent_list = ['chicken', 'buffalo', ['cat', 'dog'], 'pork']
for (i, item) in enumerate(parent_list):
print(str(i) + ': ' + str(parent_list[i]))
0: chicken 1: buffalo 2: ['cat', 'dog'] 3: pork
Delete items from list
del listVar[i]
: delete the item del listVar[i:j]
: delete the items from # [1, 3, 7, 2, 10]
del my_list[2]
my_list
[1, 3, 2, 10]
del my_list[1:3]
my_list
[1, 10]
Methods on list
list.append(value)
: appends element to end of the listlist.count(x)
: counts the number of occurrences of x in the listlist.index(x)
: returns the 1st index of x in the listlist.insert(i, x)
: inserts x at location $ i^{th} $list.pop()
: returns last element then removes it from the listlist.remove(x)
: finds and removes first x from listlist.reverse()
: reverses the elements in the listlist.sort()
: sorts the list alphabetically in ascending order, or numerical in ascending orderlist1 + list2
: merge 2 listmy_list = [1, 3, 7, 2, 10]
my_list.append(3)
my_list
[1, 3, 7, 2, 10, 3]
my_list.count(3)
2
my_list.index(3)
# my_list.index(100) -> error because 100 does not exist in my_list
1
# [1, 3, 7, 2, 10, 3]
my_list.insert(3, 50)
my_list
[1, 3, 7, 50, 2, 10, 3]
a = my_list.pop()
a # my_list will be: [1, 3, 7, 50, 2, 10]
3
my_list.append(3) # [1, 3, 7, 50, 2, 10, 3]
my_list.remove(3) # remote the 1st item only
my_list
[1, 7, 50, 2, 10, 3]
my_list.reverse()
my_list
[3, 10, 2, 50, 7, 1]
my_list.sort()
my_list
[1, 2, 3, 7, 10, 50]
total_list = my_list + [3, 5, 4]
total_list
[1, 2, 3, 7, 10, 50, 3, 5, 4]
new_list = my_list
new_list
[1, 2, 3, 7, 10, 50]
my_list.remove(3)
new_list
[1, 2, 7, 10, 50]
list
functionnew_list = list(my_list)
new_list
[1, 2, 7, 10, 50]
my_list.remove(10)
my_list
[1, 2, 7, 50]
new_list
[1, 2, 7, 10, 50]
3.4 String
String work as a sequence of characters like list
animal = 'tiger'
animal[3] # 3th character
'e'
len(animal)
5
animal[-1] # last character
'r'
Access characters of string through slice operator like list
animal[1:3] # ig
animal[:3] # tig
animal[1:] # iger
animal[:] # tiger
'tiger'
Scan through all characters of a string
for l in animal:
print(l)
t i g e r
Basic functions on string
animal = 'elephant'
animal.count('e')
2
animal.find('e')
0
animal.upper()
'ELEPHANT'
'ELEPHANT'.lower()
'elephant'
animal.replace('e', 'a')
'alaphant'
animal = ' elephant '
animal.strip()
'elephant'
3.5 Tuple
tup = ('one', 1) #2-tuple
tup
('one', 1)
tup = ('John', 25, 180) #3-tuple
tup
('John', 25, 180)
list
, values of a tuple can be accessed through indextup[0]
'John'
tup[2]
180
len(tup)
3
# del tup[1] -> error
# tup[1] = 26 -> error
for item in tup:
print(item)
John 25 180
3.6 Dictionary
Dictionary in Python is a datatype that maps hashable values to objects. Dictionary works as hashtable as in other language.
Creating dictionary
my_dict = {'one': 1, 'two': 2, 'three': 3}
my_dict
{'one': 1, 'three': 3, 'two': 2}
another_dict = {1: 'one', 2: 'two', 3: 'three', 3.14: 'pi'}
another_dict
{1: 'one', 2: 'two', 3: 'three', 3.14: 'pi'}
dict
function with named parameters.my_dict = dict(one=1, two=2, three=3)
my_dict
{'one': 1, 'three': 3, 'two': 2}
dict
function also accept key/value pairsmy_dict = dict({'one': 1, 'two': 2, 'three': 3})
my_dict
{'one': 1, 'three': 3, 'two': 2}
my_dict = dict([('one', 1), ('two', 2), ('three', 3)])
my_dict
{'one': 1, 'three': 3, 'two': 2}
Accessing dictionary values
my_dict['one']
1
'three' in my_dict
True
'four' in my_dict
False
len(my_dict)
3
Changing dictionary
my_dict['one'] = 'ichi'
my_dict
{'one': 'ichi', 'three': 3, 'two': 2}
my_dict['four'] = 4
my_dict
{'four': 4, 'one': 'ichi', 'three': 3, 'two': 2}
del my_dict['two']
my_dict
{'four': 4, 'one': 'ichi', 'three': 3}
Iterating in dictionary
for k in my_dict:
print(k + ': ' + str(my_dict[k]))
four: 4
three: 3
one: ichi
for k,v in my_dict.items():
print(k + ': ' + str(v))
four: 4
three: 3
one: ichi
Functions on dictionary
dict.keys()
: get all keys of the dictionary (return list)dict.values()
: get all values of the dictionary (return list)dict.items()
: get a list of pairsdict.iteritems()
: get item iterator for the dictionary. This is used for iterating only.dict.iterkeys()
: get key iterator for the dictionary. This is used for iterating only.dict.itervalues()
: get value iterator for the dictionary. This is used for iterating only.dict.update(new_dict)
: update a dictionary with some new key/value parisdict.copy()
: clone dict1 —> the return dict is independent from the original one.dict.clear()
: delete all keys, or reset the dictionarymy_dict = {'one': 1, 'two': 2, 'three': 3}
my_dict.keys()
['three', 'two', 'one']
my_dict.values()
[3, 2, 1]
my_dict.items()
[('three', 3), ('two', 2), ('one', 1)]
for k, v in my_dict.iteritems():
print(k + ': ' + str(v))
three: 3 two: 2 one: 1
for k in my_dict.iterkeys():
print(k)
three two one
for v in my_dict.itervalues():
print v
3 2 1
my_dict.update(one='ichi', five='go')
my_dict
{'five': 'go', 'one': 'ichi', 'three': 3, 'two': 2}
my_dict.update({'two':'ni', 'six':'roku'})
my_dict
{'five': 'go', 'one': 'ichi', 'six': 'roku', 'three': 3, 'two': 'ni'}
new_dict = my_dict
my_dict['one'] = 'ichi'
my_dict
{'five': 'go', 'one': 'ichi', 'six': 'roku', 'three': 3, 'two': 'ni'}
new_dict
{'five': 'go', 'one': 'ichi', 'six': 'roku', 'three': 3, 'two': 'ni'}
copy
functionnew_dict = my_dict.copy()
my_dict.clear()
my_dict
{}
new_dict
{'five': 'go', 'one': 'ichi', 'six': 'roku', 'three': 3, 'two': 'ni'}
3.7 Set Set can be considered as dictionary without value
my_set = set()
my_set = {1, 5, 3, 8}
my_set = set([1, 5, 3, 8])
my_set
{1, 3, 5, 8}
my_set.add(9)
my_set
{1, 3, 5, 8, 9}
my_set.remove(8)
my_set
{1, 3, 5, 9}
# my_set[1] -> error
len(my_set)
4
Operations on set
a & b
: return intersection of a & b, <=> a.intersection(b)a | b
: return union of a & b, <=> a.union(b)a - b
: return the difference of a & b, <=> a.difference(b)a ^ b
: return the exclusive of a & b, <=> a.symmetric_difference(b)a == b
: return whether a & b are the same seta <= b
: check whether a is a subset of b or not, <=> a.issubset(b)a >= b
: check whether a is a superset of b or not, <=> a.issuperset(b)a = {1, 3, 4, 5}
b = {1, 3, 6}
a & b
{1, 3}
a | b
{1, 3, 4, 5, 6}
a - b
{4, 5}
a ^ b
{4, 5, 6}
a == b
False
# a = {1, 3, 4, 5}
c = {1, 3, 4, 5}
a == c
True
a = {1, 3, 4, 5}
b = {1, 3, 6}
a <= b
False
d = {1, 5, 4, 7, 3}
a <= d
True
a <= c
True
a > c
False
a >= c
True
3.8 Type conversion
int('5')
5
float('5.6')
5.6
str(5.7)
'5.7'
str(7)
'7'
str([1, 3, 5])
'[1, 3, 5]'
set([1, 3, 5, 3])
{1, 3, 5}
list({'one': 1, 'two': 2, 'three': 3})
['three', 'two', 'one']
list({'one', 'two', 'three'})
['one', 'three', 'two']
list(('one', 'two', 'three'))
['one', 'two', 'three']
tuple([1, 3, 5, 3])
(1, 3, 5, 3)
dict([('one', 1), ('two', 2), ('three', 3)])
{'one': 1, 'three': 3, 'two': 2}
4.1 IF statement
if .. else
if .. elif .. else
: substitute for switch .. case
statement in other languagesa = 5
if a == 5:
print('a if five')
else:
print('a if five')
a if five
a = 6
if a == 5:
print('a if five')
elif a > 5:
print('a is greater than five')
else:
print('a is less than five')
a is greater than five
and
, or
, not
a = 5
b = 7
if a == 5 and b > 6:
print("Matched!")
else:
print("NOT Matched!")
Matched!
if not(a == 5) or (b <= 6):
print("Matched!")
else:
print("NOT Matched!")
NOT Matched!
4.2 FOR loop
n = 3
for i in range(n):
print(i)
0 1 2
range
function return list of items in a specific segmentrange(5)
[0, 1, 2, 3, 4]
range(2, 5)
[2, 3, 4]
words = ['We', 'are', 'learning', 'Python']
for w in words:
print(w)
We are learning Python
4.3 WHILE loop
i = 0
while i < 4:
print(i)
i += 1
0 1 2 3
4.4 ELSE clause
ELSE
clause in FOR
loop is executed after the last iterationfor w in words:
print(w)
else:
print('Out of word')
We are learning Python Out of word
ELSE
clause in WHILE
loop is executed after the last ieterationi = 0
while i < 4:
print(i)
i += 1
else:
print('Finished while')
0 1 2 3 Finished while
4.5 BREAK statement
for i in range(1, 5):
if i % 3 == 0:
break;
else:
print(i)
1 2
4.6 CONTINUE statement
for i in range(5):
if i % 2 == 0:
continue;
else:
print(i)
# Do something more here
1 3
4.7 PASS statement
pass
is just a command that do nothing. It is used a place that requires at least 1 command, but we don't want to do nothing there# Here, I want to demonstrate the syntax of FOR loop but I dont't want to output anything to save space
for i in range(10):
pass
5.1 List comprehension
x = [1, 2, 4, 5]
x_square = [i * i for i in x]
x_square
[1, 4, 16, 25]
Syntax of the above comprehension is very close to that of x_square definition in math:
city = 'tokyo'
upper = [s.capitalize() for s in city]
upper
['T', 'O', 'K', 'Y', 'O']
if
in list comprehension. In case the if
clause is placed before the for
clause, else
is required.contents = ['Ministère de l'agriculture, des forêts et des pêches', 'année', 'Taux d'autosuffisance alimentaire','consommation']
indices = [i if w == 'consommation' else None for i, w in enumerate(contents)]
print(indices)
[None, None, None, 3]
if
clause is placed after the for
clause, else
can be omitted.indices = [i for i, w in enumerate(contents) if w == 'consommation']
print(indices)
[3]
5.2 Dict comprehension
upper_count = {s.capitalize(): city.count(s) for s in city}
upper_count
{'K': 1, 'O': 2, 'T': 1, 'Y': 1}
5.3 Set comprehension
upper_set = {s.capitalize() for s in city}
upper_set
{'K', 'O', 'T', 'Y'}
def
to define function. The syntax as the following:def function_name(parameters):
command block
Function without parameter
def simple_func():
print("First line from simple_func")
print("Second line from simple_func")
simple_func()
First line from simple_func Second line from simple_func
Function with parameter
def sum(a, b):
return a + b
sum(10, 5)
15
Default parameter value
def sum_with_def(a, b=0):
return a + b
sum_with_def(10)
10
Positional arguments: order of parameters is used
def menu(drink, entree, dessert):
print('Drink: %s' % drink)
print('Entree: %s' % entree)
print('Dessert: %s' % dessert)
my_menu = menu('champagne', 'chicken', 'cake')
Drink: champagne Entree: chicken Dessert: cake
Keyword arguments: name of parameters is used
mime = menu(dessert='cake', drink='champagne', entree='chicken')
Drink: champagne Entree: chicken Dessert: cake
Gather positional arguments with *
def print_pos_argument(*args): # args will be a tuple
print(args)
print_pos_argument(1, 3, 'abc')
print_pos_argument('champagne', 'chicken', 'cake')
(1, 3, 'abc') ('champagne', 'chicken', 'cake')
Gather keyword arguments with **
def print_keyword_arguments(**args): # args will be a dictionary
print(args)
print_keyword_arguments(dessert='cake', drink='champagne', entree='chicken')
{'dessert': 'cake', 'drink': 'champagne', 'entree': 'chicken'}
Function as parameter
def add(a, b):
return a + b
def call_something(func, a, b):
return func(a, b)
value = call_something(add, 4, 5)
value
9
Inner function
def twice_of_sum(a, b):
def sum(c, d):
return c + d
return a + b + sum(a, b)
value = twice_of_sum(4, 5)
value
18
Lambda: anonymous function
g = lambda x: x ** 2
g(3)
9
def edit_story(words, func):
for w in words:
print(func(w))
stairs = ['thud', 'meow', 'thud', 'hiss']
edit_story(stairs, lambda x: x.capitalize() + '!') #Thud! ¥n Meow! ¥n Thud! ¥n Hiss!
Thud! Meow! Thud! Hiss!
map(lambda x: x * x, [1, 2, 3, 4])
[1, 4, 9, 16]
Recommended Posts