Python basics,

Source: Internet
Author: User

Python basics,

I. Basic Points
Import osimport getpassos. system ("dir") # list of directories pai_res = OS. system ("dir") # The variable value is 0, which is the command execution result. The correct value is 01__res = OS. system ("dir "). read () # directory list OS. mkdir ("newdir") # create a directory # print (sys. path) # current path of the execution file # print (sys. argv) # parameter transfer # print (sys. argv [2]) password = getpass. getpass ("m:") # password encryption print (password) ''' # pass the parameter name = 'xiao' user = input ("username :") pas = input ("password:") age = int (input ("age:") info = "--- me: % suser: % spas: % sage: % s "% (name, user, pas, age) print (info) info2 =" --- me: {a} user: {B} pas: {c} age: {d }""". format (a = name, B = user, c = pas, d = age) print (info2) info3 = "" --- me: {0} user: {1} pas: {2} age: {3 }""". format (name, user, pas, age) print (info3 )'''

  

Binary, Trielement operation
result = Value1 if Conditionelse Value2
Binary, octal, hexadecimal
Byte and string types

The most important new feature of Python 3 is to make a clearer distinction between text and binary data. The text is always Unicode, represented by the str type, and the binary data is represented by the bytes type. Python 3 does not mix str and bytes in any implicit way, which makes the distinction between the two very clear. You cannot concatenate strings and byte packets, search for strings in a byte packet (or vice versa), or input a string as a byte packet parameter (or vice versa ). This is a pieceGood thing.

In any case, the line between a string and a byte packet is inevitable. The following illustration is very important. Please remember:

A string can be encoded into a byte packet, while a byte packet can be decoded into a string.

>>>'€20'.encode('utf-8')b'\xe2\x82\xac20'
>>> b'\xe2\x82\xac20'.decode('utf-8')
'€20'

 

In this case, the string is the abstract representation of the text. A string consists of characters, which are abstract entities irrelevant to any specific binary representation. When operating on strings, we live in the ignorance of happiness. We can split and split strings and splice and search strings. We do not care about how they are represented internally. Each character in a string must be saved in several bytes. We will focus on this only when encoding strings into byte packets (for example, to send them on the Channel) or decoding strings from the byte packet (reverse operation.

The input parameters of encode and decode are encoding (or codec ). Encoding is a way to use binary data to represent abstract characters. There are many types of codes. The UTF-8 given above is one of them, and the following is another:

>>>'€20'.encode('iso-8859-15')b'\xa420'>>> b'\xa420'.decode('iso-8859-15')'€20'

 

Encoding is a crucial part of the conversion process. The bytes object B '\ xa420' is only a heap of bits. Encoding. With different codes, the meaning of these bit spaces will be different:

>>> b'\xa420'.decode('windows-1255')'₪20'

Document reference: http://www.cnblogs.com/alex3714/articles/5230609.html

 

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.