专栏名称: python
隔天更新python文章,我希望用我的努力换来劳动的成果帮助更多的人掌握一门技术,因此我要更加努力。
目录
相关文章推荐
Python爱好者社区  ·  生成式AI,彻底爆了! ·  昨天  
Python爱好者社区  ·  64k!确实可以封神了! ·  3 天前  
Python爱好者社区  ·  公司Rust团队全员被裁,只因把服务写得「太 ... ·  昨天  
Python爱好者社区  ·  强的离谱!CNN,yyds ·  2 天前  
Python爱好者社区  ·  《MCP原理与实践》—— ... ·  4 天前  
51好读  ›  专栏  ›  python

Python编程常用的十大语法和代码汇总,学会他事半功倍

python  · 公众号  · Python  · 2020-11-12 20:28

正文

请到「今天看啥」查看全文



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 列表







请到「今天看啥」查看全文