Python Basic Tutorial Learning Notes---(5) conditions, loops, and other statements

Source: Internet
Author: User
Tags ord

1. Use comma outputYou can print multiple expressions by separating them with commas. After printing, the items are automatically separated by a space. You can also output text and variable values at the same time. can be used with the string connector "+".   2. Import functions from the moduleHow to import functions from a module:Import SomemoduleFrom somemodule import somefunctionFrom somemodule import somefunction,anotherfunction,yetanotherfunctionfrom somemodule Import * (all functions are imported from a given module)if the two modules you want to import have a function, you can do this:module1.function (...)module2.function (...)
or this:From Module1 import function as Function1From module2 import function as function2  3. Value Magic(1) Sequence unpackingmultiple assignment operations can be performed simultaneously, or the values of two or more variables can be exchanged. the sequence is unpacked by the Popitem method. A sequence unpacking--or, optionally, an optional solution package--unlocks the sequence of multiple values and places them in a sequence of variables. Note that the number of variables and values around the assignment symbol (=) should be exactly the same. (2) chain-assigned valuechained Assignment (chained Assignment) is a shortcut to assigning the same value to multiple variables. x=y=somefunction ()The effect is the same as the following statement:x=somefunction ()y=xHowever, these two sets of statements and the following statements do not necessarily rank:x=function ()y=funcition ()(3) Incremental assignmentincrement assignment operator: *=,+=  4. Statement blockuse the indent character to create a statement block. You can use the Indent: tab, 4 spaces, 8 spacesonly one indentation can be used in a program. In general, using 4 spaces is the most standard. all statements in the same statement block should have the same amount of indentation. in Python, a colon identifies the beginning of a statement block and stops identifying the block of statements with the same indent character.   5. Conditional Statements(1) boolean value:False (Flase): Standard Flase,none, all types of numbers 0 (floating point, Long integer, other types, etc.), empty sequences (empty strings, empty tuples, empty lists), empty dictionaries are false. true (true): all but the false exceptions above are true, including the special value TRUE.
The standard Boolean values are true and flase, or 1 and 0. If a logical expression returns 1 or 0, the actual meaning is to return true or false. Although ' and 0 are false values, they are not equal in themselves. Pyhton are case-sensitive and have strict casing requirements like flase and true. Boolean values True and False are Boolean types, and BOOL functions can be used to convert other values to Boolean values. (2) Conditional statementsIf statement, Else statement, Elif statementBe sure to remember the colon, and then say no. the input that the Raw_input function receives is automatically saved for the text type. So use input to receive input, or you can use INT (raw_input (...)) To accept the input. In addition, if statements can be nested IF statements inside. you can not write the last else statement, and the missing object is automatically ignored. (3) comparison operators:(4) equality operator: = =(5) Same operator: isThe IS operator judges the same sex rather than equality. The variables x and Y are bound to the same list, so x is equal to Y and satisfies the same. The variable z is bound to another list with the same values and order, with the same values but not the same object. use is to determine the identity of the two (whether it is the same object), using the = = (! =) to determine the similarity of the two (equality). to avoid using the IS operator to compare two immutable values (strings or numbers). (6) In: Membership operator(7) Comparison of strings and sequences: >,<, which is compared in alphabetical order. if the strings are mixed with uppercase and lowercase letters, the comparison is also made, after all, Python is case-sensitive. If you do not want to accept the case effect, you can use the upper () and lower methods to make the relevant conversions before you compare them. letters are ordered, and the order value of a letter can be queried by the ORD function. The Ord function has the opposite function as the CHR function. other sequence types can also be compared, as is the rule. (8) Conditions of connectionconnect with and.   6. Circular statements(1) while Looprepeat a block of code if the condition is true until the condition is not true. Note with a colon. (2) for LoopSpecifies a loop range that repeats a block of code within that range. Note with a colon. (3) iterate through the dictionary elements(4) Some iterative toolsA, parallel iterationsYou can use the built-in zip function to compress two sequences together to return a list that produces a tuple. Then unpack the tuple in the loop. The zip function can be used for any number of sequences, or to cope with unequal sequences, with the shortest sequence as the length of the list. B, flip and sort iterationsTwo helpful methods: the reversed method and the sorted method. they can act on any sequence or iterative object, instead of modifying the object in situ, instead returning a flipped or sorted version.
Sorted returns a list, the reversed method returns an object that can be iterated. (5) Jump out of the loopA, breakThe break statement user jumps out/ends the loop.
B, Continueends the current loop and jumps to the beginning of the next cycle. C, while True/breakin order to achieve such a function, there can be:You can also have:While true is a loop that never stops itself. The loop is terminated only if the IF judgment statement is added in the loop and the break statement is satisfied when a preset stop loop condition is met. (6) Else clause in the loopYou can use Boolean variables to check if a loop jumps out of the middle. You can also add an else clause to the FOR loop, and the ELSE clause executes when and only if no break is called. but this for loop must be a finite loop. (7) List derivation--Lightweight loopA list derivation is a way to create a new list using another list. It works like a for loop. You can use a single for loop in the Create new list, or you can use multiple loops, or you can use it with an if condition. only the list derivation, there is no tuple derivation, so the square brackets here to the parentheses, before the 2.3 version of the error, and then produce an iterator.   7, a few special statements:(1) Pass statementUsed as a test placeholder. At this point , if you debug it will produce an error. here, if you use the PASS statement, you can pass the syntax requirements and have no other effect. (2) del statementby assigning A to none, a point-to-list (' A12 ', ' b34 ', ' c456 ') is cut off. But a as an object still floats in memory. you need to use the DEL statement to remove object A. for lists [' A12 ', ' b34 ', ' c456 ') This cannot be deleted in Python. We use the DEL statement to simply remove the reference to an object and the name of the object itself. When the list is no longer in use, the Python interpreter is responsible for the memory collection. (3) EXEC statementThe EXEC statement is used to execute the Python code stored in the string, but there are some vulnerabilities. Specifically, if a program executes a portion of a string from a user-supplied piece of content as code, the program may lose control of the execution of the code.
Therefore, you need to add a namespace to the EXEC statement, or a scope. the most useful part of the EXEC statement is the ability to dynamically create code strings. (4) eval statementThe eval statement is used for evaluation, which is a built-in function similar to exec. The EXEC statement executes a series of Python statements. eval evaluates the Python expression and returns the result value. The EXEC statement does not return any objects. as with the EXEC statement, Eval can also use namespaces.   8, Summary:   

Python Basic Tutorial Learning Notes---(5) conditions, loops, and other statements

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.