Python (vi)

Source: Internet
Author: User

1. Notes

Comments generally have the following methods

# single Comment

"" "Comment content" "" Multiline comment

#coding =utf-8 or

#coding =GBK plus Chinese notes.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2.continue statement function

To put it simply: The contince statement is to skip the rest of the loop, but not to end the loop, but rather to jump to the first line of the nearest loop body.

When running a program, if you encounter the continue statement, the program will stop the current loop, the Python continue statement after the remaining statements are ignored, the program back to the top of the loop,

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

3.pass

The pass function does nothing and is generally used to occupy a position, for example, you define a function, but the body part of the function does not know how to write, but it cannot be empty to write anything, so we need to use pass instead of this position.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4.return

Return is the meaning of the returned value, in general the function has a return

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

5.global

If you want to reassign a variable outside the function, and this variable will be used for many functions, you need to tell Python that the scope of the variable is a global variable, and the global statement can accomplish this task, that is, without the global statement, is not able to modify the global variables,

The usage of global is as follows:


>>>

>>>

>>> x =6

>>> def func ():

... global X

... x =1

...

>>> func ()

>>> Print X

1

>>>

The role of ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++66.assert assertions

A python assert assertion is a decision that declares that its Boolean value must be true, and that if an exception occurs, the expression is false, that is, he is used to test the true or false expression, and if the return value is false, the chance triggers an exception.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

7.if statements

The IF statement is also called the control flow statement, giving the condition to decide what to do next, the principle is that if the condition is true, then the contents of the statement block are executed, and if the condition is false, the statement block will not be executed.

Standard Pyhton if conditional statement format;

The general format of the IF statement is as follows:

If decision conditions:

EXECUTE statement block

The following example can be a good illustration of this problem:


>>>

>>> x = 5

>>> if x>0:

... print X

...

5

>>> if x < 0:

File "<stdin>", line 1

If x < 0:

^

Syntaxerror:invalid syntax

>>> if x < 0:

... print X

...

>>>

The contents of the string inside the statement block are not fixed, you can write anything you want to enter, and if the statement you want to execute does not exist, you can use the PASS statement.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

8.break

The function of break is to jump out of the current loop break statement can be used in the For loop and while loop, simply to say, the break statement will immediately exit the loop, the Loop code on the subsequent side will not be executed.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

9.for Cycle

A For loop statement is a loop control statement in which elements within any ordered sequence object can traverse, such as strings, lists, tuples, and other objects that can be iterated, and if statements previously mentioned are different from the use of a for statement, but can be used under a for statement for conditional statements.

Basic format for the For loop:

For Target in object:

Print assignment target

>>> for I in B:

... print I

...

S

W

D

Q

>>> Print I,

Q

>>> for I in B:

... print I,

...

s W d q

>>>

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

What is the difference between a 10.while loop statement and an IF statement:

The IF statement is true when the condition is true, the corresponding block of code is executed once, while the code block in the while is looping until the loop condition does not meet the no longer true

The general standard syntax for a while statement:

While condition:

Execute code block

Just like the following example:

>>> x = 1

>>> while x < 8:

... x+=1

... print X

...

2

3

4

5

6

7

8

>>>

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

11.else and Elif statements can also be called clauses, because they cannot be used independently, and both appear inside the If,for,while statement. The ELSE clause can add an option, while the ELIF clause is used when it needs to be checked for multiple conditions, used with if and else, and elif is shorthand for else if.

>>>

>>> x = 0

>>> if x >0:

... print "This number is greater than 0"

... elif x < 0:

... print "This number is smaller than 0"

.. else:

... print "This number equals 0"

...

This number equals 0.

>>>

There are two points to note when using the IF and ELSE statements:

1.else,elif is a child block and cannot be used alone

2. An If statement can contain multiple elif statements, but only one else statement at the end.

You can use the ELSE clause in the while and for Loop, which is only executed after the loop has ended, and if the break statement is used at the same time, the ELSE clause is skipped, so note that the ELSE clause and the break statement cannot be used at the same time!







Python (vi)

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.