Variables in python do not need to declare data types, but each variable must be assigned a value before it is used, and the variable will only be created after the assignment. So in python, a variable means that a variable has no type. The type we are talking about is the type in the memory address pointed to by the variable.
Like most other programming languages, the equal sign = is used to assign a variable in Python. The left side is the variable name, and the right side is the value or data that needs to be stored in this variable.
**Code example: **
num=10 #Integer type print(num)
operation result:
10
Python can allow multiple variables to be assigned at the same time, but it is not recommended to use this way of writing, because it looks messy, example:
a = b = c = 10
In the above example, the value 10 is assigned to the three variables a, b, and c. The process is like this: 10 is first assigned to c, then the value of c is assigned to b, and the value of b is assigned a.
In addition, you can also assign different types of values to multiple variables at the same time, for example:
a,b,c=10,12.5,"string"
In the above example, the value 10 is assigned to a, 12.5 is assigned to b, and the string "string" is assigned to c.
There are six standard data types in python, which can also be said to be data objects:
Python3 supports int, float, bool, complex (plural).
In Python 3, there is only one integer type int, which is represented as a long integer, and there is no Long in python2.
The data type is not allowed to change, which means that if the value of the numeric data type is changed, the memory space will be reallocated.
When you assign an integer to a variable, the Number object is created.
Like most languages, assignment and calculation of numeric types are very intuitive.
The built-in type() function can be used to query the type of object pointed to by the variable, similar to typeof in JavaScript.
**Code example: **
num=10 #Integer type
print(type(num))
operation result:
< class 'int'>
**Or use isinstance to judge, example: **
a=10print(isinstance(a,int))
Operation result: True
The difference between isinstance and type:
**Note: **There is no boolean type in Python2. It uses the number 0 to represent False and 1 to represent True. In Python3, True and False are defined as keywords, but their values are still 1 and 0, and they can be added to numbers.
Python supports four different numeric types:
num=10 #Integer type
fudian=12.5 #Floating point type
boolean=True #Boolean type
test=4+3j #Plural type
print(type(num))print(type(fudian))print(type(fudian))print(type(test))
operation result:
< class 'int'>
<class 'float'>
<class 'float'>
<class 'complex'>
You can use hexadecimal and octal to represent integers, examples:
number = 0xA0F # hexadecimal
print( number)
Printing result: 2575
number=0o37 # octal
print( number)
Printing result: 31
String is the most commonly used data type in Python. We can use single quotes or double quotes to create strings.
Python does not support a single character type, that is, the char type in C or Java. Even if a single character is assigned, it is used as a string in Python.
Creating a string is very simple, just assign a value enclosed in quotation marks to the variable. E.g:
var1="Hello"
var2="World"print(type(var1))print(type(var2))
operation result:
< class 'str'>
<class 'str'>
Python can access a single substring to use string subscripts to get the value, and to access multiple substrings, you can use square brackets to intercept the string. Code example:
var1="Hello World"print(var1[0]) #Take the string whose string index is 0
print(var1[0:5]) #Take string subscript 0-5 substring
operation result:
H
Hello
**String subscript: **
String subscripts start from 0
var1=”Hello World”
|||||||||
012345678
**String splicing: **
The string and the string can be spliced, and the + sign can be used directly, the code example:
var1 ="Hello"
var2 ="World"print(var1 + var2)
operation result:
HelloWorld
Python escape characters
When you need to use special characters in a character, Python uses a backslash () to escape the character.
del:
The del keyword can delete the reference of an object, and use del to delete single or multiple variables, for example:
del num #Delete a single
del num_1,num_2 #Delete multiple
After deleting, this variable can no longer be used. If it is used, an error will be reported. Examples of errors:
a=10
del a
print(a)
Run it will report the following error:
Traceback (most recent call last):
File “E:/PythonProject/Number.py”, line 5, in
print(a)
NameError: name ‘a’ is not defined
Recommended Posts