In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to define and use Python variables, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Preface
For any programming language, there is data processing, such as numbers, strings, characters, etc., we can use the data directly, at the same time, we can also save the data to variables for later use. What is a variable? It is the same in any language, and it can be thought of as a small container for "loading" the data in the program. Each container is a unique name, and we use the variable name to find the data in the variable.
Variables correspond to constants, which are small containers used to "hold" data. The difference is that the data stored by variables can be modified many times, while constants cannot be modified once a certain data is saved.
Variable assignment in Python
The process of putting data into a small container (variable) is called assignment. Python uses the equal sign = as the assignment operator in the format:
Name = value
Parameter description:
Name represents the variable name
Value represents the value, that is, the data to be stored.
Note: variable is a kind of identifier, its name can not be chosen casually, it should follow the Python identity naming convention, and avoid duplicates with Python built-in functions and Python reserved words.
For example, the following statement assigns the integer 10 to the variable n:
N = 10
From then on, n represents the integer 10, and using n means using 10.
Because the variable is not constant, the variable is just a container, just a box, we can put the data into the box, that is, assign a value to the variable, but the box can only hold one, so the data will change after each assignment. That is, the last assigned data!
The use of Python variables
When using Python variables, all you need to know is the name of the variable. Variables can be used almost anywhere in Python code, as shown in the following example:
> n = 10
> print (n) # pass variables to the function
ten
> m = n * 10 + 5 # use variables as part of four operations
> print (m)
one hundred and five
>
seventy-five
> m = m * 2 # double the value of the variable itself
> print (m)
two hundred and ten
> url = "http://www.itcdns.cn/""
Str = ":" + url # string concatenation
> print (str)
Http://www.itcdns.cnPython is a weakly typed language
In strongly typed programming languages, the type of variables should be specified when defining variables, and the assigned data must be of the same type. C language, C++ and Java are the representatives of strongly typed languages.
Let's take the use of variables in Java:
String name = "Zhang San"
Int age = 22
Strongly typed languages correspond to weakly typed languages, and scripting languages such as Python, JavaScript and PHP are generally weakly typed.
Weakly typed languages have two characteristics:
1. Variables can be assigned directly without declaration, and assigning a value to a variable that does not exist is tantamount to defining a new variable.
two。 The data type of a variable can be changed at any time, for example, the same variable can be assigned to an integer and a string.
Note that a weak type does not mean that there is no type! Weak typing means that you don't have to pay attention to types when writing code, but there are still types within the programming language. We can use the type () built-in function class to detect the type of a variable or expression, for example:
> num = 10
> type (num)
> num = 15.8
> type (num)
> num = 20 + 15j
> type (num)
> > type (315.6)
On how to define and use Python variables to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.