Part 1: Python study notes
===================
4.4. Dict dictionary table
4.4.1 Example 1: {statement}
- Is defined: d = { 'ISBN': '234234', 'Title': 'Python Started', 'Price': 39.00}
- Display value: d [ 'Title'] -> 'Python entry'
- d['Price'] --> 39.00
- Append: d [ 'Author'] = 'Jerry' Note: for the dictionary table does not exist in the key will be automatically added, but not such a list operation
- Key obtained: d.get ( 'Price') -> 39.00
- Return did not get the key: d.get ( 'price', 0.0) -> 0.0
- Support situ changes: d [ 'Price'] = 99.00
4.4.2 Example 2: function declaration
- 定义:emp = dict(name='Mike',age=20,job='Dev')
- len(emp) --> 3
- . Merge operation: 1 dep = { 'department': 'Technology'}; 2. emp.update (dep)
- Eject key: emp.pop ( 'age') -> 20
4.4.3. Statement
- {Key: value, ...}
- dict (key = value)
4.4.4 Operation
- Obtain
- D [ 'key']
- d.get ( 'key', the default)
- merge
- d.update(d2)
4.4.5. Properties
- keys()
- values()
- items() : Key+value
- Supports nested: emp = { 'age': 20, 'name': { 'firstname': 'Jerry', 'lastname': 'lee'}, 'dept': 'Administration'}
4.4.6. Sort key
- The keys () into the list
- ks = list(d.keys())
- ks.sort()
- Use global function sorted ()
- ks = d.keys()
- for k in sorted(ks):
- Print(k,d.get(k))
4.5. Tuple tuple
4.5.1. Characteristics
- Ordered set of arbitrary objects
- Access by the subscript
- It is "immutable" type
- t = (1,2,3,4,5)
- t [0] = 99 -> given
- Length is fixed, of any type, any nested
4.5.2. Statement
- (Element) i.e., (1,2)
- 1,2 omit statement () of
- index (val): Find index
- (Val) count: the number of statistics
4.5.3 Calculation
- (1,2)+(3,4) --> (1,2,3,4)
4.5.4. Assignment
- x = (40,) --> (40,)
- x = 40, --> (40,)
- len (x) -> 1
4.5.5.Named tuple
- from collections import namedtuple
- Employee = namedtuple('Employee',['name','age','department','salary'])
- jerry = Employee ( 'Jerry', age = 30, department = 'Finance Department', salary = 9000.00)
- jerry.name --> 'Jerry'
- jerry.salary --> 9000.0
4.6 File
4.6.1. The basic syntax
- file = open ( 'filename ", mode)
- mode
- r Reading
- w write
- a Append
- b binary
- 示例:f = open('data.bin','rb').read()
- + Can read, but also write
- Examples
- Hello.txt a newly created file: myfile = open ( 'hello.txt', 'w')
- myfile.write ( 'Youpin class \ n') -> 5
- myfile.write('Hello world!\n') --> 13
- A reading of file: f = open ( 'hello.txt')
- reached, f.read () reads the entire contents of which, corresponding to the pointer
- f.readline () to read each row of information
- l = open ( 'hello.txt') readlines () -.> [ 'excellent product class \ n', '! Hello world \ n']
- for line in l:
- print(line)
- Chinese showed normal: f = open ( 'course.txt', 'w', encoding = 'utf8')
- f.write ( 'class Youpin Python Tutorial \ n') -> 14
- f.write('www.codeclassroom.com') --> 21
- f.close()
- Hello.txt a newly created file: myfile = open ( 'hello.txt', 'w')
4.6.2 Operation
- read()
- readline()
- readlines()
- close()
- Examples
- x,y,z = 1,2,3
- l = [1,2,3]
- f = open('datafile.txt','w')
- f.write('{},{},{}'.format(x,y,z)) -->5
- f.write(str(l)) -->9
- f.close()
4.6.3.pickle access Python objects
- dump (objects, object files)
- load (file)
- Example 1: d = { 'a': 1, 'b': 2}
- f = open('datafile.pkl','wb')
- import pickle
- pickle.dump(d,f)
- f.close()
- open('datafile.pkl','rb').read()
- Example Two: f = open ( 'datafile.pkl', 'rb')
- data = pickle.load(f)
4.7. Summary
4.7.1. Collection
- sequence
- variable
- List list
- Immutable
- String str
- Tuple tuple
- Byte array
- variable
- Mapping
- Dict dictionary table
- set
- Set
4.7.2. Digital
- Integer
- int
- boolean
- Float
- float
- Decimal
- ... scores
4.7.3. Callable
- Function function
- Generator Generator
- Class Class
- Method Method
4.7.4. Other
- file
- None
- view
- ...
4.7.5. Internal
- Type
- ...
5. statements, expressions and flow control
5.1. Code style
5.1.1. Code Format Guide
- PEP8
- Indent 4 spaces
- No more than 79 characters in a row
- Blank line
5.2. Assignment statements
5.2.1 Example 1: x = 5
5.2.2 Example 2:. (X, y) = (5,10)
- or x,y = 5,10
5.2.3 Example 3:. [X, y, z] = [10,20,30]
- Exchange: x, y = y, x
- or [a,b,c]=(1,2,3)
5.2.4 Example 4:. A, b, c = 'uke'
- a --> 'u'
- b --> 'k'
- c --> 'e'
5.2.5. Basic
5.2.6. Sequence assignment
- Example 1: a, b, c = 'you' no problem
- a, b, c = 'youpin' error
- 1 modified to
- a,b,c = s[0],s[1],s[2:]
- 2 or modify a, b, * c = s
5.2.7 The extended sequence unpacking assignment
- * Variable, get the remaining elements to list
- Example: s = 'youpin'; a, b, * c = s
5.2.8. Multi-target assignment
- a = b = c = 'week'
5.2.9. Assignment parameterization
- x = 5; y = 6; x = x + y; x --> 11
- a,b = 1,2; a += b; a --> 3
- + = Or extend () to expand the list of values
5.3. Expression
5.3.1. Function Call
5.3.2. Method Invocation
5.3.3. Literals
5.3.4. Print Operations
- print()
- sep = 'separator'
- end = 'terminator'
- file = specified file
- 示例:print(s,url,url2,end='......\n',file=open('result.txt','w',encoding='utf8'))
5.4. Process control
5.4.1.if .. statement
- The general format
- score = 75
- if score >= 60:
- print ( 'pass')
- else:
- print ( 'fail')
- Achieve multiple branch
- Ternary operator
- a = Y if X else Z
- Example: result = 'pass' if score> = 60 else 'fail'
- a = Y if X else Z
5.4.2.while cycle
- The general format
- x = 'youpinketang'
- while x:
- print(x,end=' ')
- x = x[1:]
- break out of
- continue jump head cycle, cycle performed physiognomy
- pass placeholder
- else
5.4.3.for cycle
- The general format
- for x in target sequence:
- Example: for x in [1,2,3,4]
- Subsequent statements pass
- Example: print (x, end = '')
- for x in target sequence:
- range () Returns the generated object, not a list
- for x in range(1,100):
- enumerate()
- s = 'youpinketang'
- for (idx, item) in enumerate(s):
- print('{},{}'.format(idx + 1,item))