Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python Minimalist course (4) variables and constants

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Variables and constants

There is no distinction between constants and variables in Python. There is only an agreed practice:

The name of all uppercase letters is constant:

PI = 3.1415926

The names of all lowercase letters are variables:

Name = 'nemo'

Both variables and constants are used to store the values needed during the running of the program.

Variables change during operation and are used to store temporary values, while constants are used to store fixed values during operation. In general, constants are placed at the top and are used globally.

However, it's just a contract. Python has no grammatical requirement, so its real constant can also be changed, but generally speaking, we don't do that. Assignment

We use the equal sign to assign a value to a variable. What is an assignment?

Note that the statement that an equals 1 does not mean that an equals 1.

Instead, it allocates a piece of space in memory to store the integer 1, and then creates a variable named an in memory and points the a variable to the memory address where 1 is stored.

It doesn't matter if you can't understand this sentence, as long as you know that after the statement a = 1 is run, an is a variable, and the value it represents is 1.

Of course, an as a variable, its value will change with the re-assignment.

> a = 1 > > A1 > > a = 'nemo' > axinemo'

In the above code, the value of the a variable has changed from 1 to 'nemo'.

The value of the a variable changed from 1 to 'nemo', actually allocates a new piece of space in memory to store the string nemo, and then changes the point of a from 1 to the memory address of nemo. The original 1 does not have any variables pointing to it, so what is waiting for it will be automatically cleared by the garbage cleanup mechanism.

In Python, you don't have to specify the variable type when you declare it, or even declare it, you can just use it.

But occasionally we also need to specify the variable type, so we can first assign an initial value to the variable:

> num = 0 > char =''

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report