expressions and statements in Python and for, while loop instance code

Source: Internet
Author: User
expressions and statements in Python and for, while loop exercises

1) expression

       Commonly used expression operators: X + y, x/y, X/y, ×//, and × y logical operations: X or Y, x and Y, not X Member relational operations: X in Y, x not in Y object instance test: X is y, x isn't is y comparison operation: x < y, x > y, x < = y, x >= y, x = = y, x! = y-bit operation: x |        Y, X & y, x ^ y, x << y, x >> y unary operations:-X, +x, ~x: exponentiation: X * * y index and Shard:        X[i], x[i:j], x[i:j:stride] Call: X (...)        Take attribute: X.attribute tuple: (...)        Sequence: [...]        Dictionary: {...} Ternary selection expression: x if y else z anonymous function: Lambda args:expression generator function Send protocol: Yield x Operation priority: (...), [...], {.        ..}        S[i], S[i:j] s.attribute s (...)        +x,-X, ~x x * * y *,/,//,% +,-<<, >> & ^ | <, <=, A;, >=, = =,! = is, isn't in, not in not and or lambda

2) Statement:

    Assignment Statement    call print    : Print object    if/elif/else: Conditional judgment    for/else: sequence Iteration    while/else: normal loop    pass: Placeholder break    :     continue    def    return    yield    Global: namespace    raise: Triggering exception    import: From     : module Property access    class:    try/except/finally: Catch exception    del: Delete reference    assert: Debug Check    with/as: Environment Manager            Assignment statement:                    Implicit assignment: Import, from, Def, class, for, function parameter                    tuple and list decomposition assignment: When the left side of the assignment symbol (=) is a tuple or list, Python pairs the right object and the left target from the left and right by the location. , the number of different when the exception will be triggered, the way can be sliced,                    multi-target Assignment                    enhancement assignment: + =,-=, *=,/=,//=,%=,

3) for Loop practice

    Exercise 1: Separate display of all elements in the specified dictionary d1, similar to the following K1 v1 k2 v2 ... >>> d1 = {' x ': 1, ' Y ':            2, ' Z ': 3, ' m ': 4} >>> for (k,v) in D1.items (): Print k,v y 2                X 1 Z 3 M 4 Exercise 2: Display the list in l1=["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" in the index of the odd element; >>> L1 = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] >>> for I in range (1,len (L1), 2): Print L1[i] Mon Wed Fri Exercise 3: Will belong to the list l1=["S                 Un "," mon "," Tue "," Wed "," Thu "," Fri "," sat ", but not part of the list l2=[" Sun "," mon "," Tue "," Thu "," Sat ", all elements defined as a new list L3;             >>> L1 = ["Sun", "mon", "Tue", "Wed", "Thu", "Fri", "Sat"] >>> L2 = ["Sun", "Mon", "Tue", "Thu", "Sat"]                        >>> L3 = [] >>> for i in l1:if I not in L2: L3.append (i) >>> L3 [' Wed ', ' Fri '] Exercise 4: Known list namelist=[' stu1 ', ' stu2 ', ' stu3 ', ' stu4 ', ' stu5 ', ' stu6 ', ' Stu7                 '], delete the list removelist=[' Stu3 ', ' stu7 ', ' stu9 ']; remove each element that belongs to the Removelist list from the NameList (removelist, but not namelist ignore); >>> namelist= [' stu1 ', ' stu2 ', ' stu3 ', ' stu4 ', ' stu5 ', ' stu6 ', ' stu7 '] >>> removelist = [' Stu3                        ', ' stu7 ', ' stu9 '] >>> for i in Namelist:if i in Removelist: Namelist.remove (i) >>> namelist [' stu1 ', ' stu2 ', ' stu4 ', ' stu5 ', ' Stu6 ']

4) While loop exercise

        Exercise 1: Display all elements of the specified list individually; >>> L1 = [1,2,3,4,5] >>> i = 0 >            >> while I < Len (L1) Print L1[i] i + = 1 1 2 3 4 5 >>> l1 = [1,2,3,4,5] >>> while L            1:print l1.pop (0) 1 2 3 4 5 Exercise 2: The sum of all even numbers within 100; >>> i = 0 >&gt                ;> sum = 0 >>> While I < 101:sum + = i i + = 2                    Print sum 2550 >>> for I in Range (0,101,2):                   Sum+=i Print sum 2550 Exercise 3: Display all keys of the specified dictionary one by one, and indicate the total number of keys after the display is finished; >>> D1 = {' X ': 1, ' Y ': all, ' Z ': i1} >>> = D1.keys () >>> while I1:p               Rint i1.pop (0) Else:print len (d1) x y z 3 Exercise 4: Create a list containing all the odd numbers within 100; >>> D1 = [] >>> i = 1            ;>> While I < 101:d1.append (i) i+=2 >>> print D1 [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59,  ------------------------------------------            [] >>> for I in range (1,101,2) d1.append (i) >>> print D1 [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59 , 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99] Exercise 5: List l1=[0,1,2,3,4,5,6], list l2=["Sun", "Mon", "             Tue "," Wed "," Thu "," Fri "," Sat ", the element in the first list is the key, and the element in the second list generates a dictionary for the value d1; >>> l1 = [0,1,2,3,4,5,6] >>> L2 = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] >>> D1 = {} &GT;&G                        T;> count = 0 >>> If len (l1) = = Len (L2): While Count < Len (L1): D1[l1[count]] = L2[count] Count + = 1

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.