python 笔记 数学计算与变量——12.21

来源:互联网 发布:gdi编程 编辑:程序博客网 时间:2024/06/13 09:00

octothorpe、poundcharacter 指#,python中表注释

 

数字和数学计算

• +plus 加号

• -minus 减号

• /slash 斜杠

• *asterisk 星号

• %percent 百分号

• <less-than 小于号

• >greater-than 大于号

•<= less-than-equal 小于等于号

•>= greater-than-equal 大于等于号

 

 

小测试. ex3.py

print "I willnow count my chickens:"

print"Henss", 25 + 30 / 6

print"Roosters", 100 - 25 * 3 % 4

print "Now Iwill count the eggs:"

print 3 + 2 + 1 - 5+ 4 % 2 - 1 / 4 + 6

print "Is ittrue that 3 + 2 < 5 - 7?"

print 3 + 2 < 5-7

print "What is3 +2?", 3 + 2

print "What is5 -7?", 5 - 7

print "Oh,that's why it's false."

print "Howabout some more."

print "Is itgreater?",5 > -2

print "Is itgreater or equal?", 5 >= -2

print "Is itless or equal?", 5 <=-2

输出:

 


加入自己注释。默认情况下python不认中文

可以在头部添加# -*-coding: utf -8 -*-

# -*- coding: utf-8-*-

#我要数小鸡了

print "I willnow count my chickens:"

# 输出 Henss ,25+5=30

# 输出 Roosters ,25 *3 % 4 是取余,75/4=18.....3,100-3=97

print"Henss", 25 + 30 / 6

print"Roosters", 100 - 25 * 3 % 4

print "Now Iwill count the eggs:"

# 输出 3 + 2 + 1 - 5 +4 % 2 - 1 / 4 + 6 =1 + 0 - 1 / 4 +6 =1 + 0 + 0 + 6 = 7 ,1 / 4 为 0是因为前面运算都是整型,所以忽略后面小数点为0

print 3 + 2 + 1 - 5+ 4 % 2 - 1 / 4 + 6

# 输出 3 + 2 是否 小于 5 -7 ? ,结果错误输出false 错误

print "Is ittrue that 3 + 2 < 5 - 7?"

print 3 + 2 < 5-7

# 3 + 2 和 3 + 2的关系是什么?

print "What is3 +2?", 3 + 2

# 5 - 7 和 5 - 7的关系是什么?

print "What is5 -7?", 5 - 7

print "Oh,that's why it's false."

print "Howabout some more."

# 输出求问? 它是否更大? 5 和-2 相比

print "Is itgreater?",5 > -2

# 相似的,5 是否大于 或者等于 -2 ,结果显然,5 是大于或等于 -2 的,结果为true.

print "Is itgreater or equal?", 5 >= -2

print "Is itless or equal?", 5 <=-2

浮点型测试

print"Henss" , 25 + 30 / 6

print"table" , 1 / 4

 

print"table" , float(1) / float(4)

 



 

 

对于整型情况下

print "Is it greater or equal?",5>=-2
print "Is it equal?",5/4>=3 / 2
print "Is it equal?",5/4<=3 / 2

 

 

 

 

 

变量(variable) 和命名

cars =100

space_in_a_car = 4.0

drivers = 30

passengers = 90

cars_not_driven = cars - drivers

cars_driven = drivers

carpool_capacity = cars_driven * space_in_a_car

average_passsenger_per_car = passengers / cars_driven

 

 

print "There are ",cars,"cars available."

print "there are only", drivers,"drivers available."

print "there will be", cars_not_driven,"emptycars today."

print "we can transport", carpool_capacity,"peopletoday."

print "we have",passengers,"to carpool today."

print "we need to put about", average_passsenger_per_car,"in each car."

 

 

 

 

 

 

 

原创粉丝点击