Python Variable Basics

Source: Internet
Author: User

Variable definition

    1. The first character of an identifier must be a letter or an underscore;

    2. The remainder of the identifier can make letters, numbers, underscores;

    3. Case sensitive.

    4. Variable names should be meaningful, such as My_girl_name.


Variable type

Integral type: Boolean (True/false, commonly used in the cycle of switches), integer (length and memory-related, overflow will be automatically converted to long), long-integer;

Non-integral: double-precision floating-point, plural, decimal (non-built-in type);

Sequence type: string (str), tuple (tuple), list;

Image type: Dictionary (dict);

Collection type: mutable set (set), immutable set (Frozenset).


Test:

>>> name = "Xiaoming" #定义name >>> name ' xiaoming ' >>> name = "Xiaohong" #覆盖原name > >> name ' Xiaohong ' >>> name1 = name #令name1 =name>>> name1 ' xiaohong ' >>> name = " Xiaobai "#改变name >>> name1 #name1不变 ' xiaohong ' >>> name ' XIAOB Ai

Why does the value of name1 not change?

>>> a=12>>> b=a>>> ID (a), id (b) (31946416, 31946416) >>> a=14>>> ID (a), ID ( b) (31946368, 31946416) >>> c=12>>> ID (a), id (b), ID (c) (31946368, 31946416, 31946416)

As can be seen from the above example: when B=a, B and a This variable name does not have a relationship, but the execution of a to execute the storage address, when a re-assignment, a re-open a piece of memory space, B still point to the original space.

A=12

B=a

Such as


650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/13/wKioL1b3TIvyAEB7AAAiCYICcmM618.png "title=" a1.png "alt=" Wkiol1b3tivyaeb7aaaicyiccmm618.png "/>

A=14

Such as

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/17/wKiom1b3TGzjzZVWAAAnvDx2VXg907.png "title=" a2.png "alt=" Wkiom1b3tgzjzzvwaaanvdx2vxg907.png "/>


C=12


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/13/wKioL1b3TY7CaFHrAAAsmRY47zE120.png "title=" a3.png "alt=" Wkiol1b3ty7cafhraaasmry47ze120.png "/>

Why is the ID of C here the same as B?

In Python, a completely independent variable should open up a separate space, but in fact Python optimizes it, and if the variable is an integer between 0~255, the same value points to the same memory space.

>>> e=287>>> f=287>>> ID (e), id (f) (32235368, 32235584)

Type conversions:

>>> name =  "Xiaoming" >>> age = 10>>> long_num  = 100000000000000000>>> run = False>>> f_num=0.01>>>  type (name), type (age), type (Long_num), type (run), type (f_num) (<type  ' str ' >, <type  ' int ' >, <type  ' int ' >, <type  ' bool ' >, <type  ' float ' >) >> > age = str (age) >>> run = int (run) >>> f_num =  int (F_num) >>> type (age), type (run), type (f_num) (<type  ' str ' >, <type  ' int ' >, <type  ' int ' >) >>> age,run,f_num                               #类型改变之后, some values will also change ('  0, 0 ', >>> int (name)                                    #不能转变时会报错Traceback   (most recent call last):   File  "<stdin>", line 1, in <module>valueerror: invalid  Literal for int ()  with base 10:  ' xiaoming '

Use of quotation marks:

Strings should be enclosed in quotation marks, single quotes, double quotes, and triple quotes if you want to enter multiline text. And you can have double and single quotes in three quotation marks.

>>> print "My name is Xiaobai, ... my girl ' s name is Xiaohong,... friends call us" a good couple "... "My name is Xiaobai,my girl's name is Xiaohong,friends call us" a good couple "

Variable references:

>>> name= "Xiaobai" >>> age=25>>> g_name= "Xiaohong" >>> print "My name is%s, ... My girl ' s name is%s, ... now I am%d years old. '% ( Name,g_name,age) My name is Xiaobai,my girl's name is Xiaohong,now I am all years old.

%s: reference string;

%d: Refers to the integer type, if the floating point type will be automatically converted to integer type;

%f: Refers to floating-point type,%.3f can specify the number of decimal digits.

Python Variable Basics

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.