Python learning notes conditions, loops, and other statements

Source: Internet
Author: User
Tags aliases assert

First, Function Import

1. Provide aliases for the module

>>> Import Math as Foobar #设置math alias Foobar

>>> Foobar.sqrt (4)

Showing: 2.0

2. Provide aliases for functions

>>> from math import sqrt as Foobar

>>> Foobar (4)

Showing: 2.0

Second, assign the value

1, multi-value assignment

>>> x,y,z=1,2,3

>>> print (x, y, z)

Show:

2, multi-value exchange

>>> x,y=y,x

>>> print (x, y, z)

Display: 2,1,3

3. Increment Assignment

>>> x=2

>>> x+=1

>>> x*=2

>>> x

Showing: 6

Iii. conditional execution and if statements

1. If nesting code

>>> Name=input (' What's your name? ‘)

>>> if Name.endswith (' Gumby '):

>>> if Name.startswith (' Mr. '):

>>> print (' Hello, Mr Gumby ')

>>> elif name.startswith (' Mrs. ')

>>> print (' Hello, Mrs. Gumby ')

>>> Else:

>>> print (' Hello, Gumby ')

>>> Else:

>>> print (' Hello,stranger ')

2, is, the identity operator, and = = are similar, but in fact not the same

>>> x=y=[1,2,3]

>>> z=[1,2,3]

>>> x==y

Display: True

>>> x==z

Display: True

>>> x is y

Display: True

>>> X is Z

Display: False

3, in, membership operator

>>> if ' s ' in Name:

>>> print (' Your name contains the letter "s")

>>> Else:

>>> print (' Your name does not contain ' s '. ')

4. Assert, Assert

>>>age=-1

>>> assert 0<age<100, ' the must be realistic '

Showing: assertionerror:the must be realistic

5. While loop

>>> x=1

>>> while x<= 100:

>>> Print (x)

>>> x+=1

6. For loop

>>> numbers=[0,1,2,3,4,5,6,7]

>>> for number in numbers:

>>> Print (number)

Python learning notes conditions, loops, and other statements

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.