Python Tutorial - 2 - Numbers

本文介绍了Python中的数值类型,包括整数和浮点数,以及它们的算术运算如加减乘除、取模和指数运算。文章详细解释了运算符的用法,如检查奇偶性、运算符函数和运算顺序。还讨论了变量的递增、内置函数如`abs()`和`round()`,以及数字和字符串之间的转换。最后提到了比较运算和类型转换的重要性。
摘要由CSDN通过智能技术生成

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

printtype(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=bc+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 模的运算 根本概念

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:

Python 教程之运算符(9)—— Python 中的运算符函数

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值