day02 python function basis

'' '' '' 
'' '
Listing:
Definition: [], the values can be stored in a plurality of arbitrary types,
separated by commas.
Generally used to store students' interests, class periods, etc. ...
'' '
# define a list of students, can be stored for more than one student
# list ([' money Yao, '' Bruce Lee ',' Zhang whole eggs', ' Zhao Tiezhu '])
# Students. = [' money Yao ',' Bruce ',' Zhang whole egg ',' Zhao Tiezhu ']
# Print (Students. [. 1]) # Lee
#
# student_info = [' Bo ', 84,' male ', [' bubble 8 ',' 9 drink '













# Print (len (student_info)) # 4
#
# # 4, members of the operations in and not in
# Print ( 'Bo' in student_info) # True
# Print ( 'Bo' not in student_info) # False
#
# # 5, additional
# student_info = [ 'Bo', 84, 'male', [ ' bubble 8', 'drink 9']]
# # append a list of values at the end of student_info
# student_info.append ( 'Anhui cattle students, Hefei University' )
# Print (student_info)
#
# # 6, delete
## delete the list index value of 2
# del student_info [2]
# Print (student_info)

# 7, cycle
# for Student in student_info:
# Print (Student)

# required master:
# student_info = [ 'Yin Haoqing', 95, 'female', [ ' embarrassed dance', 'shouted Michael'], 95]
# # 1.index get a list index value of
# print (student_info.index(95)) # 1
#
# # 2.count get a list of values in a number of
# Print (student_info.count (95)) # 2
#
# # 3. values, default take the last value in the list, similar to delete
# # If pop () parentheses write index, the index takes a value corresponding to
# student_info.pop ()
# Print (student_info)
# # 2 of the index value extracted list, and assigned to the variable name sex
# student_info.pop sex = (2)
# Print (Sex)
# Print (student_info)

# student_info = [ 'Yin Haoqing', 95, 'female', [ ' embarrassed dance', 'shouted Michael'], 95]
#
# # 4. remove, to list a removing a first value
# student_info.remove (95)
# Print (student_info) # [ 'Yin Haoqing', 'FEMALE', [ 'embarrassed dance', 'call wheat'], 95]
#
# name = student_info. Remove ( 'Yin Haoqing')
# Print (name) None #
# Print (student_info) # [ 'FEMALE', [ 'embarrassed dance', 'call wheat'], 95]

# 5. insert value
# Student_info = [ 'Yin Haoqing', 95, 'female', [ ' embarrassed dance', 'shouted Michael'], 95]
# # # in student_info, the index for the 3 position into the "Hefei"
# # student_info.insert (3, 'Hefei')
# # Print (student_info)

# 6.extend consolidated list
# student_info1 = [ 'Yin Haoqing', 95, 'female', [ ' embarrassed dance 1', 'shout Mai 2'], 95]
# student_info2 = [ 'Louyi Fu', 94, 'female', [ ' embarrassed dance 1', '2 wheat call']]
# # student_info2 all the values inserted into student_info1
# student_info1.extend (student_info2)
# Print (student_info1 )

#Define:
# tuple ((. 1, 2,. 3, 'five', 'six'))
tuple1 = (. 1, 2,. 3, 'five', 'six')
Print (tuple1) # (. 1, 2,. 3 , 'five', 'six')
# priority control operation:
# 1, according to the index value (forward + reverse take take): only take
Print (tuple1 [2]). 3 #

# 2, a slice (GU head regardless of the end, step)
# 5-1 to the slice start from 0 in steps of 3
Print (tuple1 [0:. 5: 3]) # (. 1, 'five')

# 3, the length of the
print (len (tuple1)) # 5

# 4, members of the operations in and not in
Print (1 in tuple1) # True
Print (1 not in tuple1) # False

# 5, circulation
for Line in tuple1:
# Print (Line)
# Print default end parameters \ n
print (line, end = '_ ')
Immutable type: 
the value of a variable modified, the memory address must be different.
Digital type
int
a float

string type
# STR
#
# tuples
# tuple
#
# Variable Type:
# Type List
# List
#
# dictionaries
# dict
#


# '' '
# # immutable
# # int
# Number = 100
# Print (ID (Number)) # 1,434,810,944
# Number = 111
# Print (ID (Number)) # 1,434,811,296
#
# # a float
# SAL = 1.0
# Print (ID (SAL)) # 2771339842064
# SAL = 2.0
# Print (ID (SAL)) # 2,771,339,841,896
#
Str1 = # '! Python Hello'
# Print (ID (str1)) # 1,975,751,484,528
# str2 = str1.replace ( 'Hello', 'like')
# Print (ID (str2)) 1,975,751,484,400 #


# Variable Type:
# List

= List1 [. 1, 2,. 3]

list2 = List1
list1.append (. 4)

# List1 and list2 point is the same memory address of a
Print (ID (List1))
Print (ID (list2))
Print (List1)
Print (list2 )

Dictionary Type: 
action:
in the {}, can be stored in a plurality of values separated by a comma,
to access the key-value, high value of speed.

Defined:
Key must be immutable type, value may be any type
'' '

# dict1 = dict ({' Age ': 18 is,' name ':' Tank '})
# dict1 = {' Age ': 18 is,' name ':' Tank '}
# Print (dict1) {#' Age ': 18 is,' name ':' Tank '}
# Print (type (dict1)) # <class' dict'>

# value, the dictionary name + [ ], corresponding to the value written in parentheses key
# Print (dict1 [ 'Age'])

# priority control operation:
# 1, the access key press value: may be preferably stored
# deposit a level: dict1 dictionary values 9 to
# dict1 [ 'Level'] =. 9
# Print (dict1) {# 'Age': 18 is, 'name': 'Tank', 'Level':}. 9
# Print (dict1 [ 'name'




# Print ( 'name' in dict1) # True
# Print ( 'Tank' in dict1) # False
# Print ( 'Tank' not in dict1) # True
#
# # 4, delete
# del dict1 [ 'Level']
# Print (dict1) {# 'Age': 18 is, 'name': 'Tank'}
#
# #. 5, button keys (), the value of values (), on the key-value items ()
# # give the dictionary all key
# Print ( dict1.keys ())
# # All values obtained in the dictionary values
# Print (dict1.values ())
# # give all the dictionary items
# Print (dict1.items ())

#. 6, loop
# loop through all of the dictionary Key
# Key in dict1 for:
# Print (Key)
# Print (dict1 [Key])

# GET
dict1 = { 'Age': 18 is, 'name': 'Tank'}
# Print (dict1.get ( 'Age'))

# [] Value
# Print (dict1 [ 'Sex']) # a KeyError: 'Sex'

# GET values
Print (dict1.get ( 'Sex')) None #
# if no sex, to set a default value for
print (dict1.get ( 'sex', 'male'))



if the judge: 
Syntax:
if judging criteria:
# If the conditions are met, then here code execution
logic code

elif judgment conditions:
# If the conditions are met, here is the code execution
logic code

the else:
# If the determination is not satisfied, the execution here Code
logic code
'' '

# Analyzing two operand size
X 10 =
Y = 20 is
Z = 30

# retracted shortcuts, tab moves four spaces to the right, shift + tab moves four spaces left
IF X> Y:
Print ( X)

elif Z> Y:
Print (Z)

the else:
Print (Y)

'' '
the while loop
syntax:
the while conditional:
# execute here established
logic code

BREAK # out the present layer cyclic
Continue # end of this cycle, the next cycle
'' '

Str1 = # 'Tank'
#
# # the while loop
# the while True:
# the INPUT name = ( 'Please enter the characters guess:') .strip ()
# IF name == 'Tank':
# Print ( 'Tank Success!' )
# BREAK
#
# Print ( "Please reenter! ')

# limit cycles
str1 =' Tank '
# initial value
NUM = 0 # 0. 1 2. 3

# the while loop
the while NUM <. 3:
name = iNPUT (' enter guess characters: ') .strip ()
IF name ==' Tank ':
! Print (' Tank Success')
BREAK

! Print ( 'Please enter')
NUM = +. 1
 
File: 
Open ()

to write the file
wt: write text

read files
rt: read text

additional write file
at: Append text

Note: You must specify character encoding, in what way to write
it is what opened. Such as: utf-8

execution python file:
1. Start the python interpreter first loaded into memory.
2. Load written python files to the interpreter.
3. Detection python syntax to execute code.
SyntaxError: syntax error!

Open the file will have two resources:
1.python program
2. operating system to open the file
'' '

# write text files
# parameters a: absolute path of the file
# parameters II: Operation mode mode of file
# three parameters: encoding specified character encoding
# F = Open ( 'file.txt', MODE = 'wt', encoding = 'UTF-. 8')
# f.write ( 'Tank')
# f.close () # close the operating system files resources


read text file # RT == R & lt
# F = Open ( 'file.txt', 'R & lt',
Print # (reached, f.read ())
# f.close ()
#
#
# # append written text file
# A = Open ( 'file.txt', 'A', encoding = 'UTF-. 8')
# a.write ( '\ n HEFEI')
# a.close ()



'' '
context of the management of document processing.
with Open () AS f' handle '
' ''
# write
# with open ( 'file1.txt', 'w' , encoding = 'UTF-. 8') AS F:
# f.write ( 'Murphy's Law')
#
# # read
# with open ( 'file1.txt', 'r', encoding = 'utf-8') as F:
# reached, f.read RES = ()
# Print (RES)
#
# # additionally
# with Open ( 'file1.txt', 'A', encoding = 'UTF-. 8') AS F:
# f.write ( ' Siege ')
# # F.Close ()



'' '
picture, audio, video write
rb mode, read binary not necessary to specify the character encoding
' ''

# Reading photo cxk.jpg
with Open ( 'cxk.jpg', 'RB') AS F:
RES = reached, f.read ()
Print (RES)

JPG = RES

# writing the binary stream cxk.jpg cxk_copy.jpg file
with open ( 'cxk_copy1.jpg', 'wb') AS f_w:
f_w.write (JPG)


'' '
with managing multiple files
' ''
# is managed by with two f_r open file handles open, f_w
Open with ( 'cxk.jpg', 'RB') AS F_r, Open ( 'cxk_copy2.jpg', 'WB') AS f_w:
# F_r handle by the stream read out the binary image
res = f_r.read ()
# by f_w handle the flow picture binary file written cxk_copy.jpg
f_w.write (res)

def function name (parameter 1, parameter 2, ...): 
"" "Note: declare function" ""
logic code

return Return Value

def: defind defined.
Function name: must see its name knowing Italian.
(): Receiving the incoming external parameters.
Notes: used to declare the action function.
return: the return value to the caller.
'' '

' ''
Three forms defined functions:
1. No function parameters
necessary to receive incoming external parameters.

2. Reference function
needs to receive incoming external parameters.

3. empty functions

pass


function calls:
the function name + () call

'' '

# # 1. No function parameter
# DEF Login ():
# = INPUT User (' Enter a username ') .strip ()
# pwd = INPUT ( 'Please enter your password') .strip ()
#
# IF the User == 'Tank' and pwd == '123':
# Print ( 'the Login successful!'


Print # ( 'Login error!')
#
#
Memory address function # #
# Print (Login)
#
#
# # function call
# Login ()


# 2. There reference function
# username, password for receiving incoming external value
the Login DEF # (username, password):
# = the iNPUT the user ( 'Please enter your user name') .strip ()
# pwd = the iNPUT ( 'Please enter your password') .strip ()
#
# == IF the user username and pwd = password =:
# Print ( 'Login successful!')
#
# the else:
# Print ( '! Login error')
#
#
# # function call
## if the function needs to receive parameters when defining a caller must pass through their participation
Login # ( 'Tank', '123')


# 3. null function
'' '
the ATM:
1. Log
2.Registration
3. Withdraw
4. Withdrawal
5. Transfers
6. Repayment
'' '


# # sign-on feature
# DEF the Login ():
# # representative of what not to do
# Pass
#
#
# # Register function
# DEF the Register ():
# # representative of nothing do
# Pass
#
#
# # payment function
# rePay DEF ():
# Pass

# ...


'' '
function parameters:
' ''
# During the definition phase: x, y call parameter.
DEF FUNC # (X, Y): # X, Y
# Print (X, Y)
#
# # calling phase: 10, 100 call arguments.
FUNC # (10, 100)


# '' '
# position parameter:
Position # parameter
# argument position
# must eleven positions in accordance with parameter passing.
# '' '
## in the definition phase: position parameter
# def func (x, y) :
Print # (the X-, the y-)
# #
# # # call the stage: 10, 100, said location argument.
FUNC # (10, 100) 10 100 #
#
# '' '
# key parameters:
# Keyword argument
# follow the keyword parameter passing.
# '' '
## position parameter X, Y
# DEF FUNC (X, Y):
# Print (X, Y)
#
# # call stage: x = 10, y = 100 call key parameter.
# FUNC (Y = 111, X = 10) # 10 111

# is not less transmission
# func (y = 111) # error TypeError


# no more pass
# func (y = 111, x = 222, z = '333') # given TypeError


'' '
default parameters:
in the definition phase, as the parameter set default values
' ''

# DEF foo (X = 10, Y = 20 is):
# Print (X, Y)
#
# # does not pass parameters, the default parameter
# foo ()
#
# # pass parameters using the parameters passed
# foo (200, 300)

Nested function is defined: 
defined function within the function.

Object function:
memory address of a function called a constructor object.

Namespace function:
Built-in:
Python's own parser are called "built-in namespace."

Global:
all wore head to write the variables, functions ... are called "full name space."

Partial:
inside the function definitions are referred to as "local name space."

Namespace load order:
Built ---> --- Global> local

namespace lookup order:
local ---> --- Global> Built-in
'' '


nested definitions function #
DEF func1 ():
Print (' from ... func1 ')

DEF func2 ():
Print (' ... from func2 ')


# function object
Print (func1)
DEF F1 ():
Pass
DEF F2 ():
Pass

DIC1 = {'. 1 ': F1,' 2':

Choice == IF '. 1':
Print (DIC1 [Choice])
DIC1 [Choice] ()
elif Choice == '2':
Print (DIC1 [Choice])
DIC1 [Choice] ()
X 10 =

# namespace
# Function the nested definitions
DEF func1 ():
# = X 20 is
Print ( '... from func1')
Print (X) given #
X = 30
DEF func2 ():
Print ( '... from func2')
func1 ()

Guess you like

Origin www.cnblogs.com/zhusiwen/p/11085447.html