Dry goods "variables based on python"

There is not much content today, pure dry goods without moisture

Let's understand the variables of the python foundation together :
 

 

Variable definitions

A variable is understood literally as a variable that can be assigned to a different value, and there will be a different amount; every time a variable is created, a space is opened in the computer memory to store the value assigned within the specified range.

message = "Hello world"

The message is a variable, and the Hello world in quotes is the value of the variable message

Variable naming convention

Variables are not named arbitrarily in python. The following rules must be followed:
1. Variable names can only contain letters, numbers, and underscores. Note that the variable name can start with a letter or underscore, but cannot start with a number, otherwise an error will be reported
>>> 1_message ='Hello world'

SyntaxError: invalid token

>>> message_1 = 'Hello world'

>>> print(message_1)

Hello world

2. Variable names cannot contain spaces and can be separated by underscores.

>>> message 1 = 'Hello world'

SyntaxError: invalid syntax

Should be set to message_1

3. System keywords cannot be used as variable names. For example, some built-in function names or special-purpose words, such as print, otherwise an error will be reported
>>> print ='Hello world'

>>> print(print)

Traceback (most recent call last):

  File "<pyshell#8>", line 1, in <module>

    print(print)

TypeError: 'str' object is not callable

4. In python, variables are case sensitive. For example, Message and message are not equal, but two different variables. In the actual programming process, lowercase letters should be used as much as possible to avoid unnecessary errors.

5. When naming variables, try to be concise and descriptive. student_num is more understandable than s_n.

Variable assignment

In python, there is no need to declare the type of assignment when assigning a variable
>>> message_1 = 123 #integer number

>>> type(message_1)

<class 'int'>

>>> message_2 ='Hello' #string

>>> type(message_2)

<class 'str'>

>>> message_3 = [1,2,3,4] #List

>>> type(message_3)

<class 'list'>

The above are different types of data assignments, as well as data types such as floating-point numbers, tuples, and dictionaries, which will be discussed later.

You have also seen in the above code that the equal sign "=" is an operator for assignment, used to assign a value to a variable, the right side is the value to be assigned, and the left side is the variable name.

Each variable must be assigned before it is used, and the variable will be created after the variable is assigned

>>> message

Traceback (most recent call last):

  File "<pyshell#35>", line 1, in <module>

    message

NameError: name 'message' is not defined

Otherwise, an error will be reported, indicating that it is undefined.

Let’s talk about multiple variable assignments

In Python, users are allowed to assign values ​​to multiple variables at the same time:

>>> a = b = c = 2

>>> a + b

4

>>> a, b, c = 1,2,3

>>> a + c

4

It’s not as good as a practice, so hurry up and tap it.

Guess you like

Origin blog.csdn.net/lyw5200/article/details/108437339