Python base 8 (expressions and statements)

Source: Internet
Author: User

One,print and import information

Print ' Age : ',   # #  Print multiple expressions separated by commas age:22
 Import somemodule             #  importing functions from module imports   math as Foobar>>> FOOBAR.SQRT (4)2.0 from import *      #  Import all features from a given module

Second, assign the value

1. Sequence unpacking: The sequence of multiple values is untied

>>> values = 1,2,3>>> values (1, 2, 3)>>> x,y,z=values>> > x1>>> x,y=y,x       #  swapping two variables print x, y, z2 1 3

2, chain-assigned value

>>> x = y = somefunction

3. Increment Assignment

x +=1 for standard operators such as *,/,%

>>> x = 2>>> x +=1>>> x *=2>>> x6

Third, Python statements

If statement, Else statement, elif statement, conditional expression, while statement, for statement, break statement, continue statement, pass statement, iterators (iterator), list resolution

1Name = Raw_input ('What is your name?')2 ifName.endswith ('Gumby'):3     ifName.startswith ('Mr.'):4         Print 'Hello,mr,gumby'5     elifName.startswith ('Mrs.'):6         Print 'Hello,mrs,gumby'7     Else:8         Print 'Hello,gumby'9 Else:Ten     Print 'Hello,stranger'
Number = input (")if and number >=1:      #  Boolean operator    print'great'else:      Print'wrong'

Assertion: instead of letting the program crash at a later time, it's better to crash the error condition as it appears.

 if  not   Condition:crash program  
assert 0 < Age < +,   'The agemust is realistic'     #  condition, you can add a string to explain the assertion Traceback (most recent call last  ):"<pyshell#99> "  in <module>    assert 0 < Age < +,   'The agemust is realistic  'assertionerror:the age must being realistic

Cycle:

1. While loop

 while a<B:    Print A,   #  Note the comma at the end, which causes all output to appear on one line    a+=1      1 2 3 4 5 6 7 8 9

2. For loop

Python base 8 (expressions and statements)

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.