# 1 in the list to define a list of students, can be stored for more than one student
List ([ 'money Yao', 'Bruce', 'Zhang whole egg', 'Zhao Tiezhu'])
Students. = [ 'money Yao', 'Bruce', 'Zhang whole egg', 'Zhao Tiezhu']
Print (Students. [. 1 ]) # Lee
student_info = [ 'Bo', 84, 'male', [ ' bubble 8', '9 drink']]
# Bo take all students interested
Print (student_info [. 3])
# students to take a second Bo hobby
Print (student_info [. 3] [1])
# priority control operation:
# 1, access by index value (forward + reverse access access): deposit may be taken to
print (student_info [-2 ]) # Bo
# 2, a slice (care regardless of the end, step)
Print (student_info [0:. 4: 2]) # [ 'Bo', 'MALE']
#. 3, the length of the
print (len (student_info)) # 4
# 4, members of the operations in and in not
Print ( 'Bo' in student_info) # True
Print ( 'Bo'in student_info Not) False #
#. 5, additionally
student_info = [ 'Bo', 84, 'male', [ ' bubble 8', '9 drink']]
# value is added at the end of a list student_info
student_info.append ( 'Anhui cattle students, Hefei University')
Print (student_info)
# 6, delete the
# to delete the list index value of 2
del student_info [2]
Print (student_info)
# 7, cycling
for student in student_info:
Print (Student)
# extra attention to the need to master:
student_info = [ 'Yin Haoqing', 95, 'female', [ ' embarrassed dance', 'shouted Michael'], 95]
# 1.index obtain a value of the index list
Print (student_info.index (95)) # 1
# 2.count get a list of the number of a value of
Print (student_info.count (95)) # 2
# 3. values, default take the last value in the list, similar to the deleted
# If the pop () parentheses written index corresponding to the index value is taken
student_info.pop ()
Print (student_info)
# remove list index value of 2, and assigned to the variable name sex
sex = student_info.pop (2)
Print (Sex)
Print (student_info)
student_info = [ 'Yin Haoqing', 95 'FEMALE', [ 'embarrassed dance', 'call wheat'], 95]
# 4 removed, the value of a first value in the list to remove
student_info.remove (95)
Print (student_info) # [ 'Yin Haoqing', 'female', [ 'embarrassed dance', 'shouted Michael'], 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', 'call wheat'] 95]
# in student_info, the index of the position of the insert 3 "Hefei"
student_info.insert (3, 'Hefei')
Print (student_info)
# 6.extend coalescing list
student_info1 = [ 'Yin Haoqing', 95, 'female ', [' embarrassed dance 1 ',' call wheat 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)
2. Data type ancestral
# Tuple ((1, 2, 3, ' five', 'six'))
tuple1 = (. 1, 2,. 3, 'five', 'six')
Print (tuple1) # (. 1, 2,. 3, 'five ',' six ')
# priority control operation (important):
# 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 = '_')
3. immutable type with variable # # immutable type 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)) # 2771339841896
= str1 '! Python Hello'
Print (ID (str1)) # 1,975,751,484,528
str2 = str1.replace ( 'Hello', 'like')
Print (ID (str2)) 1975751484400 #
# 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)
4. The method of built-in dictionaries dict1 = dict ({ 'age' : 18, '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 parenthesis key
Print (dict1 [ 'Age'])
# priority control operation:
# 1, the access key press value: can be kept preferably
kept a level: 9 value to dict1 dictionary
dict1 [ 'Level'] =. 9
Print (dict1) # { 'Age': 18 is, 'name': 'Tank', 'Level':. 9}
Print (dict1 [ 'name']) # Tank
# 2, the length len
#. 3, member operator in and not in only the determination key dictionary
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 all the dictionary key
Print (dict1.keys ())
# dictionary all values obtained values
Print (dict1.values ())
# get all the items in the dictionary
Print (dict1.items ())
#. 6, the cycle
all the dictionary traversal cycle # Key
for in dict1 Key:
print (Key)
print (dict1 [Key])
GET
dict1 = { 'Age': 18 is, 'name': 'Tank'}
print (dict1.get ( 'Age'))
# [] value
print (dict1 [ 'Sex']) # KeyError: 'Sex'
# GET value
print (dict1.get ( 'Sex')) # None
# if no sex, to set a default value
print (dict1.get ( 'sex', 'male') )
5. Process control
# 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 into the
' ''
str1 = 'Tank'
# the while loop
the while True:
name = the iNPUT ( 'Please enter the characters guess:') .strip ()
IF name == 'Tank':
Print ( '! Tank Success')
BREAK
Print (' re-enter ! ')
# limit cycles
str1 =' tank '
# Initial value
# 0 = 0. 1 NUM 2. 3
# the while loop
the while NUM <. 3:
name = INPUT ( 'Enter guess character:') .strip ()
IF name == 'Tank':
Print ( '! Tank Success')
BREAK
print ( 'Please reenter!')
NUM = +. 1
6. File processing
Write Text File #
# a parameter: the absolute path of the file
# two parameters: operating mode mode 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 a text file == RT R & lt
F = Open ( 'file.txt', 'R & lt', encoding = 'UTF-. 8')
Print (reached, f.read ())
f.close ()
# append written text file
A = Open ( 'file.txt', 'A', encoding = 'UTF-. 8')
a.write ( '\ n HEFEI')
a.close ()
# write
with Open ( 'file1.txt', 'w', encoding = 'UTF-8') AS f:
f.write ( 'Murphy's Law')
# read
Open with ( 'file1.txt', 'R & lt', encoding = 'UTF-. 8') AS F:
RES = F.read()
print(res)
# 追加
with open('file1.txt', 'a', encoding='utf-8') as f:
f.write ( 'Siege')
f.close ()
# reading photo cxk.jpg
with Open ( 'cxk.jpg', 'RB') AS F:
RES = reached, f.read ()
Print (RES)
JPG = RES
# the cxk.jpg binary stream written cxk_copy.jpg file
with open ( 'cxk_copy1.jpg', 'wb') AS f_w:
f_w.write (JPG)
# managed through with two f_r open file handles open , f_w
with Open ( 'cxk.jpg', 'RB') AS F_r, Open ( 'cxk_copy2.jpg', 'WB') AS f_w:
# stream to read out the binary image by F_r handle
res = f_r.read ()
# by f_w handle the flow picture binary file written cxk_copy.jpg
f_w.write (res)
7. The basic function of the three forms of defined functions: 1. No function parameters necessary to receive incoming external parameters. 2. Reference function
Need to receive external incoming parameters.
3. empty function
function call:
function name + () call
# 1. No function parameter
DEF Login ():
User = INPUT ( 'Enter Username') .strip ()
pwd = INPUT ( "Please enter your password '). Strip ()
IF the User == 'Tank' and pwd == '123':
Print ( 'the Login successful!')
the else:
( '! the Login error') Print
# memory address of the function
Print (the Login)
# function call
login ( )
# 2 has a function of parameters
username, password for receiving incoming external value
DEF Login (username, password):
user = iNPUT ( 'enter a username') .strip ()
pwd = iNPUT ( "Please enter your password ' ) .strip ()
IF the User == == username and password pwd:
Print ( 'the Login successful!'
Print ( 'Login error!')
# function call
# if function will receive parameters when defining a caller must pass through their participation
Login ( 'Tank', '123')
3. empty functions
# login function
def login () :
# do not mean anything
Pass
# registration function
DEF the Register ():
# do nothing on behalf of
Pass
# payment function
DEF rePay ():
Pass
parameter # function:
3 in the definition phase: x, y called shape parameters.
FUNC DEF (X, Y): # X, Y
Print (X, Y)
# calling phase: 10, 100 call arguments.
FUNC (10, 100)
Position # Parameters:
Position # parameter
# argument position
# must eleven positions in accordance with parameter passing.
During the definition phase #: position parameter
DEF FUNC (X, Y): # X, Y
Print (X, Y)
# calling phase: 10, 100, said location argument.
func (10, 100) # 10 100
# Keyword arguments:
# Keyword argument
# pass parameters by keyword.
# Position parameter X, Y
DEF FUNC (X, Y):
Print (X, Y)
# calling phase: 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 transmission
func (y = 111, x = 222, z = '333') # error TypeError
# default parameters:
# During the definition phase, as the parameter's default value
DEF foo (X = 10, Y = 20 is):
Print (X, Y)
# does not pass parameters, the default parameters
foo ()
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 search order:
Local ---> Global ---> Built
# 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': F2}
choice = INPUT ( 'select numbers:')
IF == choice '. 1':
Print (DIC1 [choice])
DIC1 [choice ] ()
elif Choice == '2':
Print (DIC1 [Choice])
DIC1 [Choice] ()
X 10 =
# namespace
# nested function definitions
DEF func1 ():
# = X 20 is
Print ( 'from func1 ... ')
Print (X) given #
X = 30
DEF func2 ():
Print (' ... from func2')
Func1 ()