Python Learning Note two

Source: Internet
Author: User

---restore content starts---

A. Python comparison of several data types.

The following aspects are compared:

1. Whether it is variable.

Immutable type: The value of a variable can change, the ID also changes, the equivalent of creating a new object, so a modified value, the ID is changed, this type is called an immutable type.

Mutable type: The value of a variable can vary, and the ID does not change, so it is called a mutable type.

Example:

#数字类型样例
X=10print (ID (x))
Output:1374668752
# Change the value of X to 100 to see if the ID has changed?
x=100
Print (ID (x))
Output:1374671632
You can see that the value is modified, the ID is changed, the value cannot be modified, and the ID remains unchanged, so the number type belongs to the immutable type.


#字符串样例:

x= ' Yangjianbo '
Print (X,id (x))
#修改x的值, compare two IDs, whether they are the same.
x= ' Wangyanhe '
Print (X,id (x))

The result of the output: The value of the found string has been modified, and its ID has changed, indicating that a new reference has been generated, so the string type is also immutable.

#列表样例:
x=[' Yangjianbo ', ' wangyanhe ', ' gaozhongmin ']
Print (X,id (x))
#修改一下某个元素 to see if the list X,id changed.
x[0]= ' Gushuwei '
Print (X,id (x))
Results of the output:

[' Yangjianbo ', ' wangyanhe ', ' gaozhongmin '] 37527688
[' Gushuwei ', ' wangyanhe ', ' gaozhongmin '] 37527688

Conclusion: Modifying an element in a list does not affect the ID of the entire list, so the list belongs to a mutable type.

#元组样例:

x= (' Yangjianbo ', ' wangyanhe ', ' gaozhongmin ')
Print (X,id (x))

x[0]= ' Gushuwei '
Print (X,id (x))
Output result: Direct error, because the tuple is only readable, cannot write.
Conclusion: tuples are immutable types.

#字典
x={' name ': ' Yangjianbo ', ' age ': ten, ' bumen ': ' Xitongbu '}
Print (X,id (x))

x[' name ']= ' Wangyanhe '
Print (X,id (x))
Output Result:

{' name ': ' Yangjianbo ', ' age ': ten, ' bumen ': ' Xitongbu '} 31218928
{' name ': ' Wangyanhe ', ' age ': ten, ' bumen ': ' Xitongbu '} 31218928

Conclusion: The value of the dictionary can be modified, and the ID will not change, so the dictionary belongs to a mutable type.

2. Orderly or unordered.

Ordered and unordered, data types can be indexed to read the data.

#数字类型
x=10
Print (X[0])
Conclusion: Error, number type, cannot use index to read out, it is not an ordered data type.
#字符串
x= ' aabbcc4455 '
Print (x[9])
Conclusion: strings can be indexed to read the characters in a string, and the string is an ordered data type.
#列表
x=[' Yangjianbo ', ' wangyanhe ', ' gaozhongmin ']
Print (x[0])
Conclusion: The list reads an element and must specify the index of the element, so the list is an ordered data type.

#元组
x= (' Yangjianbo ', ' wangyanhe ', ' gaozhongmin ')
Print (X[0])

#字典
x={' name ': ' Yangjianbo ', ' age ': ten, ' bumen ': ' Xitongbu '}
Print (x[' name '])
Conclusion: The dictionary reads one of the values and must specify the key name, which cannot be read by the index. So the dictionary is not an ordered data type.

3. Can read and write.

In addition to tuples that are not writable, other types support read and write.

4. Save a value or multiple values.

A numeric type has only one value,

String has only one value

List Multiple values

Tuple Multiple values

Dictionary Multiple values

Two. Some features of the number type

Two variable exchange values.

x=10

Y=20

   X,y=y,x

Print (x)
Print (y)

Reference multiple variables to a value.
x=y=z=100
Print (X,y,z,id (x), id (y), id (z))

Three. Some features of the string

Viewing string Lengths

x= ' aaa11111 ' Print (len (x))

string Slices

x= ' abc12345abc ' Print (X[1:9:3])
The first number 1 means starting at index 1,
The second number 9 indicates the end of the 第9-1 index
The third number 3 represents the step size, starting at index 1, and one of the three steps.

Result of output: B25

member Operators

x= ' abc12345abc ' Print (' A ' in x) print (' B ' isn't in x)
Results of the output:

True
False

   Remove spaces

function with string strip () Lstrip () Rstrip ()

Strip () The default is to remove the white space character \ \ r \ n.

Name = "Yangjianbo" Print (name) Name=name.strip () print (name)
The output of the Yangjianbo, there is no previous blank.

Name = "yangjianbo*****"
Name=name.strip (' * ')
Print (name)
Result of output: ignores the * number character.
Name = "yangjianbo*****"
Name=name.strip (' an ')
Print (name)
Output result: I want to ignore an, this an IS in the middle of the entire string, not both ends, the result of the output is still the original value

Conclusion: The middle character cannot be ignored, only the head and tail characters can be ignored.

  

Split

start with a character

End With character

Python Learning Note two

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.