第二章
message = "Hello Python world"
print(message)
message = "Hello Python world"
print(message)
message = "Hello Python Crash Course world"
print(message)
name = "Eric"
print("“Hello "+name+", would you liketo learn some Python today?”")
name = "Ada Lovelace"
print(name.title())
print(name.upper())
print(name.lower())
print("Albert Einstein oncesaid,“Apersonwho never madea mistake never tried anything new.”")
name = "Albert Einstein"
message= "“Apersonwho never madea mistake never tried anything new.”"
print(name + " once said, " + message)
favorite_language = "\n Python \t"
print(favorite_language)
favorite_language.lstrip()
favorite_language.rstrip()
favorite_language.strip()
print(5 + 3)
print(4 * 2)
print(8 / 1)
print(9 - 1)
message = 888
print("My favorite number is " + str(message))
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!
蒂姆·彼得斯的《Python禅》
美胜于丑。
显式比隐式好。
简单总比复杂好。
复杂总比复杂好。
平的比嵌套的好。
稀疏比密集好。
可读性很重要。
特殊情况不足以违反规则。
虽然实用胜过纯洁。
错误永远不应该悄无声息地过去。
除非明确沉默。
面对模棱两可的情况,拒绝猜测的诱惑。
应该有一个——最好只有一个——显而易见的方法来做到这一点。
虽然这种方式一开始可能并不明显,除非你是荷兰人。
现在总比没有好。
虽然永远都比现在好。
如果实现很难解释,那就不是个好主意。
如果实现很容易解释,这可能是一个好主意。
名称空间是一个非常好的主意——让我们做更多的工作吧!
第三章
names = ['trek', 'cannondale', 'redline', 'specialized']
for name in names:
print(name)
names = ['trek', 'cannondale', 'redline', 'specialized']
for name in names:
print('Hello '+name+" good morning!")
names = ['car', 'bicycle', 'bus']
for name in names:
print('my favorite vehicle is ' + name + '.')
persons = ["唐僧","孙悟空","猪八戒"]
for person in persons:
print('您好 '+person+' ,我想请你吃饭!')
print('Sorry the '+persons[1]+' has no time these days ,please call someone else.')
persons[1]='白骨精'
for person in persons:
print('您好 '+person+' ,我想请你吃饭!')
print('My lovely friends ,i have find a bigger desk and now we can invite more friends')
print(persons)
persons.insert(0,'牛魔王')
persons.insert(2,'如来')
persons.append('沙悟净')
print(persons)
for person in persons:
print('您好 '+person+' ,我想请你吃饭!')
print('Oh my God ,now i have to pick two of you!')
for index in range(2,len(persons)):
pop_person=persons.pop()
print("sorry "+pop_person+" i can't help it ,because the book says.")
for person in persons:
print('Dear '+person+ ' everything went according to plan.')
del persons[-1]
del persons[0]
print(persons)
place = ['beijing','shandong','yunnan','qingdao','hangzhou']
print(place)
print(sorted(place))
print(place)
print(sorted(place,reverse=True))
print(place)
place.reverse()
print(place)
place.reverse()
print(place)
place.sort()
print(place)
place.sort(reverse=True)
print(place)
persons = ["唐僧","孙悟空","猪八戒"]
for person in persons:
print('您好 '+person+' ,我想请你吃饭!')
print(persons)
print(len(persons))
第四章
pizzas = ['beef','tomato','Cheese']
for pizza in pizzas:
print('My favorite pizza is '+pizza+'.')
print('I really like pizza.')
animals = ['dog','cat','snake']
for animal in animals:
print('A '+animal+' is would make a great pet')
print('Any oftheseanimals would makea great pet!')
for value in range(1,21):
print(value)
numbers = []
for x in range(1,10001):
numbers.append(x)
for number in numbers:
print(number)
numbers = []
for x in range(1,1000001):
numbers.append(x)
print(min(numbers))
print(max(numbers))
print(sum(numbers))
numbers = []
for x in range(1,21,2):
numbers.append(x)
for number in numbers:
print(number)
numbers = []
for x in range(3,31,3):
numbers.append(x)
for number in numbers:
print(number)
numbers = []
for x in range(1,11):
numbers.append(x**3)
for number in numbers:
print(number)
numbers = [value**3 for value in range(1,11)]
print(numbers)
numbers = [x**3 for x in range(1,11)]
for number in numbers:
print(number)
print('The first three items in the list are:')
print(numbers[0:3])
print('Threeitems fromthe middle ofthelistare:')
print(numbers[3:6])
print('“Thelast threeitems in thelistare:')
print(numbers[-3:])
pizzas = ['apple pizza','banana pizza','chili pizza']
for pizza in pizzas:
print('I like ' + pizza.title())
print('\nI really love pizza!')
friend_pizzas = pizzas[:]
pizzas.append('milk pizza')
friend_pizzas.append('black pizza')
print('\nMy favorite pizzas are:')
for pizza in pizzas:
print(pizza)
print("\nMy friend's favorite pizzas are:")
for pizza in friend_pizzas:
print(pizza)