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

How to assign variables in Python

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

Share

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

This article mainly explains "how to assign values to variables in Python". The explanation in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "how to assign values to variables in Python" together.

1 Python variables overview

Variable is called variable in English. As defined in Introduction to Computer Science,"High-level programming languages allow descriptive names to point to locations in main memory instead of numerical addresses, and such names are called variables. It is named this way because, as the program executes, changing the value stored in this location changes the value associated with the name change. "

Formally, each variable has a unique name.

#Example: a is variable, 1 is value. a = 12 Python variable naming

Variable names are a type of identifier that must conform to Python's naming conventions for identifiers.

What is an identifier?

An identifier is a name, and its primary function is to be the name of variables, functions, classes, modules, and other objects.

Naming rules for identifiers:

Identifiers consist of letters (A-Z and a-z), underscores, and numbers.

The first character of an identifier cannot be a number.

Identifiers cannot be the same as Python keywords.

There is no limit to the length of the identifier, but case sensitivity.

Note:

1. Identifiers that begin with underscores have special meanings and should be avoided unless required by a particular scenario.

2. The identifier may be a Chinese character. However, we should avoid using Chinese characters as identifiers as much as possible.

3 Python variable assignment 3.1 Python assignment overview

Python variable assignment refers to assigning a value to a variable (or, more precisely, storing it in the storage area identified by the variable).

Variables do not need to be declared in Python. Each variable must be assigned a value before it can be used, and the variable cannot be created until it is assigned a value.

3.2 Basic format of Python variables

Basic format: variable = value

count = 100 #integer variable miles = 12.5 #floating-point variable name = 'Zhang san' #string print(count, miles, name)

Run Results:

100 12.5 Zhang san

3.3 Other assignment formats for Python variables 3.3.1 Assign the same value to multiple variables simultaneously

Format: var_1 = var_2 = var_3 = value

num1 = num2 = num3 = 12print(num1)print(num2)print(num3)

Run Results:

12

12

12

3.3.2 Assign Different Values to Multiple Variables Simultaneously

Format: var_1, var_2, var_3 = value_1, value_2, value_3

num1 = num2 = num3 = 12print(num1)print(num2)print(num3)

Run Results:

1

2

3

4 Exchange of Python variable values

Examples:

num = 20name = 'Tom'num, name = name, numprint(num)print(name)

Run Results:

Tom

20

5 View variable data types

In Python, a variable is simply a variable, it has no type, and what we call "type" is the type of the object in memory to which the variable refers.

5.1 View variable data types

Use Python's built-in function type(). The type() function returns the type of the object.

count = 100 #integer variable miles = 12.5 #floating-point variable name = 'Zhang san' #String print(type(count))print(type(miles))print(type(name))

Run Results:

\

5.2 Get ID of variable in memory

Use Python's built-in function id(). The id() function returns the id of the object.

count = 100 #integer variable miles = 12.5 #floating-point variable name = 'Zhang san' #string print(id(count))print(id(miles))print(id(name))

Run Results:

1724240694608

1724241729392

1724242062320

Reference:

Variables in Python

Thank you for reading, the above is the content of "how to assign variables in Python". After studying this article, I believe everyone has a deeper understanding of how to assign variables in Python. The specific use situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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

Development

Wechat

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

12
Report