Other Statement 1 in the notes of the basic python Tutorial: python loop statement

Source: Internet
Author: User

Other Statement 1 in the notes of the basic python Tutorial: python loop statement

  • Print-related

Print can print multiple expressions, as long as they are separated by commas (,), a space is inserted between each parameter in the result. Use + to avoid spaces, as shown in figure

>>> Print 'Age: ', 42
Age: 42
>>> Print 'hello' + ',' + 'World'
Hello, world

Add a comma at the end of the print statement. The following statement is printed in the same line as the previous statement, as shown in figure

print 'hello',print 'world'

Result

>>>
Hello world

  • Import Problems

From sometmodule import somefunc, anotherfunc, yetanotherfunc

From somemodule import * -- import all functions in the module

Import somemoudle as othermodulename -- give the module an alias

Form somemodule import somefunc ad otherfuncname -- alias for the imported Function

  • Assignment

Sequence unpacking or optional replacement-unpacks the sequence of multiple values and then stores them in the sequence of variables, as shown in figure

>>> Scoundrel = {'name': 'Robin ', 'firlfriend': 'marion '}
>>> Key, value = scoundrel. popitem ()
>>> Key
'Firlfriend'
>>> Value
'Marion'

Chained assignment -- shortcut for assigning the same value to multiple variables, such

X = y = somefunction ()

Incremental Value assignment: place the expression operator on the left of the value assignment operator =, as shown in figure

X + = 1

  • Statement Block

Colon (:) is used to identify the beginning of the statement block. Each statement in the block is indented (with the same number of indentations ). When it is rolled back to the same indentation as the closed block, it indicates that the current block has ended.

  • Threesome

Pass -- the program does not need to do anything.

Del -- delete the object, but it does not affect the value, as shown in figure

>>> X = y = [1, 2]
>>> Y [1] = 'P'
>>> Y
[1, 'P']
>>> X
[1, 'P']

>>> Del x
>>> X

Traceback (most recent call last ):
File "<pyshell #51>", line 1, in <module>
X
NameError: name 'X' is not defined
>>> Y
[1, 'P']

Exec -- execute a string statement

>>> Exec ("print 'hello, world! '")
Hello, world!

Eval -- evaluate the value of an expression

>>> Eval ("4 + 56 ")
60

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.