-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-basic.py
More file actions
51 lines (40 loc) · 905 Bytes
/
python-basic.py
File metadata and controls
51 lines (40 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding: utf-8 -*-
chmod a+x hello.py
./hello,py
name = raw_input('please enter your name: ')
print 'hello,', name
# multiple lines
print '''line1
... line2
... line3'''
# char<->utf-8
u'ABC'.encode('utf-8')
#'ABC'
u'中文'.encode('utf-8')
# '\xe4\xb8\xad\xe6\x96\x87'
print '\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8')
# 中文
'Hi, %s, you have $%d.' % ('Michael', 1000000)
# 'Hi, Michael, you have $1000000.'
u'中文'.encode('gb2312')
# '\xd6\xd0\xce\xc4'
# last element of list: list[-1]
classmates.append('Adam')
classmates.insert(1, 'Jack')
classmates.pop(1)
# multiple types or lists in lists
# tuple of one element
t = (1,)
# loop
sum = 0
for x in range(101):
sum = sum + x
print sum
# function
cmp(1, 2)
# -1
def my_abs(x):
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
# return multiple values == return a tuple