Follow these steps to get a quick look at Python, with the goal of using Python to handle some simple problems or to write some tools.
1. Write Hello world2. Learn the grammar of If,while,for 3. Learn how the language represents strings and arrays, map. 4. Learn how to define functions and use functions 5. Learn how to create objects, and use objects. 6. Write a simple IO example. Prints a line in a document that includes a string.
1. Write Hello worldPython is a scripting language that features a cross-platform, concise syntax, and a complete library of methods. Python is a good choice when it comes to solving some simple problems quickly. Python also has the ability to build large web sites, such as the watercress is written in Python. Most of the release versions of download python:linux contain python. Under Windows in http://www.python.org/download/you can download the latest version of Python, I'm using 3.3.3. After installation, add the folder path of the installation to the system behind the path variable of that vehicle. Write a Python program after you enter Python on the command line. You can also write a Python program via the python-enabled idle editor, idle to make the code pre-highlighted, and have breakpoint debugging. But this idle function is not very powerful. I used the idea to have a Python plugin in idea to write python using idea. Search Python at the plugin to find a plugin called Python. After the plugin is installed, the SDK for configuring Python is complete. Then you can write the simplest Python program.
Then the run will print out Hello world. Note: Python 3.3.3 uses print with parentheses, and Python 2.x can use print ' HELLO WORD ' directly. Then make sure that there are no spaces or tabs at the beginning of print. In addition, the same is used in Python ' and '. Another point is indentation, in Python the same level of the statement must have the same indentation (the first line of space or tab is equal, followed by the mention).
2. Learn the grammar of If,while,forWith a few small examples, it is clear to know the syntax if syntax for these three statements:?
A = 2if a==2: = a+2##这里需要注意缩进, Python is the use of indentation to distinguish between the level of print(a )
While syntax:
A = 1 while a<5: print(a) +=1
For syntax:
for in range (1, 5):##range返回一个序列的数 print(i)
3. Learn how the language is represented by string and array, map. To define an array:
shoplist=['C','Eee','QQQQ'] Shoplist.append ('HHHH')##添加一个元素Print(shoplist) shoplist.sort ()##数组排序Print(shoplist)delSHOPLIST[0]##删除一个元素PrintshoplistPrint(', '. Join (a))##间隔符设为 "," number, output data
Tuples: There is also the concept of a tuple in Python, which is also a sequence and an array that is different from the string that cannot be modified. Define the tuple with parentheses and commas in parentheses:
Meta = (3333,2222,333)print ('length:', Len (meta))
The yuan lease is usually used in printed statements:
Age=19name= ' Peter ' Print ('%s is%d years old '% (name,age))
Python takes everything as objects, including functions. Dictionary: The dictionary concept in Python is equivalent to map.
A = {'Peter':'[email protected]','Anne':'[email protected]'}##定义字典Print(a['Peter']) if 'Peter' inchA:Print('Peter is in') forKey, ValueinchA.items ():##打印键和值 Print(Key +':'+value)dela['Anne']##删除对应key的键和值 forIinchA.items ():Print(i)
Simple answer string operation:
Animal ='Elephant' ifAnimal.startswith ('Ele'):##字符串是否以ele开头 Print('Start witch ele') Print('a' inchAnimal##字符串是否包括 ' A ' stringPrint(Animal.find ('Ant'))##找到 ' ant ' first occurrence position, no return-1
4. Learn how to define functions and use functionsDefine functions such as: Returns the maximum value
def Returnmax (A, b ): if a>B: return a returnprint( Returnmax (100,39))
5. Learn how to create objects, and use objects.
classPerson:##定义一个类num = 0##a是一个类变量 defSay (self, word):Print(self.name+'say:', Word)returnSelf.namedef __init__(self, name):##__init__方法相当于java中的constructor.Person.num + = 1Self.name= Name##这里name是一个对象变量 def __dosth(self):##使用__开头的方法和变量表示是私有变量 Print('I am Private method') defSayname (self):Print('I am', Self.name) self.__dosth() P= Person ('Peter') P.say ('Hello World') P.sayname ()
Inherited:
classPeter (person):##继承自Person def __init__(self, height): person.__init__(Self,'Peter') Self.height=Heightdefsayheight (self):Print('I am%d feet tail'%Self.height) Peter= Peter (6) Peter.sayname () peter.sayheight ()
6. Write a simple IO example. Prints the line of a file, including a string.
f = Open (r'F:\11.txt','R')##打开一个文件只读w = open (r'F:\33.txt','W')##打开一个文件可写 forLinchF:ifL.find ('Iampeter') >0:w.write (L)
Only 5 lines This program is finished, very concise, so that you can use Python to do some simple things, follow-up and further learning Python. It can be noted that Python's syntax is simple and uniform, easy to remember, and the indented code style makes the code clear.
Python Quick Start