The path of Python (ii)

Source: Internet
Author: User
Tags iterable


Python type



The pypy of this type is faster than the above, because the interpreter is compiled into a byte code and then compiled into a machine code. So the runtime runs the machine code directly. It is compiled to be slow and run fast.
The above several, the interpreter is compiled in the time, is compiled into byte code. The runtime then runs the bytecode file, the virtual machine is compiled into machine code, and then the CPU executes. Compile fast and run slowly.


Coding
ASCII (American Standard Code for Information interchange, United States Standards Information Interchange Code) is a set of computer coding systems based on the Latin alphabet, mainly used to display modern English and other Western European languages, which can be used up to 8 Bit to represent (one byte), that is: 2**8 = 256, so the ASCII code can only represent a maximum of 256 symbols.
Unicode (Uniform Code, universal Code, single code) is a character encoding used on a computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more
UTF-8, which is compression and optimization of Unicode encoding, does not use a minimum of 2 bytes, but instead classifies all characters and symbols: the contents of the ASCII code are saved with 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes ...






Receive incoming parameters
Python file.py parameters

Processed by the Sys module, SYS.ARGV represents all parameters
#cat test.py
Import Sys
Print (SYS.ARGV)
Print ("The first parameter is the file name:%s, the second parameter is%s"% (sys.argv[0],sys.argv[1))

#python test.py 1
[' test.py ', ' 1 ']
The first parameter is the file name:test.py, and the second parameter is 1


string concatenation of the Evil + number
Newstring= ' Alex '
[' A ', ' l ', ' e ', ' X ']
[' A ', ' l ', ' e ', ' x ']+ ' y ' ==> [' a ', ' l ', ' e ', ' x ', ' Y '], the original [' A ', ' l ', ' e ', ' X '] still exist.


Meta-group
Elements of a tuple cannot be changed, elements of a tuple can be changed
>>> a= (1,2,{' key ': ' Value '})
>>> a[2][' key ']= ' OK '
>>> Print a
(1, 2, {' key ': ' OK '})
>>> a[2]=3
Traceback (most recent):
File "<stdin>", line 1, in <module>
TypeError: ' Tuple ' object does not support item assignment
>>>



Dictionary
Keys keys
Value values
Key value pairs items
>>> a={' key1 ': ' value1 ', ' key2 ': ' value2 '}
>>> A.keys ()
[' Key2 ', ' Key1 ']
>>> a.values ()
[' value2 ', ' value1 ']
>>> A.items ()
[(' Key2 ', ' value2 '), (' Key1 ', ' value1 ')]
>>>



__name__






Object





int common built-in features





>>> age=-18
>>> Age.bit_length ()
5
>>> age.__abs__ ()
18
>>> ABS (age)
18
>>> age.__add__ (100)
82
>>> age.__divmod__ (4)
(-5, 2)
>>> age.__abs__ (). __divmod__ (4)
(4, 2)

Python3 only __eq__ ()
Python 3.4.0 (default, June 19 2015, 14:20:21)
[GCC 4.8.2] on Linux
Type "Help", "copyright", "credits" or "license" for more information.
>>> age=18
>>> age.__eq__ (19)
False

Python3 operations
>>> 5/6
0.8333333333333334
>>> 5//6
0
>>> re = 5.__floordiv__ (6)
File "<stdin>", line 1
Re = 5.__floordiv__ (6)
^
Syntaxerror:invalid syntax
>>> result=age.__floordiv__ (8)
>>> Print (Result)
2
>>> result=5.__floordiv__ (8)
File "<stdin>", line 1
RESULT=5.__FLOORDIV__ (8)
^
Syntaxerror:invalid syntax
>>> a=5
>>> result=a.__floordiv__ (8)
>>> Print (Result)
0


>>> Print type (age.__float__ ())
<type ' float ' >
>>>






Str
__contains__ (' string ')
Capitalize ()
Casefold ()
Center (Width,fillchar=none)
Ljust (Width,fillchar=none)
Rjust (Width,fillchar=none)
Count (Sub,start=none,end=none)
Encode
EndsWith (Suffix,start=none,end=none)

>>> name= ' newbility '
>>> name.__contains__ (' Go ')
False
>>> name.__contains__ (' eo ')
False
>>> name.__contains__ (' e ')
True
>>> name.capitalize ()
' Newbility '
>>> name.capitalize (). Casefold ()
' Newbility '
>>> Name.center (20, ' * ')
' *****newbility****** '
>>> name.count (' i ')
2
>>> name.count (' i ', 0,5)
1
>>> name.endswith (' y ')
True
>>> name.endswith (' x ')
False
>>> name.endswith (' W ', 0,2)
False
>>> name.endswith (' W ', 0, 3)
True
>>> name.encode (' GBK ')
B ' Newbility '
>>> name.encode (' Utf-8 ')
B ' Newbility '
>>> name= ' cattle '
>>> Print (name)
Cow
>>> name.encode (' Utf-8 ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' Nam ' is not defined
>>> name.encode (' Utf-8 ')
B ' \xc5\xa3 '
>>> name.encode (' GBK ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Unicodeencodeerror: ' GBK ' codec can ' t encode character ' \u0163 ' in position 0:illegal multibyte sequence
>>> name.encode (' Unicode ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Lookuperror:unknown Encoding:unicode
>>> name.encode (' ASCII ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Unicodeencodeerror: ' ASCII ' codec can ' t encode character ' \u0163 ' in position 0:ordinal not in range (128)
>>> Name
Cattle
>>> Name.ljust (20, ' * ')
' Cow ******************* '
>>> name.ljust (2, ' * ')
' Ox * '
>>> Name.center (20, ' * ')
' ********* cow ********** '
>>> name.rjust (2, ' * ')
' * ox '
>>>



Str
Expandtabs (tabsize=8)
Find (Sub,start=none,end=none)
RFind (Sub,start=none,end=none)
Index (Sub,start=none,end=none)
Rindex
Format (String)

Join (iterable)
Lower ()
Lstrip (Chars=none)
Rstrip (Chars=none)
Partition (Sep)
Rpartition
Replace (Old,new,count=none)
Split (Sep=none,maxsplit=-1)
Rsplit
StartsWith (Prefix,start=none,end=none)
Swapcase ()
Title ()
Upper ()
Zfill (width)




>>> name= ' new\tbility '
>>> Name
' New\tbility '
>>> Name.expandtabs ()
' New Bility '
>>> Name.expandtabs (2)
' New Bility '
>>> name= ' newbility '
>>> name.find (' ew ')
1
>>> name.find (' O ')
-1
>>> name.rfind (' ew ')
1
>>> name.rfind (' W ')
2
>>> name.rfind (' i ')
6
>>> name.index (' ew ')
1
>>> name.index (' O ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Valueerror:substring not found
>>> name.rindex (' i ')
6
>>> "_". Join (name)
' N_e_w_b_i_l_i_t_y '
>>> Name.lower ()
' Newbility '
>>> Name.upper ()
' Newbility '
>>> Name.swapcase ()
' Newbility '
>>> Name.strip (' i ')
' Newbility '
>>> "_". Join (name). Strip ('_')
' N_e_w_b_i_l_i_t_y '
>>> "_". Join (name). Strip (_)
‘‘
>>> name.strip (' y ')
' Newbilit '
>>> name.rstrip (' y ')
' Newbilit '
>>> Name.rstrip (' i ')
' Newbility '
>>> Name.lstrip (' i ')
' Newbility '
>>> Name.lstrip (' n ')
' Ewbility '
>>> Name.lstrip (' e ')
' Newbility '
>>> name.partition (' i ')
(' newb ', ' I ', ' lity ')
>>> name.rpartition (' i ')
(' Newbil ', ' I ', ' Ty ')
>>> name.replace (' i ', ' X ', 1)
' Newbxlity '
>>> name.replace (' i ', ' X ', 2)
' Newbxlxty '
>>> name.split (' i ')
[' Newb ', ' l ', ' Ty ']
>>> name.rsplit (' i ')
[' Newb ', ' l ', ' Ty ']
>>> Name.startswith (
...
Keyboardinterrupt
>>> name.startswith (' e ')
False
>>> name.startswith (' e ', 1)
True
>>> name.startswith (' E ', 2)
False
>>> Name.title ()
' Newbility '
>>> Name.zfill ()
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:zfill () takes exactly 1 argument (0 given)
>>> Name.zfill (10)
' 0newbility '
>>> Name.zfill (1)
' Newbility '
>>> Name.zfill (20)
' 00000000000newbility '
>>> Name.zfill () isalnum ()
True
>>>









Maketrans and translate in str







List




Pop returns the value taken out of the list, and the list changes


>>> A
[' 1 ', ' 1 ']
>>> li = List (' AA ', ' BB ', ' cc ', 8,9)
>>> Li
[' AA ', ' BB ', ' cc ', 8, 9]
>>> Li.append (a)
>>> Li
[' AA ', ' BB ', ' cc ', 8, 9, [' 1 ', ' 1 ']
>>> A.clear ()
>>> A
[]
>>> B=a
>>> b
[]
>>> li.count (' 1 ')
0
>>> li.count (' AA ')
1
>>> li.append (' AA ')
>>> li.count (' AA ')
2
>>> Li.extend (LI)
>>> Li
[' AA ', ' BB ', ' cc ', 8, 9, [], ' AA ', ' AA ', ' BB ', ' cc ', 8, 9, [], ' AA ']
>>> li.index (' AA ')
0
>>> li.insert (' BB ', 1)
Traceback (most recent):
File "<stdin>", line 1, in <module>
TypeError: ' str ' object cannot is interpreted as an integer
>>> Li.insert (1, ' BB ')
>>> Li
[' AA ', ' BB ', ' BB ', ' cc ', 8, 9, [], ' AA ', ' AA ', ' BB ', ' cc ', 8, 9, [], ' AA ']
>>> Li.pop ()
' AA '
>>> Li.pop ()
[]
>>> C=li.pop ()
>>> C
9
>>> Li
[' AA ', ' BB ', ' BB ', ' cc ', 8, 9, [], ' AA ', ' AA ', ' BB ', ' cc ', 8]
>>> Li.remove ([])
>>> Li
[' AA ', ' BB ', ' BB ', ' cc ', 8, 9, ' AA ', ' AA ', ' BB ', ' cc ', 8]
>>> Li.reverse ()
>>> Li
[8, ' cc ', ' BB ', ' AA ', ' AA ', 9, 8, ' cc ', ' BB ', ' BB ', ' AA ']
>>> Li.sort ()
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:unorderable types:str () < int () #python3 the strings and numbers are not sorted together.
>>>


>>> List (1,2,4)
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:list () takes at the most 1 argument (3 given)
>>> list ((1,2,4))
[1, 2, 4]
>>> a=list ((1,2,4))
>>> A
[1, 2, 4]
>>> a=list ([+])
>>> A
[1, 2, 3]
>>> a=list (' 1 ')
>>> A
[' 1 ']
>>> a=list (1)
Traceback (most recent):
File "<stdin>", line 1, in <module>
TypeError: ' int ' object is not iterable
>>>




Tuple note count and index method



Dict
Clear ()
Copy ()
Fromkeys (seq)
Get (K,d=none)
Items ()
Keys ()
VALUES ()
Pop (K,d=none)
Popitem ()
SetDefault (K,d=none)
Update (E=none)


>>> DIC
{' K1 ': [K2]: [99]}
>>> dic1=dic.copy ()
>>> Dic1
{' K1 ': [K2]: [99]}
>>> Dic.clear ()
>>> DIC
{}
>>> Dic1
{' K1 ': [K2]: [99]}
>>> dic1.get (' K1 ')
[77, 88, 99]
>>> dic1.get (' K3 ')
>>> Print (Dic1.get (' K3 '))
None
>>> Dic1.keys ()
Dict_keys ([' K1 ', ' K2 '])
>>> Dic1.items ()
Dict_items ([' K1 ', [[+], []], (' K2 ', [99])])
>>> dic1.values ()
Dict_values ([[77, 88, 99], [99]])
>>> Dic1.pop ()
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:pop expected at least 1 arguments, got 0
>>> dic1.pop (' Key3 ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Keyerror: ' Key3 '
>>> dic1.pop (' K2 ')
[99]
>>> Dic1
{' K1 ': [77, 88, 99]}
>>> dic1.setdefault (' K3 ', ' new ')
' New '
>>> Dic1
{' K1 ': [K3]: ' New '}
>>> Dic1.update (DIC)
>>> Dic1
{' K1 ': [K3]: ' New '}
>>> dic1.update ([1,23])
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:cannot convert dictionary Update sequence element #0 to a sequence
>>> dic1.update (())
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:cannot convert dictionary Update sequence element #0 to a sequence
>>> dic1.update ({1:1,2:2})
>>> Dic1
{1:1, 2:2, ' K1 ': [New], ' K3 ': '}

>>> A
{' BB ': ' BBB ', ' AA ': ' AAA '}
>>> a.fromkeys ([' BB ', ' AA '])
{' BB ': None, ' AA ': none}
>>> a.fromkeys ([' BB ', ' AA '], ' III ')
{' BB ': ' III ', ' AA ': ' III '}
>>> a.fromkeys ([' BB ', ' AA ', ' cc '], ' III ')
{' CC ': ' III ', ' BB ': ' III ', ' AA ': ' III '}
>>> A.fromkeys ([' CC '], ' III ')
{' CC ': ' III '}
>>>







The path of Python (ii)

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.