• Skip to main content
  • Skip to primary sidebar
…

...

cuộc sống là phải luôn khám phá.

  • Home
  • Music Box
  • Các trang web hay
  • Giới thiệu
  • Liên kết website
Bạn đang ở đây:Trang chủ / IT / Python Basic: Getting to Python (part 4)

Python Basic: Getting to Python (part 4)

22/10/2021 tác giả: Mạnh Hùng Bình luận >

python

Continue from part 3.

Python Lists

In short, Lists are used to store multiple items in a single variable, they are convenient data structures for representing a sequence of data A sequence type is one that supports the membership operator (in), the size function ( len( ), slices( ) ), and is iterable . In that regard, a list is similar to a string, except a string can hold only characters, however, a list can hold any Python object (can either be the same data type, or varying types).

There are several other ways that Python provides for storing aggregate data: tuples, dictionaries, set. But let’s focus on Lists first.

Lists are created by putting elements, comma-separated, and enclosing by square brackets:

lst = [5, -6, 3, 0, -1]

Lists are pretty important and and widely used, you could see them in nearly every Python program. I’m interested in matrices, linear algebra, matrix transformations and arrays because I’m working with finite element software, so lists, as well as NumPy arrays (I would like to talk about NumPy later too) are an integral part to learn.

A list can also be empty to be put objects in later:

a = [ ]

To access the individual element contained in a list via their position within the list. We can use square brackets like list [index]:

lst = [1, -4, 0, 6, -3] # Assign a list wih objects
lst[0] = 5 # Make the first element 5
print(lst[1]) # Print the second element
lst[4] = 10 # Make the last element 10
print([1, 2, 3][1]) # Print second element of given list
print(lst) # Print a list via a variable

The number within the square brackets is called the index. The subscript terminology is borrowed from mathematicians who use subscripts to reference elements in a mathematical vector or matrix. Unlike the convention often used in mathematics, however, the first element in a list is at position zero, not one. So in the above example, the first element of list lst is lst[0], not lst[1], and lst[1] is second element.

So it can be inferred that, of a zero beginning index, if list a holds n elements, the last element in lst is lst[n-1], not lst[n]

If lst is a list with n elements, and i is an integer such that 0 ≤ i <n, then lst[n] is an element in the list.

There is also negative list index. A negative list index represents a negative offset from an imaginary element one (index 0) past the end of the list. So that means for a list lst, the expression lst[-1] represents the last element in lst.

For the example below, I’ll print a list call data in reverse, to evince the use of negative indices.

def print_some_list():
    data = [10, 20, 30, 40, 50, 60, 70, 80]
    for i in range(len(data) + 1):
        if i == 0:
            continue
        print(data[-i])

print_some_list()

Lists can contain any arbitrary objects, or heterogeneous, a list can hold elements of varying types.

Lists can be nested, and lists are also mutable.

Here’s an example of a convoluted list, but it’s really not that complicated when you already grasp the basic:

lst = [9.5, "apple", 69, "W", ["chocolate", 5, "pie"], 777, "KyoStinV"]
lst = [9.5, "apple", 69, "W", ["chocolate", 5, "pie"], 777, "KyoStinV"]

print(lst[0], lst[-7]) #  lst[0] == lst[-7] = 9.5
print(lst[1], lst[-6]) #  lst[1] == lst[-6] = 'apple'
print(lst[1][2], lst[-6][2]) #  lst[1][2] == lst[-6][2] = 'p'
print(lst[4][2][0], lst[-3][2][0], lst[4][-1][0], lst[-3][-1][-3]) 
#  lst[4][2][0] == lst[-3][2][0] == lst[4][-1][0] == lst[-3][-1][-3] = 'p'

Note that, here the second and third indices come after lst are belong to str element, since string is also a sequence data type.

(To be continued…)

Reader Interactions

Để lại bình luận: Hủy

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Primary Sidebar

Chuyên mục

  • fun
  • IT
  • News
  • testing

python

Python Basic: Getting to Python (part 4)

In short, Lists are used to store multiple items in a single variable, they are convenient data structures for representing a sequence of data. In that regard, a list is similar to a string, except a string can hold only characters, however, a list can hold any Python object.

python

Python Basic: Getting to Python (part 3)

Python Functions: (user-defined or from libraries) function is a block of code which only runs when it is called, to do a complete job (module), is named, and can be called to execute multiple times at many places in the program. What happens if you don’t use function? What I can think about is repetitive works, hard to debug, hard to expand the whole code, so usually when working on a large project you need to split your code into many small modules.

Tạo một website dễ dàng với Hugo

Hôm nay chúng ta cùng thử generate 1 website với Hugo, theo như lời giới thiệu ở trang chủ “The world’s fastest framework for building websites.”

Steam_logo

DMCA.com Protection Status

Copyright ©2022 · pquan.info - All Rights Reserved ·