The fifth lesson of the basic Python tutorial 0121

Source: Internet
Author: User

Learn basic things before learning conditions, loops, and other statements.

1.print output comma

More information on >>> #print and import
>>> #使用逗号输出
>>> print ' age ', 42
Age 42
>>>
(1, 2, 3)
>>> print (+/-)
(1, 2, 3)
>>> Print
1 2 3
>>> Print 1 2 3
Syntaxerror:invalid syntax
>>> name= ' CHR '
>>> age=42
>>> Print Name,age
CHR 42
>>> print name+ ', ' +age

Traceback (most recent):
File "<pyshell#10>", line 1, in <module>
Print name+ ', ' +age
Typeerror:cannot concatenate ' str ' and ' int ' objects
>>> print name, ', ', age
CHR, 42//Obviously output in this way will increase the space
>>>

Confirm that all features of a package are imported

Import Package *

2, some tips:

······ Multiple assignment operations can be performed simultaneously

>>> x,y,z=1,2,3
>>> print x, Y, Z
1 2 3

······ Exchange values for two variables

>>> x,y=y,x
>>> print x, Y, Z
2 1 3
In fact, these two operations are sequence unpacking.

>>> values=1,2,3
>>> values
(1, 2, 3)
>>> x,y,z=values //Note the number of assignments for both groups should be uniform. Otherwise, an exception is thrown
>>> print x, Y, Z
1 2 3

A simple method of Popitem

>>> hooby={' peo1 ': ' Basketball ', ' Peo2 ': ' runing ', ' peo3 ': ' Shopping '}
>>> Key.value=hooby.popitem () //The error between key and value is written here. Cause assignment errors when corrected again , you will not get the first set of data in the dictionary.

Traceback (most recent):
File "<pyshell#32>", line 1, in <module>
Key.value=hooby.popitem ()
Nameerror:name ' key ' is not defined
>>> Key,value=hooby.popitem ()
>>> Print Key,value
Peo2 runing
>>>

The following is the result of a second call to the first set of data in the dictionary, indicating that the results are correct.

>>> myhooby={' peo1 ': ' Basketball ', ' Peo2 ': ' runing ', ' peo3 ': ' Shopping '}
>>> Key1,value1=myhooby.popitem ()
>>> Print Key1,value1
Peo1 Basketball

For chained assignments and incremental assignments, similar to the C language, except that Python can also target strings, and C does not.

Continue tomorrow conditional and conditional statements.

The fifth lesson of the basic Python tutorial 0121

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.