Recommended white Moon Black feather python online tutorial
White Moon Black Plume station in the beginner's perspective for everyone to arrange a Python learning tutorial, to help you quickly master the program development skills.
http://www.python3.vip/doc/tutorial/python/home/
Variables and comments in this document directory
- The concept of variables
- Naming rules for variables
- Changes in variable values
We already know that in the Python language, all data types are objects .
Integers, decimals are objects,
The strings to be learned later, tuples, lists, functions, modules, classes, and class instances are objects.
An object can be considered a generic term for all Python data types.
The concept of variables
Let's consider a question:
What do we say about a person or an object in the language of our people?
For example, when chatting, mention one of your classmates, mention a team
Yes, with their names, Prof. Xiaogang Wang classmates, Lakers, etc.
Let's say we don't allow the use of the names of things when we speak, and we can't talk about them.
Human language is so, computer language is the same, if the data object, but there is no name, like this
43‘你好,今天天气真不错‘
The code behind us, how to use this number 43 and the string ' Hello, the weather is really good today '?
So the data object of the programming language also needs a name.
The name of the object in the Python language is the variable .
We can give names to data objects like this.
age = 43weather = ‘你好,今天天气真不错‘
An equal sign between the defined variable name and the data object
Where age is the name of the Number object 43, we can also say: The value of the variable age is a numeric object 43
The following statement, commonly called, defines a variable with an age value of 43, or a variable age of 43.
age = 43
Weather is the string object ' Hello, it's a nice day ' name, we can also say: The value of variable weather is a string object ' Hello, it's a nice day '
Later, we use these two objects again, we can use their name, variable name.
When the code executes, the interpreter sees the variable name to know that it represents the corresponding data object.
Like what
print(age)print(weather)
As you run, you can see that you can print out the contents of the object as well.
Naming rules for variables
Variable names are not random, there are certain rules.
It usually starts with a letter and cannot start with a number, like a 1var.
You can use numbers or underscores in the middle of a variable name, as follows.
Hello, Var1, Age2name, Age_name
A good engineer, variable names are not random, and variable names can often represent what they mean to the data object.
Look at this code later, according to the name to know what it means
such as StartTime, your_name, such a variable name, read it and know what it means
A variable name like a b c is not good because it doesn't know what it means.
Note that the case of variable names cannot be changed. For example, StartTime and startTime are two different variable names.
Changes in variable values
Why is the name of the object called a variable? Because it will change:)
In the Python language, the value of a variable can change
It is particularly important to note that there are two scenarios for this change:
First: The variable points back to the new object
is to re-assign the value, like
age = 43 age = ‘hello‘
When the second statement is executed, the variable age is not the name of the Number object 43, but the name of the string object ' Hello '.
The assignment of variables has some special wording.
var = 1 var = var + 1 # 把var 的值加1 后,再赋给var, var 就变成了 2 var += 1 # 等价于 var = var + 1 ,执行后var 就变成了 3
The second type of variable points to the object itself has changed
This situation requires that the object that the variable points to is a mutable data type. For example: A list, a dictionary, or a custom class instance object.
These kinds of things we will learn later. Here, let's give you an example of a Dictionary object.
info = {‘name‘ :‘黑羽白月‘, ‘height‘:‘180cm‘} info[‘height‘] = ‘175cm‘ print(info)
The second statement is that the value of the Dictionary object corresponding to the variable has changed.
Comments
Although the Python language is a programming language that is easy to understand and read.
But not human language after all.
We write code in order to later people can read, and even later can look back, can read, you need to join some of our familiar words, that is, human language, auxiliary understanding.
These auxiliary understandings are comments.
These words can be any language, as long as it is convenient for you to read the code on the line, Chinese, English, French can be.
Python comments begin with # , followed by comments
Like what
# info 变量 记录 作者的信息,包括 名字,身高,体重info = {‘name‘ :‘黑羽白月‘, ‘height‘:‘180cm‘}# 改变 作者的身高记录info[‘height‘] = ‘175cm‘print(info) # 打印身高到屏幕上
Well, with the comments above, the code is easier to read.
It is important to note that the comments are usually not affected by the execution of the code (except for some very special comments, such as the file encoding comments at the beginning of the code, which will be spoken later)
Comments can be a single line, such as:
# info 变量 记录 作者的信息,包括 名字,身高,体重info = {‘name‘ :‘黑羽白月‘, ‘height‘:‘180cm‘}
It can also be followed by the code, such as:
print(info) # 打印身高到屏幕上
You can also have multiple lines of comments in succession, such as:
# info 变量 记录 作者的信息# 包括 名字,身高,体重info = {‘name‘ :‘黑羽白月‘, ‘height‘:‘180cm‘}
White Moon Black Feather python online tutorial