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 python numeric types and placeholders

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use python number types and placeholders. The introduction in the article is very detailed and has certain reference value. Interested friends must read it!

1. Data Type 1. Determination of Data Type

Number => int float complex bool

container => str list tuple set dict

Type of judgment:

isinstance(data, type) => Returns True if this is the type, otherwise returns False

lst = [1,2,3]res = isinstance(lst,list)print(res) #If lst is replaced by int, the output error tup = 1, #If no comma is added, what data is what type, is the element identifier print(isinstance(tup,tuple))

isinstance(data,(type 1, type 2, type 3… ) ) => True if one of the types matches the condition in parentheses, false otherwise

res = isinstance("abc" , (str , list ,tuple) )res = isinstance("abc" , (set , list ,tuple) )print(res)

Number Type:

int:

intvar = 100 int var is an integer variable print(intvar)

type Gets the type of a value:

res = type( intvar )print(res) #Print out class represents a type called int

id Get the address of a value:

res = id(intvar)print(res) #Each time the number runs out is different, this is a set of memory mechanisms

Binary representation of integers:

intvar = 0b101 #101=5 111=7... print( intvar , type( intvar ) , id(intvar) )

int in octal:

intvar = 0o10 #0~7 7+1 = 0o10print( intvar , type( intvar ) , id(intvar) )

int in hexadecimal:

intvar = 0xff #0xf => binary 0000 1111print( intvar , type( intvar ) , id(intvar) )2.float

There are limits on the range and fractional precision, but they can be ignored in conventional calculations.

The range of values is about-10308~10308

There is an uncertain mantissa in floating-point operations, not a bug, and the uncertain mantissa generally occurs around 10^-16,round() is very effective

round(x,d) : round x,d is the number of decimals truncated

floatvar = 3.15print(floatvar , type(floatvar) )

Presentation 2 (scientific notation):

floatvar = 6.7e3 #Move the decimal point backward by 3 digits to get a decimal point, which requires a decimal point at the end of the value.0floatvar = 9e-2 #decimal point moved 2 digits left print(floatvar , type(floatvar))4.3e-3 ->> 0.0043 9.6E5 ->> 960000.03.bool Boolean boolvar = False # False = boolvar = True # True stands for true print( boolvar , type(boolvar) )4.complex Complex type

Structure: real + imaginary

3+4j 3: represents real 4j: represents imaginary

What is J?

If there's a number whose square equals minus one, then that number is j, and scientists think there is j expressing a high-precision type;

complexvar = 3+4jcomplexvar = 5-90jcomplexvar = 5jprint( complexvar , type(complexvar) )complex(real, imaginary)res = complex(3,4)res = complex(5,-90)res = complex(0,5)print(res)5. Escape characters

\ + Character => Syntax

You can make meaningful characters meaningless.

You can make meaningless characters meaningful.

\n : newline

\r\n : New line

\t : Indent (horizontal tab)

\r : Pulls the string\r following directly to the beginning of the current line

Meta string r "string" does not escape character prototype output:

path = r"E:\python35\nat"print(path)6. Format string

%d integer placeholder:

strvar = "Tingting Lu bought %d LV bags" % (5)print(strvar)

%2d occupies 2 digits, if there are not enough two digits, take a space to fill in the digits, and the original string is placed on the right;

strvar = "Tingting Lu bought %2d LV bags" % (5)print(strvar)

%-2d occupies 2 digits, if there are not enough two digits, take spaces to fill in, and the original character string is placed on the left;

strvar = "Tingting Lu bought %-2d LV bags" % (5)print(strvar)

%f Float Placeholder:

%f Floating-point placeholder decimal places 6 decimal places by default

strvar = "Lu Tingting bought LV bag, spent %f yuan" % (5)print(strvar)

%.2f Floating-point placeholder decimal places default to 2 decimal places [rounding]

strvar = "Tingting Lu bought a LV bag and spent %.2f Yuan" % (5.5678)print(strvar)

%s String Placeholder:

strvar = "%s" % ("It's hot today")print(strvar)

Comprehensive case:

strvar = "Lu Tingting bought %d lv bags, spent %.2f yuan, mood %s" % (5,10000.6789,"very cool, anyway not my own money")print(strvar) above is "python number types and placeholders how to use" All the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

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