Python basic data types

Note content: basic data type

Date of note: 2017-10-20


Basic data type


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

Multiple variable assignment

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.


Standard data type

There are six standard data types in python, which can also be said to be data objects:


Number (number, integer type)

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 (string type)

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 access subcharacters in a string

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

Python basic data types
Python basic data types
02. Python data types
Python basic syntax and number types
Python basic syntax (1)
Python learning-variable types
Python data model
Python basic summary
Python data analysis
python data structure
Python basic operators
Python data format-CSV
Python basic syntax generator
Python basic drawing tutorial (1)
Python data analysis-data update
Python function basic learning
Python data analysis-apply function
Python data analysis-data selection
Basic syntax of Python
Basic knowledge of Python (1)
Detailed explanation of data types based on Python
Python data analysis-data establishment
Python basic syntax iteration
Python basic knowledge question bank
Python Data Science: Neural Networks
Python common data structure collation
Python3 crawler data cleaning analysis
Python parses simple XML data
Python basic syntax list production
Python Data Science: Logistic Regression
Python data structure and algorithm
Python basic drawing tutorial (two)
Python Data Science: Regularization Methods
Python Data Science: Related Analysis
Python Data Science: Linear Regression
Python Faker data forgery module
Python Data Science: Chi-Square Test
Python introductory notes [basic grammar (on)]
Python Data Science: Linear Regression Diagnosis
Detailed explanation of python sequence types
Python entry notes [basic grammar (below)]
Python file operation basic process analysis
Python crawler basic knowledge points finishing
Real zero basic Python development web
Python realizes online microblog data visualization
Is python suitable for data mining
Automatically generate data analysis report with Python
Python access to npy format data examples
Python basic actual combat-guess the age game
Comprehensive summary of Python built-in exception types
Can variable types be declared in python
Java or Python for big data analysis
Python uses pandas to process Excel data