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 use variables and data types in python

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail how to use variables and data types in python. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Python variable

1. What is a variable?

A variable is a name that refers to a specific numerical value. In other words, when you create a variable, you will open up a space in the computer's memory to store the data you want to save. When referencing this data, you can directly use the variable you created to represent the data you saved. Let's take an inappropriate example: suppose you have a variable x with a value of 48. This is shown in the following figure.

2. Why should variables be used?

① facilitates code maintenance (because it is convenient to use in-memory data). ② saves disk space and memory space (because of the pointing relationship, you only need to put one copy in memory).

3. How to define variables?

① directly uses variable name = value, eg.:a=1

② assigns variable name 1, variable name 2 = numeric 1, numeric value 2 eg.:a,b=2,3

③ and equal assigned variable name 1 = variable name 2 = numeric eg.:a=b=4

4. Points for attention in the use of variables

① A variable intelligently references a value. ② must conform to the specification. ③ must be assigned before you use it.

5. Comparison with C++ and Java

① Python is a dynamically typed variable that does not require a type declaration when assigning values to variables.

② Python variable is different from clocked variables, when a variable is assigned, the compiler opens up a space for the value of the value, and the variable points to this space. When the variable changes its value, it does not change the value stored in this space, but changes the space that the variable points to, so that the variable points to another space. As you can see from the following figure, when the value x is assigned to y, the numerical identification number of the y variable becomes the numerical identification number of the variable x, where the function id () represents the unique identification number of the memory data.

Python data type

1. Why should data types be distinguished?

① distinguishes storage space because different data types take up different space in memory.

② can do different operations according to different data types. Eg.: if it is the addition of integers. Then the output from print (6: 6) is: 12; if it's the addition of a string. Then the output of print ("4" + "6") is: 46

2. Commonly used data types

Python3 has six standard data types, which are divided into mutable data and immutable data.

① immutable data (3): Number (number), String (string), Tuple (tuple)

② variable data (3): List (list), Dictionary (dictionary), Set (collection).

For these six data types.

Currently, there are only two data types that need to be understood: Number (number) and String (string). The characteristics of each data type will be analyzed and summarized separately.

3. Number (number) type, String (string) type and type conversion

Number (digital) type: mainly supports int, float, bool, complex (plural).

String (string) type: strings are mainly enclosed in single quotation marks or double quotation marks, with backslashes\ escaping special characters.

Data type query: mainly through the type () function to query. If you want to know the data types of 6, and "6". You can inquire in the following ways.

4. Data type conversion

Sometimes, you need to convert the built-in types of data, for example, you will encounter num=3+ "6" when using python. If you want to get a num result of 9, running the program directly will definitely report an error, as follows:

In this case, you need to convert the data type, and the conversion method is very simple, you only need to use the data type as the function name, such as int (), float (), str (), chr (), and so on. For the above case, you only need to write print (3 + "6") as print (3+int ("6")) to get the result you want.

This is the end of this article on "how to use variables and data types in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please 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.

Share To

Internet Technology

Wechat

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

12
Report