day02 python

List
Definition: re [], the values can be stored in a plurality of arbitrary types, separated by commas. General student interests, class periods, etc. for storage.
# Define a list of students, the student can store a plurality of
Student = [ 'NAME1', 'NAME2', 'NAME3', 'NAME4']
Print (Student [. 1]) # NAME2
= student_info [ 'NAME1', 28, 'MALE', [ 'chifan', 'Heshui']]
# fetch all interested
Print (student_info [. 3])
# interested in taking a second
print (student_info [3] [1 ])
The priority control operations:
1. Press the index value to access (access + Reverse Forward Access): also desirable to deposit
Print (student_info [-2])
2. slice (care regardless of the end, step)
Print (student_info [0: 4: 2 ]) # [ ' Bo', 'MALE']
3. length
Print (len (student_info)). 4 #
4. members in operation in and Not
#Print ( 'Bo' in student_info) # True
#Print ( 'Bo' not in student_info) #False
# 5. Additionally
student_info = [ 'Bo', 14, 'male', [ ' eat', 'drink']]
# value is added at the end of a list student_info
student_info.append ( 'Hefei')
Print (student_info)
# 6. Delete
to delete the list index value of 3
del student_info [3]
Print (student_info)
You need to know:
student_ info = [ 'NAME2', 95, 'FEMALE' [ 'embarrassed dance', 'shouted Michael'], 95]
1. index value to obtain a list of index
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, delete similar, if pop () parentheses write index, taking the index value corresponding
student_ info. pop () pr int (student_ info)
takes a value list index 2, and assigned to the sex variable name
sex = student_ info. POP (2)
Print (sex)
Print (student_ info)
4. removed, the value of a first value in the list to remove
student_ info. remove (95)
Print (student_ info) # [ 'NAME2', 'FEMALE', [ "embarrassed dance '' shout wheat '], 95]
name = student_ info. the Remove (' NAME2 ')
Print (name) #none
Print (student_ info) # [' FEMALE ', [embarrassed dance', 'shout wheat' , 95]
5. Insert value
= student_info [ 'NAME2', 20.'female ', [' tiaowu ',' hanmai '], 20 is]
# in student_info, the insertion of an index of 3' Hefei '
student_info.insert (3,' Hefei ' )
Print (stduent_info)
6.extend coalescing list
student_info1 = [ 'NAME2', 20 is, 'FEMALE', [ 'tiaowu', 'hanmai'], 20 is]
student_info2 = [ 'NAME3', 22 is, 'MALE', [ ' chifan ',' shuijiao ']]
# student_info2 all the values into the student_info1
student_info1.extend (student_info2)
Print (student_info1)
Tuple:
Definition:
in () may be any type storing a plurality of values, separated by commas. Note:
tuples and lists different - like that, only when defining initialization value can not be modified. Advantage:
occupies memory resources smaller than list.
Immutable type: change the variable value of the memory address must be different
numeric types: int float string Type: str tuples: tuple
Variable Type:
List type: list dictionaries: dict
Dictionary Type:
action: in the {}, separated by commas can store a plurality of values, access to key-value, speed value
defined: key Tourmaline is immutable, value may be any type
1. Press the key memory value: preferably be kept
# deposit a level: 9 to the value dict1 dictionary
dict1 [ 'Level'] = 9
Print (dict1) {# 'Age': 18 is, 'name': 'Xiao', 'Level': }. 9
Print (dict1 [ 'name']) #xiao
2. length len
3. Not in members and in operation (determination key only dictionary)
Print ( 'name' in dict1) #True
Print ( 'Xiao' in dict1 ) #False
Print ( 'Xiao' Not in dict1) #True
4. remove
del dict1 [ 'Level']
Print (dict1) {# 'Age': 18 is, 'name': 'Xiao'}
5. The button keys () the value values (), key-value pairs items ()
# get all the key dictionary
print (dict1. Keys ())
# All values obtained values dictionary
Print (dict1.values ())
# give all the dictionary iyems
Print (dict1.items ())
6. The loop
# loop through all of the dictionary Key
for Key in dict1:
    Print (Key)
    Print (dict1 [Key])
dict1 = { 'Age': 18 is, 'name': 'Xiao' }
Print (dict1.get ( 'Sex'))
# if no sex, to set a default value for
print (dict1.get ( 'age') )
if the judge
syntax:
if the judgment condition:
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
else:
If the determination is not satisfied, the execution of code here
logic code
# Analyzing two numbers size
X 10 =
Y = 20 is
Z = 30
IF X> Y:
    Print (X)
elif Z> Y:
    Print (Z)
the else:
    Print (Y)
#while loop
syntax:
the while conditional:
    # perform this establishment the
    logic code
break out of this loop
continue the end of this cycle, the next cycle enters
the while True:
    name = iNPUT ( 'Please input character guess:') Strip ().
    IF name == 'Xiao':
        Print ( 'Xiao Success! ')
        BREAK
    print ( 'Please reenter!')
# limit cycles
STR = 'Xiao'
# initial value
NUM = 0
#while circulating
the while NUM <. 3:
    name = INPUT ( "Please enter the character guess: '). Strip ()
    IF == name 'Xiao':
        Print ( '! Xiao Success')
        BREAK
    Print ( '! Please re-enter')
    NUM = +. 1
During the execution of python file:
1. first start the python interpreter, loaded into memory.
2. Load written python files to the interpreter.
3. Detection python syntax to execute code.
Note: You must specify character encoding, write in what way, what would be opened. Such as: utf-8
Append text file written
A = Open ( 'file.txt', 'A', encoding = 'UTF-8')
a.write ( '\ the n-HEFEI')
a.close
The context of the management of document processing:
#with can manage open open files will automatically call close () when finished with closing files
with open ()
with open () AS f "handle"
# write
with open ( 'file1.txt ',' W ', encoding ='. 8-UFT ') AS F:
f.write (' Murphy's Law ')
# read
with open (' file1.txt ',' r ', encoding =' uft-8 ') F AS:
RES = reached, f.read ()
Print (RES)
# additionally
with Open ( 'file1.txt', 'A', encoding = '. 8-UFT') AS F:
f.write ( 'Siege')
#f .close ()
with a plurality of management files
# managed by two with f_r open file handles open, f_w
with Open ( 'name.jpg') AS F_r, Open ( 'name_copy.jpg', '' WB) AS f_w:
# by F_r handle the picture binary stream read out
RES = f_r.read ()
# write the picture binary stream name_copy.jpg file by f_w handle
f_w.write (res)
Functions
What is a function?
Function refers to is actually a tool
benefits of using functions:
1. To solve the problem of redundant code
2. Make the code more clear structure
3. Easy managed
to use the function must follow: define, after calling;
three forms defined functions : no reference function (need to receive incoming external parameter) with a reference function (need to receive incoming external parameters), empty function (Pass)

1. No function parameter
DEF Login ():
    User = INPUT ( 'Enter a username') .strip ()
    pwd = INPUT ( 'Enter Password') .strip ()
    IF User == 'Xiao' and pwd == '123':
        Print ( '! Login successful')
    the else:
        Print ( 'Login error!')
# memory address of the function
Print (Login)
# function call
Login ()
2. function has parameters
username, password for receiving external mass the value of
DEF Login (username, password):
    user = iNPUT ( 'enter a username') .strip ()
    pwd = iNPUT ( 'enter password') .strip ()
    IF user == == username and password pwd :
        Print ( 'the Login successful!')
    the else:
        Print ( 'the Login error!' )
# Function call
# if function will receive parameters when defining a caller must pass through their participation
the Login ( 'xiao', '123')
3. Empty function
# login feature
DEF the Login ():
# do nothing on behalf of
pass
Parameters Parameter:
# definition phase: x, y call parameter.
FUNC DEF (X, Y):
Print (X, Y)
# calling phase: 10,100 call parameter
FUNC (10,100)

Guess you like

Origin www.cnblogs.com/LiXue-/p/11086340.html