Python operations on list

#访问列表元素
food =['bunana','apple','orange']
print(food[0])
print(food[1])
print(food[2])
print("hello "+food[0]+"!")
print("hello "+food[1]+"!")
print("hello "+food[2]+"!")

# Review
Food = [ 'bunana', 'Apple', 'Orange']
Food [0] = 'hahaha'
Print (Food)

# increasing
Food = [ 'bunana', 'Apple', 'Orange']
#. 1, the end increase
food.append ( 'watermelon')
food.append ( 'tomato')
Print (Food)
# 2, insert elements in the list
food.insert (0, "potato")
Print (Food)
# 3, delete
# if you know to delete an element position in the list, use the del statement.
food del [0]
Print (food)
# Method pop () to delete the end of the list of elements, and allows one to use it then
A = food.pop ()
Print (food)
Print (A)
#remove remove elements based on the values
food .remove ( "watermelon")
Print (Food)
# permanent sort
food.sort ()
Print (Food)
# reversal
food.





# Create a list that contains the first 10 integer (i.e. 1 to 10) square
A = []
for I in Range (1,11):
a.append (I ** 2)
Print (A)

# of a list of numbers performing simple statistical calculation
B = max (a)
B1 = min (a)
B2 = SUM (a)
Print (B, B1, B2)

# loop used for printing a number 1 to 20 (inclusive).
in Range A for (1,21):
Print (A)

# Create a list containing the number 1 to 1 000 000, then use a for loop print these numbers, seeking the maximum, minimum, sum of
C = []
for in range A (1,1000001):
c.append (A)
Print (max (C))
Print (min (C))
Print (SUM (C))

# odd. 6: by () function to specify a third range creating a parameter list containing an odd number of 1 to 20; a for loop reuse these figures are printed
D = []
for I in Range (1,21):
! IF I = 0% 2:
Print (I )
d.append (I)
Print (D)

# Multiples of 3: Create a list that contains 3 to 30 number divisible by 3; then using a digital loop for this list are printed out.
= E []
for I in Range (3,31,3):
Print (I)
e.append (I)
Print (E)

# Cube: A cube referred to by the same number three. For example, in Python, cubic ** 2 3 2 with FIG. Create a list that contains the first 10 cubic integer (i.e. 1 to 10), for a re-use cycle
# Cube these rings are printed.
= F []
for I in Range (1,11):
V = I **. 3
Print (V)
f.append (V)
Print (F)

# cubic Analysis: parsing a list used to generate a list, which contains the first 10 integer cubic
G = [I **. 3 for I in Range (1,11)]
Print (G)

# slice
G = [. 1,. 8, 27, 64, 125, 216, 343, 512, 729, 1000]
# the first three, the first 5, 3 start to finish, the three replication list
Print (G [0:. 3])
Print (G [:. 5])
Print (G [. 3:])
Print (G [- . 3:])
Print (G [:])

# traverse sections
= name [ "laoli", "laosong", "Laozhang"]
for I in name [. 1:. 3]:
Print (I)

# practice list
# Guest list: If you could invite anyone to dinner together (whether living or deceased), who would you invite? Please create a list, 
# which contains at least three people you want to invite; then, use the
print messages # this list, invite these people to dinner with you.

= mingdan [ 'laosong', 'laosun', 'laozheng']
for A in mingdan:
Print ( "Hello" + A + "!, available for purchase to EAT Today night")
#laozheng not something to
print (mingdan [2] + "you shi mei FA LAI EAT!")
mingdan [2] = 'laorui'
for a in mingdan:
Print ( "the Hello" + a + "!, is available for purchase to EAT Today night")
# find a larger dining table mingdan1
Print ( "A Geng Big CAN have have zhuo")
# 3 at the invitation
mingdan.append ( "Xiaozheng")
mingdan.insert (0, "Xiaosong")
mingdan.insert (2, "xiaosun"



# Down list: just buy a new dining table that can not be delivered on time, you can only invite two guests.
Print ( "Sorry, the zhi you CAN TWO people Come!")
Print (len (mingdan))

# delete each one a notification
Print ( "Sorry" + mingdan [5] + "you CAN not Come!")
mingdan. POP ()
Print ( "Sorry" + mingdan [4] + "you CAN not Come!")
mingdan.pop ()
Print ( "Sorry" + mingdan [3] + "you CAN not Come!")
mingdan. POP ()
Print ( "Sorry" + mingdan [2] + "you cAN not come!")
mingdan.pop ()

Print (mingdan)
# notify the rest of the can
for a in mingdan:
Print ( "the Hello" + a + ", welcome to eat today night!"









Guess you like

Origin www.cnblogs.com/hainabaichuan/p/11788158.html