003-python base-variable

Source: Internet
Author: User
Tags uppercase letter

I. Definition of variables

Variables are used to store various identifiers that require temporary storage of data that can be changed while the program is running, a variable should have a name, and a storage unit in memory that holds the value of the Variable.

II. Declaration of variables

1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3   4 " Wupeiqi "

The code above declares a variable named: name, and the value of the variable name is: "wupeiqi"

The role of a variable: a nickname that refers to what is stored in an address in memory

Iii. naming rules for variables

    • Variable names can only be any combination of letters, numbers, or underscores
    • The first character of a variable name cannot be a number
    • The following keywords cannot be declared as variable names
    • [' and ', ' as ', ' assert ', ' break ', ' class ', ' Continue ', ' def ', ' del ', ' elif ', ' else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' Print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']

Note: in python, uppercase and lowercase letters are considered to be two different characters. As a result, sum and sum are two different variable Names. In general, variable names are expressed in lowercase letters , consistent with People's daily habits to increase Readability.

Iv. naming habits of variable names

The definition of variable name in the premise can express its role, the more concise the better, can use a word to express clearly do not use two.

Name the complex variable, and select the underscore "_" or the uppercase letter of the first Write. Such as:

Checkcurrentconncount

Check_current_conn_count

V. Assignment of variables and understanding in memory

1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3 4 " Wupeiqi " 5 " Alex "

1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3 4 " Wupeiqi " 5 name2 = name1

When the variable name1 is assigned to another variable name2, the interpreter simply assigns the memory address pointed to by name1 to name2, so name1 and name2 do not have a direct association, except that they both point to the same memory address. This is why you point name1 to a new address, and the value of name2 remains the Same.

1Name1 ="Wupeiqi"2Name2 ="Alex"3 Print("name1 ID is", id (name1))4 Print("name2 ID is", id (name2))5 Print("". Center (40,"-"))6Name2 =name17 Print("name1:", Name1)8 Print("name2:", Name2)9 Print("name1 ID is", id (name1))Ten Print("name2 ID is", id (name2)) one Print("". Center (40,"-")) aName1 ="New" - Print("name1:", Name1) - Print("name2:", Name2) the Print("name1 ID is", id (name1)) - Print("name2 ID is", id (name2)) -  - # Output +NAME1 ID is10728928 # NAME1 's initial ID -Name2 ID is10729600 # Name2 's initial ID +---------------------------------------- a Name1:wupeiqi at Name2:wupeiqi -NAME1 ID is10728928 -Name2 ID is10728928 # Name1 Assign the memory address to Name2 ----------------------------------------- - name1:new - Name2:wupeiqi inNAME1 ID is9362712 # name1 pointing to the new memory address -Name2 ID is10728928 # name2 No change

003-python base-variable

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.