每次打开编辑器就需要输入:#!/usr/bin/env python3
1、print("output#1:i am excited to learn python.")
output#1:i am excited to learn python.
2、两个数值相加
x=4
y=5
z=x+y
print("output#2:four plus five equals{0:d}.".format(z))
output#2:four plus five equals9.
3、两个列表相加
a=[1,2,3,4]
b=["first","second","third","fourth"]
c=a+b
print("output#3:{0},{1},{2}".format(a,b,c))
output#3:[1, 2, 3, 4],['first', 'second', 'third', 'fourth'],[1, 2, 3, 4, 'first', 'second', 'third', 'fourth']
用了.format就可以用逗号将三个部分分隔开。
4、x=9
print("output#4:{0}".format(x))
output#4:9
5、print("output#5:{0}".format(3**4))
output#5:81
6、print("output#6:{0}".format(int(8.3)/int(2.7)))
output#6:4.0
7、print("output#7:{0:.3f}".format(8.3/2.7))
output#7:3.074
8、print("output#8:{0:.1f}".format(y))
output#8:12.0
9、print("output#9:{0:.2f}".format(r))
output#9:2.67
10、print("output#10:{0:.4f}".format(8.0/3))
output#10:2.6667
要想使用 math 模块中的一些函数,只需在脚本开头 shebang 行的下方添加 from math import
[function name] 。例如,可以将下面的代码行添加到 first_script.py 中,在 shebang 行下面:
#!/usr/bin/env python3
from math import exp, log, sqrt
11、print("output#11:{0:.4f}".format(exp(3)))
output#11:20.0855
12、print("output#12:{0:.2f}".format(log(4)))
output#12:1.39
13、print("output#!3:{0:.1f}".format(sqrt(81)))
output#13:9.0
14、print("output#14:{0:s}".format("i'm excited to learn python.") )
output#14:i'm excited to learn python.
15、print("Output #15: {0:s}".format("This is a long string. Without the backslash\\
it would run off of the page on the right in the text editor and be very\\
difficult to read and edit. By using the backslash you can split the long\\
string into smaller strings on separate lines so that the whole string is easy\\
to view in the text editor."))
Output #15: This is a long string. Without the backslashit would run off of the page on the right in the text editor and be verydifficult to read and edit. By using the backslash you can split the longstring into smaller strings on separate lines so that the whole string is easyto view in the text editor.
用反斜杠将一个非常长的字符串分段显示在多个行中,以使它易于理解和编辑。尽管这个字符串分布在脚本的多个行中,它还是一个字符串,并作为一个完整的字符串被打印出来。在这种将长字符串分成多行的方法中,要注意的是反斜杠必须是每行的最后一个字符。
16、print("Output #16: {0:s}".format('''You can use triple single quotes
for multi-line comment strings.'''))
Output #16: You can use triple single quotes
for multi-line comment strings.
分行显示
17、print("Output #17: {0:s}".format("""You can also use triple double quotes
for multi-line comment strings."""))
Output #17: You can also use triple double quotes
for multi-line comment strings.
分行显示
18、string1="this is a"
string2="short string"
sentence=string1+string2
print("output#18:{0:s}".format(sentence))
output#18:this is ashort string
19、print("output#19:{0:s}{1:s}{2:s}".format("she is ","very "*4," beautiful."))
output#19:she is very very very very beautiful.
注意空格
20、m=len(sentence)
print("output#20:{0:d}" .format(m))
output#20:21
21、string1 = "My deliverable is due in May"
string1_list1 = string1.split()
string1_list2 = string1.split(" ",2)
print("Output #21: {0}".format(string1_list1))
Output #21: ['My', 'deliverable', 'is', 'due', 'in', 'May']
22、print("Output #22: FIRST PIECE:{0} SECOND PIECE:{1} THIRD PIECE:{2}"\\
.format(string1_list2[0], string1_list2[1], string1_list2[2]))
Output #22: FIRST PIECE:My SECOND PIECE:deliverable THIRD PIECE:is due in May
23、string2 = "Your,deliverable,is,due,in,June"
string2_list = string2.split(',')
print("Output #23: {0}".format(string2_list))
Output #23: ['Your', 'deliverable', 'is', 'due', 'in', 'June']
24、print("Output #24: {0} {1} {2}".format(string2_list[1], string2_list[5],\\
string2_list[-1]))
Output #24: deliverable June June
25、print("output#25:{0}".format(','.join(string2_list)))
output#25:Your,deliverable,is,due,in,June
閱讀更多 阿信的小薔薇 的文章