《笨方法学python》--习题3

本文详细解析了Python中的数学运算符和逻辑运算符的使用方法,包括加法、减法、乘法、除法、取余等数学运算,以及大于、小于、等于等逻辑判断。通过具体实例展示了如何进行数字计算和逻辑判断。

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

习题3 数字和数学计算

#!usr/bin/python
#--coding:utf-8--

print "I will now count my chickens:"

#表达式中只有数学运算符,就输出运算结果,与字符串用逗号分隔
print "Hens", 25.0 + 30 / 6
print "Roosters", 100.0 - 25 * 3 % 4

print "Now I will count the eggs:"

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

#字符串内有数学运算符和逻辑运算符,均不做任何处理,直接输出
print "Is it true that 3 + 2 < 5 - 7?"
#字符串外的表达式中有逻辑运算符,输出逻辑运算结果True/False
print 3.0 + 2 < 5.0 - 7

print "What is 3 + 2?", 3.0 + 2
print "What is 5 - 7?", 5.0 - 7

print "Oh, that's why it's False."
print "How about some more."

print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2

运行结果:

I will now count my chickens:
Hens 30.0
Roosters 97.0
Now I will count the eggs:
7.0
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5.0
What is 5 - 7? -2.0
Oh, that's why it's False.
How about some more.
Is it greater? True
Is it greater or equal? True
Is it less or equal? False

其他:

  1. 数学运算符号:
    + plus 加法运算
    - minus 减法运算
    / slash 取整运算
    * asterisk 乘法运算
    % percent 取余运算
  2. 逻辑运算符号:
    greater-than
    less-than
    greater-than-equal
    less-than-equal
    1. 浮点数:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值