Python Day 2-loop, python Day 2

Source: Internet
Author: User

Python Day 2-loop, python Day 2

Dictionary Loop
# Dic = {'name': 'chras _ lee ', 'age': 18} # for I in dic. items (): # print I, I [1] # A variable loop dictionary # dic = {'name': 'chras _ lee ', 'age ': 18} # for I in enumerate (dic): # print (I, I [1], dic [I [1]) # Use the enumerate loop dictionary # dic = {'name': 'chras _ lee ', 'age': 18} # for k, v in dic. items (): # print (k, v) # cycle Dictionary of two variables # dic = {'name': 'chras _ lee ', 'age ': 18} # for k in dic. keys (): # print (k, dic [k]) # Use dic. keys value # dic = {'name': 'chras _ Lil', 'age': 18} # for v in dic. values (): # print (v) # Use dic. values directly retrieves the value # dic = {'name': 'chras _ Lil', 'age': 18} # for k in dic: # print (k, dic [k]) # common method # direct for loop dictionary is the key used to traverse the dictionary.
Loop string# Mes = 'hello' # for I in mes: # print (I) # for Loop string # mes = 'hello' # for I in enumerate (mes ): # print I # Use enumerate to traverse strings as tuples # mes = 'hello' # for I in mes [:-1]: # print (I) # reverse Loop
Loop list# L = ['A', 'B', 'C'] # for I in l: # print (I) # loop list # l = ['A ', 'B', 'C'] # for I in enumerate (l): # print (I) # Use enumerate to traverse the list as tuples # l = ['A', 'B', 'C'] # for I in l [:-1]: # print (I) # inverted Loop
Cyclic tuples# T = ('A', 'B', 'C') # for I in t: # print (I) # cyclic tuples ## t = ('A ', 'B', 'C') # for I in enumerate (t: # print (I) # Use enumerate to traverse tuples as tuples # t = ('A ', 'B', 'C') # for I in t [:-1]: # print (I) # inverted Loop

 

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.