Python Study Notes 1: Data Type, python Study Notes
I. Python file type
1. Source Code
Hello. py:
1 #!/usr/bin/python2 print "hello world"
2. byte code: The python source file generated after compilation with the extension "pyc"
Compile method (compile. py ):
import py_compilepy_compile.compile('hello.py')
Then execute:
$ python compile.py
A binary hello. pyc file is generated.
3. Optimization code: optimized source file with the extension pyo
Execute the following command optimization in the command line:
$ python -O -m py_compile hello.py
Ii. python Variables
Python variable assignment (variable and immutable)
Iii. python Operators
1. Integer Division //
That is, only the integer part in the result is taken:
>>> 3.0 // 21.0>>> 3 // 21
2. Power Calculation **
>>>3**327>>>3**29
3. Logical and, logical or, logical non-
>>> 1>2 and 3>2False>>> 2>1 and 3>2True>>>1>2 or 3>2True>>>1>2 or 1>3False>>> not 1>2True>>> not 2>1False
4. Operator priority:
Iv. python Data Types
1. Number: Integer, long integer, floating point, plural
The plural (expressed by j ):
>>> a=3.14j>>> type(a)<type 'complex'>>>>
2. String: Same as single double quotes
>>> str1="a">>> str2='a'>>> id(str1)140710100204544>>> id(str2)140710100204544>>> mail="""tom:... i am jack... goodbye... """>>> print mailtom: i am jack goodbye>>>
Slice: [Start: stop: step]
That is, [start index: End index: Step value]
Start index: same as other languages, it starts from 0. In the sequence from left to right, the index of the first value is 0, and the last value is-1.
End index: the slice operator takes the index until it does not contain the index value.
Step Size: The default value is one after another. If the value is 2, it indicates performing the next fetch operation. When the step size is positive, it indicates taking from left to right. If it is negative, it indicates taking from right to left. The step size cannot be 0.
NOTE: If it is a string [:] mode, it is [start: stop].
Example:
>>>exam="abcdefghi" >>>print exam[:-1]abcdefgh >>>print exam[2:]cdefghi >>>print exam[:7:2]aceg >>>print exam[:3:-1]ihgfe
Note:The last row is output in reverse order because the third parameter is-1, but the index is not in reverse order.
3. List: Process the data structure of a group of ordered projects, which is variable type data, expressed in [], including multiple projects separated by commas.
>>> l=['jim',25,'male']>>> l['jim', 25, 'male']>>> type(l)<type 'list'>>>> l[0]'jim'>>> l[0]='tom'>>> l[0]'tom'>>> l['tom', 25, 'male']
Empty list:
>>>l=[]
There is only one value:
>>>l=['abc']
List common methods:
Value: list [index] or list [start: end: offset]
Append: list. append (x );
Delete: del (list [index]) or list. remove (list [index])
Modify: list [index] = x
Search: var in list
>>>l=['a',1]>>>'a' in lTrue>>>'b' in lFalse
4. tuples: Similar to the list, it is immutable just like a string, that is, you cannot modify the tuples (only one can be re-created in memory ).
- Project definitions separated by commas in parentheses for tuples
- Tuples are usually used to securely use a group of values for statements or user-defined functions. That is, the values of the used tuples do not change.
>>>userinfo=("tom", 30, "man")>>>userinfo[0]"tom"
Empty tuples:
>>>t=()
Single element tuples:
>>>t=(1,)
Note: The comma (,) cannot be omitted.
Receiving tuples with variables:
>>> t('jim', 25, 'man')>>> name,age,gender=t>>> name'jim'>>> age25>>> gender'man'>>>a,b,c=(1,2,3)>>>a1>>>b2>>>c3
5. Dictionary: Unique ing type in python (hash table)
Creation method:
(1 ){}
>>>dic={0:0,'a':'abc'}>>>dic[0]0>>>dic['a']'abc'
(2) Use the factory method dict ()
>>> fdict=dict([('x',1),('y',2)])>>> fdict{'y': 2, 'x': 1}
(3) fromkeys (): the elements in the dictionary have the same value. The default value is None.
>>> d={}.fromkeys(('x','y',0), -1)>>> d{'y': -1, 'x': -1, 0: -1}
Common dictionary methods:
(1) access updates with key values
(2) del dict1 ['a'] Delete the elements whose key value is a in the dictionary.
(3) dict1.pop ('A') deletes and returns an element whose key is 'A '.
(4) dict1.clear () delete all dictionary elements
(5) del dict1: Delete the entire dictionary
(6) str (dict1) to string
(7), get (key [, msg]) if the key does not exist, return the msg (not empty) value.
(8) dict1.items () returns the list of key-value pairs
>>> d{'a': 1, 'b': 2}>>> d.items()[('a', 1), ('b', 2)]
(9) and dict1.keys () return the list of the dictionary keys.
>>> d{'a': 1, 'b': 2}>>> d.keys()['a', 'b']
(10) dict1.setdefault (key, default = None) returns its value if the key exists; otherwise, dict1 [key] = default
(11) dict1.update (dict2): add the key-value pairs in dict2 to the dictionary dict1. If there are duplicate key-value pairs, overwrite them. Otherwise, add them.
(12). len (dict1) returns the number of items in the dictionary.
>>> d{'a': 1, 'b': 2}>>> len(d)2
6. Sequence: Lists, strings, and tuples are all sequences.
Slice: a sequence is followed by square brackets. There are a pair of optional numbers separated by colons. Numbers are optional and colons are required. For example:
>>>exam="abcdefghi" >>>print exam[:-1]abcdefgh >>>print exam[2:]cdefghi >>>print exam[:7:2]aceg >>>print exam[:3:-1]ihgfe
Index: The sequence is followed by a square brackets with a number (which can be a negative number ). For example:
>>>exam="abcdefghi" >>>print exam[0]a
5. Basic sequence operations
1. len: sequence length
>>>exam="abc" >>>len(exam)3
2. +: connect two sequences
>>>str1="abc" >>>str2="d">>>str1+str2abcd
3. *: repeated sequence elements
>>>str1="abc " >>>str1*3abc abc abc
4. in: determines whether an element is in a sequence.
>>>str1="abc " >>>'c' in str1True>>>'x' in str1False
5. max: returns the maximum value.
>>>s='12345'>>>max(s)'5'
6. min: returns the minimum value.
>>>s='12345'>>>min(s)'1'
7. cmp (tuple1, tuple2): compare whether the two sequence values are the same
>>>str1='abc'>>>str2='123'>>>cmp(str1,str2)1>>>str1='1'>>>cmp(str1,str2)-1>>>str1='123'>>>cmp(str1,str2)0
VI,