[輸入]
source_code/appendix_c_python/example00_helloworld.py
print "Hello World!"
[輸出]
$ python example00_helloworld.py
Hello World!
注釋不會被Python執行。它以字符#開頭,以行尾結束。
[輸入]
# source_code/appendix_c_python/example01_comments.py
print "This text will be printed because the print statement is executed."
#這只是一個注釋,不會被執行
#print "Even commented statements are not executed."
print "But the comment finished with the end of the line."
print "So the 4th and 5th line of the code are executed again."
[輸出]
$ python example01_comments.py
This text will be printed because the print statement is executed
But the comment finished with the end of the line.
So the 4th and 5th line of the code are executed again.
Python的一些有效數據類型如下所示。
數字數據類型:整型、浮點型。
文本數據類型:字符串型。
複合數據類型:元組、列表、集合、字典。
C.2.1 整型
整數數據類型只能存儲整數值。
[輸入]
# source_code/appendix_c_python/example02_int.py
rectangle_side_a = 10
rectangle_side_b = 5
rectangle_area = rectangle_side_a * rectangle_side_b
rectangle_perimeter = 2*(rectangle_side_a + rectangle_side_b)
print "Let there be a rectangle with the sides of lengths:"
print rectangle_side_a, "and", rectangle_side_b, "cm."
print "Then the area of the rectangle is", rectangle_area, "cm squared."
print "The perimeter of the rectangle is", rectangle_perimeter, "cm."
[輸出]
$ python example02_int.py
Let there be a rectangle with the sides of lengths: 10 and 5 cm.
Then the area of the rectangle is 50 cm squared.
The perimeter of the rectangle is 30 cm.
C.2.2 浮點型
浮點數據類型也可以存儲非整數的有理數值。
[輸入]
# source_code/appendix_c_python/example03_float.py
pi = 3.14159
circle_radius = 10.2
circle_perimeter = 2 * pi * circle_radius
circle_area = pi * circle_radius * circle_radius
print "Let there be a circle with the radius", circle_radius, "cm."
print "Then the perimeter of the circle is", circle_perimeter, "cm."
print "The area of the circle is", circle_area, "cm squared."
[輸出]
$ python example03_float.py
Let there be a circle with the radius 10.2 cm.
Then the perimeter of the circle is 64.088436 cm.
The area of the circle is 326.8510236 cm squared.
C.2.3 字符串
字符串變量可以用於存儲文本。
[輸入]
# source_code/appendix_c_python/example04_string.py
first_name = "Satoshi"
last_name = "Nakamoto"
full_name = first_name + " " + last_name
print "The inventor of Bitcoin is", full_name, "."
[輸出]
$ python example04_string.py
The inventor of Bitcoin is Satoshi Nakamoto.
C.2.4 元組
元組數據類型類似於數學中的向量。例如,tuple = (integer_number, float_number)。
[輸入]
# source_code/appendix_c_python/example05_tuple.py
import math
point_a = (1.2,2.5)
point_b = (5.7,4.8)
#math.sqrt計算浮點數的平方根
#math.pow計算浮點數的冪
segment_length = math.sqrt(
math.pow(point_a[0] - point_b[0], 2) +
math.pow(point_a[1] - point_b[1], 2))
print "Let the point A have the coordinates", point_a, "cm."
print "Let the point B have the coordinates", point_b, "cm."
print "Then the length of the line segment AB is", segment_length, "cm."
[輸出]
$ python example05_tuple.py
Let the point A have the coordinates (1.2, 2.5) cm.
Let the point B have the coordinates (5.7, 4.8) cm.
Then the length of the line segment AB is 5.0537115074 cm.
C.2.5 列表
Python中的列表指的是一組有序的數值集合。
[輸入]
# source_code/appendix_c_python/example06_list.py
some_primes = [2, 3]
some_primes.append(5)
some_primes.append(7)
print "The primes less than 10 are:", some_primes
[輸出]
$ python example06_list.py
The primes less than 10 are: [2, 3, 5, 7]
C.2.6 集合
Python中的集合指的是一組無序的數值集合。
[輸入]
# source_code/appendix_c_python/example07_set.py
from sets import Set
boys = Set(['Adam', 'Samuel', 'Benjamin'])
girls = Set(['Eva', 'Mary'])
teenagers = Set(['Samuel', 'Benjamin', 'Mary'])
print 'Adam' in boys
print 'Jane' in girls
girls.add('Jane')
print 'Jane' in girls
teenage_girls = teenagers & girls #intersection
mixed = boys | girls #union
non_teenage_girls = girls - teenage_girls #difference
print teenage_girls
print mixed
print non_teenage_girls
[輸出]
$ python example07_set.py
True
False
True
Set(['Mary'])
Set(['Benjamin', 'Adam', 'Jane', 'Eva', 'Samuel', 'Mary'])
Set(['Jane', 'Eva'])
字典是一種數據結構,可以根據鍵存儲數值。
[輸入]
# source_code/appendix_c_python/example08_dictionary.py
dictionary_names_heights = {}
dictionary_names_heights['Adam'] = 180.
dictionary_names_heights['Benjamin'] = 187
dictionary_names_heights['Eva'] = 169
print 'The height of Eva is', dictionary_names_heights['Eva'], 'cm.'
[輸出]
$ python example08_dictionary.py
The height of Eva is 169 cm.
C.3 控制流
條件語句,即我們可以使用if語句,讓某段代碼只在特定條件被滿足的情況下被執行。如果特定條件沒有被滿足,我們可以執行else語句後面的代碼。如果第一個條件沒有被滿足,我們可以使用elif語句設置代碼被執行的下一個條件。
[輸入]
# source_code/appendix_c_python/example09_if_else_elif.py
x = 10
if x == 10:
print 'The variable x is equal to 10.'
if x > 20:
print 'The variable x is greater than 20.'
else:
print 'The variable x is not greater than 20.'
if x > 10:
print 'The variable x is greater than 10.'
elif x > 5:
print 'The variable x is not greater than 10, but greater ' + 'than 5.'
else:
print 'The variable x is not greater than 5 or 10.'
[輸出]
$ python example09_if_else_elif.py
The variable x is equal to 10.
The variable x is not greater than 20.
The variable x is not greater than 10, but greater than 5.
C.3.1 for循環
for循環可以實現迭代某些集合元素中的每一個元素的功能,例如,range集合、列表。
C3.1.1 range的for循環
[輸入]
source_code/appendix_c_python/example10_for_loop_range.py
print "The first 5 positive integers are:"
for i in range(1,6):
print i
[輸出]
$ python example10_for_loop_range.py
The first 5 positive integers are:
1
2
3
4
5
C3.1.2 列表的for循環
[輸入]
source_code/appendix_c_python/example11_for_loop_list.py
primes = [2, 3, 5, 7, 11, 13]
print 'The first', len(primes), 'primes are:'
for prime in primes:
print prime
[輸出]
$ python example11_for_loop_list.py
The first 6 primes are:
2
3
5
7
11
13
C3.1.3 break和continue
for循環可以通過語句break提前中斷。for循環的剩餘部分可以使用語句continue跳過。
[輸入]
source_code/appendix_c_python/example12_break_continue.py
for i in range(0,10):
if i % 2 == 1: #remainder from the division by 2
continue
print 'The number', i, 'is divisible by 2.'
for j in range(20,100):
print j
if j > 22:
break;
[輸出]
$ python example12_break_continue.py
The number 0 is divisible by 2.
The number 2 is divisible by 2.
The number 4 is divisible by 2.
The number 6 is divisible by 2.
The number 8 is divisible by 2.
20
21
22
23
C.3.2 函數
Python支持函數。函數是一種定義一段可在程序中多處被執行的代碼的好方法。我們可使用關鍵詞def定義一個函數。
[輸入]
source_code/appendix_c_python/example13_function.py
def rectangle_perimeter(a, b):
return 2 * (a + b)
print 'Let a rectangle have its sides 2 and 3 units long.'
print 'Then its perimeter is', rectangle_perimeter(2, 3), 'units.'
print 'Let a rectangle have its sides 4 and 5 units long.'
print 'Then its perimeter is', rectangle_perimeter(4, 5), 'units.'
[輸出]
$ python example13_function.py
Let a rectangle have its sides 2 and 3 units long.
Then its perimeter is 10 units.
Let a rectangle have its sides 4 and 5 units long.
Then its perimeter is 18 units.
C.3.3 程序參數
程序可以通過命令行傳遞參數。
[輸入]
source_code/appendix_c_python/example14_arguments.py
#引入系統庫以使用命令行參數列表
import sys
print 'The number of the arguments given is', len(sys.argv),'arguments.' print 'The argument list is ', sys.argv, '.'
[輸出]
$ python example14_arguments.py arg1 110
The number of the arguments given is 3 arguments.
The argument list is ['example14_arguments.py', 'arg1', '110'].
C.3.4 文件讀寫
下面程序將向文件test.txt寫入兩行文字,然後讀取它們,最後將其列印到輸出中。
[輸入]
# source_code/appendix_c_python/example15_file.py
#寫入文件"test.txt"
file = open("test.txt","w")
file.write("first line\n")
file.write("second line")
file.close()
#read the file
file = open("test.txt","r")
print file.read()
[輸出]
$ python example15_file.py
first line
second line
*聲明:版權歸原作者所有,如來源信息有誤或侵犯權益,請聯繫我們刪除或授權事宜。
文章轉載於公眾號:python