Basic data types and strings
1, Variable
1. Three elements of variables: variable name, variable value and variable data type
2. Format: variable name = variable value
3. Output variable: Print (variable name)
""" variable """ # 1,Three elements of variables: variable name, variable value and data type of variable value # 2,Define the format of the variable: variable name = Variable value money = 1080 # Variable name ---> money Variable value ---> 1080 Data type of variable ---> int # 3,Output variables: print(Variable name) print(money, type(money)) # type() View the data type of the variable
4. Naming rules for variables
-
Variable names are composed of letters, numbers and underscores (numbers cannot start)
-
Case sensitive (A and a are two different variables)
-
Do not conflict with keywords and system reserved words
"""Naming rules for variable names""" # Variable names are composed of letters, numbers and underscores (numbers cannot start) a = 6 print(a, type(a)) a2 = 'Hello World' print(a2, type(a2)) student_name = 'Zhang Shengsheng' print(student_name, type(student_name)) # Case sensitive( A,a Are two different variables) A = 3 a = 6 print(A) print(a) # Do not conflict with keywords and system reserved words(in,on,class)
2, Data type and data type conversion
1. Data type
-
Integer type (int): indicates integer -- > 1, - 2235
-
Floating point type: represents decimal -- > 1.5, 3.14, 88.5;
-
String type (str): 'hello'
-
Boolean: indicates true / false judgment -- > true / false
-
Null value type (None): indicates that there is no -- > blank and no data
------->Get the value type in the data: type() function
"""data type""" # Integer type( int): Represents an integer age = 12 print(age, type(age)) # Floating point type( float): Represents a decimal high = 178.9 print(high, type(high)) # String type( str): 'hello' name = "Li Zijie" print(name, type(name)) # Boolean type( bool): Indicates true or false judgment --->True/False a = True b = False print(a, type(a)) print(b, type(b)) # Null type( None): It means nothing ---> Blank, no data address = None print(address, print(address))
2. Data type conversion
-
Function int(): int (str()), int(float()) str are integers and will not be rounded
-
Function float(): float (str()), float(int())
-
Functions str(): str (int()), str(float())
-
Function bool():bool(""), bool(0), bool(None) False, other True
"""Data type conversion""" # 1,function int() : int(str()),int(float()) str Itself is an integer and will not be rounded(round()function) a = "65" # String to integer type, which must be in integer form print(type(a)) charge_a = int(str(a)) print(type(charge_a)) # String to floating-point type must be in decimal form b = "3.58" print(type(b)) charge_b = float(str(b)) print(type(charge_b)) # 2, function float() : float(str()) , float(int()) c = "3.58" print(type(c)) charge_c = float(str(c)) print(type(charge_c)) c1 = 88 print(c1, type(c1)) charge_c1 = float(int(c1)) print(c1, type(charge_c1)) # function str() : str(int()),str(float()) d = 22 print(type(d)) charge_d = str(int(d)) print(type(charge_d)) d1 = 3.15 print(type(d1)) charge_d1 = str(float(d1)) print(type(charge_d1)) # function bool():bool(" "),bool(0),bool(None) False rest True f = 6 e = 9 print(bool(f)) print(bool(e)) print(bool("")) print(bool(0)) print(bool(None))
3, Operator
1. Arithmetic operator
-
Addition, subtraction, multiplication and division (+, -, *, /)
-
Power (* *)
-
Only the division of the integer part of the result (/ /) is retained
-
Surplus (%)
"""Arithmetic operator """ # Addition, subtraction, multiplication and division(+,-,*,/) a = 6 b = 7 print(a + b) # 13 print(a - b) # -1 print(a * b) # 42 print(round(a / b, 2)) # round()Keep the specified decimal places and will be rounded # Power(**) c = 9 print(c ** 2) print(c ** 3) # Division that preserves only the integer portion of the result(//) d = 6 f = 5 print(d // f) # Surplus(%) a1 = 11 a2 = 5 print(a1 % a2)
2. Assignment operator
operator | describe |
---|---|
= | Simple assignment operator |
+= | Additive assignment operator |
-= | Subtraction assignment operator |
*= | Multiplication assignment operator |
/= | Division assignment operator |
%= | Modulo assignment operator |
**= | Power assignment operator |
//= | Rounding assignment operator |
"""Assignment Operators """ # = Simple assignment operator a = 5 # The right is assigned to the left print(a) # += Additive assignment operator a1 = 3 a1 = a1 + 1 a1 += 2 print(a1) # -= Subtraction assignment operator b = 10 b = b - 1 b -= 1 print(b) # *= Multiplication assignment operator c = 3 c = c * 3 c *= 2 print(c) # /= Division assignment operator d = 6 d = d / 3 d /= 3 print(round(d, 2)) # %= Modulo assignment operator e = 1 e = e % 2 e %= 1 print(e) # **= Power assignment operator f = 3 f = f ** 3 f **= 4 print(f) # //=Rounding assignment operator g = 10 g = g // 6 g //= 6 print(g)
3. Comparison operator
operator | describe |
---|---|
== | be equal to |
!= | Not equal to |
> | greater than |
< | less than |
>= | Greater than or equal to |
<= | Less than or equal to |
"""Comparison operation""" # The result is bool Boolean value # ==be equal to # !=Not equal to # > greater than # < less than # >=Greater than or equal to # <=Less than or equal to a = 6 b = 8 print(a == b) # F print(a > b) # F print(a < b) # T print(a >= b) # F print(a <= b) # T print(a != b) # T
4. Logical operator
operator | Logical expression | describe | |
---|---|---|---|
and | x and y | x. If y is true, it is true. If one is false, it is false | |
or | x or y | x. If one of y is true, it is true, and both are false and false | |
not | not x | If x is false, not x is true |
"""Logical operator""" a = 9 b = 10 # and x and y x,y All true is true, and one false is false print(a < b and b > a) # or x or y x,y If one is true, it is true, and both are false and false print(a + b > b and a + b < a) # not not x If x False, then not x For true print(not a > b)
5. Member operator
operator | describe |
---|---|
in | Returns True if a value is found in the specified sequence, otherwise False. |
not in | Returns True if no value is found in the specified sequence, otherwise False. |
"""member operator """ list_a = [1, 5, 8, 9, 45] # in Returns if a value is found in the specified sequence True,Otherwise return False. print(1 in list_a) print("a" in list_a) # not in Returns if no value is found in the specified sequence True,Otherwise return False. print("b" not in list_a) print(45 not in list_a)
6. Identity operator (id() function)
operator | describe |
---|---|
is | is determines whether two identifiers refer to an object |
is not | is not determines whether two identifiers are referenced from different objects |
# Variable data structure( list),Even if it looks the same, the memory address is different # Immutable data type(Integer, string),If they look the same, their memory addresses are the same """Identity operator""" # id()function a = 6 b = 4 c = 6 print(id(a)) print(id(b)) print(id(c)) list_a = [123456, "321"] list_b = [2, 8, 9] print(id(list_a)) print(id(list_b)) # is Is to determine whether two identifiers refer to an object print(a is c) print(a is b) print(list_a is list_b) # is not Determines whether two identifiers are referenced from different objects print(a is not b) print(list_a is not list_b) print(a is not c)
4, String and escape character
1. String format
-
Single line:
str_a = "this is a str"
-
Multiline
str_b = """ This is a string Who am I? """
# Format: # Single line name = "Li Zijie" print(name, type(name)) name2 = "Zhang" \ "l" print(name2) # Multiline str_a = """I'm fine today, don't you think""" str_b = """Everybody I have a present""" print(str_a, type(str_a)) print(str_b)
2. Escape character
Escape character | describe | effect |
---|---|---|
\n | Line feed | Print wrap |
\ | Escape character | Print characters with special meaning |
\t | Tab | Empty 4 characters |
"""Escape character""" # \n Line feed print("hello everyone!\n The show begins") # \ Escape character print("hello everyone!\\n The show begins") # \t Tab 4 characters empty print("hello everyone!\t The show begins")
3. String format match
Symbol | describe |
---|---|
%c | Formatted characters and their ASCII codes |
%s | format string |
%d | Format integer |
%u | Format unsigned integer |
%o | Format unsigned octal number |
%x | Format unsigned hexadecimal number |
%X | Format unsigned hexadecimal number (upper case) |
%f | Format floating-point numbers to specify the precision after the decimal point |
%e | Formatting floating point numbers with scientific counting |
%p | Format the address of a variable with a hexadecimal number |
"""String format match""" print("Hello, my name is%s" % "Li Zijie" + "\n I this year%d" % 21, "\n My rise%s" % "179.9")
4. String splicing (literal interpolation of string)
- "str".format()
Set the specified location in the default order | "{}{}".format("h","e") |
---|---|
Set specified location | "{0}{1}".format("h","e") |
Pass variables by name | My name is {name}. I'm {age} years old format(name = "Zhang Hu", age = 8) |
"""String splicing""" # Set the specified location in the default order "{}{}".format("h","e") print("This is{}{}".format("h", "e")) # Set specified location"{0}{1}".format("h","e") Indexes print("This is{1}{0}".format("h", "e")) # Pass variables by name "My name is{name},this year{age}year".format(name = "Zhang Hu",age = 8) print("My name is{name},this year{age}year".format(name="Zhang Hu", age=8))
- f"xxxx{}
name = "Li Zijie" age = 20 print(f"My name is{name},this year{age}")
- +: you need to ensure that each variable value must be of string type
print("Hello, my name is%s" % "Li Zijie", "!I this year%d" % 21 + "My rise%s" % "179.9")
5, String common API
1. Conversion string of jion list
"""jion List conversion string""" list_a = ['a', 'p', 'p', 'l', 'e'] print("".join(list_a))
2. split data segmentation operation
"""split Data segmentation operation""" a = "apple red" print(a.split(" ")) b = "hello world" print(b.split(" "))
3. replace replaces the target string with the desired character
""" replace Replace the target string with the desired character""" a = "My name is lizijie" print(a) print(a.replace("lizijie", "lizihao"))
4. strip removes leading and trailing spaces
""" replace Replace the target string with the desired character""" a = " My name is lizijie " print(a) print(a.strip())