Basics of Python variables

Introductory tutorials, case source code, learning materials, readership

Please visit:  python666.cn

Hello everyone, welcome to Crossin's programming classroom!

1. What is a variable

The so-called "variable" is the quantity that can change. (This explanation is really listening to what you say, like listening to what you say)

This concept comes from mathematics and is used in the field of programming to refer to an abstract concept in computer language that can store calculation results or represent values.

Variables can be accessed by variable name. Last time we talked about the naming rules of variable names in Python ( can Python use Chinese variable names? ), today we will talk more about variables.

2. Why there are variables

A classmate who just learned Python on the first day asked:

I don’t understand why you need to use variables, just write the value you need directly, why bother?

If our programs just output "Hello World" or do simple arithmetic operations, of course we don't need variables.

But the actual program is not so simple.

In the code, all the symbols, numbers, letters, text, etc. we see are collectively called data. Computer programs are composed of these data according to certain grammatical rules.

There will be various data in a real program. These data cannot be fixed and will need to be calculated and processed. In order to use these data more conveniently, we need to name these data. To put it simply, a variable is the name of the data, used to access the data, and distinguish different data through different variable names.

For example, a character in a game, its position, its level, its attack power, defense power, current blood volume, etc., these data need to be managed by variables. One radish and one hole, and a value must be associated with a variable, so that the program can handle it.

3. Use of variables

The use of variables is nothing more than two situations, one is to store the value, and the other is to extract the stored value.

In the Python language, variables need to follow the principle of "create first, use later".

To create a variable in Python, use the equal sign (=) to assign a value to a variable name. like:

a = 1

This creates a variable a with a value of 1.

Then you can use this variable. like:

print(a)

Or use it to create another variable. like:

b = a + 2

Since it is said that it is a "variable" quantity, it is natural to reassign the variable. like:

a = 3

In this way, the value of a is changed to 3.

If you directly use a variable name that has not been assigned a value, a NameError error that the name is not defined will be raised, causing the program to stop. like:

print(x)

Will report an error:

NameError: name 'x' is not defined

4. Types of variables

The data in the program is varied, including numbers, text, and complex composite structures. In order to facilitate management, corresponding data types are required.

Python is a dynamically typed language. Unlike static programming languages ​​such as C and C++, it is not necessary to specify the variable type when creating a variable. Instead, it is determined according to the type it is assigned to and is mutable.

for example:

a = 1
print(type(a))

Python's built-in type function can check the type of a variable. Here you can see that the output is <class 'int'>, which is an integer type.

If you add after these two sentences:

a = 'a'
print(type(a))

The program will not report an error, and output <class 'str'>, that is, the variable a has been modified to a string type.

I will talk about more about the type later, so I won’t go into it today.

5. Newbie questions

If you ask me, what is the most common variable problem that novices encounter? The results may surprise you.

Because in my experience, a pit that many beginners will step on is:

Misspelled variable name!

For example, a variable called name was assigned earlier, but after a few lines, it was written as mame. So I got a NameError like this:

1f9e340768a28b9d98599afc730abbfb.png

So, if you see an error saying that a variable of yours is not defined inexplicably, then look for spelling mistakes in all the places used.

Of course, at this time, the role of the IDE is reflected: a qualified IDE will draw a wavy line on your misspelled variable name.

b21a43a9b01e6a54d107ba51f7df0c9a.png

What else do you want to know about variables in Python? Welcome to discuss in the message area.


The following is the video time, welcome everyone to pay attention, like, and forward:


Crossin's new book " Action on Code: Learning PYTHON Programming from Zero Basics (CHATGPT Edition) " is now on the market.

6023a6bd31f57b9e2465e177c0305cbc.jpeg

This book strives to be easy to understand, so that zero-based "novice" who has no programming experience at all can learn Python. The content starts from the most basic steps of environment construction, and gradually goes deep into common practical applications. While explaining the knowledge points, it is equipped with corresponding code examples, so that readers can learn and practice to deepen their understanding. The book also innovatively uses ChatGPT as an aid to programming learning, leading readers to explore a new mode of learning programming in the AI ​​era.

Readers and friends of the official account can contact me in the background after purchase and join the reader exchange group. Crossin will open the accompanying reading mode for you and answer all your questions when reading this book.

Thank you for retweeting and liking ~


_Previous article recommendation_

Can Python use Chinese variable names?


If you want to learn about paid quality courses and teaching Q&A services

Please reply in Crossin's programming classroom : 666

1640f2c2a4c3f622adad730b0a3d1129.jpeg

Guess you like

Origin blog.csdn.net/qq_40523737/article/details/131672149