Fourth Python shell: code structure

Source: Internet
Author: User

Python's unique syntax:

    • Do not use the semicolon concluding sentence, but enter;
    • Differentiate code blocks by code indentation;
    • If, while, for, and so on, do not have parentheses, but no colon (:).

How do I divide a line of commands into multiple lines?

" lizhixin. ">>> mynameis'lizhixin. '
4.1 Article statements and loop statements

How do I compare using if, elif, and else?

if a < 2:    print ("Yes")elif a > 2 :     Print ("no") Else :     Print ("chaos")    Yes

How do I connect conditional expressions?

Not in Python! , && and | | Operators, respectively, with not, and and or instead.

 not 1False and 1 < 2trueor 1 > 2True

What are the false values in Python?

>>> falsefalse>>> None>>> "' > >> []>>> () () >>> {} {}>>> set () set ()

How do I use while to loop?

 while Count <= 5:    Print(count)    + = 1    12345

How do I jump out of loops?

 while True:     = Input ("string to calpitalize [type Q to quit]:")    if'  Q':        break    print(stuff.capitalize ())    String to calpitalize [type Q to Quit]:li zhi Xinli zhi xinstring to calpitalize [type Q to Quit]:q>>>

How do I jump to the start of a loop (a loop that skips a particular condition)?

>>> whileTrue:value= Input ("Interger [Q to quit]:")    ifValue = ='Q':         Break Number=int (value)ifNumber% 2 = =0:Continue    Print(Number,"Squared is", number*Number ) Interger [Q to quit]:Squared is1Interger [Q to quit]:2Interger [Q to quit]:Squared is9Interger [Q to quit]:4Interger [Q to quit]: Q

How does an else outside the loop work?

>>> numbers = [1,3,5]>>> position =0>>> whilePosition <len (Numbers): number=Numbers[position]ifNumber% 2 = =0:Print('Found even number', number) Breakposition+ = 1Else:    Print("No even number found.") No even number found.

* How do I iterate with for (very unique)?

The for in Python is awesome, and can traverse the entire data structure in the case of data structures and concrete implementations that are unknown:

>>> rabbits = ['a','b','C','D']>>> current =0>>> whileCurrent <Len (rabbits):Print(Rabbits[current]) current+ = 1ABCD>>> forRabbitinchRabbits:Print(rabbit) ABCD
' Cat '  for inch Word:     Print (letter)    Cat
 >>> a = { " a  " :  " b  " ,  " c  " :  " d  " ,  " e  " :  " f  "  }  >>> for  key in   A:  print   (key) Ace  
 >>> for  value in   A.values ():  print   (value ) BDF  
 >>> for  item in   A.items ():  print   (item) ( /span> " a   ,  " b   '  < span> < span> 
 for inch A.items ():     Print (key, value)    a BC de f

How do I iterate with the zip () function?

Using parallel iterations for multiple sequences is an iteration, which is equivalent to merging multiple sequences in order.

>>> C = 0.1, 0.2, 0.3>>> a = (1, 2, 3)>>> B = ['a','b','C','D']>>> C = 0.1, 0.2, 0.3, 0.4, 0.5,>>> forE, F, ginchZip (A, B, c):PrintE' 'F' ', G)1 A 0.12 B 0.23 c 0.3

List (), Zip (), and dict () can be used flexibly.

How to generate a sequence of natural numbers for a specific interval?

Range () is used like a slice, but uses ', ' instead of ': '.

 for  in range (0,3):    Print(x)    012>>> list (range (0,31, 2]

4.2 Derivation (Python-specific)

You can combine loops and conditions by creating data structures from one or more iterators.

List-derived

 for  in range (1,6)]>>> number_list[1, 2, 3, 4, 5]
 >>> a_list = [number for  number in  Range (1,6) if  number% 2 ==1]  > >> a_list[ 1, 3, 5] 
>>> rows = range (1,4)>>> cols = range (1,3) for inch Cols]  for inch Cells:     Print (cell)    (1, 1) (1, 2)(2, 1) (2, 2) (3, 1)(3, 2)

Dictionary derivation type

' Letters '  for inch Word} >>> letter_counts{'l't'  R's'e': 2}

Function

What are the different ways to use parameters in Python?

def func (A, B, c):     Print ("'", c)      >>> func (1, 2, 3is are      3
>>> func (c = 1, b = 2, a = 3is was is      1
>>> d = [1, 2, 3]>>> func (*is are      3
>>> e = {'C'b'a': 3 }>>> func (** is are      1

Generator

Decorative Device

Namespaces and Scopes

Abnormal

Fourth Python shell: code structure

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.