03.06 Python學習-代碼

每次打開編輯器就需要輸入:#!/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


分享到:


相關文章: