1. Assigning values to variables
>>> a = 1>>> b = a>>> A = 2>>> B1
It can be seen that when B is assigned to a, it is not changed by the change of a, or the original value. The principle here is that B, when assigning a, is doing a block of memory where B points to the value of a variable, so when a variable changes the value, a variable stores a new address block in memory and does not affect the memory point of the B variable.
2. Simple Assignment
Chained assignment
A=b=1
Assigning multiple variables at the same time
User,passwd= ' Jack ', ' xxxx '
3. Input function for Python 2.x
In Python 2.x, the input function accepts a value that requires a type, for example, a string type that needs to be quoted, without quotes being treated as a variable
4. Constant settings in Python
Constant concept: cannot be changed;
There is no concept of constants in Python, and if you need to set a variable that is not changed, you can see the code in all uppercase variables, which is immutable.
5. Importing the module method
Import OS #导入os模块from os import os.path #仅导入os模块中的os. Path method
6. What is PYC and when is the PYc file returned?
6.1 PYc is a bytecode file for explanatory languages that can be read by the interpreter.
6.2 It is explained here that PYC is not always present, but only when the py file is imported as a module (that is, imported by the other PY program import).
The 6.3 interpreter eventually executes the PYc file instead of the py file.
6.4 Import a PY file after importing, generated the PYc file, then changed the py file, PYc file will be changed according to the modified time to new in the Py file generation PYc
7. Long integer type has been canceled in Python 3.x
8.
The following string concatenation should be avoided so that the name variable opens up a separate memory space, consuming unnecessary memory
name = ' Jack ' Print (' name is ' +name)
Change the concatenation of strings to a formatted string notation:
name = ' Jack ' Print (' name is%s '% name)
9. Shards in the list
>>> name = [' Jack ', ' Eason ', ' Tony ', ' John ', ' Python ']>>> name[0:2][' Jack ', ' Eason '] #列表分片包含头不包含 Tail >>> name[2:][' Tony ', ' John ', ' Python '] #如果要输出到最后一个元素, Shard End Index not written
10. How to check the list
name = [' Jack ', ' Eason ', ' Tony ', ' John ', ' Python ']# add Name.insert (2, ' xxx ') #在列表索引2位置上, insert element value ' xxx ' name.append (' xxx ') # Append an element to the list at the end of the value of ' xxx ' # delete name.remove (' xxx ') # remove element ' xxx ' Name.pop (2) # Delete index position 2 element del Name[2:4] # delete 2 to 4 element (shard delete hair, same contains header does not contain tail) # Change name[1] = ' xxx ' # change the value of index position 1 to ' xxx ' # check name.index (' xxx ') # Find the index position of the first element matching to ' xxx '
11. Deep copy and shallow copy
# shallow copy >>> num = [1,2,3,[1,3,5]]>>> num2 = num.copy () # Copy num list to num2>>> num[0] = 0>>> Num[0, 2, 3, [1, 3, 5]]>>> num2[1, 2, 3, [1, 3, 5]] # num occurs that change, num2 does not change >>> num[0, 2, 3, [1, 6, 5]]>> ;> num2[1, 2, 3, [1, 6, 5]] # The nested list of num is changed, and num2 will change.
In shallow copy, the nested list is not copied directly in the shallow copy, but instead copies the point of an address, so when Num's nested list is changed, NUM2 will follow the change, and if the nested list needs to be copied completely, you can use deep copy.
# deep copy >>> num = [1,2,3,[1,3,5]]>>> import copy>>> num2 = copy.deepcopy (num) >>> num [3] [1] = 6>>> num[1, 2, 3, [1, 6, 5]]>>> num2[1, 2, 3, [1, 3, 5]]
Extension: Shallow copy and chained list assignment are different
>>> num = num2 = [1,2,3,[1,3,5]]>>> Num2[0] = 6>>> num[6, 2, 3, [1, 3, 5]] # You can see this chained assignment, and it doesn't copy function, instead is a ' hard link ' function like a file in Linux, when any one change is changed
12. Some methods of string
# Remove the trailing spaces name = ' Jack ' Name.strip () # string split into sequence >>> ' 1+2+3+4+5 '. Split (' + ') [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']# sequence composed of strings >> > dir = [', ' usr ', ' bin ', ' env ']>>> '/'. Join (dir) '/usr/bin/env ' # string formatting method >>> msg = ' Hello, {name} ' &G t;>> Print (Msg.format (name= ' Alex ')) Hello, alex>>> msg = ' Hello, {0}, {1} ' >>> print (Msg.format ( ' Alex ', ' Jack ')) Hello, Alex, jack# formatted output string >>> name = ' Eason ' >>> print (Name.center (40, '-')) # prints the name, Occupies a total of 40 character positions, using '-' padding-----------------Eason in the blanks------------------
13. Some methods of the dictionary
# Get a key corresponding value Dic.get (key) #区别于dic [key] This value, if key does not exist, get method return none, and Dic[key] will error # get all key value Dic.keys () # Get all value Values Dic.values () # Delete dictionary one item del dic[xxx][xxx] Dic.pop (key) # Set key default value Dic.setdefault (key, values) # take DIC's key value if not present , a key is created by default, and the default value is values
This article is from "Cool King" blog, please be sure to keep this source http://coolk.blog.51cto.com/1752609/1775159
Python Learning Note-day2