文章目录
Numbers in Python
/*
- File: numbers.md
- Project: 2_numbers
- File Created: Tuesday, 7th March 2023 10:50:46 am
- Author: Hanlin Gu (hg_fine_codes@163.com)
- Last Modified: Saturday, 11th March 2023 4:16:53 pm
- Modified By: Hanlin Gu (hg_fine_codes@163.com>)
*/
1. Integers and Floats
The build in function type() gives one the type of the variable.
num = 3
print(type(num))
Output:
<class 'int'>
num = 3.0
print(type(num))
Output:
<class 'float'>
1.1. Arithmetic Operators
# Arithmetic Operators
# Addition: 3 + 2
# Subtraction: 3 - 2
# Multiplication: 3 * 2
# Division: 3 / 2
# Floor Division: 3 // 2
# Exponent: 3 ** 2
# Modulus: 3 % 2
print(3 + 2)
print(3 - 2)
print(3 * 2)
print(3 / 2)
print(3 // 2)
print(3 ** 2)
print(3 % 2)
Output:
5
1
6
1.5
1
9
1
Note that in Python 2, 3 / 2 gives 1 which is the floor of 3 / 2. But in Python 3, 3 / 2 gives the right answer 1.5.
floor of a number n means the largest integer that is less than n.
ceil of a number n means the smallest integer that is bigger than n.
Based on where the negative number is, the floor division gives the same result. However, the modulo operator % gives different answers.
print(-5 // 2)
print(5 // -2)
print(5 // 2)
print(-5 // -2)
Output:
-3
-3
2
2
modulo a % b is calculated by
a
=
b
∗
c
+
r
a = b*c + r
a=b∗c+r, where c is an integer and r is the modulus. The rule is that b and r have the same sign, which means that if b is negative then r is also negative. The sign of r has no relationship with a.
print(5 % 2)
print(-5 % 2)
print(5 % -2)
print(-5 % -2)
Output:
1
1
-1
-1
print(5 % 3)
print(-5 % 3)
print(5 % -3)
print(-5 % -3)
Output:
2
1
-1
-2
Reference:
python中remainder_从C++和Python除法的区别谈谈求模(Modulus)和取余(Remainder)
1.1.1. Check even or odd number by a % 2
If a % 2 equals to 0 then a is even. If a % 2 is 1 means a is odd.
print(2 % 2)
print(3 % 2)
print(4 % 2)
print(5 % 2)
print(-5 % 2)
Output:
0
1
0
1
1
1.2. Methods in operator
Reference:
The arithmetic operators can also be written as
import operator as op
print(op.add(3, 2)) # summation 3 + 2
print(op.sub(3, 2)) # subtraction 3 - 2
print(op.mul(3, 2)) # multiplication 3 * 2
print(op.truediv(3, 2)) # true division 3 / 2
print(op.floordiv(3, 2)) # floor division 3 // 2
print(op.pow(3, 2)) # exponent 3 ** 2
print(op.mod(3, 2)) # modulo operator: modulus of 3 / 2
Output:
5
1
6
1.5
1
9
1
The comparison methods are
import operator as op
print(op.lt(3, 2)) # check if 3 is less than 2
print(op.gt(3, 2)) # check if 3 is greater than 2
print(op.le(3, 2)) # check if 3 is less or equal to 2
print(op.ge(3, 2)) # check if 3 is greater or equal to 2
print(op.eq(3, 2)) # check if 3 is equal to 2
print(op.ne(3, 2)) # check if 3 is not equal to 2
Output:
False
True
False
True
False
True
1.3. Order of operations
The order of operation is the same as arithmetic operations. And brackets have the priority of calculation.
1.4. Increment a Variable
num = 1
num = num + 1
print(num)
Output:
2
num = 1
num += 1
print(num)
Output:
2
And one can use this syntax to other operators as well.
num = 1
num *=5
print(num)
Output:
5
1.5. Build-in functions to work with numbers
- Absolute value:
abs()
print(abs(-2))
Output:
2
- Round the value to the nearest integer:
round()
print(round(2.49))
print(round(2.50))
print(round(2.51))
Output:
2
2
3
print(round(-2.49))
print(round(-2.50))
print(round(-2.51))
Output:
-2
-2
-3
round() function can take a second argument to show how many digits one would like to round to.
Round to the first digit after the decimal.
print(round(2.50, 1))
print(round(2.75, 1))
Output:
2.5
2.8
However, round(num, 0) gives the round value of num and returns as float.
print(round(2.50, 0))
print(round(2.51, 0))
Output:
2.0
3.0
- Comparison operators
Comparison will return boolean value which is true or false.
# Comparison
# Equal: 3 == 2
# Not Equal: 3 != 2
# Greater Than: 3 > 2
# Less Than: 3 < 2
# Greater or Equal: 3 >= 2
# Less or Equal: 3 <= 2
num_1 = 3
num_2 = 2
print(num_1 == num_2)
print(num_1 != num_2)
print(num_1 > num_2)
print(num_1 < num_2)
print(num_1 >= num_2)
print(num_1 <= num_2)
Output:
False
True
True
False
True
False
Notice that single equal = is assignment, the double equal == means compare whether the two values are equal or not. The exclamation mark equal != means not equal.
2. Numbers vs. String
Some of the variables may look like numbers but indeed are strings.
num_1 = '100'
num_2 = '200'
print(num_1 + num_2)
Output:
100200
This is essentially two strings concatenated together by +, rather than the plus operation for two numbers.
To convert the string to numbers, one can use casting.
Reference
Type Casting in Python (Implicit and Explicit) with Examples
num_1 = '100'
num_2 = '200'
num_1 = int(num_1)
num_2 = int(num_2)
print(num_1 + num_2)
Output:
300
本文介绍了Python中的数值类型,包括整数和浮点数,以及它们的算术运算如加减乘除、取模和指数运算。文章详细解释了运算符的用法,如检查奇偶性、运算符函数和运算顺序。还讨论了变量的递增、内置函数如`abs()`和`round()`,以及数字和字符串之间的转换。最后提到了比较运算和类型转换的重要性。
5375

被折叠的 条评论
为什么被折叠?



