The first reason to learn Python is that it is relatively simple and has plenty of time for winter vacation, and I heard that the functions are also very good. The most important thing is that I will use it in this year's project.
And Liu Rujia’s book says that it would be a shame if a good Acmer doesn’t know a little Python. Don't talk nonsense, look at the learning content of the first day!
How to write Python 1. Enter python under cmd to enter development mode 2. Python editor: IDLE, aptana, I use IDLE here
Constants and variables 1. The definition of constants: the method of using objects, (although I still don’t know how python implements object programming)
1 class _const(object):
2 class ConstError(TypeError): pass
3
4 def setattr(self,name,value):
5 if self.dict.has_key(name):
6 raise self.ConstError, "Can't rebind const(%s)" % name
7 self.dict[name] = value
8
9 def delatter(self,name):
10 if name in self.dict:
11 raise self.ConstError, "Can't unbind const(%s)" % name
12 raise NameError, name
13
14 import sys
15 sys.modules[name] = _const()
1 print 'It's a dog'
2
3 print "hello\nhello"
1 print "hello boy\nhello boy"
2
3 print r'hello boy\nhello boy'
1 #string repeat
2 string="Yinjian"
3
4 print string*20
5
6 _str = "YinJianPython"
7
8 c = _str[0]
9 print c
10
11 # The slicing operator [a:b] is a,b-1 which is closed left and opened right
12 _ str1 = _str[:2]
13 print _str1
14
15 _ str1 = _str[:3]
16 print _str1
17
18 _ str1 = _str[:]
19 print _str1
Data type 1, basic data type (number + string) 2, list
stu = ["Yinjian","xixi"]
print stu[1]
stu[1] = "xixi"
print stu[1]
1 stu = ("Yinjian","Tom")
2
3 print stu[1]
4
5 stu = (1,2,3)
6 print stu[1]
1 set1 = set("skdfjsofd")
2 set2 = set("dsflksdf")
3
4 print set1&set2
5
6 print set1|set2
7
8 print set1-set2
9
10 new = set(set1)
11 print new
1 lis = [1,2,2,3,3,"hello","hello","xixi"]
2
3 sett = set(lis)
4
5 print sett
6
7 lislen = len(lis)
8
9 print lislen
10
11 settlen = len(sett)
12 print settlen
pickle (solution to use an object for a long time, 1: load into memory, 2: third-party file)
import pickle
lista = ["ming yue ","ji shi ","you"]
listb = pickle.dumps(lista)
print listb
listc = pickle.loads(listb)
print listc
group = ("ba jiu ","wen ","qing tian")
f1 = file('1.pk1','wb')
pickle.dump(group,f1,True)
f1.close()
f2 = file('1.pk1','rb')
t = pickle.load(f2)
print t
f2.close()
Reprinted at: https://www.cnblogs.com/TreeDream/p/6345173.html
Recommended Posts