Python-based data types and variables

Source: Internet
Author: User

A. Variable
1 What is a variable declaration variable
#变量名 = variable Value
Age=18
gender1= ' Male '
gender2= ' Female '
2 Why should I have a variable

Variable action: "Change" = Change, "volume" and "metering/saving State"
The operation nature of the program is a series of state changes, the purpose of the variable is to save the state, the change of the value of the variable is the result of the program running different.

3 types and objects of variable values
The program needs to deal with a lot of states, so there are different types of variable values, x= ' seven ', variable value ' seven ' storage and memory, binding a name X, the value of the variable is the data we want to store.

All data in Python is built around the concept of objects that contain some basic data types: numbers, strings, lists, tuples, dictionaries, etc.
All the data stored in the program is an object,
Object Three Properties
? Identity: Memory address, can be obtained with ID ()
? Type: Determines what type of value the object can hold, what action to perform, what rules to follow, and what type () to use to get
? value: Real data saved by an object
Note: We are defining the data type, just like this: X=1, internally generated 1 This memory object is automatically triggered and we don't have to care

1 The type of the object is also referred to as the category of the object, and Python customizes the type-specific method for each type, greatly facilitating the developer's handling of the data
2 Creating an object of a particular type is also known as creating an instance of that type, and the concept of a factory function is derived from this
4 mutable objects and immutable objects
Once the instance is created, the identity and type are immutable,
If the value is not modifiable, the object is immutable
If the value can be modified, it is a mutable object

5 Container objects
An object that contains a reference to another object, called a container or collection, can be used as a container in Python: list, tuple, dictionary

6 properties and methods for objects
A property is the value of an object, which is a function that will perform certain operations on the object itself when called, using the. operator to access the properties and methods of the object, such as
A=3+4j
A.real

b=[1,2,3]
B.append (4)

7 Identity comparison, type comparison, value comparison
X=1
Y=1
X is y #x与y是同一个对象 and is a comparison of ID, which is the identity
Type (x) is type (y) #对象的类型本身也是一个对象, so you can compare the identities of two object types with IS
x = = y #== compares the values of two objects for equality


8 Assigning operations to variables
? The difference from the C language is that the variable assignment operation has no return value
? Chained assignment: Y=x=a=1
? Multivariate assignment: x,y=1,2 x,y=y,x
? Increment Assignment: X+=1

Two. Data type
2.2 Basic data types

2.2.1 Number:
Characteristics:
1. Only one value can be stored
2. Once defined, cannot be changed
3. Direct Access
Categories: Integer, Long, Boolean, floating point, plural

int: grade, age, grade, ID number, QQ number, mobile number
level=10
Python's integer is equivalent to Long in C, and integers in Python can be expressed in decimal, octal, and hexadecimal notation.
>>> 10
Ten---------> Default decimal
>>> Oct (10)
' 012 '---------> Octal denotes integers with a prefix "0" before the value
>>> Hex (10)
' 0xa '---------> Hexadecimal denotes integer, prefix 0X or 0x before the number
The difference between python2.* and python3.* about integral type
python2.*
On a 32-bit machine, the number of integers is 32 bits and the value range is -2**31~2**31-1, which is -2147483648~2147483647

On a 64-bit system, the number of integers is 64 bits and the value range is -2**63~2**63-1, which is -9223372036854775808~9223372036854775807
python3.* Plastic Length Unlimited


Float type: Height, weight, salary, temperature, price
height=1.81
salary=3.3
Python's floating-point number is a decimal number in mathematics, similar to a double in the C language.
In operations, the result of an integer and floating-point operation is a floating-point number
Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation,
The decimal position of a floating-point number is variable, for example, 1.23*109 and 12.3*108 are equal.
Floating-point numbers can be written in mathematical notation, such as 1.23,3.14,-9.01, and so on. But for very large or very small floating-point numbers,
We have to use scientific notation to replace 10 with E, 1.23*109 is 1.23e9, or 12.3e8,0.000012
Can be written in 1.2e-5, and so on.
Integers and floating-point numbers are stored internally in different ways, and integer operations are always accurate, and floating-point arithmetic may have
The rounding error.


2.2.2 String

String STR: It is a collection of ordered characters that is used to store and represent basic textual information, "or" or "" "that contains content called strings, contained in quotation marks (single, double, three), consisting of a string of characters

Characteristics:
1. Only one value can be stored
2. Non-volatile
3. Define the character set in left-to-right order, the subscript is accessed sequentially from 0, ordered
Add:
1. Both single and double quotation marks cannot suppress the meaning of special characters, if you want all the characters in quotation marks to nonspacing special meaning, precede the quotation marks with R, such as Name=r ' L\THF '
2.unicode string with R must precede r, e.g. Name=ur ' L\THF '

Use (descriptive data): Name, gender, address, education, password: alex3714
Name= ' Egon '

Value:
First, make it clear that the string as a whole is a value, except that it is special:
There are no character types in Python, strings consist of a string of characters that you want to take out of the string
can also be obtained in the form of subscript

Name: Gets the value that is the whole of the string
NAME[1]: Get the character that is the second place

string concatenation:
>>> msg1= ' Hello '
>>> msg2= ' World '
>>>
>>> MSG1 + MSG2
' Hello World '
>>> RES=MSG1 + MSG2
>>> Print (RES)
Hello World

>>> msg1*3
' Hellohellohello '

2.2.3 List
Definitions: [] separated by commas, by index, holding various data types, each position represents an element
Characteristics:
1. can store multiple values
2. Can modify the value of the specified index position, variable
3. Define list elements in left-to-right order, order access from 0, ordered
Use (save multiple values, can be modified): hobbies, equipment, girlfriends
hobby=[' play ', ' eat ', ' sleep ']
Method:
Hobby.append
Hobby.remove
Operation:
View:
>>> girls=[' Alex ', ' WSB ', [' Egon ', ' YSB ']
>>> Girls[2]
[' Egon ', ' YSB ']
>>> Girls[2][0]

Increase
Girls.append (Element)
Delete
Girls.remove (Element)
Del girls[index of elements]
Modify
girls[0]= ' ALEXSB '

Dictionary dict: defined in {}, comma-separated, each element in the form of a key:value
Definition: {key1:value1,key2:value2},key-value structure, key must be hash
Characteristics:
1. can store multiple values
2. Can modify the value of the specified key, variable
3. Unordered

Dictionary:
Purpose: Store multiple values, which is the same as the list, and the value can be any data type
Characteristics: Each value is a one-to-one correspondence, i.e. key, which emphasizes that key must be
Immutable types: Strings, numbers
student_info={
' Age ': 81,
' Name ': ' Alex ',
' Sex ': None,
' Hobbies ': [' zsb0 ', ' zsb1 ', ' zsb2 ', ' Zsb30 ']
}


Operation:
View
>>> student_info={
... ' Age ': 81,
... ' Name ': ' Alex ',
... ' Sex ': None,
... ' Hobbies ': [' zsb0 ', ' zsb1 ', ' zsb2 ', ' Zsb30 ']
... }
>>>
>>> student_info[' age ']
81
>>> student_info[' Hobbies ']
[' zsb0 ', ' zsb1 ', ' zsb2 ', ' Zsb30 ']
>>> student_info[' Hobbies '][2]
' ZSB2 '
Increase
student_info[' stu_id ']=123456

Delete
Del student_info[' stu_id ']

Modify
student_info[' name ']= ' ALEXSB '


Boolean: True False
Purpose: Used to judge

>>> pinfo={' name ': ' Oldboymei ', ' age ': +, ' sex ': ' Female '}
>>>
>>>
>>> pinfo[' age '] > 50
True
>>> pinfo[' sex '] = = ' Female '
True

Python-based data types and variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.