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 is the special data type of python day3

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about the special data types of python day3. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Collection: list

Declaration method: a = [1Magne2pr 3P4] (it can also be a string, floating point, etc.)

Access the collection by index:

You can also access the collection in reverse order:

Add a new element:

There are two ways: 1.insert (index number, values) inserts the new element into the specified location

2.append (values) appends directly to the last element of the index

Delete element: pop (index number)

Tuple: tuple, elements immutable, disordered output

Declaration: t = ('Adam',' Lisa', 'Bart')

Create a single element tuple:t= ((a))

Although the elements of the tuple are immutable and the output is disordered, we can change the array in it.

A = ['Bart','Jack']

T = ('Adam',' Lisa', ['Bart','Jack']) at this point we can change the array a

By changing the value of the tuple t in this way

Dictionary:

Declaration of the dictionary:

S = {'Adam':1

'Lisa':2

'Bart':3

'Paul':4} (key:value

Key:value

Key:value)

Access to dictionary elements: s [key]

For exp:print s ['Adam'] result: 1

Access the value of dict through key. As long as key exists, dict returns

Reply to the corresponding value. If key does not exist, it will directly report an error: KeyError.

There are two ways to avoid KeyError:

First, determine whether key exists or not, using the in operator:

If 'Paul' in d:

Print d ['Paul']

Another access method: s.getkey. This method will not report an error with or without the key.

Updates to the dictionary:

New elements can be added to the dictionary directly in the form of s [key] = value. If the key already exists, the original value will be replaced.

The traversal of the dictionary:

S = {'Adam':1

'Lisa':2

'Bart':3

'Paul':4}

For i in s:

Print i

Collection:

Declaration of the collection: a=set ([1, 2, 3, 4, 5])

Add elements to the collection:

A.add (6)

Delete the elements of the collection:

A.remove (2)

Traversal of the collection:

A=set ([1, 2, 3, 4, 5])

For i in a:

Print i

This is what the special data type of python day3 shared by the editor is like. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow 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