In [1]:
import this


The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

I. Native DataTypes

Number


In [2]:
## integer
a = 1
b = 0x10            # 16
print(type(a))      # <class 'int'>


<class 'int'>

In [3]:
## float
c = 1.2
d = .5              # 0.5
g = .314e1          # 3.14
print(type(g))      # <class 'float'>


<class 'float'>

In [4]:
## complex
e = 1+2j
f = complex(1, 2)
print(type(e))      # <class 'complex'>
print(f == e)       # True


<class 'complex'>
True

String


In [5]:
s1 = '🐶\n'
s2 = "Dogge's home"
s3 = """
Hello,
Dogge!
"""
print(type(s1))     # <class 'str'>
print("%s, %s, %s" % (s1, s2, s3))


<class 'str'>
🐶
, Dogge's home, 
Hello,
Dogge!


In [6]:
## Length
print(len(s1))      # 2


2

In [7]:
## Slicing
s = '学而时习之'
print('{0}:{1}'.format(s[0], s[-2]))    # 学:习


学:习

Byte


In [8]:
# Byte
## 0-255/x00-xff
byt = b'abc'
print(type(byt))    # <class 'bytes'>
print(byt[0] == 'a')# False
print(byt[0] == 97) # True


<class 'bytes'>
False
True

In [9]:
## Length
print(len(byt))     # 3


3

Boolean


In [10]:
True
False
print(type(True))   # <class 'bool'>


<class 'bool'>

None


In [11]:
print(None is None) # True
print(type(None))   # <class 'NoneType'>


True
<class 'NoneType'>

List


In [12]:
l = ['python', 3, 'in', 'one']
print(type(l))      # <class 'list'>


<class 'list'>

In [13]:
## Length
print(len(l))       # 4


4

In [14]:
## Slicing
print(l[0])         # 'python'
print(l[-1])        # 'one'
print(l[1:-1])      # [3, 'in']


python
one
[3, 'in']

In [15]:
## Alter
l.append('pic')     # None
print(l)
# l == ['python', 3, 'in', 'one', 'pic']

l.insert(2, '.4.1') # None
print(l)
# l == ['python', 3, '.4.1', 'in', 'one', 'pic']

l.extend(['!', '!'])
print(l)
# l == ['python', 3, '.4.1', 'in', 'one', 'pic', '!', '!']


['python', 3, 'in', 'one', 'pic']
['python', 3, '.4.1', 'in', 'one', 'pic']
['python', 3, '.4.1', 'in', 'one', 'pic', '!', '!']

In [16]:
print(l.pop())             # '!'
print(l)
# l == ['python', 3, '.4.1', 'in', 'one', 'pic', '!']

print(l.pop(2))           # '.4.1'
print(l)
# l == ['python', 3, 'in', 'one', 'pic', '!']

l.remove("in")
print(l)
# l == ['python', 3, 'one', 'pic', '!']

del l[2]
print(l)
# l == ['python', 3, 'pic', '!']


!
['python', 3, '.4.1', 'in', 'one', 'pic', '!']
.4.1
['python', 3, 'in', 'one', 'pic', '!']
['python', 3, 'one', 'pic', '!']
['python', 3, 'pic', '!']

In [17]:
print(l.index('pic'))       # 2


2

Tuple


In [18]:
tp = (1, 2, 3, [4, 5])
print(type(tp)) # <class 'tuple'>


<class 'tuple'>

In [19]:
## Length
print(len(tp))  # 4

print(tp[2])    # 3
tp[3][1] = 6
print(tp)       # (1, 2, 3, [4, 6])


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

In [20]:
## Single element
tp = (1, )      # Not tp = (1)
print(tp)


(1,)

In [21]:
## Assign multiple values at once
v = (3, 2, 'a')
(c, b, a) = v
print(a, b, c)  # a 2 3


a 2 3

Set


In [22]:
st = {'s', 'e', 'T'}
print(type(st)) # <class 'set'>


<class 'set'>

In [23]:
## Length
print(len(st))  # 3


3

In [24]:
## Empty
st = set()
print(len(st))  # 0


0

In [25]:
st = {}
print(type(st)) # <class 'dict'>


<class 'dict'>

In [26]:
## Alter
st = set(['s', 'e', 'T'])
st.add('t')     # st == {'s', 'e', 't', 'T'}
st.add('t')     # st == {'s', 'e', 't', 'T'}
st.update(['!', '!'])
print(st)
# st == {'s', 'e', 't', 'T', '!'}

st.discard('t') # st == {'s', 'e', 'T'}
st.remove('T')  # st == {'s', 'e'}
st.pop()        # 's'
print(st)
# st == {'e'}

st.clear()      # st == set()
print(st)


{'!', 'T', 'e', 's', 't'}
{'e', 's'}
set()

Dict


In [2]:
dic = {}
print(type(dic))    # <class 'dict'>

dic = {'k1': 'v1', 'k2': 'v2'}
print(dic)


<class 'dict'>
{'k2': 'v2', 'k1': 'v1'}

In [3]:
## Length
print(len(dic))     # 2


2

In [4]:
print(dic['k2'])            # 'v2'
print(dic.get('k1'))        # 'v1'
print(dic.get('k3', 'v0'))  # 'v0'

dic['k2'] = 'v3'
print(dic)                  # {'k1': 'v1', 'k2': 'v3'}

print('k2' in dic)          # True
print('v1' in dic)          # False


v2
v1
v0
{'k2': 'v3', 'k1': 'v1'}
True
False

II. Operators & Casting

III. Flow Control

If


In [6]:
import sys
if sys.version_info.major < 3:
    print("Version 2.X")
elif sys.version_info.major > 3:
    print("Future")
else:
    print("Version 3.X")


Version 3.X

Loop

for


In [7]:
for i in "Hello":
    print(i)


H
e
l
l
o

while


In [8]:
prod = 1
i = 1
while i < 10:
    prod = prod * i
    i += 1
print(prod)


362880

IV. Function

V. Class

VI. Module

VII. Pythonic

VIII. Standard Libraries


In [ ]: