Python Learning notes (2)

Source: Internet
Author: User
Tags one more line

Unicode string: GB2312 encoding for Chinese generated Python internal encoding is Unicode encoding
Unicode usually uses two bytes to represent a character, the original English encoding from a single byte into a double-byte, only need to fill the high byte all 0 can be
A string expressed in Unicode with U ' ... '
such as: Print U ' Chinese ' (Do not add u Chinese can not be displayed)
The representation of a string inside a python is Unicode encoding, so in encoding conversions, it is often necessary to use Unicode as an intermediate encoding, that is, decoding other encoded strings decode to Unicode, and then encode from Unicode encoding to another encoding

The role of Decode is to convert other encoded strings into Unicode encodings, such as Str1.decode (' gb2312 '), to convert gb2312 encoded string str1 to Unicode (or Unicode (str1, ' gb2312 ') )
The role of encode is to convert Unicode encoding into other encoded strings, such as Str2.encode (' gb2312 '), to convert Unicode encoded string str2 to gb2312 encoding
The default encoding of the string in the code is consistent with the encoding of the aunt file itself

Window default encoding GBK linux default encoding UTFs
integers and floating-point numbers :
Python supports direct four blending of floating-point numbers and integers (the arithmetic rules are exactly the same as the arithmetic rules in mathematics)
(Integer and integer operations are still integers, floating-point numbers and floating-point numbers are floating-point numbers, and integer and floating-point operations are floating-point numbers)
Remainder:%
Boolean type : False True
Python regards 0, empty string ' ' and none as false for other numeric and non-empty strings as true

Python collection type :
list: A list is a collection of ordered (sequentially arranged) elements that can be added and removed at any time
[] Enclose the list element, which is a list object.
You usually assign a list to a variable so that you can refer to the list with France than the two.
l=[' Michale ', 100,true] Note: Because Python is a dynamic language, its list element does not require the same data type to be empty list l=[]
Python accesses list print l[0] #打印列表中第一个元素 by index (index starting from 0)
Positive sequence starting from 0, reverse order starting from-1:: When the index number is negative, the content in the list is reversed, remembering that the last space of the list is numbered-1 starts (cannot be crossed)

To add a new element :
Append () Adds a new element to the list tail such as: L.append (' Paui ')
Insert () Inserts an element into the index number, the first parameter is the index number, and the second parameter is the new element to be added
such as: L.insert (0, ' paile ') L is the variable name assigned to the list, add Paile to index 0, the original is automatically moved back one

To Delete a new element (POP):
Pop () default delete first, pop (index number), delete element at position of index number

replacement element : l[index number]= ' Paul ', directly assigned

Create tuple: Tuple is another ordered list, Chinese translation is "tuple", once a tuple is created, it cannot be modified, when a tuple is created, all elements are enclosed in () For example: t= (' CCFDVG ', ' vfgth ', ' BGH ')
Access: t[0], or t[-1] as List
Empty tuple t= ()

To create a cell element tuple:
The () of a cell tuple is treated as a priority (1) as an integer 1-element bracket ending Plus, for example: t= (1,)
When printing cell elements, also automatically added, print t (1,)
"mutable" tuple (pointing unchanged, pointing things can change)
t= (' A ', ' B ', [' A ', ' B '])
L=T[2]
l[0]= ' X '
l[1]= ' Y '
Print T
(' A ', ' B ', [' X ', ' Y ']) #实质是改变了list的元素

Conditional judgment and looping statements for Python
If statement:
Age=20
If age>=18:
print ' Your age was ', age
Print ' adult '
print ' END '
The indentation of the Python code (4 spaces) rules, code with the same indentation as a block of code, the above 3, 4 lines of print statements constitute a block of code (but not including the 5th row of print)
NOTE: The If statement is followed by an expression. Then use: to represent the code block start
Tap the code in an interactive mode environment. To pay special attention to indentation, and exit indent, you need to hit one more line to enter
For example:

>>age=20
>>if age>=18:
... print ' Your is ', age
... print ' adult '
...

If-else Statement :
For example:
Age=20
If age>=18:
print ' adult '
else: (Note the following else:)
print ' teenager '

If-elif-else Statement :
For example:
If age>=18:
print ' adult '
Elif age>=6:
print ' teenager '
Else
print ' Kid '

For Loop :
For example:
l=[' Adma ', ' Lisa ', ' Bart '
for name in L;
Print Name
Note: The name variable is defined in the For loop (this is a temporary variable name that can be customized), which means that each element in the list is taken out in turn, the element is assigned to name, and then the for loop body is executed

While loop :
Another loop that differs from the For loop is the while loop, which does not iterate over the elements of the list or tuple, but rather determines whether the loop ends with an expression.
For example, to print an integer no greater than n from 0:
n=10
X=0
While X<n
Print X
X=x+1
The while loop first judges the next x<n, if true, executes the block of code for the loop body, otherwise, exits the loop
In the loop, x=x+1 will cause X to increase and eventually exit the loop because the x<n is not formed.

Break Exit Loop
When using a For loop or while loop, if you want to exit the loop directly inside the loop, you can use the break statement
**

Continue Continue circulation * *
During the loop. You can use break to eject the current loop, or you can skip the subsequent loop code with continue and continue to the next loop
For example:
The while loop of calculation 0-100 is reformed by adding the continue statement so that only the odd number is computed and:
Train of thought: if judge to be even, continue skips.
Implementation code:

Multiple loops (nested loops): Inside the loop, you can also nest loops

Python Learning notes (2)

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.