Python base Point

Source: Internet
Author: User
Tags switch case

1. Formatted output

Method One:

Print "Username is%s"% name

Print "The girl's age was%d, address is%s"% (age, address)

Method Two:

Print "The girl's age was {0}, address is {1}". Format (age,address)

2. Enter

x= raw_input ("Please enter a number":)

x= Int (raw_input ("Please enter a number":))

3. List,tuple,dict

list, lists, length element contents are variable, city = [' Beijing ', ' Shanghai ', ' Hangzhou ', ' Chengdu ']

add Element city.append (' Nanjing ')

Tuple, tuple, immutable, love= (' l ', ' o ', ' V ', ' e ')

DIC, dictionary, key-value, antonym = {' big ': ' small ', ' good ': ' bad ', ' black ': ' White ', ' water ': ' Fire '}

Key must be immutable and cannot be duplicated

4. Iterating through an array

For I in range (3): # [0,1,2]

For I in Range (1,6): #[1,2,3,4,5]

For CityName in city:

Print CityName

Traversal of the Dictionary:

(1) for Sidea in antonym:

Print Sidea, ': ', Antonym[sidea]

(2) for Sidea,sideb in Antonym.items ():

Print Sidea, ': ', sideb

print ' {0} and {1} is a pair of antonym '. Format (SIDEA,SIDEB)

5. sys.argv

SYS.ARGV is a list of parameters

If you have documents test.py

Execute Python test.py, then SYS.ARGV has only one element, argv[0]= './test.py '

If you execute Python test.py Hello, then SYS.ARGV has 2 elements, argv[1]= ' Hello '

6. __name__, __main__

' __ ' starts with a variable that the permission is private

__NAME__, __main__ is built-in

As in the test.py

def func ():

print "Hello"

if __name__ = = ' __main__ ':

Func ()

Then, when executing Test.py,python test.py, the func () is called

The Func () is not called when the module is import test in other modules.

7. Map, reduce, filter, sorted

These are the built-in functions of Python.

Map implementations function for each element of a list, map (function name, listname)

Reduce is the iteration of results after each function, reduce (function name, listname); As the result of Redue (add,listname) is cumulative sum

Filter is a function that filters out some elements that match the return value of a function, such as filter (Is_odd,listname), to filter out the odd number in the list

Sorted built-in sort function, default ascending, can add CMP function to make own collation, sorted (listname), Sorted (listname,cmp)

such as descending:

DEF CMP (x, y):

If X<y:

Return 1

Elif X>y:

Return-1

Else

return 0

8. Python annotations

Solo

Multi-Line ""

Use of docstrings:

In the function, you can extract the part of the comment with "...".

such as Def MyFunc ():

"This function was to test docstrings

A fashion feature in Python "

Call: Print myfunc.__doc__

9. Some features of Python:

    • Process-oriented, object-oriented support
    • Very concise:
      • Any piece of code can be executed, unlike C,java has a main entry
      • Most statements do not need to be enclosed (), such as if i>3: rather than as if in C,java (i>3)
      • Logical branch of the way: As the beginning of a layer of logic, the difference between the different logical layers of 4 spaces
      • Dynamic type language, variable assignment directly with, no type description, unlike C,java are strongly typed, at this point PHP is also weak type, but the PHP variable name to add $, and Python even this has omitted
    • Switch case is not supported, in fact, it is also the embodiment of simplicity, because the switch statement is actually more than if elif code volume, function and the same
    • Case sensitive

10. Higher-order functions

Is that the return value is a function, that is, the function is returned in the function

Python base Point

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.