data type
Number
Integer
An integer is a type that represents an integer. The type name is int.
Variables are abstract concepts that store data. Assign values to variables with the equal sign =, Python allows multiple variables to be assigned at the same time.
The name of a variable is called the variable name. The variable name cannot be a Python reserved word (print, for...).
The value of the variable can be modified when the program is running.
>>> a = 10 >>> b, c = 1, 5 # Assign values to both variables at the same time >>> d = b + c >>> print(a, b, c, d) 10 1 5 6 >>> a = int(1.6) # Modify the value of a, int(1.6) to convert 1.6 to integer (round down) >>> print(a) 1 >>> type(a) <class 'int'>
When you assign an integer to a variable, the type of the variable is int.
Reasonable use of spaces can increase the readability of the program. print(a,b,c,d) and d=b+c are easier for humans to read than print(a,b,c,d) and d=b+c. When programming, we should pay attention to the readability of the program. This is beneficial to the understanding and maintenance of the code in the future.
Floating point
A float is a type that represents a decimal number. The type name is float. When you assign a floating-point number to a variable, you create a floating-point object.
>>> GPA = 3.2 >>> tmp = 28.6 >>> print(GPA, tmp) 3.2 28.6 >>> type(GPA) <class 'float'>
Boolean
Bool is a type that represents True and False. The type name is bool. There are only two values: True and False.
>>> is_prime = True >>> is_even = False >>> print(is_prime, is_even) True False >>> type(is_prime) <class 'bool'>
Floating point type (Complex)
Python also supports complex numbers. Complex numbers are composed of real and imaginary parts, which can be represented by a + bj or complex(a, b). Both real part a and imaginary part B of complex numbers are floating-point.
String (String)
A string is a data type representing text. The type name is str. In Python, strings are enclosed by a pair of single quotes' ', double quotes'' or three quotes' '' '. Strings can be spliced with a plus sign.
>>> name = 'Monkeyhbd' >>> hello = "Hello, I'm" # If there are single quotation marks in the text, use double quotation marks >>> print(hello + ' ' + name) # Print the splicing of three strings Hello, I'm Monkeyhbd
Backslash \ special characters can be escaped. If you don't want the backslash to escape, you can add an r in front of the string to represent the natural string
>>> print('Mo\nkeyhbd') Mo keyhbd >>> print(r'Mo\nkeyhbd') Mo\nkeyhbd
In the above example, \ n represents line feed.
The string can be accessed or sliced by subscript index, and the index (subscript) of the first character of the string is 0. The following figure illustrates how to use string indexing
-9 -8 -7 -6 -5 -4 -3 -2 -1 (0) +---+---+---+---+---+---+---+---+---+ | M | o | n | k | e | y | h | b | d | +---+---+---+---+---+---+---+---+---+ 0 1 2 3 4 5 6 7 8 9
name = 'Monkeyhbd' print(name[0]) # Index from front to back print(name[3]) print(name[-1]) # Index from back to front print(name[3: 6]) # String slicing print(name[: -3]) # Slice from start to penultimate character print(name[: 2] + name[3: 6]) # String splicing
Operation results
M k d key Monkey Mokey
List
A list is a sequence containing various objects. The type is called list. In Python, the list is enclosed by a pair of brackets []. The objects in the list are called elements, and the elements are separated by commas. Objects in the list can be of different types. Lists can also contain (nested) lists.
Like a string, the index of the first element of the list is 0 and the index of the penultimate element is - 1. The list can also be sliced and spliced.
box = [100, 'Red', 'Blue', 5] print(box) print(box[0]) # visit print(box[-2]) print(box[1: -1]) # section print(box + box) # Splicing
Operation results
[100, 'Red', 'Blue', 5] 100 Blue ['Red', 'Blue'] [100, 'Red', 'Blue', 5, 100, 'Red', 'Blue', 5]
The list can be updated. The value of the element can be accessed or modified through the index, or the element can be added to the end of the list with the append method. The pop method deletes the last element of the list (and returns the value of the deleted element).
students = ['monkeyhbd', 'Liclout', 'Dijkstra'] print(students) students[0] = 'Monkeyhbd' # Modify the value of the element print(students) students.append('Linus') # Append element print(students) students.pop() # Delete element print(students)
Operation results
['monkeyhbd', 'Liclout', 'Dijkstra'] ['Monkeyhbd', 'Liclout', 'Dijkstra'] ['Monkeyhbd', 'Liclout', 'Dijkstra', 'Linus'] ['Monkeyhbd', 'Liclout', 'Dijkstra']
operator
Arithmetic operator
operator | describe |
---|---|
+ | The addition of two numbers, or the connection between a string and a list |
- | Subtract two numbers |
* | Multiplication of two numbers, or repetition of string and list |
/ | Divide two numbers |
% | Remainder (modulo) returns the remainder of the division |
** | exponentiation |
// | Rounding, returns the quotient of the division, rounding down |
print(1 + 2) print('Py' + 'thon') # String splicing print([1, 2] + [3, 4, 5]) # Splice list print(3 - 5) print(2 * 4) print('Emm' * 3) # Duplicate string print(['Tom', 18] * 3) # Duplicate list print(1 / 3) print(3 ** 2) # 3 ^ 2 print(2 ** 10) # 2 ^ 10 print(5 % 3) print(5 // 3)
Operation results
3 Python [1, 2, 3, 4, 5] -2 8 EmmEmmEmm ['Tom', 18, 'Tom', 18, 'Tom', 18] 0.3333333333333333 9 1024 2 1
Assignment Operators
operator | describe |
---|---|
= | Assign the value (or operation result) to the right of the equal sign to the variable to the left of the equal sign |
+= | a += 2 is equivalent to a = a + 2 |
-= | a -= 2 is equivalent to a = a - 2 |
*= | a *= 2 is equivalent to a = a * 2 |
/= | a /= 2 is equivalent to a = a / 2 |
%= | A% = 2 is equivalent to a = a% 2 |
**= | a **= 2 is equivalent to a = a ** 2 |
//= | a //= 2 is equivalent to a = a // 2 |
a = 1 print(a) a += 2 print(a) a -= 1 print(a) a *= 2 print(a) a /= 2 print(a) word = 'Ha' word *= 3 print(word) urls = ['www.baidu.com', 'www.bing.com'] urls += ['gitee.com/monkeyhbd', 'github.com/monkeyhbd'] print(urls)
Operation results
1 3 2 4 2.0 HaHaHa ['www.baidu.com', 'www.bing.com', 'gitee.com/monkeyhbd', 'github.com/monkeyhbd']
Comparison operator
The comparison operation always returns a Boolean value of True or False
operator | describe |
---|---|
== | Are the left and right sides equal |
!= | Are the left and right sides unequal |
> | Is the left greater than the right |
>= | Is the left greater than or equal to the right |
< | Is the left smaller than the right |
<= | Is the left less than or equal to the right |
print(1 == 2) print('string' == 'string') print(1 != 2) print(1 > 2) print(1 >= 1) print(1.98 < 2) print(1 <= 2)
Operation results
False True True False True True True
Since floating-point numbers are approximately stored in the computer, do not use = = to compare two floating-point numbers. The correct way is to compare whether the absolute value of their difference is less than a given precision.
Logical operator
operator | describe |
---|---|
and | And operator, the result is true when both sides are true, otherwise it is false |
or | Or operator. If one side is true, the result is true, otherwise it is false |
not | Non operator, not True is False, not False is True |
tmp = 17 if tmp > 10 and tmp < 25: # It can be abbreviated as 10 < TMP < 25 print('The temperature is appropriate')
Operation results
The temperature is appropriate