Python training the next day - the basics 2

'' '' '' 
'' '
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 ([' Joe Smith ',' John Doe ',' king of five '' Zhao six '])
# Students. = [' John Doe ',' John Doe ',' Wang Wu ',' six Zhao ']
# Print (Students. [. 1]) John Doe #
#
# student_info = [' Bo ', 84 'MALE', [ 'bubble 8', '9 drink']]
# # Bo take all interested students
# print (student_info [3])
# # # students to take a second hobby Bo
# print (student_info [3 ] [1])
#
# # priority operation control:
# 1 #, by accessing the index value (forward + reverse access access): may be taken to deposit
# print (student_info [-2]) # Bo
#
# # 2, a slice (care regardless of the end, step)
# Print (student_info [0:. 4:



#
# # 4, members of the operation in and Not in
# Print ( 'Bo' in student_info) # True
# Print ( 'Bo' Not in student_info) # False
#
# #. 5, additionally
# student_info = [ 'Bo', 84, 'male', [ 'bubble 8', 'drink 9']]
# # append a list of values at the end of student_info
# student_info.append ( 'hello, my school')
# Print (student_info)
#
# # 6, delete
# # to delete the list index value of 2
# del student_info [2]
# Print (student_info)

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

# need to know:
# student_info = [ 'little two' 95, 'female', [ 'embarrassed dance', 'shouted Michael'], 95]
# # 1.index obtain a value of the index list
# Print (student_info.index (95)) # 1
#
# # 2.count the number of the list get a value of
Print # (student_info.count (95)) # 2
#
# # 3. values, default take the last value in the list, similar to delete
# # If pop () brackets write the index, the index takes a value corresponding to
# student_info .pop ()
# Print (student_info)
# # extracted list an index value of 2, and assigned to the variable name sex
# student_info.pop sex = (2)
# Print (sex)
# Print (student_info)

# student_info = [ ' small two ', 95,' female ', [' embarrassed dance ',' shouted Michael '], 95]
#
# # 4. remove, the value of a first value in the list to remove
# student_info.remove (95)
# Print (student_info) # [ 'little two', 'female', [ 'embarrassed dance', 'shouted Michael'], 95]
#
# name = student_info.remove ( 'little two')
# Print (name ) None #
# Print (student_info) # [ 'FEMALE', [ 'embarrassed dance', 'call wheat'], 95]

# 5. insert value
# student_info = [ 'two small',95, 'female', [ 'embarrassed dance', 'shouted Michael'], 95]
In student_info ###, the index of the position of the insert 3 "INSTITUTE"
# # student_info.insert (3, 'COLLEGE')
## Print (student_info)

# 6.extend coalescing list
# student_info1 = [ 'two small', 95 'FEMALE', [ 'embarrassed dance 1', 'call wheat 2'], 95]
# student_info2 = [ 'small four', 94, 'female', [ ' embarrassed dance 1', 'call wheat 2']]
# # the values inserted into all student_info2 student_info1
# student_info1.extend (student_info2)
# Print (student_info1)

'' 'tuple data type' ''

#define:
# tuple ((. 1, 2,. 3, 'five', ' six '))
tuple1 = (. 1, 2,. 3,' five ',' six ')
Print (tuple1) # (. 1, 2,. 3,' five ',' six ')
# priority control operations:
# 1, according to the index value (Forward + Reverse take take): only take
Print (tuple1 [2]). 3 #

# 2, a slice (care regardless of the end, step)
# 0 from the slice to the beginning 5-1 in steps 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 parameter is \ the n-
Print ( Line, End = '_')

'' '' ''
'' '
immutable:
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)) 2771339841896 #
#
# str1 = 'Python Hello!'
# Print (ID (str1)) # 1975751484528
# 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)

"" "" ""
'' '
dictionaries:
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 ( '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 ()
# # dictionary to obtain all key
# Print (dict1.keys ())
# # dictionary to give All values values
# Print (dict1.values ())
# # get all the items in the dictionary
# Print (dict1.items ())

# 6, loop
# loop through all 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']) # KeyError: 'sex'

# Get the value of
print (dict1.get ( 'sex') ) # None
# If no sex, to set a default value
Print (dict1.get ( 'Sex', 'MALE'))

'' '' ''
'' '' ''
'' '
IF determination:
Syntax:
IF Analyzing conditions:
# If the condition is established, where the code execution
logic code

elif Analyzing conditions:
# If the condition is established, where the code executes
logic code

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

# Analyzing two numbers 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)

'' '
while loop
syntax:
while conditional:
# Established here execution
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 = INPUT (' Enter guess the character: ') .strip ()
# IF name ==' Tank ':
# Print (' Tank Success')!
# BREAK
#
# Print ( 're-enter')!


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

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

Print (' please re-enter! ')
A = 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
' ''

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!')
#
# the else:
'! the Login error' # Print (











# Username, password for receiving incoming external value
# DEF Login (username, password):
# = INPUT User ( 'Enter a username') .strip ()
# pwd = INPUT ( 'Enter Password') .strip ()
#
# IF the User pwd == == username and password:
( '! the Login successful') Print #
#
# the else:
# Print ( 'the Login error!')
#
#
# # function call
# # If the function is defined reception parameters required, the caller must pass through its reference
# Login ( 'Tank', '123')


# 3. null function
'' '
the ATM:
1. Log
2. Register
3. withdraw
4. Cash
5. transfer
6. repayment
'' '

# # sign-on feature
# def login ():
## representatives to do nothing
# Pass
#
#
# # Register function
The Register DEF # ():
# # nothing to do on behalf of
# Pass
#
#
# # payment function
# DEF RePay ():
# Pass

# ...


'' '
function parameters:
' ''
# in the definition phase: x , y called 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): # X, Y
# Print (X, Y)
# #
# # # calling phase: 10, 100, said location argument.
FUNC # (10, 100) 10 100 #
#
# '' '
# Keyword parameters:


# '' '
## 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 parameters passed
# foo (200 is, 300)

'' '' ''
'' '
nested function is defined:
inside the function defined functions.

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

Namespace function:
Built-in:
python parser comes with both called "built-in namespace."

Global:
All wore head to write 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 ': F2}

choice = INPUT (' select numbers: ')
IF == choice'. 1 ':
Print (DIC1 [choice])
DIC1 [choice] ()

elif choice ==' 2 ':







# Nested function 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/dadahappy/p/11086757.html