python常⽤编程代码⼤全_Python编程常⽤的⼗⼤语法和代码
汇总,学会他事半功倍...
Python是⼀种通⽤的编程和脚本语⾔。它的简单性和丰富的库使得快速开发⼀个符合现代技术需求的应⽤成为可能。Python代码写在后缀为.py⽂件中,可以⽤命令python执⾏。
C.1.1 Python的“Hello World”实例
以下为⼀个基于Python的简单程序,仅打印⼀⾏⽂本。
[输⼊]
source_code/appendix_c_python/example00_helloworld.py
print "Hello World!"
[输出]
$ python example00_helloworld.py
Hello World!
C.1.2 注释
注释不会被Python执⾏。它以字符#开头,以⾏尾结束。
网页素材gif[输⼊]
# 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
html5的基本标签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.
C.2 数据类型
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
comparetoignorecase大小比较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'])
C.2.7 字典
字典是⼀种数据结构,可以根据键存储数值。
[输⼊]
# source_code/appendix_c_python/example08_dictionary.py dictionary_names_heights = {}
dictionary_names_heights['Adam'] = 180.
帝国cms忘记登录账号密码
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语句设置代码被执⾏的下⼀个条件。
学python编程入门
[输⼊]
# 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循环
[输⼊]

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。