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 basic data types in Python?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about the basic data types in Python, which many people may not know very well. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Basic numeric type

First, let's get to know the basic types of numbers:

Integer type

Floating point type

Plural type

Integer type

1 the integer type is actually the same definition as the integer in mathematics, the integer type includes positive integer and negative integer, and there is no range of values. The pow (xPowery), pow () function, which is used to calculate the y-power 2 integer type of x, also has four kinds of binary relations:

Decimal 100 99-125

Binary 0101001101 001110101

Octal starts with 0o or 0O 0o123,-0O456

Hexadecimal starts with 0x or 0X 0x9a

Floating point type

Numbers with decimal points and decimals

The range of values is basically unlimited.

It is important to note that there is an indefinite Mantissa in the operation between floating-point numbers, which is not the bug of the language, but a common occurrence in a lot of programming. For example. 0.1 + 0.3 = 0.4 but 0.1 + 0.2 = 0.30000000004 because of the binary-to-decimal conversion problem: 0.1

0.000110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100

10-160.100000000000055511151231257827021188404541015625 (decimal representation) 0.1 + 0.2 binary denotes decimal, but not exactly the same result infinitely close to 0.3, but there may be Mantissa

Plural type

The type of plural is the same as the definition of plural in mathematics. We can understand it first and explain it in detail later.

Numeric operation operator

In programming, the operation of numbers is very important, and the operation between numbers is also the most common operation, which is a good foundation for mastering the operation of numbers.

X + y x plus y

X-y x subtracts from y

X * y x multiplied by y

X / y x divided by y

The integer quotient of x / y x is divided by y and the integer quotient of x and y is 10. The result is 3.

+ X represents x itself.

-X means negative X

X% y remainder, modular operation 10% 3 is 1

Digital operation correlation function

Abs () absolute value function abs (- 10) = 10

Divmod (XBI y) take remainder quotient function to output two remainder sum quotient divmod (10p3) = (3p1)

Pow (x, y) power function

Round (x [, d]) is rounded, d is the reserved decimal place, the default value is 0 round (- 10.123, 2) the result is-10.12

The maximum function output of max (x1 ~ x2 ~ x ~ n) is one of the largest.

Min (x1 ~ x2 ~ xn) minimum function outputs the smallest one of them.

Int (x) turns X into an integer

Float (x) turns X into a floating point type

Complex (x) makes X plural

Case analysis-the power to make progress every day

You can take a look at the following example: 365 days a year, progress 1 ‰ a day, how much cumulative progress? 1.001365 days a year, a retrogression of 1 per thousand a day, how much is left? 0.999365 so how to solve the problem through the program?

Dayup = pow (1.001565) # 1/1000 progress every day

Daydown = pow (0.999365) # retrogression 1/1000 per day

Print ('upward value: {: .2f}, downward value: {: .2f}' .format (dayup,daydown))

Let's think about another question: how to calculate the strength of 365 days a year, five working days a week, an improvement of 1% a year, two rest days a week, and a retrogression of 1% a day?

Dayup = 1.0 # define an initial progress value

Dayfoctor = 0.01Definitions progress 1% per day

For i in range (365): # establish a for cycle 365 days a year

If I% 7 in [0je 6]: # Modulo operation

Dayup = dayup* (1-dayfoctor)

Else:

Dayup = dayup* (1+dayfoctor)

Print ('Power of working days: {: .2f}' .format (dayup))

(running results) Power of working days: 4.63

For i in range (365):

If I% 7 in [0je 6]:

By analyzing this code, we can see that we can build a for loop. I is the number of days of progress and can cycle automatically for a year. If I% 7 in [0BI 6], modulo I is calculated, if the remainder is 0, it is the weekend, if the remainder is 6, it is Saturday, it is the time of retrogression. Then let's move on to this question: at what level does the workday model have to work as hard as 1% every day? A Jun: 365 days a year, 1% progress every day, non-stop B: 365 days a year, 5 days a week, 2 days off, rest days reduced by 1%, how hard do you have to work to catch up with A?

Def dayup (df): # create function

Dayup = 1. Setting initial value

For i in range (365):

If I% 7 in [0je 6]:

Dayup = dayup*

Else:

Dayup = dayup* (1 + df)

Return dayup

Dayfoctor = 0.01,

While dayup (dayfoctor) < 37.2 # if the growth of a year is less than the progress of A, continue to run.

Dayfoctor + = 0.001

Print ('weekday effort: {: .3f}' .format (dayfoctor))

This code draws lessons from the code of the previous question, but in order to judge whether it has caught up with A, we add a while statement, which is used as a judgment. Through while, we can let the program judge whether it exceeds A, and if it does not exceed, the progress value increases by 0.001. If it exceeds A, it jumps out of the program and ends running.

After reading the above, do you have any further understanding of the basic data types in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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