Handbook of Python3.5 cultivation 2

Source: Internet
Author: User

Variables and keywords


Variables point to various types of names, and later use this value, the direct reference to the name, not to write a specific value.

You can assign an integer value to a variable, and if it doesn't feel appropriate, assign the string to a variable.

In Pyhon, an equal sign (=) is an assignment statement that assigns any numeric type to a variable.

* The string must begin with a quotation mark and end with a quotation mark.

The same variable can be assigned repeatedly, and can be different types of variables

For example:

A=123a= ' ABC ' Print (a)

The output is: ABC

A language that is not fixed in variable types is called a dynamic language, and corresponds to a static language. Static language in the definition of variables is the type of the variable must be specified, if the assignment of the type mismatch will be an error, compared to static language, dynamic language more flexible.

When you cannot determine the type of a variable or data, you can confirm it with the interpreter's intrinsic function type.

For example:

>>> type (' Hello,world ') <class ' str ' >

The resulting type of the test is a string type (str).

>>> type <class ' int ' >

The result type of the test is shaping (int).

>>> Type (3.0) <class ' float ' >

The result type of the test is floating-point (float).

>>> a = ' test type ' >>> type (a) <class ' str ' >

The resulting type of the test is a string type (str).

>>> b=100>>> type (b) <class ' int ' >

The result type of the test is shaping (int).

>>> c=3.0>>> Type (c) <class ' float ' >

The result type of the test is floating-point (float).

As long as the value is enclosed in "or", it belongs to the string.

Make a comparison as follows:

>>> type (' Test Duyuheng ') <class ' str ' >>>> type ("du") <class ' str ' >>>> type (" ") <class ' str ' >>>> type (" 3.0 ") <class ' str ' >>>> b=3>>> type (b) <class ' int ' >>>> c=3.0>>> type (c) <class ' float ' >

* Do not equate the equal sign of an assignment with the equal sign in mathematics.

For example:

>>> a=100>>> a=a+200>>> Print (a) 300

Understanding the representation of variables in computer memory

>>> a= ' ABC '

At this point Python has done two things:

① creates a string of ' ABC ' in memory

② creates a variable of a in memory and points it to ' ABC '

You can also assign the variable A to the variable B, which is actually to point the variable B to the data that the variable a points to.

>>> a= ' ABC ' >>> b=a

What if a value is assigned to a, what does it print? The complete code is as follows:

>>> a= ' ABC ' >>> b=a>>> a= ' XYZ ' >>> print (b)

Idea Finishing:

When executing a= ' abc ', the interpreter creates the string ' abc ' and variable A, and points the variable to ' ABC '.

When executing B=a, the interpreter creates the variable B and points B to the string (ABC) to which a points.

When executing ah = ' xyz ', the interpreter creates the string ' xyz ' and changes the point of a to ' XYZ ', but B does not change.

So the last variable to output is

>>> print (b) ABC

Variable name

The program often chooses a meaningful name as the variable name to mark the purpose of the variable.

A variable name is a string of any length, consisting of numbers or characters, that must begin with a letter. Using uppercase letters is legal, but variable names are recommended to start with lowercase letters.

*pyhon are case-sensitive

For example:

>>> name= ' Duyuheng ' >>> name= ' python ' >>> print (name) duyuheng>>> print (name)

Python

The underscore ' _ ' can appear in the variable name

* Often used to connect multiple phrases

For example:

>>> n_ame= ' du Yu Heng ' >>> print (n_ame) du Yu Heng

Other than the above-mentioned combinations are illegal variable names, and there is no use of the keyword as the variable name.

There are 33 keywords in *python that cannot be used as variable names.

False class finally is return

None continue for Lambda try

True def from Nonlocal while

And Del Global not with

As Elif if or yield

Assert Else Import Pass

Break except in raise


This article from "Duyuheng" blog, declined reprint!

Handbook of Python3.5 cultivation 2

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.