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

这篇博客介绍了Python中的数学计算,包括加减乘除、比较运算符,以及如何使用注释。通过示例代码展示了算术操作,如整数除法、取余,以及条件判断。还提及了变量的概念,并给出了一段关于车辆拼车问题的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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."

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值