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

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

Share

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

This article introduces the knowledge of "how to use the basic data types in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Data types in Python

Number (digital)

String (string)

List (list)

Tuple (tuple)

Set (collection)

Dictionary (dictionary)

classification

Immutable data types: Number, String, Tuple

Variable: List, Dictionary, Set

Operation of variables in Python

Python supports multivariable assignment, such as:

A = b = c = 1

Print (a)

Print (b)

Print (c)

At this time, there are three variables: axi1, bread1, cyste1.

A, b, c = 1, 2, 3

Print (a)

Print (b)

Print (c)

At this time, axiom 1 ~ 2 ~ 2 ~ 3

This allows you to exchange the values of two variables, such as:

A = 1

B = 2

A, b = b, a

Print (a)

Print (b)

It is not difficult to find that the values of these two variables have been exchanged.

Other languages can only use the third variable to exchange two variables:

A = 1 # assignment

B = 2 # assignment

C = a # assigns the value of the first variable to the third variable, where c saves 1

A = b # assigns the value of the second variable to the first variable, where a saves 2

B = c # then assign the value of the third variable defined to the second variable, and the 1 saved by c is assigned to b to complete the exchange

Print (a)

Print (b)

View data types

Type (x), such as:

A, b, c = 1, True, 1.0

Print (type (a), type (b), type (c))

# result a, b, c = '1cm,' True', '1.0'

Print (type (a), type (b), type (c))

# result

Number

Temp = '123' # non-numeric string will make an error

Print (type (temp)) #

Res = int (temp)

Print (type (res), res) # 123temp = "b"

Res = int (temp, base=16) # the base can be specified here, and be careful not to go beyond the range of the base. The default is 10.

Print (type (res), res) print (1 = = True) # True

Print (0 = = False) # True

You can hardly use other mathematical operations about the conversion between Number and String. If you want to know about it, please try it yourself. The contents are as follows:

Abs (x) returns the absolute value of the number

Ceil (x) is rounded backward

Cmp (XBI y) if x

< y 返回-1, 若 x == y 返回0, 若 x >

Y returns 1. Python 3 uses (x > y)-(x0)

Log10 (x) returns the logarithm of x with a base of 10

Max (x1 ~ x2...) Returns the maximum value of a given parameter, which can be a sequence

Min (x1 ~ x2...) Returns the minimum value of a given parameter, which can be a sequence

Modf (x) returns the integer part and the decimal part of x, the numerical symbols of the two parts are the same as x, and the integer part is represented as a floating point.

Pow (xPowery) returns the value after the xresume operation.

Round (x [, n]) returns the value of x rounded, where n is the reserved decimal place

Sqrt (x) returns the square root of the number x

String

Gets the length of the string

Temp = ''

Print (len (temp)) # 14

Slice value

# [start position index: end position index, excluding: step size]

Temp = "abcdefg"

Res1 = temp [:] # Note: indispensable

Print (res1) # abcdefg

Res2 = temp [0:-1:2]

Print (res2) # ace

Formatting of a string

# format one-to-one by default

Temp1 = "my name is {}, and I am {} years old this year!"

Res1 = temp1.format ('salted fish', 22)

Print (res1) # my name is salted fish, and I'm 22 years old!

# formatted by index

Temp2 = "my name is {1}, and I am {0} years old this year!"

Res2 = temp2.format ('salted fish', 22)

Print (res2) # my name is 22, salted fish is old this year!

# key-value pair

Temp3 = "my name is {name}, and I am {age} this year!"

Res3 = temp3.format (age=22,name=' salted fish')

Print (res3) # my name is salted fish, and I'm 22 years old!

Find

# find the first location. Find ("e", [start position], [end position]) Note that the end position is not included.

# find return index, cannot find return-1

Temp = "hahe"

Res = temp.find ("h", 1pm 3)

Print (res)

Lower

# convert all uppercase letters in a string to lowercase letters

Temp = "AAAaaaBBbb"

Res=temp.lower ()

Print (res) # aaaaaabbbb

Upper

# convert all lowercase letters in a string to uppercase letters

Temp = "abc"

Res=temp.upper ()

Print (res) # ABC

Strip

# remove spaces before and after a string (line breaks, tabs, specified parameters)

# remove special symbols such as newline characters, tabs, spaces and so on by default

Temp = "an aa\ n"

Res=temp.strip ()

Print (res) # an aa

Join

# string concatenation

The # join method concatenates each bit of the string with the called string, except for the last bit

Temp1 = 'abcd'

Temp2 ='-'

Res1 = temp2.join (temp1)

Print (res1) # a-b-c-d

# is not a direct concatenation of two strings

Temp3 = 'abc'

Temp4 = '123'

Res3 = temp3+temp4

Print (res3) # abc123

Replace

# replace the specified string replace ('string to be updated', 'updated string', [update several strings, default all, no error is reported if the number of digits exceeds])

Temp = ''

Res = temp.replace ("a", "e", 50)

Print (res) # hehehehe "how to use basic data types in Python" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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