Python3 Fifth day (conditions, loops, functions)

Source: Internet
Author: User
Tags variable scope

If statement:

If condition 1:

..

Elif Condition 2:

..

Else

..

Each condition is followed by a colon to represent the statement to be executed after the condition is met.

While loop:

While condition:

...


While condition:

...

Else

...


For loop:

for Var in seq:

...

Else

...

Iterators have two basic methods: ITER () and Next ()

strings, lists, tuples, all can be used to create iterative objects

Create an Iterative object: ITER (List/string/tuple)

You can make a for loop on an iterative object, or you can use next to access the next element in the iteration object.

Function

definition of the function:

Defined by Def, followed by the function name, parentheses (), the parameters are defined in parentheses, with the colon happy function body.

def function name (parameter list):

function contents

[Return var] #当没有return语句时, function returns none

function must be defined before calling the

mutable types and immutable types

String,tuples,numbers is a non-modifiable object, List,dict,set is a modifiable object, for a variable of a non-modifiable object, when the function argument is passed, the value of the object is modified, not the object itself.

Parameters:

Required parameters, keyword parameters, default parameters, indeterminate length parameters

Required parameters: The parameters must be in the correct incoming order, and the number should be the same.

Keyword parameter: match parameter value by parameter name, e.g. func (age=12,name= ' 2222 ')

Default parameter: def func (name,age=12), called when the value of the age parameter, can not be passed.

Indefinite length parameter: def func ([Arg,] *var_args_tuple), with the asterisk (*) variable name, which holds all the unnamed variable arguments.

Anonymous functions:

Using lambda to define anonymous functions

Lambda has its own namespace and cannot access arguments outside of an expression and variables in the global namespace

Grammar:

Lambda arg[,arg[,arg....]]:express

such as Lambda Arg1,arg2:arg1*arg2. Returns the product of a two parameter

Variable scope:

There are four types of scopes:

L (local) Local

In functions outside of the E (enclosing) closure function

G (Global) overall

B (built-in) built-in

Variable lookup rules l->e->g->b, ranging from small to large lookups

Only Module,class,def Lambda will introduce a new scope.

Global and nonlocal:

To use global Var to re-declare variables when an internal scope modifies an external scope variable

When modifying variables in nested scopes (the scope of enclosing), use nonlocal var to re-declare variables




Python3 Fifth day (conditions, loops, functions)

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.