Python list learning (1)

Why do I need a list?

The list is equivalent to a container. It can be a series of related values ​​stored in this container. For example, now you want to store the type of fruit, then before if by way of a fruit with a variable storage, the code will become very bad. Proper storage would be to define a variable of type list, and then put the names of all these fruits are stored in this list.

 

How to define the list ###: Use in the form of two left and right brackets in the array corresponds to a list of other programming languages 

# Fruits = [ 'Apple', 'Banana', 'Orange']
#
# = Apple Fruits [0]
# Banana Fruits = [. 1]
# = Orange Fruits [2]
#
# Print (Apple)
# Print (Banana)
# Print (Orange)

## through a list of
## 1.for version traversal cycle

# fruits = [ 'apple', ' Banana ',' Orange ']
# Print (type (Fruits))
# for Fruit in Fruits:
# Print (Fruit)

## 2.while version traversal cycle
# index = 0
# Fruits = [' Apple ',' Banana ' , 'Orange']
# len = length (Fruits)
# the while index <length:
Fruits Fruit = # [index]
# = index +. 1
# Print (Fruit)

## nested list (multidimensional arrays corresponding to other programming languages): list can store any type of data,
# Of course, including a list of their own type. That is: the list may be stored listing:
# test_list = [l, 2,3, [ 'A', 'B', 'C']]
# for TEMP in test_list:
# # Print (TEMP)
# type IF (TEMP ) == list:
# for temp_ in TEMP:
# Print (temp_)
# the else:
# Print (TEMP)

## adding the list:
# A = [l, 2,3]
# B = [3,4, 5]
C = a + B #
# Print (C)


## slicing operations list: 1. start position: a start position
# 2 end position: will take a position to the end of element
# 3 step: default is 1, If the step is negative, then right to left, if the step is an integer of from left to right
# A = [1,2,3,4,5,6,7,8,9]
# # # A = TEMP [0:. 6]
# # A # TEMP = [:] take all values #
# # temp = a [0 :: 2] # take odd
# TEMP = A [-1 :: -. 1]
# Print (TEMP)

# ## start position and end position can in fact be negative, but if it is negative, the last element is -1, and so on

## lists the assignment
# the greet = [ 'Hello', 'World']
# the greet [. 1] = 'Lucy'
# Print (the greet)

 

Guess you like

Origin www.cnblogs.com/godblessmehaha/p/11094815.html