Python Basics variables

Source: Internet
Author: User

First, what is a variable

Variables are one of the language features of all programming languages, so it's important to understand variables when you want to learn programming. So what is a variable? From the name of the main divided into two parts of change and quantity, change is changed, change, quantity is value, data, variables in programming language is used to store the abstract concept of data, its value can be changed.

Second, the role of variables

So what does the variable do? Look at the following scenario, and you're going to figure out what the month is, and the total expenses, the ledger information is as follows

Eat

Buy clothes

Traffic

Entertainment consumption

June 1

35

236

10

200

June 2

30

120

20

147

June 3

150

15

223

June 4

45

22

187

.................

Total

265

356

186

1200

The expenses are settled as follows:

Dinner: 35+30+150+45+...=265

Buy Clothes: 236+120=356

Access: 10+20+15+22+...=186

Entertainment consumption: 200+147+223+187+...=1200

Total consumption: 265+356+186+1200=2007

Find out, we calculate the total consumption is the result of the calculation of the various expenses, but we can not know in the process to calculate the amount of expenditure in advance, how to do it. It's time for our variables to come out, and we just need to store the results of each expenditure in different variables, and when we calculate the total consumption, the values of the various expenses are summed up to get the total consumption. From here we can see that the function of the variable is to temporarily store the data in memory, in case the subsequent code continues to call.

Iii. How to define and use variables

We know what variables are and what variables are, and how to define variables and use them. Variable definitions have three elements: variable name, assignment symbol, and variable value.

1. Variable name

The variable name is also called an identifier, and the variable name defines the following three rules:

1) Variable names can only use letters, numbers, underscores

2) The first character of a variable name cannot be a number

3) The Python keyword cannot be used as a variable name, such as [' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' EXEC ' ', ' finally ', ' for ', ' from ', ' global ', ' if ', ' Import ', ' on ', ' is ', ' lambda ', ' no ', ' or ', ' pass ', ' print ', ' raise ', ' retur n ', ' Try ', ' while ', ' with ', ' yield ']

The name of the variable is written in the following two specifications:

1) variable names for multiple word connections should be written in one of the following two formats

1.1 Ageofoldboy The first letter of each word

1.2 Age_of_oldboy Each word is connected by an underscore, which is also the official recommended way of writing

2) The writing of constants

There is no concept of constants in Python, and it is generally customary to capitalize the variable names all to indicate that the variable is constant, but this is only a hint, such as oldboy_age=84

2. Assignment symbols

The assignment symbol will be described in detail in the Python basics operator, where for the moment only the simplest assignment symbol is "=", such as a=2, which assigns the variable value 2 to the variable a,b=a represents the value of variable A to variable B.

3. Variable Value

The value of the variable is the data we want to store, it can be any form of data, numbers, strings, lists, Ganso, dictionaries, collections .... All can

We define how variables are used. It's simple, just use the variable name to call the value of the variable it stores, see the following code:

Name= ' Alex '

Print (name)

The above code we define a variable name and assign the string Alex to it, and then use the variable name variable value to output with the print function.

Iv. Python garbage Collection

Programmers often need to write code to delete unused variables, to free up space, or else as the program runs memory will be full and then report memory overflow error. Fortunately, Python programmers do not need to do this because the Python garbage collection mechanism automatically removes those unused variables to free up memory space. So how does python know which variables are not used? This is actually done by a thing called reference counting in Python, what is the reference count, to illustrate the reference count we need to say something about Python's assignment in particular.

The variable value assigned to a variable in Python is actually a space in memory to store the value of the variable in the reference relationship of the memory space where the variable name is stored and the value of the variable, just like a room tag, let others know how to find this room, and will add a reference count, The reference count responds to the room's number. So how to increase the reference count and how to reduce the reference count? The way to increase the reference count is to define a new variable and assign a variable to another variable, or assign the variable to a different variable, such as A=1, B=a, c=b, which will increase the reference count of the memory space where the value of the variable is 1, and reduce the reference count by deleting the variable and assigning the variable a value. When the reference count is reduced to 0 o'clock, Python will assume that the data stored in this memory is useless and will then empty the memory.

Python Basics variables

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.