Python System Study Notes (1)

Source: Internet
Author: User

1. write the first Helloworld. When you learn a new programming language, the first program you write and run is usually a "HelloWorld" program, this has become a traditional method of typing python at the shell prompt of the command line to start the interpreter. Enter print 'Hello world' and press Enter. You can see the output word Hello World. (Python 2.7 3.3 is now a function) how to compile a Python program below is a standard process for saving and running Python programs. 1. Open your favorite editor. 2. Enter the program code in the example. 3. Save it as a file with the file name given in the comment. By convention, I save all Python programs with the extension. py. 4. Run the interpreter command python program. py or run the program using IDLE. 2. Repeated operator exercises !! Python operator list operator description x + y, x-y plus, minus, "+" can be reloaded as the connector x * y, x ** y, x/y, x % y multiply, square, division, and remainder. "*" can be reloaded as duplicates, and "%" can be reloaded as formatting <span>, <span>, <, <=, =, <> ,! = Comparison operator + =,-=, * =,/=, % =, ** =, <span>, <=, & =, ^ =, | = auto-incrementing operator x | y by bit or x ^ y by bit or x & y by bit and ~ X bitwise decimal x <span>, x <yx shifts the y-bit to the left or right is, is not is equivalent to testing in, not in is a member test or, and, not logical operators x [I], x [I: j], x. y, x (...) index, Shard, limited reference, function call (...), [...], {...}, '... 'tuples, lists, dictionaries, conversion to string operator precedence list of operators (from highest to lowest) operator description 'expr' String Conversion {key: expr ,...} dictionary [expr1, expr2. ..] list (expr1, expr2 ,...) tuples function (expr ,...) function call x [index: index] Slice x [index] subscript index value x. attribute reference ~ X returns x,-x, positive, negative x ** y Power x * y, x/y, x % y, division, modulo x + y, x-y plus, minus x <y>, x <y shift x & y by bit and x ^ y by bit or x | y by bit or x, x <yspan>, x = y, x! = Y, x <= y, x <y compare x is y, x is not y equals test x in y, the x not in y member judges not x logic NO x and y logic and x or y logic or lambda arg ,...: exprLambda anonymous function 3. if Statement 1. if else Statement 2. if... elif... elif .. when writing nested conditional statements for else3. if statements, avoid using nested statements as much as possible. Nested statements are not easy to read and may ignore some possibilities. Exercise [python] # score = raw_input ("score:") # score = int (score) score = 85 # if else demo1 if (score> 60 ): print 'pass' else: print 'fail '# if elif else demo2 if (score> 90): print 'A' elif (score> 80) and (score <90 ): print 'B' elif (score> 70) and (score <80): print 'C' else: print 'D' # if include if demo3 a = 3; B = 4; c = 5; if (a> B): if (c> a): print 'max is C' else: print 'max is a' else: if (B> c): print 'max is B' Else: print 'max is C' print 'done' 4.for Statement 1. the first line of a general Python for loop defines a value assignment target (or [some targets ]), and the object you want to traverse. The first line is followed by the statement block you want to duplicate (usually with indentation) for <target> in <object >:< statements> else: <statements> when the ptyhon runs the for loop, the elements in the sequence object are assigned to the target one by one, and then the loop subject is executed for each element. A cyclic subject generally uses a value-assigned object to reference the current element in the sequence, just like the cursor Of The traversal sequence. The variable name used as the value assignment target in the first line of for is usually the variable acting on by the for statement (possibly new ). This variable name is nothing special and can be modified in the loop body. However, when the control is back to the top of the loop, it is automatically set to the next element of the sequence. After the loop, this variable generally references the recently used elements, that is, the final element of the sequence, unless the loop is exited through a break statement. The for statement also supports a selected else block, which works like in a while loop: If the break statement is not met when the loop leaves, the break and continue statements will be executed (that is, all the elements of the sequence have been accessed) in the for loop, just like the while loop. The complete for loop format is as follows: for <target> in <object >:< statements> if <test>: break if <test>: conitnueelse: <statements> [python] a = ['a1', 'a2 ', 'a3'] B = ['b1 ', 'b2'] # will iterate 3 times, # the last iteration, B will be None print "Map:" for x, y in map (None, a, B): print x, y # will iterate 2 times, # the third value of a will not be used print "Zip:" for x, y in zip (a, B): print x, y # will iterate 6 times, # It will iterate over each B, for each a # producing a slightly different outpu print "List:" for x, y in [(x, y) for x in a for y in B]: print x, y [python] view plaincopy # demo for 'for' #-*-coding: cp936-*-# for in I in range (): print I # step 2 for I in range (, 2): print I; # break for I in range (1, 5): if (I = 6): break else: print 'break hello' # quantity import math for I in range (50,100 + 1): for j in range (2, int (math. sqrt (I) + 1): if I % j = 0: break else: print I # continue for I in range (1, 5): if (I = 4 ): continue print 'no met continue 'else: print I 5. the general format of the while statement while loop is as follows: while <test >:< statements1> if <test2>: break if <test3>: continue if <test4>: passelse: <statements2> break and continue can appear in any place of the while (or for) loop body, but are usually further nested in the if statement, and corresponding operations are performed according to certain conditions. [Python] # demo for while a = 4; while (a> 0): print a; a = A-1; if (a = 1): break else: www.2cto.com print 'no meet break' # continue a = 4; while (a> 0): print a; a = A-1; if (a = 1 ): continue print 'no meet continue'

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.