Python study notes - explore 5 data types

As an easy-to-learn and powerful programming language, Python has attracted many programming enthusiasts. In the process of learning Python, it is very important to understand its data types. Here I will introduce you to five common data types in Python, namely: integers, floating point numbers, strings, lists, and dictionaries, and will show how to use these data types through vivid examples.
insert image description here

integer (int)

In Python 3, the length of an integer value is dynamic and can change as needed. This means you can use very large integer values ​​in your programs without worrying about fixed length limitations. Integer types in Python 3 are infinite precision and can represent arbitrarily large integers. This is different from other programming languages, which usually have a fixed integer length limit (such as 32 or 64 bits).

In Python 3, the length of an integer is limited only by system memory. Integers of any length can be used as long as your system has enough memory to store the integer value.

print(123 + 1)
124

In Python, if a number does not have any prefix, it will be interpreted as a decimal number. This means you can use numbers without a prefix directly in your code and they will be treated as decimal values.

x = 10
print(x)  # 输出:10

y = 3.14
print(y)  # 输出:3.14

z = 100_000
print(z)  # 输出:100000

Integer base

The following strings can be used to identify numbers in other bases:

  • Binary 0b: 0Bstart with or , for example "0b1010" means 10 in decimal.
  • Octal 0o: 0Ostart with or , for example "0o12" means 10 in decimal.
  • Hexadecimal 0x: 0Xstart with or , for example "0xA" means 10 in decimal.

These prefix strings can be prepended to integer values ​​to indicate different bases. Regardless of the number of integers represented in Python, its data type is int.

type(10)
<class 'int'>

type(0o10)
<class 'int'>

type(0x10)
<class 'int'>

type(0b10)
<class 'int'>

float float type

Floating point numbers in Python can be specified using a decimal point or expressed using scientific notation. Scientific notation is represented by multiplying the base (usually 10) by the exponent. In Python, you can use the character 'e' or 'E' to represent scientific notation.

num1 = 3.14  # 使用小数点表示浮点数
num2 = 2e3  # 2乘以10的3次方,即2000.0
num3 = -1.5E-2  # -1.5乘以10的-2次方,即-0.015

print(num1)  # 输出: 3.14
print(num2)  # 输出: 2000.0
print(num3)  # 输出: -0.015

special floating point numbers

Almost all platforms represent floating-point numbers in Python as 64-bit double-precision values ​​according to the IEEE 754 standard.

The maximum value of a floating point number is about 1.8 × 1 0 308 1.8 \times 10^{308}1.8×10308

print(1.79e308)
1.79e+308

# 超出范围指向无穷大
print(1.8e308)
inf

The minimum value of a floating point number is about 5.0 × 1 0 − 324 5.0 \times 10^{-324}5.0×10324

print(5e-324)
5e-324

print(1e-325)
0.0

Floating point numbers are represented internally as binary (base-2) fractions, and most decimal fractions cannot be represented as binary fractions with complete accuracy, so the internal representation of floating point numbers is an approximation of the actual value. Since the difference between the actual value and the represented value is very small, it usually does not cause a BUG.

plural

Complex numbers (complex) represent complex numbers, that is, numbers that have real and imaginary parts. The representation method of complex numbers is: a + bj, where ais the real part and bis the imaginary part, jrepresenting the imaginary unit. Complex numbers are widely used in scientific computing, signal processing and other fields.

g = 3 + 4j
h = -2 - 3J

Computes the sum of two complex numbers.

x = 3 + 4j
y = 1 - 2j

z = x + y

print("复数和:", z)

character type

Python uses strings ( str ) to represent sequences of character data. Strings can be delimited using single or double quotes.

print("name 張郃 is a string.")
name 張郃 is a string.

type("name 張郃 is a string.")
<class 'str'>

print('name 曹操 is too.')
name 曹操 is too.

print(type('name 曹操 is too.'))
<class 'str'>

The string can also be empty.

print('')

When a string contains the same quote character as the delimiter, it's easiest to use another type of quote for the delimiter. If the string needs to contain single quotes, delimit it with double quotes and vice versa.

# 错误的方法
print('私は趙雲('ちょううん)です')
SyntaxError: invalid syntax

print('私は趙雲("ちょううん")です')
'私は趙雲("ちょううん")です'

print("私は趙雲('ちょううん')です")
"私は趙雲('ちょううん')です"

escape sequences in strings

Escape sequences can be used in strings to represent special characters or sequences of characters. Suppresses the interpretation of certain characters that normally have special meaning in strings.

escape character explain visualize the results
Start delimiter terminated string with single quote Literal single quote ( ') character
" Start delimiter terminated string with double quotes Literal double quote ( " ) character
<newline> terminate input line Newlines are ignored
\ Introducing escape sequences Literal backslash ( \ ) character
# 错误的方法
print('私は趙雲('ちょううん)です')
SyntaxError: invalid syntax

# 正确的方法
print('私は趙雲(\'ちょううん)です')
私は趙雲('ちょううん)です

print('私は趙雲(\"ちょううん)です')
私は趙雲("ちょううん)です

print('私は趙雲(\\ちょううん)です')
私は趙雲(\ちょううん)です

# 换行符终止行输入的错误
print('私は
SyntaxError: EOL while scanning string literal


# 换行符正确的用法
print('私は趙雲(\
ちょううん)です')
私は趙雲(ちょううん)です

Apply special meaning to characters

In Python, you can use the escape sequence \t to represent a tab character.

escape character explain
\a ASCII bell (BEL) character
\b ASCII backspace (BS) character
\f ASCII form feed (FF) character
\n ASCII line feed (LF) character
\N{} the given character in the Unicode database
\r ASCII carriage return (CR) character
\t ASCII horizontal tab (TAB) character
\uxxxx Unicode character xxxx with 16-digit hexadecimal value
\Uxxxxxxxx Unicode characters xxxxxxxx with 32-bit hexadecimal value
\v ASCII vertical tab (VT) character
\ooo octal character ooo
\xhh character hh with hexadecimal value

Here is sample code to create a string containing tabs:

print('私は趙雲\tです')
私は趙雲	です

raw string

When processing strings, precede the string with r or R if you want to preserve escape sequences and not interpret backslash characters. This ensures that escape sequences in raw string literals are preserved.

print('趙雲\n字:子龍')
趙雲
字:子龍

print(r'趙雲\n字:子龍')
趙雲\n字:子龍

print('趙雲\\字:子龍')
趙雲\字:子龍

print(R'趙雲\\字:子龍')
趙雲\\字:子龍

triple quote string

In Python, there is also a way to use triple quotes (single or double) to delimit strings. This method can contain single quotes, double quotes, and newlines without escaping them.

print('''趙雲 "字:'子龍''')
趙雲 "字:'子龍

print("""趙雲
字:
子龍""")
趙雲
字:
子龍

Boolean type

The Boolean type represents a logical value and has only two values: Trueand False. The Boolean type is usually used in scenarios such as conditional judgment and loop control. Here are some examples of Boolean types:

l = True
m = False

print(type(True))
<class 'bool'>

print(type(False))
<class 'bool'>

Determines whether a number is even.

num = 6
is_even = (num % 2 == 0)
print("是否为偶数:", is_even)

Guess you like

Origin blog.csdn.net/qq_20288327/article/details/130665217