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

What are the data types in Python

2025-03-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces what data types there are in Python, which can be used for reference by interested friends. I hope you will gain a lot after reading this article. Let's take a look at it.

1. String

(1) concept

The English name of the string is string, or str for short. A string is a combination of characters. The letters, numbers, Chinese characters and symbols you usually see are all characters. Strings can be used to represent words, statements, or even mathematical formulas. To sum up, all single quotation marks, double quotation marks, and three quotation marks are strings, regardless of whether the quotation marks are Chinese, English, French, numbers, symbols, or even Martian.

Code demonstration 1:

Name=' Xiaoming'

Code demo 2:

Print ("hello")

In the above code, both Xiaoming and hello are of string type.

(2) string concatenation

The simple method of string concatenation is to use the string concatenation symbol + to concatenate the variables that need to be concatenated. It should be noted that only strings and strings can be concatenated. For example:

2. Integer

Integer is integer in English, or int for short. Integers are positive, zero, and negative integers. To put it bluntly, there is no decimal point. And without quotation marks, print directly with the print () function. Code demonstration:

Note: no other words, letters or symbols can appear in integers!

3. Floating point number

(1) concept

Floating point number, called float in English. In Python, pure numbers with decimal points are defined as floating-point numbers. Floating point is one of the common data types we learn from. To put it simply, a floating point is one more decimal point than an integer. For example, 3.1415926, 95.27, 999.00 and so on.

(2) characteristics

Although a floating point number has one more decimal point than an integer, it has a bug. Because of the characteristics of floating-point numbers, when it is calculated in the computer, the result is not so accurate, there will be a rounding difference. For example:

Why is it that instead of 0.46, it prints a number smaller than 0.46? This is because when Python calculates floating point numbers, it converts 0.35 and 0.11 into binary numbers. Binary numbers are represented by 0 and 1 and enter 1 every 2.

(3) binary conversion

0.35 (decimal) = 0.010110011001100110011001100110011001100110011001100111 (binary)

0.11 (decimal) = 0.000111000010100011110101100010100011110101110000101001 (binary)

In this decimal conversion process, there is an error, which results in the error between us and the result. Then, after the two binary numbers are added, the resulting binary result is converted into a decimal.

4. Query of data type-type () function

Just put the contents of the query in parentheses and you can use the type () function. But for Python, you only issued a query type command, and the type () function has been executed, so if you want to display the query results on the terminal, you also need to complete the code and put the query results of the type () function into print (), for example print (type (name)). For example:

5. Data conversion

(1) Str () function

No matter what type you used to be, the Str () function converts any data to a string type as long as it is in parentheses. Alternatively, you can use quotation marks to convert strings directly without the Str () function.

(2) int () function

But with regard to the use of the int () function, you can use the int () function to cast only if the contents of the string are pure numbers. Int () cannot be used for conversion of text, English, or Martian; strings in the form of floating-point numbers cannot be cast using int (), but floating-point numbers can be converted using the int () function. The essence of the int () function is to convert data to integers. For floating-point numbers, it is rounded, that is, only the integer part is taken away, and no rounding operation is done.

(3) float () function

The use of the float () function is consistent with the methods of str () and int (), which also put the data to be converted in parentheses. The float () function can convert integers and strings to floating-point types, but only if the contents of the string must be numeric.

Thank you for reading this article carefully. I hope the article "what are the data types in Python" shared by the editor will be helpful to you? at the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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