Python Learning--for Loop

Source: Internet
Author: User

For loop

First, using the For loop for simple functions

" "multiplication Table 1--9 multiplication table for loop for digital traversal" " forIinchRange (1,10):#in line         forSinchRange (1,i+1):#column in        Print('%s X%s=%s'% (s,i,s*i), end ="')    Print()" "stars to achieve the stars in a triangular arrangement" " forIinchRange (1,9):#Line     forSinchRange (Len Range (1,9))-i):Print(' ', end ="')     forNinchRange (1,2*i):#column        Print('*', end ="')        Print()    " "Amount Lowercase + uppercase splits a logarithmic combination of circular numbers" "List_num= ['0',' One','Ii.','three','Restaurant','Wu','Lu','Qi','BA','JIU']list_u= ['Garden','Pick','Bai','Qian',' million']input_num= Input ('Enter Number:') Len_u=Len (input_num) forIinchInput_num:len_u-=1Print("{0}{1}". Format (List_num[int (i)],list_u[len_u]), end="')Print('Whole')

Run:

e:\python_vs_code\directory[directory]>d://py3.6//python.exe e:/python_vs_code/directory[directory]/demo0802/py_for_ mult.py1 x 1=11 x 2=2   2 x 2=41 x 3=3   2 x 3=6   3 x 3=91 x 4=4   2 x 4=8   3 x 4=12   4 x 4=161 x 5=5<  C7/>2 x 5=10   3 x 5=15   4 x 5=20   5 x 5=251 x 6=6   2 x 6=12   3 x 6=18   4 x 6=24   5 x 6=30   6 x 6=361 x 7=7   2 x 7=14   3 x 7=21   4 x 7=28   5 x 7=35   6 x 7=42   7 x 7=491 x 8=8   2 x 8=16
   3 x 8=24   4 x 8=32   5 x 8=40   6 x 8=48   7 x 8=56   8 x 8=641 x 9=9   2 x 9=18   3 x 9=27   4 x 9=36   5 x 9=45   6 x 9=54   7 x 9=63   8 x 9=72   9 x 9=81       *      *     *    * * * * * * *******
   *********  *********** **************************** input number:1234 Yi Bai san san pick Garden Whole

Second, the derivation formula:

list derivation: VA=[rxp forOutinchListif(out condition)] Get two list intersection va3=[10,20,30,40]va4=[20,30,50,60,70]va5=[num forNuminchVa3ifNuminchVA4] Acquire and set VA6=[num forNuminchVa3ifNum not inchVA4]Print(VA6)Print(va4+va6) Get the difference set Va7=[num forNuminchVa4ifNum not inchVA3]Print(VA7)--------------------------------------------------------------

# get an odd twice-fold value between 0-99
Va2=[i*2 for I in range (+) if i%2!=0]

Print (' Va2, ', VA2)


Generator Objects (tuple derivation) Gen= (i forIinchRange (5)) Print(gen)#For I in Gen: #print (i)------------------------------------------------------------dictionary derivation type mcase= {'a': 10,'b': 34,'A': 7,'Z': 3,'B': 20,'b': 50}var={keyexcept:vaexcept forKeyinchDictionary Object}#if K.lower () in [' A ', ' B ', ' Z '] judging conditionsMcase_frequency ={k.lower (): Mcase.get (K.lower (), 0)+ Mcase.get (k.upper (), 0)#Part III Implementation forKinchMcase.keys ()#The first step ifK.lower ()inch['a','b','Z']#The second judge performs true to perform part III-------------------------------------------------------Set Derivation type Mcase_key={key forKeyinchMcaseifkey== key.lower ()}

Three, the same requirements of the three different solutions

" "Number of numbers and letters in the statistics string 1. Use exceptions to handle non-numeric try...except2. Use built-in functions to determine non-numeric isdigit () 3. Use character conversions to determine the range of alphanumeric letters. Ord () character representable ASCII chr () ASCII transcoding character" "str_for='adashgdkahfqa223354564'Num=0str_num=0" "First Kind" " forIinchstr_for:Try: Int (i) Num+=1exceptValueerror:str_num+=1Print('Number:'Num'\ n"'Letters:', Str_num)" "The second Kind" "NUM1=0str_num1=0 forIinchstr_for:ifI.isdigit () = =True:num1+=1Else: Str_num1+=1Print('Number:', NUM1,'\ n"'Letters:', STR_NUM1)" "Third Kind" "num2=0str_num2=0 forIinchstr_for:ifOrd'0') < Ord (i) <ord ('9'): num2+=1elifOrd'a') <= Ord (i) <=ord ('Z')orOrd'A') <= Ord (i) <= Ord ('Z'): str_num2+ = 1Print('Number:', Num2,'\ n"'Letters:', str_num2)

Iv. deduction of "amount of capitalization conversion, dictionary deduction Exercise"

List_num = ['0',' One','Ii.','three','Restaurant','Wu','Lu','Qi','BA','JIU']list_u= ['Garden','Pick','Bai','Qian',' million']input_num= Input ('Enter Number:') Len_u=Len (input_num) Num_info= [Int (input_num)//10**i%10 forIinchRange (len_u-1,-1,-1)] forIinchNum_info:len_u-=1Print('%s%s'% (List_num[i],list_u[len_u]), end ="')Print('Whole')" "The Dictionary object initialization definition data The key is a str type length greater than or equal to 5 of the information obtained using the derivation formula" "dict_1= {'a': 12,123:'ABCD','12345': True,'Sadasdds':'18895','ASD':'D'}dict_infer= {K:v forKvinchDict_1.items ()ifType (k) ==str andLen (k) >= 5}Print(Dict_infer)

Python Learning--for 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.