标签:style color io os ar for sp on art
Python 3.4
注释
# this is a comment
print
print(format(input, format_string))
print(‘the story be about {0}, {1} and {other}‘.format(‘apple‘, ‘monkey‘, other=‘cc‘))
print(‘the story be about {0} and {1}‘.format(‘apple‘, ‘monkey‘))
print(‘\n‘)
print x,y
print("""
this is multiple lines
second line
""")
table = {‘Sjoerd‘: 4127, ‘Jack‘: 4098, ‘Dcab‘: 7678}
for name, phone in table.items():
print(‘{0:10} ==> {1:10d}‘.format(name, phone))
字符串
word = "123456"
word[0] # 1
word[-1] #6
word[0:2] #12
word = (‘aa‘
‘bb‘)
数组
squares = [1, 4, 9, 16, 25]
squares[0] # 1
squares + [11,22] # [1, 4, 9, 16, 25, 11, 22]
letters = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]
letters[2:5] = [‘C‘, ‘D‘, ‘E‘] # [‘a‘, ‘b‘, ‘C‘, ‘D‘, ‘E‘, ‘f‘, ‘g‘]
letters[2:5] = [] # [‘a‘, ‘b‘, ‘f‘, ‘g‘]
letters[:] = [] # []
len(letters) # 4
# 多维数组
a = [‘a‘, ‘b‘, ‘c‘]
n = [1, 2, 3]
x = [a, n] # [[‘a‘, ‘b‘, ‘c‘], [1, 2, 3]]
x[0] # [‘a‘, ‘b‘, ‘c‘]
x[0][1] # ‘b‘
for i, v in enumerate([‘tic‘, ‘tac‘, ‘toe‘]):
print(i, v)
questions = [‘name‘, ‘quest‘, ‘favorite color‘]
answers = [‘lancelot‘, ‘the holy grail‘, ‘blue‘]
for q, a in zip(questions, answers):
print(‘What is your {0}? It is {1}.‘.format(q, a))
元组
不可改其内的值
t1=("a", 11, "aa")
t2=("ab", 1, "dfdf")
字典
map = {"voltage": "four million", "state": "bleedin‘ demised"}
map[‘voltage‘]
list(map.keys())
‘voltage‘ in map
dict([‘a‘, 1],[‘b‘, 2])
dict(a=1, b=2)
knights = {‘gallahad‘: ‘the pure‘, ‘robin‘: ‘the brave‘}
for k, v in knights.items():
print(k, v)
条件控制
while condition:
block
if condition:
block
elif condition2:
block
else:
block
words = [1,2,3]
for w in words
block
for i in range(5):
block
range(start, end, step)
range(0, 10, 3)
break, continue
函数定义
def ask_ok(prompt, retries=4, complaint=‘Yes or no, please!‘):
while True:
ok = input(prompt)
if ok in (‘y‘, ‘ye‘, ‘yes‘):
return True
if ok in (‘n‘, ‘no‘, ‘nop‘, ‘nope‘):
return False
retries = retries - 1
if retries < 0:
raise OSError(‘uncooperative user‘)
print(complaint)
Lambda表达式
def make_incrementor(n):
return lambda x: x + n
f = make_incrementor(42)
f(0) # 42
f(1) # 43
解包范围
list(range(3, 6)) # [3,4,5]
args = [3,6]
list(range(*args))
函数文档
print(my_function.__doc__)
函数 annotation
def f(ham: 42, eggs: int = ‘spam‘) -> "Nothing to see here":
print("Annotations:", f.__annotations__)
print("Arguments:", ham, eggs)
f(‘wonderful‘)
modules
import test.python.lesson1.fibo
fib = test.python.lesson1.fibo.fib
from test.python.lesson1.fibo import fib, fib2
输入、输出
print()
类
self : 对象自己, 默认参数,实例化不需要传递
c = MyClass()
继承、多重继承
标签:style color io os ar for sp on art
原文地址:http://my.oschina.net/gofan/blog/337079