"Python programming from the beginning to the practice" _ Chapter Fourth _ Operation list

Source: Internet
Author: User

The For loop iterates through the list

Pizzas = ['Pizzahut','Dicos','KFC'] forPizzainchPizzas:Print("I like"+ Pizza +"pizza!")Print("I really like pizza") Run result I like Pizzahut pizza! I like Dicos pizza! I like KFC pizza! I really like pizza
Note: When writing a For loop, you can specify any name for the temporary variable used to store each value in the list, preferably a meaningful one, and Python determines the relationship between the line of code and the previous line of code according to the indentation, and the same indented code belongs to the same rank;do not omit the colon after the for statement:function range () makes it easy to generate a series of numbers
 for  in range (0,5):    print  (value)# run result 01234

Note that only the second value that you specify is stopped after printing. Using range (), if the output does not sign as expected, try adding one or minus one of the specified values.

Create a list of numbersYou can enter the output of range () into the function list () and convert it directly to lists.
Numbers = List (range (1,5))print  (numbers)# Run result [1, 2, 3, 4]

You can also specify the step size

Numbers = List (range (1,10,2))print  (numbers)# Run result [1, 3, 5, 7, 9]

Add a value of 1-10 squared to a list .
squares = []for number in range (1,11):    square = number **2           #这两步可以简写为squares. Append (number**2)    Squares.append (square)        #print (squares) #运行结果 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

 

List Parsing
 for  in range (1,11)]print  (squares)# Run result [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

To use this syntax, first specify a descriptive list name, such as squares, and then specify an opening parenthesis and define an expression

Slice

To create a slice, you specify the index of the first element and the last element to be used. As with function range (), Python stops after reaching the element that precedes the second index you specify.

Players = ['Charles','Martina','Michael','Eli']Print(Players[0:3])#Run Results['Charles','Martina','Michael']
Players = ['Charles','Martina','Michael','Eli']Print(players[1:])#if the last position is not specified, it is cut to the tail#Run Results['Martina','Michael','Eli']
Players = ['Charles','Martina','Michael','Eli']Print(Players[:3])#Run Results['Charles','Martina','Michael']
Players = ['Charles','Martina','Michael ','Eli']print (players[-2:])#  Running Results ['Michael'Eli']

You can use a for loop to traverse the data of a slice

 players = [ " charles  " ,  " martina   ", "  michael   ", "   Eli   " ]   For  player in  players[1:4]:  print   #   run result  martinamichaeleli  

can be indexed [:] To copy the list, if you simply use the equals sign to assign a list to another list, it can be understood that both lists point to the same list space, changing one of the list will change the other.

My_foods = ['Pizza','Falafel','Carrot Cake']friend_foods=My_foodsmy_foods.append ('Cannoli') Friend_foods.append ('Ice Cream')Print("My Favorite foods is:")Print(My_foods) My favorite foods are:['Pizza','Falafel','Carrot Cake','Cannoli','Ice Cream']Print("\nmy friend ' s favorite foods is:")Print(friend_foods) My friend's favorite foods is:['Pizza','Falafel','Carrot Cake','Cannoli','Ice Cream']

Meta-group

Tuples use parentheses to identify, elements within the tuple can not be deleted, not directly assigned to modify!
Dimensions = (200,50)print  (dimensions[0])print (dimensions[1])#  Running Results 20050

You can use a for loop to traverse the entire tuple

Although you cannot modify the elements of a tuple, you can assign values to the variables of the storage tuple.Therefore, you can only redefine the entire tuple if you want to modify it.
Dimensions = (200,50) for in dimensions:    print= (100,120 ) for in dimensions:    print  (dimension)#  Running Results 20050100120

"Python programming from the beginning to the practice" _ Chapter Fourth _ Operation list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.