Python novice tutorial 1, variables

1.1 Create variables

In Python, we use the "=" sign to create variables. On the left of the equal sign is the name of the variable, and on the right of the equal sign is the content of the variable.

For example:

num = 23
name = 'Shawn'

It should be noted that in Python, when creating a variable, you must give the variable a content. If you create a variable like this, the program will report an error.

num =
name = 

1.2 Naming conventions for variables

In Python, the naming of variables must have certain rules:

·Only English letters, mathematics or underscores can appear

·No spaces, Chinese or other symbols

The beginning of the variable name cannot be a number

If you are naming the variable and do not follow this rule, then Python will also report an error.

Programming Challenge Challenge

Which of the following variable names is correct?

A num123 B123num C_num D+Sun Xiaodi

The correct answer will be announced in the next issue

1.3 Scope

In Python, different variables have different scopes of action. Generally speaking, we call the variable defined in this function [local variable] , which can only be used in this function.

For example, the num variable in the following code:

def say():
    num = 10
    print(num)
say()
print(num)

Run this code like this:

Python控制台
10
NameError:name ‘num’ is not defined on line 5
第5行:没有找到这个变量,使用变量前请赋值

We can see that the program will report an error. He told us [This variable was not found]. This is because our num variable was created in a function. When we use it outside of the function, Python will think that this variable does not exist. Will report an error.

The variable defined in the function, we call it [global variable]. We can use the value of this variable in various places in the Python file.

For example, the num variable in the following code:

num = 10
def say():
    print(num)
say()
print(num)

In this code, we created a variable named num. Immediately following the previous piece of code, we created a say function in which the value of the variable num is printed. Then the say function is called, outside the function, the num value is printed again.

The result of running this code is this:

Python控制台
10
10

We can see that the num variable defined outside the function can be printed in various places.

This is the difference between [global variables] and [local variables] .

In actual programming, the following situation occasionally occurs:

num = 10
def say():
    num = 1
    print(num)
say()
print(num)

Run this code, we can find that the final printed result is like this:

Python控制台
1
10

Hey, why the value of num is 1 for one time and 10 for another time?

This is because of the difference between [global variables] and [local variables].

We modify the value of global variables in the say function. But Python is very confused at this time. He will regard the statement num = 1 in the say function as creating a local variable and assigning this variable to 1. Therefore, he will not change the value of the global variable.

To modify the global variable in the function, we need to use the global statement to tell Python that this variable is a global variable. like this:

num =  10
def say():
    global num
    num = 1
    print(num)
say()
print(num)

In the function, the global statement declares num as a global variable, so the final printed result is like this:

Python控制台
1
1

1.4 Operation symbols

In Python, there are many different operation symbols. Here are some commonly used operation symbols and their meanings:

symbol meaning
+ Plus, a+b means a plus b
- Minus, ab means a minus b
* Multiply, a*b means a multiplied by b
/ Divide, a/b means dividing a by b
% Take the modulus and return the remainder of the division
// Divide, return the integer part of the quotient

1.5 small test

a = 10
b = 30
c = a * 2 + b

Run this code, what is c equal to

A 10                B 30                C 50               D 100

The correct answer will be announced in the next issue

Guess you like

Origin blog.csdn.net/m0_52519239/article/details/111410202