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 tuples, lists, deconstructions, and loops

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

Share

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

This article mainly explains how to use Python tuples, lists, deconstruction and loops. The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's ideas to study and learn how to use Python tuples, lists, deconstruction and loops.

List

A list is a collection of items. Lists are mutable: you can change their elements and sizes. Therefore, they are similar to an ArrayList in ListC#, an in Java, and arrays in JavaScript.

You can assign a list like this, and then access its elements through a zero-based index:

Foo = [1,2, True, "mixing types is fine"] print (foo [0]) # 1foo [0] = 3print (foo [0]) # 3

The append method adds an element to the end of the list. The insert method places the element at the index you specify:

Foo = [1,2,3] foo.append (4) print (foo) # [1,2,3,4] foo.insert (0,0.5) print (foo) # [0.5,1,2,3,4]

To delete an element at the index, use the del keyword:

Del foo [2] print (foo) # [0.5,1,3,4] tuple

A tuple is another type of project collection. Tuples are similar to lists, but they are immutable. A tuple is assigned as follows:

Foo = 1,2, True, "you can mix types, like in lists"

You will often see tuples in the format (1, 2, "a") with parentheses. Parentheses around tuple values are used to help improve readability or depending on the context. For example, 1,2 + 3,4 means different from (1,2) + (3,4)! The first expression returns a tuple (1, 5, 4) and the second returns (1, 2, 3, 4).

Work from the list, foo [index], in the same way as getting the value of a tuple, representing the zero-based index of the element in index. If you try to change one of the elements, you can see that the tuple is immutable:

Foo [0] = 3 # will raise a TypeError

This applies to lists, but not to tuples.

Tuples also have no append, remove, and other methods.

You can also return tuples from a function, which is a common practice:

Def your_function (): return 1,2

This returns a tuple (1, 2).

If you want a tuple with only one element, place a comma after the element:

Foo = 1, negative exponent and slice

Python's index is more powerful than I've demonstrated so far. They provide some features that do not exist in C#, Java, and so on. One example is a negative index, where-1 refers to the last element,-2 refers to the penultimate element, and so on.

My_list = [1,2,3] print (my_list [- 1]) # 3

This applies to lists and tuples.

In addition, you can get a slice of a list or tuple by specifying the start, end, or index of the start and end elements of the slice. This produces a new list or tuple that contains a subset of elements. Here are a few examples to demonstrate:

My_list = [0,1,2,3,4,5] print (my_list [1:2]) # [1,2] print (my_ List1 [2:]) # [2,3,4,5] print (my_list [: 2]) # [0,1] print (my_ List1 [0: 4:2]) # [0,2] print (my_list [- 3 my_ List1]) # [3 4] print (my_list [::-1]) # [5,4,3,2,1,0]

The slice symbol is [start:stop:step]. If start remains empty, the default is 0. If the end remains empty, the list ends. The: step symbol is optional. So::-1 means "from 0 to the end of the list, step-1", which returns the reverse list.

Slicing never improves IndexErrors. When out of range, they only return an empty list.

Deconstruction

Suppose you have a tuple (or list) that contains a known number of elements, such as three. And suppose you have three different variables, one for each tuple element.

Python provides a feature called deconstructing (or unpacking), which can decompose collections with one line:

My_tuple = 1,2,3a, b, c = my_tuple

Now a = 1, b = 2, and c = 3.

My_list = [1,2,3] a, b, c = my_list

This is useful when dealing with functions that return tuples, and there are many such functions in the Python ecosystem, as well as when dealing with AI-related libraries.

Cycle

You may be familiar with three loops: for, foreach, and while. Python only provides while and foreach loops (it uses the for keyword! ). But don't worry. As we'll see later, it's easy to create a loop that behaves exactly like a for loop.

This is an iterative list of Python loops:

Fruits = ["Apple", "Banana", "Pear"] for fruit in fruits: print (fruit)

You can also traverse the tuples:

Fruits = "Apple", "Banana", "Pear" for fruit in fruits: print (fruit)

Typically, you can use for loops on each iterator. Iterators and how to create your own iterators will be discussed in more depth in a later article.

If you want a C-style for loop instead of a foreach loop, you can iterate through the result of the range function, which returns a range of iterators:

For i in range (10): print (I)

The final printed number is 9. This is equivalent to the following C code snippet:

For (int I = 0; I < 10; iTunes +) {Console.WriteLine (I);}

This range function provides more than a count from zero to a given number. You can use range (x, 10), where xwill be the first array element, to specify a different starting number. You can use the third parameter to specify the step size, such as range (0,10,2).

From high to low is the creation range: range (10, 0,-1). The first element will now be 10 and the last one will be 1. In fact, range (0,10) is not the opposite of range (10,0,-1), because the second parameter will not be included in the range.

The while loop in Python looks very similar to what you already know:

While condition: # code

Python also provides break and continue statements that work exactly the same way as statements in C #, Java, JavaScript, and many other languages.

While True: if input () = = "hello": break thank you for reading, the above is the "Python tuple, list, deconstruction and loop how to use" the content, after the study of this article, I believe you on the Python tuple, list, deconstruction and loop how to use this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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