Fifth day of Python (review data type)

Source: Internet
Author: User

Supplement to data types for the previous days
1, Shaping int
A. Several methods of input
A = 123
a = Int (123)
For when we enter a = 123 on the device, in fact python inside they will convert him to int (123), for the following parentheses in the inside of Python is actually called __init__

Internal optimization of B.int
They are shared with one address in memory, as in the following cases:
N1 = 123
N2 = N1
For the following scenario, they use different addresses in memory to store
N1 = 123
N2 = 123
But inside Python he would optimize the number between 5----127 and think they were the usual numbers, so in the program they would still use the same memory address to store

C. Limitation of length
Different for the operating system, the length of int is also different
32bit-2**31-----------2**31-1
64bit-2**63-----------2**63-1
But for Python, if we use more than the maximum number of int then he will not give an error, but will convert to a long plastic. Which is the addition of l to the number.
The range of long shaping can be said to be infinitely large, as long as your memory is larger, he will be able to consume the memory

2, String, str

A. How to create
S1 = "Rasir"
S2 = str ("Rasir")
B. Unique features, generally need to remember
S1.strip ()----------------------------remove spaces at both ends of the string
S1.startswith ()----------------------lookup string, whether it is the same as the one you are looking for
S1.find ()-----------------------------find a character in a string
S1.replace ()-------------------------Replace a sequence in a string with the character you want to replace
S1.upper ()---------------------------to capitalize all the letters of a string
S1.isapha ()---------------------------Find if there are only letters in the string if only the letters return true otherwise false

C. Public functions
Index: Only one element can be taken
Slices: Multiple elements can be taken

Encoding, for
Example:
temp = "Hello"
For I in temp:
Print (i)
bytes_list = bytes (i,encoding= ' Utf-8 ')
Print (bytes_list)
For I in Bytes_list:
Print (i)
Print (Bin (i))
Output Result:
You
B ' \xe4\xbd\xa0 '
228
0b11100100
189
0b10111101
160
0b10100000
Good
B ' \XE5\XA5\XBD '
229
0b11100101
165
0b10100101
189
0b10111101
Summary: For UTF-8 encoding, Chinese characters are stored in three bytes, and will be performed in 16 binary, while the GBK code is 2 bytes to store
For versions 2.7 and 3.7, when the for loop outputs a kanji, the version of 2.7 is output in bytes, while for 2.7 the version is output as a character
From the above example, we can see that for the For loop, it automatically converts the other binary into decimal.

d. Str and bytes Conversions
Span style= "COLOR: #800080" > (1), convert string to byte :
A = ' small white '
N1 = bytes (A, encoding= ' Utf-8 ')
N2 = bytes (A, encoding= ' GBK ')
Print (n1)
Print (n2)
Output:
B ' \xe5\xb0\x8f\xe7\x99\xbd '
B ' \xd0\xa1\xb0\xd7 '

(2), convert bytes to string
A = ' small white '
N1 = bytes (A, encoding= ' Utf-8 ')
N2 = Bytes (A, encoding= ' GBK ')
New1 = str (n1,encoding= "Utf-8")
New2 = str (n2, encoding= ' GBK ')
Print (New1, NEW2)
Output:
Small white small white
Summary: bytes can force definitions to create bytes, and can be used to convert other types to bytes
str can force a string to be defined, or it can be used to convert other types of objects to the string
for a man utf-8 is stored in three bytes and GBK is stored in two bytes

3. Lists: List
a1 = [' Cui ', ' rain ', ' Sir ']----------------Create a list
A2 = list ()-------------------------------Create an empty listing
a2 = list ([' Cui ', ' rain ', ' Sir '])
def __init__ (self, seq= ())---------------------------------------if the object to be converted is iterative (so to understand that it can be iterated for a loop)

(1), String conversion to List
Example:
A = ' small white '
A1 = List (a)---------------------------at this point a within the parentheses corresponds to a for loop
Print (A1)
Output result: [' small ', ' white ']

(2), converting tuples to lists
Example:
A = (' Cui ', ' rain ', ' Sir ')
a1= List (a)----------------------------------------------------for loop, each element of the loop as a list element
Print (A1)
Output Result:
[' Cui ', ' rain ', ' Sir ']

(3), Convert dictionary to list
Example:
A = {' Rain ': ' Sir ',
' Yan ': ' Sea '}
A1 = List (a)
A2 = List (A.values ())
a3 = List (A.items ())
Print (A1)
Print (A2)
Print (A3)
Output Result:
[' Rain ', ' Yan ']
[' Sir ', ' Sea ']
[(' Rain ', ' Sir '), (' Yan ', ' Sea ')]

A, special features of the list
A1.clear ()---------------------------------------------------------------------Clear list feature
A1.append ()-------------------------------------------------------------------Append an element
A1.extend ()--------------------------------------------------------------------append elements in an iterative manner
A1.insert ()--------------------------------------------------------------------add element, add element at specified position
A1.reverse ()-------------------------------------------------------------------reverses the list

B. Common characteristics
can be indexed--------------------must be an element after index index
can be sliced--------------------for slices If the slice is preceded by a character then the slice is also a character, if before is the list then also the list
Can all be used for loops

C, nested indexes
a1 = [' Rain ', ' Sir ', ' Sea ', 123,{' aaa ': ' BBB ', ' CCC ': {' ddd ': ' Eee '}]
A2 = a1[4]["CCC" [' DDD ']

4, Ganso. tuple
A, create and convert
A1 = (' Cui ', ' rain ', ' Sir ')
a2 = tuple (A1)---------------------------------can also be iterated
a3 = tuple ((' Cui ', ' rain ', ' Sir ')
B, basic characteristics
Count------------------------------------------------How many elements are counted
Index------------------------------------------------find an element and return to the location
C, elements cannot be modified
A1 = (' Cui ', ' rain ', ' Sir ', [' hell ', ' Keke '])
Print (A1)
A1[3].append (' while ')
Print (A1)
Output Result:
(' Cui ', ' rain ', ' Sir ', [' hell ', ' Keke '])
(' Cui ', ' rain ', ' Sir ', [' hell ', ' Keke ', ' while ')
Summary: For Yuanzulai said his element is not modifiable, but the contents of his element can be modified, to the above example, if we want to modify the element ' Cui ' is not to be


Important: Generally speaking, strings will not change the original content when they use a function, whereas for list,tuple,dict it is usually modified on the original object.

5. Dictionary, Dict
A, create and convert
N1 = {' Rain ': ' Sir ', ' tan ': ' AAA '}
N1 = Dict ({' Rain ': ' Sir ', ' tan ': ' AAA '})

List to String

a1 = [' Rain ', ' Sir ']

A2 = Dict (Enumerate (A1))
Print (A2)


b, the basic characteristics
N1.fromkeys ()--------------------------------------------------used to create a new dictionary

If there is a Staticmethod word on the top of a method then this is called with a class, as usual we usually use objects to invoke the method
Example:
N1 = Dict.fromkeys ([' K1 ', ' K2 ', ' K3 '],[])
Print (N1)
n1["K1"].append (' BBB ')
Print (N1)
Output Result:
{' K1 ': [], ' K2 ': [], ' K3 ': []}
{' K1 ': [' BBB '], ' K2 ': [' BBB '], ' K3 ': [' BBB ']}
Total knots:
VLAN values created later for Fromkeys they use the same memory address. This also explains why the value of K1 is added after adding the

N1.get ()------------------------------------------------------------------------------------------------------- -------------to get value based on the key value, unlike the index, if there is no key value then the get will return a null value, and the index will not be an error, where D is the default padding value, when Vlaue is not available we can use the default value to fill

Fifth day of Python (review data type)

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.