<< 第二天 || 第四天 >>
第三天
布尔值(Boolean)
布尔数据类型表示两个值之一:True或False。一旦我们开始使用比较运算符,这些数据类型的使用就会很清楚。与 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 使用关键字and、or和not作为逻辑运算符。逻辑运算符用于组合条件语句:
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 天的挑战,您在通往卓越的道路上领先了三步。现在为您的大脑和肌肉做一些锻炼。
练习 - 第三天
- 将您的年龄声明为整数变量
- 将您的身高声明为浮点变量
- 声明一个存储复数的变量
- 编写一个脚本,提示用户输入三角形的底边和高度,并计算该三角形的面积(面积 = 0.5 xbxh)。
Enter base: 20 Enter height: 10 The area of the triangle is 100
- 编写一个脚本,提示用户输入三角形的 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
- 使用提示获取矩形的长度和宽度。计算它的面积(面积=长x宽)和周长(周长=2x(长+宽))
- 使用提示获取圆的半径。计算面积 (area = pi xrxr) 和周长 (c = 2 x pi xr),其中 pi = 3.14。
- 计算 y = 2x -2 的斜率、x 截距和 y 截距
- 斜率为 (m = y2-y1/x2-x1)。求点 (2, 2) 和点 (6,10) 之间的斜率和欧氏距离
- 比较任务 8 和 9 中的斜率。
- 计算 y 的值 (y = x^2 + 6x + 9)。尝试使用不同的 x 值并找出在什么 x 值时 y 将为 0。
- 找出“python”和“dragon”的长度,然后做一个虚假的比较语句。
- 使用and运算符检查是否在“python”和“dragon”中都找到“on”
- 我希望这门课程不会充满行话。使用in运算符检查句子中是否有行话。
- dragon 和 python 都没有'on'
- 查找文本python的长度并将值转换为float并将其转换为字符串
- 偶数能被 2 整除,余数为零。你如何使用 python 检查数字是否为偶数?
- 检查 7 除以 3 的底数是否等于 2.7 的 int 转换值。
- 检查“10”的类型是否等于 10 的类型
- 检查 int('9.8') 是否等于 10
- 编写一个脚本,提示用户输入小时数和每小时费率。计算人的工资?
Enter hours: 40 Enter rate per hour: 28 Your weekly earning is 1120
- 编写一个脚本,提示用户输入年数。计算一个人可以活多少秒。假设一个人可以活百年
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
🎉恭喜!🎉