In front of the article: it takes more than 15 minutes (the last large size is enough) to give a general impression on python. This article is based on the pattern of an article by Chen Hao DANIEL: http://coolshell.cn/articles/42539.html.w.bicycle search :lua site: coolshell.cn
Python ranks among 3 P (perl, php, python. The industry defines it as follows: 1) scripting language: 2) glue language:
After reading this article, I will leave it as a memorandum,
This 99-page booklet describes older versions of python2.3. My ubuntu comes with python2.7.6.
And put the white deer in the teeth: 1) tips: python declares that the variable is dynamic, a = 1, a = 'hhh' indicates the type of the variable only after it is run. Enter python in the python mode under ubuntu, and enter exit () to return to ubuntu.1) print to automatically wrap the statement, if '\ n' is added, the program will change to two lines. 2) output variable. % indicates the variable. 3) Comment on a single ticket: # print ('so I cannot print this line') three single quotes or three double quotes and multiple lines comment: '''print ('so I cannot print these lines ') ''' 4) running environment: in windows, install pythonand open python.exe to run the command. E: \ python \ Doc \ here is the ptyhon manual. In linux, python is used. After I change the source, ptyhon2.7 is available for system updates (For information about source change, google: Change the source site: oschina.net) 5) when a path name is involved, Chinese characters may be garbled. (In fact, I still encountered a Chinese problem, so I decided not to use Chinese.) the solution is as follows: # This Python file uses the following encoding: UTF-8 1.1 Printing
A = 'Hello world' print 'Hello world' python3.x does not support new print (a) version of print ('Hello World') in earlier versions | hello world
Python performs the plus (+ =) operation automatically, just like the string class. The join Operation executes a plus (+ =) operation print ('hhh ', a, 'hhh') for each character in the brackets ') print ('hhh ', a, 'aaa '. join ('hhh') | hhh hello world hhh | hhh hello world haaahaaah note the following comma and percent sign print ('What is a: ',) print ('print twice a: % s, % s' % (a, a) | what is a: hello world | print twice a: hello world, hello world
1.2 data type: 1.2.1 numeric type: int, long int, float (2.3e-3 = 2.3*0.001), complex (plural:-5 + 4j) 1.2.2 string: single quotes: a = 'Hello world' | hello world double quotation marks: a = 'Hello world' | three single quotation marks hello world: a = '''hello world' ''' 1.3 OPERATOR: >>> 2*3 | 62) control statement: each statement must be followed by a colon :. A semicolon is not required at the end of a sentence. Sequence. Select: if A: elif B: else: loop: while True: do something break. The range in the loop here can only be used for integer operation.
>>> for i in range(1,5): print(i)else: print('loop over') 1234loop over
UsrName = ['allen2', 'allen2'] # This is a List data structure while True: s = input ('Please enter your name: ') for checkName in usrName: if s = checkName: print ('weclome: % s' % s) break elif s = 'root': print ('weclime: ', s) break s2 = input ('enter q to quit: ') if s2 = 'q': print ('get away! ~! ') Break
If you want to find out, you can practice it yourself.
3) Functions and modules: functions without Parameters
def syHi(): print('i am allen.lidh')syHi()>>> i am allen.lidh
Default function parameters: All parameters on the right of the default parameter must have default values. (The Compiler matches parameters from left to right. The compiler will check whether the variables have default values only after the passed parameters are matched)
Def syHi (a = 1, B, c) # def syHi (a, B = 4, c = 5): print ('a: ',, 'B:', B, 'c: ', c) syHi (1, 2) >>> a: 1 B: 2 c: 5
Local and global variables of parameters
def syHi(x): x=3 global y y=22 x=7y=77syHi(x)print('x:',x,'y:',y)
Output: >>> x: 7 y: 22
Call between modules: 1) import pyFile. The file name starting with a number cannot be referenced. Import 01 This is incorrect. Import one will be nice first in two. py contains the following: # two. pydef sayhi (): print ('I am from two. py ') version = 0.2. call between modules in py.
Module import pyFile
import twotwo.sayhi()print(two.version)>>> i am from two.py0.2
Module from pyFile import var1, func1
from two import sayhi,versionsayhi()print(version)>>> i am from two.py0.2
4) Data Structure: Linked List. (Not interesting in Chinese) | Tuple (array) | Dictionary (Dictionary: Key-Value Pair) linked list is relatively casual, elements can be deleted, added, or assigned values, but arrays cannot: immutable like string. shortlist:
>>> myList=['item1','item2']>>> myList.append('item3')>>> print('length:',len(myList),'list[0]:',myList[0])('length:', 3, 'list[0]:', 'item1')>>> del myList[0]>>> myList.sort()>>> for i in myList:print i... item2item3
Tuple:
>>> myTuple=('item1','item2')>>> yourTuple=('item3',myTuple)>>> print(yourTuple[1][0])item1
DIctionary:
>>> myDic={'happy':'laughing',... 'sad':'laughed'}>>> #add a key-value item... myDic['sb']='no-brain'>>> del myDic['sad']>>> for me,you in myDic.items():... print('me:',me,' while you ',you)... ('me:', 'sb', ' while you ', 'no-brain')('me:', 'happy', ' while you ', 'laughing')
Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Note: to learn more about these data structures, enter python in ubuntu, and then enter help (tuple.
Take tuple as an example:
>>> a=tuple()>>> a()>>> a=tuple('hahah')>>> a('h', 'a', 'h', 'a', 'h')>>> b=a+a>>> b('h', 'a', 'h', 'a', 'h', 'h', 'a', 'h', 'a', 'h')>>> for i in a: print i... hahah>>> b==aFalse>>> b>=aTrue>>>
------------------------- 5) writing scripts in the big data and cloud computing era, linux O & M should be good. In fact, I want to do the same. Writing scripts should be the basis of O & M work. The following script changes the file name of/home/allen-li/hhh.gif to '/home/allen-li/allen_lidh.gif'
Import ossource = '/home/allen-li/' fileName = raw_input ('Enter the file you want to operate on: using 'mycommand = "mv '% s' % S'" % (src_file, des_file) # unix Command Line print (myCommand) if OS. system (myCommand) = 0: print 'Return true' else: print 'Return false'
Output: the output result is that the name of the gif is changed.
allen-li@allenli-U80V:~$ python /home/allen-li/mypy1.pyenter the file you want to operate on:hhhmv '/home/allen-li/hhh.gif' '/home/allen-li/allen_lidh.gif' return True
This gif makes me laugh, but the picture is too big to pass, okay
6) Object-oriented: the inheritance notes __init () _ are used to complete class initialization, similar to the constructor C ++, self can push the pointer class A (B) of this in C ++: indicates that A inherits from B.
class Person:def __init__(self,name,sex):self.name=nameself.sex=sexdef syHi(self):print ("name:'%s' sex:'%s'" %(self.name,self.sex));class Student(Person):def __init__(self,name,sex,sid):Person.__init__(self,name,sex)self.sid=siddef syHi(self):Person.syHi(self)print("sid:'%s'" %self.sid)class Teacher(Person):def __init__(self,name,sex,tid):Person.__init__(self,name,sex)self.tid=tiddef syHi(self):Person.syHi(self)print("tid:'%s' " %self.tid)s=Student('allen.lidh2','boy2',100)t=Teacher('allen.lidh3','boy3',100100)s.syHi()t.syHi();
Output:
Allen-li @ allenli-U80V :~ $ Python/home/allen-li/desktop/mypy1.pyname: 'allen. lidh2 'sex: 'bo2' sid: '000000' name: 'allen. lidh3 'sex: 'boy3 'tid: '123'
7) file operations and exceptions
Import timetry: f = file ('/home/allen-li/desktop/allen.lidh.txt', 'wb + ') # What is the difference between the file here and the following open? S = "hello I am allen. running H \ n in shu "# f. write (bytes (s, "UTF-8") # This is python 3. x. f. write (s) # The write and read operations cannot be performed at the same time. If you have learned the operating system, you should know that this is the Buffer Zone. f = open ('/home/allen-li/desktop/allen.lidh.txt ', 'wb + ') while True: # line = f. readline () line = f. read (16) #16 bytes read once if len (line) = 0: breakprint (line) finally: f. close () print ('Bye: % s' % time. strftime ('% Y-% m-% d-% H-% m') # Time Function
>>> Allen-li @ allenli-U80V :~ $ Python/home/allen-li/desktop/mypy1.pyhello I am allen. Drawing h in shubye: 2014-04-30-00-42
8) Pants:
The student next door bought a book about the python library which is very thick...
To make up for it, paste the Lambda example. C ++ 11 lists this feature as a standard.
Def makeTwice (n): return lambda s: s * ntwice = makeTwice (2) print (twice (7) print (twice ('hhh') >>> allen-li @ allenli-U80V :~ $ Python/home/allen-li/desktop/mypy1.py14hhhhhh # I feel like this is a non-mainstream regular expression,
The words below: writing a blog is really time-consuming, so understanding how to grasp the backbone will allow you to live for another 20 years. Obviously, this blog is as messy and messy as shi. I feel that the language is a hundred things, because the functions they provide to me are similar. In the complex language, you can see the abstract, and then the breeze will bloom.
Learn about python: a byte of python foreign language website 1
W3school