@codejan
2017-06-09T13:24:17.000000Z
字数 8274
阅读 583
python
>>> 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 是一个动态的解释型(字节码编译)语言。它在运行之前并不会去对代码做出额外的“翻译”的工作,计算机会直接在它被运行的时候,一边理解和执行它,一边判断程序中是否有造成没有办法执行的部分(语法错误)。
我们需要了解一下什么情况不属于应该注释的情况。首先,我们不应该为怪异的逻辑添加注释,因为本身设计的很反人类的逻辑应该被重构成易读、易懂的代码,注释并不应该为此买单;其次,一看就显而易见的东西不需要注释,特别是看命名就显然可以理解的内容,是不应该出现在注释中的;最后要注意,不要在注释中描述程序的运行过程,因为这样做往往没有任何意义,未来的你和其他合格程序员应该都可以自己完成简单的运行过程分析。
Python 中的注释内容是以 #开头的,任何代码行的行中出现#后的内容都会被注释,而不被解析。当我们需要写多行的注释内容时,我们采用连续多行的每行开头都加#的方式来进行书写。
同时,Python 提供了另外一种让局部程序不被执行的方式,你可以在你希望不被执行的代码块前后两行分别加上’’’这样的三个单引号。有的地方会称这种方式为多行注释,但是我们并不建议用于添加注释内容时使用,而建议你仅在需要让部分代码暂时不被执行时临时使用这个方式让程序避开这部分的执行。
Python 中可以直接写 if 2 < a < 13:
name = raw_input('What is your name:')
print 'Your name is ' + name
a = 'China'
a_len = len(a)
b = 'length of a is '
print b + str(a_len)
a = 'This is a string\
let you to understand'
a = 'In\na line'
b = r'In\na line'
print a
print b
print a.lower()
print b.upper()
s = 'HelloabcdWord'
print s.isalpha()
print s.isdigit()
print s.startswith('Hello')
print s.endswith('World')
#字符串切取
tower = 'abcdefg'
print tower[1:4] #cde
print tower[3:] #defg
print tower[:-2] #abcde
name = 'Wangmu Niangniang'
age = 5000
height = 1.73
print name + ' is a ' + str(age) + '-year-old woman with height ' + str(height)
print '%s is a %d-year-old woman with height %g' % (name, age, height)
s = u'I want to say \u4e2d\u56fd\u4e07\u5c81'
print s
在条件语句的 if 下可以有任何类型的值,数值0以及空字符串''和其他空类型都代表了“假”;数值1则表示了“真”。同时,我们还有一种叫布尔类型的值,它只可能为两种值,分别是也可以表示“假”的 False(会被转成整数 0 来理解)和表示“真”的True(会被转为整数 1 来理解)。
weather = 'rainy day'
bag = 'nothing in the bag'
if weather.find('rain') != -1: #注意是冒号
bag = bag.replace('nothing', 'unbrella')
print bag
list = [100, 23, 45]
print list[0]
print list[1]
print list[2]
print len(list)
hello = ['hi', 'hello']
world = ['earth', 'field', 'universe']
hello.append('nihao')
print hello
hello.extend(world)
print hello
hello = ['hi', 'hello']
hello.insert(0, 'nihao')
print hello
print hello.index('hi')
hello = ['nihao', 'hi', 'hello']
hello.remove('nihao')
print hello
hello.pop(0)
print hello
manager = 'tuotatianwang,taibaijinxing,juanliandajiang'
manager_list = manager.split(',')
print manager_list
new_manager = ' '.join(manager_list)
print new_manager
gardens = [7204, 3640, 1200, 1240, 71800, 3200, 604]
total = 0
for num in gardens:
total += num
print total
print sum(gardens)
first = 0
second = 1
# Please write code here
while first < 100:
print first
first, second = second, first + second
print 'Everything is done'
n = int(raw_input())
a,b = 0, 1
for i in range(n):
a,b=b,a+b
print a
numbers = [1, 4, 2, 3, 8, 3, 0]
print sorted(numbers)
print sorted(numbers, reverse=True) #打印逆序数列
def china_first(item):
if item == 'China':
return 0
else:
return len(item)
country = ['jp', 'China', 'USA', 'Thai']
print sorted(country, key = len)
print sorted(country, key = china_first)
tuple = (1, 2, 'hi')
print len(tuple)
print tuple[2]
tuple = (1, 2, 'bye')
print tuple
def plus_one(tuple):
return tuple[0] + 1, tuple[1] + 1, tuple[2] + 1
t = (1, 4, -1)
(x, y, z) = plus_one(t)
print x
print y, z
bat = {}
bat['b'] = 'baidu'
bat['a'] = 'alibaba'
bat['t'] = 'tencent'
print bat
print bat['a']
bat['a'] = 'amazon'
print bat['a']
print 'b' in bat
print 'x' in bat
bat = {'a': 'alibaba', 'b': 'baidu', 't': 'tencent'}
print bat.keys()
print bat.values()
print bat.items()
bat = {'a': 'alibaba', 'b': 'baidu', 't': 'tencent'}
for value in bat.values():
print value
for key in bat:
print key
for k, v in bat.items():
print k, '>', v
boss = {}
boss['name'] = 'robin'
boss['age'] = 45
boss['height'] = 1.78
print 'The boss named %(name)s is %(age)d-year-old and %(height)g tall.' % boss
num = 6
list = ['a', 'b', 'c', 'd']
dict = {'a': 1, 'b': 2, 'c': 3}
del list[0]
del list[-2:]
print list
del dict['b']
print dict
del num
print num
import re
str = 'A cute word:cat!!'
match = re.search(r'word:\w\w\w',str)
if match:
print 'found', match.group()
import re
print re.search(r'..g', 'piiig').group()
print re.search(r'\d\d\d', 'p123g').group()
print re.search(r'\w\w\w','@@abcd!!').group()
import re
print re.search(r'pi+', 'piiig').group()
print re.search(r'pi*', 'pg').group()
import re
print re.search(r'[abc]+', 'xxxacbbcbbadddedede').group()
#acbbcbba
print re.search(r'[a-d]+', 'xxxacbbcbbadddedede').group()
#acbbcbbaddd
import re
str = 'purple alice-b@jisuanke.com monkey dishwasher'
match = re.search('([\w.-]+)@([\w.-]+)',str)
if match:
print match.group()
print match.group(1)
print match.group(2)
import re
str = 'purple alice@jisuanke.com, blah monkey bob@abc.com blah dishwasher'
tuples = re.findall(r'([\w\.-]+)@([\w\.-]+',str)
print tuples
a = raw_input()
a = a.replace(' ','%20')
print a
import sys
n = int(raw_input())
a = map(int,sys.stdin.readline().split())
b = int(raw_input())
#print a
for i in range(n):
for j in range(n):
if a[i] + a[j] == b and j > i:
print i+1, j+1
break
a,b,c,d=0,0,0,0
s=raw_input()
for x in s:
if('a'<=x<='z' or 'A'<=x<='Z'):
a = a+1
elif('0'<=x<='9'):
b = b+1
elif(x == ' '):
c = c+1
else:
d = d+1
print a,b,c,d
n = int(raw_input())
a,b = 0, 1
for i in range(n):
a,b=b,a+b
print a
from __future__ import print_function
import sys
a = map(int,sys.stdin.readline().split())
n = len(a)
for i in range(2,n,2):
for j in range(i+2,n,2):
#print i, j
#print i%3!=0, j%3!=0, a[i]>a[j]
if ((i%3)!=0) and ((j%3)!=0) and a[i-1]>a[j-1]:
a[i-1],a[j-1]=a[j-1],a[i-1]
for i in range(3,n,3):
for j in range(i+3,n,3):
if a[i] < a[j]:
a[i-1],a[j-1]=a[j-1],a[i-1]
for x in a: print(x,end=' ')