Python Learning record (v)

Source: Internet
Author: User

1. Print and import P83

Import ... as ...

2. Assign Value

3. Statement block
A statement block is a set of statements that executes or executes multiple for when the condition is true. You can create a statement block by placing a space in front of the code to indent the statement.
Use a colon: represents the beginning of a statement block

False: False, None, 0, "", (), []
NOTE: []! = False []: Empty Dictionary

()! = "" (): Empty tuple and sequence

Name = Raw_input (' What's your name? ')
If Name.endswith (' Yilia '):
print ' Hello%s '%name
Else
print ' input error '

4. Comparison operators
x = = y
X < y
X > Y
X <= y
X >= y
X! = y # x <> y = = x! = y
X is y # x and y are the same object: point to the same storage space = =: content is equal
X is not y # x and y are different objects
x in Y # x is a member of the Y container (for example: sequence)
x not in Y # x is not a member of the Y container
0<x<y # Legal

>>> x = [+ +]
>>> z = [[+]
>>> y = x
>>> x = = y
True
>>> x = = Z
True
>>> y = = Z
True
>>> x is y # x and y are the same object
True
>>> x is z # x and Z are not of the same object
False
>>> y is X
True
>>> y is Z
False

5. Assertion
Program crashes directly when errors occur
An assertion is used when certain conditions must be true. Keywords: assert

num = input (' Enter a number: ') # Note that using numbers here, you must use input
assert-10 < num < 10

6. While loop and for loop
Range function
>>> Range (0,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> [x*x for x in range (1,10)]
[1, 4, 9, 16, 25, 36, 49, 64, 81]



Traverse Dictionary
>>> x = {' name ': ' Yilia ', ' age ': ' + ', ' class ': [' math ', ' 中文版 ', ' music '}

>>> for key in x:
... print key, ' corresponds to ', X[key]
...
Age corresponds to 16
Name corresponds to Yilia
class corresponds to [' Math ', ' 中文版 ', ' music ']


>>> for Key,value in X.items ():
... print key, ' corresponds to ', value
...
Age corresponds to 16
Name corresponds to Yilia
class corresponds to [' Math ', ' 中文版 ', ' music ']

Jump out of the loop:
Break
Continue


Simple example:
Girls = [' Alice ', ' Birnice ', ' Clacrice ']
Boys = [' Chris ', ' Arnold ', ' Bob ']

lettergirls={}
For girl in Girls:
Lettergirls.setdefault (Girl[0], []). Append (Girl)
Print [B + ' + ' + G for B in boys-G in lettergirls[b[0]]

[' Chris + Clacrice ', ' Arnold + Alice ', ' Bob + Birnice ']

7. Three-person pass del exec

Pass: Used to replace empty blocks of code
Del: Delete objects that are no longer in use
>>> x = 9
>>> x = None # Remove Object reference
>>> x
>>> x = 9
>>> del x # removes not only the object's reference, but also the object name, without deleting the value itself
>>> x
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' x ' is not defined


>>> x = 9
>>> y = x
>>> y
9
>>> x
9
>>> del X
>>> x # x does not exist
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' x ' is not defined
The value of >>> y # y still exists
9

EXEC: Execute Python code in a string
>>> exec "print ' Hello,world '"
Hello,world

Eval: Computes a Python expression (written as a string) and returns the result

Python Learning record (v)

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.