Starting today , Xiaobai, I will lead you to learn Python Introduction to zero foundation . This column will explain + Practice mode , Let's get familiar with it Python The grammar of , application , And the basic logic of the code .
Variables are values stored in memory , When creating variables, we will open up a space in memory . Variables are used to store data , stay Python Variables in do not need to be declared .
Example :
a = 10 # Create variables a, And assignment 10
b = " I'm Xiaobai " # Create variables b, And assignment " I'm Xiaobai "
print(a, b) # Debug output
Output results :
10 I'm Xiaobai
notes : print
Function is used to output the value of a variable
Python The memory space in is divided into three parts : Code section , Static data area , And dynamic data area . Dynamic data area is divided into stack and heap . ( Understanding can )
Simply draw the logic behind the above code :
print()
, Extract the value corresponding to the variable in the heap , And output it on the console error 1, Call variables that are not created :
a = 1 # Definition a Variable
print(b) # Try to output undefined b Variable
Output :
Traceback (most recent call last):
File "C:/Users/Windows/Desktop/ lecture / The first lesson Common errors in variables .py", line 2, in <module>
print(b) # Try to output undefined b Variable
NameError: name 'b' is not defined
stay Python Medium variables do not need to be typed .
Python There are several data types in :
Example :
a = 1 # plastic (int) Variable
b = 1.23 # floating-point (float) Variable
c = True # Boolean type (bool)
print(a, b, c) # Debug output variable value
print(type(a), type(b), type(c)) # Debug output variable type
Output :
1 1.23 True
<class 'int'> <class 'float'> <class 'bool'>
notes : adopt type()
function , python Will return the type of variable .
Example :
d = " I'm Xiaobai " # character string (string)
e = [1, 2, 3] # list (list)
f = (1, 2, 3) # Tuples (tuple)
g = {" Course content ": "Python Basics 2022 newest "} # Dictionaries (dict)
print(d, e, f, g) # Debug output variable value
print(type(d), type(e), type(f), type(g)) # Debug output variable type
Output results :
I'm Xiaobai [1, 2, 3] (1, 2, 3) {' Course content ': 'Python Basics 2022 newest '}
<class 'str'> <class 'list'> <class 'tuple'> <class 'dict'>
notes : adopt type()
function , python Will return the type of variable .
Example :
a = 1.23 # Create a floating point (float)
b = int(a) # Convert floating point type to integer type (int)
print(a, b) # Debug output variable value
print(type(a), type(b)) # Debug output variable type
Output results :
1.23 1
<class 'float'> <class 'int'>
Example :
a = 2 # Create integer type (int)
b = bool(a) # Convert integer type to Boolean (bool)
print(a, b) # Debug output variable value
print(type(a), type(b)) # Debug output variable type
Output results :
True
<class 'int'> <class 'bool'>
notes : Dangfei 0 When the number of is converted to Boolean, it is True, Instead of False.
Example :
a = " I'm Xiaobai " # Create string
b = int(a) # Strong conversion to integer
print(a, b) # Debug output variable value
print(type(a), type(b)) # Debug output variable type
Output results :
Traceback (most recent call last):
File "C:/Users/Windows/Desktop/ lecture / The first lesson Data type conversion .py", line 21, in <module>
b = int(a)
ValueError: invalid literal for int() with base 10: ' I'm Xiaobai '
notes : Some types cannot be cast .