Python 30天: 第三天 -- 运算符

文章介绍了Python中的基本数据类型,包括布尔值、整数、浮点数和复数,以及相关的运算符如赋值、算术运算、比较和逻辑运算。此外,文中还提供了编程练习,涉及三角形和矩形的面积、周长计算,以及字符串比较和逻辑检查等。
摘要由CSDN通过智能技术生成

<< 第二天 || 第四天 >>

第三天

布尔值(Boolean)

布尔数据类型表示两个值之一:TrueFalse。一旦我们开始使用比较运算符,这些数据类型的使用就会很清楚。与 JavaScript 不同, True 的首字母T和 False 的首字母F应该大写。 

示例:布尔值

print(True)
print(False)

运算符 (Operators)

Python 语言支持多种类型的运算符。在本节中,我们将重点关注其中的几个。

赋值运算符

赋值运算符用于为变量赋值。让我们以 = 为例。数学中的等号表示两个值相等,但在 Python 中,它意味着我们将一个值存储在某个变量中,我们称之为赋值或为变量赋值。下表显示了不同类型的 python 赋值运算符,取自w3school

算术运算符: 

  • 加法(+):a + b
  • 减法(-): a - b
  • 乘法(*):a * b
  • 除法(/):a / b
  • 模数(%):a % b
  • 楼层划分(//): a // b
  • 求幂(**):a ** b

例子:整数 

# Arithmetic Operations in Python
# Integers

print('Addition: ', 1 + 2)        # 3
print('Subtraction: ', 2 - 1)     # 1
print('Multiplication: ', 2 * 3)  # 6
print ('Division: ', 4 / 2)       # 2.0  Division in Python gives floating number
print('Division: ', 6 / 2)        # 3.0         
print('Division: ', 7 / 2)        # 3.5
print('Division without the remainder: ', 7 // 2)   # 3,  gives without the floating number or without the remaining
print ('Division without the remainder: ',7 // 3)   # 2
print('Modulus: ', 3 % 2)         # 1, Gives the remainder
print('Exponentiation: ', 2 ** 3) # 9 it means 2 * 2 * 2

示例:浮点数

# Floating numbers
print('Floating Point Number, PI', 3.14)
print('Floating Point Number, gravity', 9.81)

示例:复数 

# Complex numbers
print('Complex number: ', 1 + 1j)
print('Multiplying complex numbers: ',(1 + 1j) * (1 - 1j))

让我们声明一个变量并分配一个数字数据类型。我将使用单字符变量,但请记住不要养成声明此类变量的习惯。变量名应始终便于记忆。

例子:

# Declaring the variable at the top first

a = 3 # a is a variable name and 3 is an integer data type
b = 2 # b is a variable name and 3 is an integer data type

# Arithmetic operations and assigning the result to a variable
total = a + b
diff = a - b
product = a * b
division = a / b
remainder = a % b
floor_division = a // b
exponential = a ** b

# I should have used sum instead of total but sum is a built-in function - try to avoid overriding built-in functions
print(total) # if you do not label your print with some string, you never know where the result is coming from
print('a + b = ', total)
print('a - b = ', diff)
print('a * b = ', product)
print('a / b = ', division)
print('a % b = ', remainder)
print('a // b = ', floor_division)
print('a ** b = ', exponentiation)

例子:

print('== Addition, Subtraction, Multiplication, Division, Modulus ==')

# Declaring values and organizing them together
num_one = 3
num_two = 4

# Arithmetic operations
total = num_one + num_two
diff = num_two - num_one
product = num_one * num_two
div = num_two / num_one
remainder = num_two % num_one

# Printing values with label
print('total: ', total)
print('difference: ', diff)
print('product: ', product)
print('division: ', div)
print('remainder: ', remainder)

让我们开始连接点并开始利用我们已经知道的东西来计算(面积、体积、密度、重量、周长、距离、力)。

例子:

# Calculating area of a circle
radius = 10                                 # radius of a circle
area_of_circle = 3.14 * radius ** 2         # two * sign means exponent or power
print('Area of a circle:', area_of_circle)

# Calculating area of a rectangle
length = 10
width = 20
area_of_rectangle = length * width
print('Area of rectangle:', area_of_rectangle)

# Calculating a weight of an object
mass = 75
gravity = 9.81
weight = mass * gravity
print(weight, 'N')                         # Adding unit to the weight

# Calculate the density of a liquid
mass = 75 # in Kg
volume = 0.075 # in cubic meter
density = mass / volume # 1000 Kg/m^3

比较运算符 

在编程中我们比较值,我们使用比较运算符来比较两个值。我们检查一个值是否大于或小于或等于其他值。下表显示了取自w3shool的 Python 比较运算符。

示例:比较运算符 

print(3 > 2)     # True, because 3 is greater than 2
print(3 >= 2)    # True, because 3 is greater than 2
print(3 < 2)     # False,  because 3 is greater than 2
print(2 < 3)     # True, because 2 is less than 3
print(2 <= 3)    # True, because 2 is less than 3
print(3 == 2)    # False, because 3 is not equal to 2
print(3 != 2)    # True, because 3 is not equal to 2
print(len('mango') == len('avocado'))  # False
print(len('mango') != len('avocado'))  # True
print(len('mango') < len('avocado'))   # True
print(len('milk') != len('meat'))      # False
print(len('milk') == len('meat'))      # True
print(len('tomato') == len('potato'))  # True
print(len('python') > len('dragon'))   # False


# Comparing something gives either a True or False

print('True == True: ', True == True)
print('True == False: ', True == False)
print('False == False:', False == False)

 除了上述比较运算符,Python还使用:

  • is:如果两个变量是同一个对象(x 是 y),则返回 true
  • is not:如果两个变量不是同一个对象(x 不是 y),则返回 true
  • in:如果查询列表包含某个项目(x in y),则返回 True
  • not in:如果查询列表中没有某个项目(x in y),则返回 True
print('1 is 1', 1 is 1)                   # True - because the data values are the same
print('1 is not 2', 1 is not 2)           # True - because 1 is not 2
print('A in Asabeneh', 'A' in 'Asabeneh') # True - A found in the string
print('B in Asabeneh', 'B' in 'Asabeneh') # False - there is no uppercase B
print('coding' in 'coding for all') # True - because coding for all has the word coding
print('a in an:', 'a' in 'an')      # True
print('4 is 2 ** 2:', 4 is 2 ** 2)   # True

逻辑运算符 

与其他编程语言不同,python 使用关键字andornot作为逻辑运算符。逻辑运算符用于组合条件语句:

print(3 > 2 and 4 > 3) # True - because both statements are true
print(3 > 2 and 4 < 3) # False - because the second statement is false
print(3 < 2 and 4 < 3) # False - because both statements are false
print('True and True: ', True and True)
print(3 > 2 or 4 > 3)  # True - because both statements are true
print(3 > 2 or 4 < 3)  # True - because one of the statements is true
print(3 < 2 or 4 < 3)  # False - because both statements are false
print('True or False:', True or False)
print(not 3 > 2)     # False - because 3 > 2 is true, then not True gives False
print(not True)      # False - Negation, the not operator turns true to false
print(not False)     # True
print(not not True)  # True
print(not not False) # False

 🌕你有无限的能量。您刚刚完成第 3 天的挑战,您在通往卓越的道路上领先了三步。现在为您的大脑和肌肉做一些锻炼。

练习 - 第三天

  1. 将您的年龄声明为整数变量
  2. 将您的身高声明为浮点变量
  3. 声明一个存储复数的变量
  4. 编写一个脚本,提示用户输入三角形的底边和高度,并计算该三角形的面积(面积 = 0.5 xbxh)。
        Enter base: 20
        Enter height: 10
        The area of the triangle is 100
  5. 编写一个脚本,提示用户输入三角形的 a 边、b 边和 c 边。计算三角形的周长(周长 = a + b + c)。
    Enter side a: 5
    Enter side b: 4
    Enter side c: 3
    The perimeter of the triangle is 12
  6. 使用提示获取矩形的长度和宽度。计算它的面积(面积=长x宽)和周长(周长=2x(长+宽))
  7. 使用提示获取圆的半径。计算面积 (area = pi xrxr) 和周长 (c = 2 x pi xr),其中 pi = 3.14。
  8. 计算 y = 2x -2 的斜率、x 截距和 y 截距
  9. 斜率为 (m = y2-y1/x2-x1)。求点 (2, 2) 和点 (6,10) 之间的斜率和欧氏距离
  10. 比较任务 8 和 9 中的斜率。
  11. 计算 y 的值 (y = x^2 + 6x + 9)。尝试使用不同的 x 值并找出在什么 x 值时 y 将为 0。
  12. 找出“python”和“dragon”的长度,然后做一个虚假的比较语句。
  13. 使用and运算符检查是否在“python”和“dragon”中都找到“on”
  14. 我希望这门课程不会充满行话。使用in运算符检查句子中是否有行话。
  15. dragon 和 python 都没有'on'
  16. 查找文本python的长度并将值转换为float并将其转换为字符串
  17. 偶数能被 2 整除,余数为零。你如何使用 python 检查数字是否为偶数?
  18. 检查 7 除以 3 的底数是否等于 2.7 的 int 转换值。
  19. 检查“10”的类型是否等于 10 的类型
  20. 检查 int('9.8') 是否等于 10
  21. 编写一个脚本,提示用户输入小时数和每小时费率。计算人的工资?
    Enter hours: 40
    Enter rate per hour: 28
    Your weekly earning is 1120
  22. 编写一个脚本,提示用户输入年数。计算一个人可以活多少秒。假设一个人可以活百年
    Enter number of years you have lived: 100
    You have lived for 3153600000 seconds.

   23.编写一个显示下表的Python脚本

1 1 1 1 1
2 1 2 4 8
3 1 3 9 27
4 1 4 16 64
5 1 5 25 125

🎉恭喜!🎉

<< 第二天 || 第四天 >>​​​​​​​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

舍不得,放不下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值