Python learning notes (2) process control functions

Source: Internet
Author: User

Python Study Notes (2)


Process Control
If... else... elif
Example:
If 1> 2:
Print 'aaa'
-------------------------------
If 1> 2:
Print 'aaa'
Else:
Print 'bbbbbb'
--------------------------------
If 1> 2:
Print 'aaa'
Elif 1> 0:
Print 'bbbbbb'
Else:
Print 'cccccc'
----------------------------------
If 2> 1:
Print 'aaa'
Print 'bbbbbb'
Output: AAAA
BBBB
-----------------------------------
If 2 <1:
Print 'aaa'
Print 'bbbbbb'
Output: BBBB

If indentation indicates the running range
Logical operators
And/or/not

For
For x in 'abcde ':
Print x
Output:
A
B
C
D
E

Range (start, stop [, step])Generation Sequence
Range (10) generates [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Range (5, 9) generates [5, 6, 7, 8]
Range (, 2) 2 is a step, generating [1, 3, 5, 7, 9]
Example:
For x in range (10 ):
Print x, 'aaa'
Output 10 times x + AAAA
In python, loops are different from those in C. You can also add elsefor x in range (5 ):
Print x
Else:
Print 'end'
Output end after output
For x in range (5 ):
Print x
If x> 2:
Break
Else:
Print 'end'

But in this way, the end will not output
Return break, continue, pass placeholder, code village
Convenient sequence and Dictionary

Convenience SequenceExample: a = ['A', 'B', 'C', 'D', 'D']
For x in range (len ()):
Print a [x]
Or for x in:
Print x

Convenient dictionary:A = {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'D '}
For x in:
Print x
Will output the dictionary key
For x in:
Print a [x]
Output value
For x, y in a. items ():
Print x, y
The sequence x that generates key-value pairs using items () is the key, and y is the value.
While
Example:
I = 0
While I <10:
Print I
I + = 1

Output 0 to 9
While can also be added with else
I = 0
While I <10:
Print I
I + = 1
Else:
Print 'end'
Output 0 ~ The end will be output after 9, and the else content from the break will not be executed.
Function
Function Definition
Define implementation a + B
Example:
Def add (a = 1, B = 2 ):
Return a + B
Print add ()

Output 5

You can define the default parameters.
Def add (a = 1, B = 2 ):
Print a + B
Add ()
Output 3
* And **
* Indicates a tuples or list.
** Indicates a dictionary.

Def add (a, B ):
Return a + B
T = (1, 2)
Print add (t)
This will cause errors
If you change to print add (* t), no error will occur.
If two more parameters are written in add (,), an error is defined as def add (a, B, * args): return a + B, args is a redundant parameter for storing tuples. Here args is (3, 4)
Def f (name = 'name', age = '0 '):
Print "name: % s, age: % s" % (name, age)
F (* ('wang', '18 '))
F (* ('18', 'wang '))
Output: name: wang, age: 18
Name: 18, age: wang


Def f (name = 'name', age = '0 '):
Print "name: % s, age: % s" % (name, age)
F (name = 'wang', age = '18 ')
F (age = 18, name = 'wang ')
Both f Functions output name: wang, age: 18 instead of name = 18, age = wang.
In this case, def f (name = 'name', age = '0 '):
Print "name: % s, age: % s" % (name, age)
A = {'age': '18', 'name': 'wang '}
F (**)

Name: wang, age: 18

Def f (name = 'name', age = '0', * args, ** kwargs ):
# Print "name: % s, age: % s" % (name, age)
Print kwargs
F (x = 'x', y = 'y ')

{'Y': 'y', 'x': 'X '}
Lambda expressions
Def add (x, y ):
Return x + y

G = lambda x, y: x + y # lambda expression, equivalent to add

Print add (1, 2)
Print g (1, 2)

Reduce
Implement factorial using reduce
Def f (x, y ):
Return x * y


L = range (1, 6)
Print reduce (f, l) # reduce takes 2 at a time

















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.