@ExcitedSpider
2018-01-30T10:01:13.000000Z
字数 4317
阅读 1253
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!
#Wrong! Python intepreter don't know 18 is a int or two char
#TypeError: can only concatenate str (not "int") to str
print("Happy "+18+" years old")
#Yes!Using str()
print("Happy "+str(18)+" years old")
moto=['yamaha','suzuki']
#add to rear
moto.append('honda')
#delete
del moto[0]
moto.pop()
moto.remove('yamaha')
print(moto)
['yamaha', 'suzuki', 'honda']
motos=['yamaha','suzuki','suzuki']
for moto in motos:
print(moto)
#equals for(int i;i<5;i++) in c++
for value in range(1,5):
print(value)
#1 2 3 4
numbers=list(range(2,11,2))
print(numbers)
#[2, 4, 6, 8, 10]
>>>digits=[1,2,3,4,5,6]
>>>min(digits)
0
>>>max(digits)
6
>>>sum(digits)
21
#It's so amazing!
squares = [value**2 for value in range(1,11)]
print(squares)
numlists=[1,2,3,4,5,6,7,8,9,10]
print(numlists[0:3])
print(numlists[:3])
print(numlists[3:])
print(numlists[-3:])
#[1, 2, 3]
#[1, 2, 3]
#[4, 5, 6, 7, 8, 9, 10]
#[8, 9, 10]
numlists=[1,2,3,4,5,6,7,8,9,10]
numlists2=numlists[:]
#numlist2=numlists Wrong! This expression will copy reference
numlists2.append(11)
print(numlists)
print(numlists2)
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
tupleints=(10,20,30)
and <=> && in cpp
or <=> || in cpp
>>>cars = ['audi','bmw','subaru','toyota']
>>>print('bmw' in cars)
True
>>>print('byd' in cars)
False
elif
=else if
cars = []
if cars:
print("I have cars!")
else:
print("I don't have a car!")
#also can be write in one line
aliens={
'color':'green',
'points':5
}
print(aliens['color'])
print(aliens['points'])
aliens['name']='holo' #add a key-value pair
print(aliens)
del aliens['color']
#also can be write in one line
aliens={
'color':'green',
'points':5
}
for key,value in aliens.items():
print("the alien's "+key+" is value")
for item in aliens.items():
print(item)
for key in aliens.keys():
print(key)
for value in sorted(aliens.values()):
print(value)
message=input("tell me something:")
print("You say "+message)
tell me something:hello
You say hello
message=input("How old are you:")
#input is a string
#using type cast to get int
if int(message)>18:
print('You are an adult!Welcome')
def greet_user(Username='Noname'):
"""Hello"""
print(Username+", Hello!")
greet_user(Username='David')
def change(value):
value=10
val=5
change(val)
print(val)
#5
def change(values):
for i in range(0,len(values)):
values[i]=10
vals=[5]
change(vals)
print(vals)
#[10]
def change(values):
for value in values:
value=10
vals=[5]
change(vals[:]) //safe mode, cost time
print(vals)
#[5]
def make_pizza(size,*toppings):
#notice the order of args,*means toppings is a tuple.
"""make pizza with variable ingredients"""
for top in toppings:
print(top+" add!")
print("This is a "+str(size)+"-inch pizza")
make_pizza("mushroom",'peppers','cheese')
#mushroom add!
#peppers add!
#cheese add!
def build_profile(first,last,**userinfo):
#key-value pair as varargs,use two '*'
"""创建用户信息"""
profile={}
profile['first_name']=first
profile['last_name']=last
for key,value in userinfo.items():
profile[key]=value
return profile
first='albert'
last='einstein'
profile=build_profile(first.title(),last.title(),
major='physics',
local='princeton')
print(profile)
#{'first_name': 'Albert', 'last_name': 'Einstein', 'major': 'physics', 'local': 'princeton'}
import mod as p
#别名
p.fun()
from mod import *
fun() #fun is a function in mod