[Python]python Learning Notes (i)--grammar

Source: Internet
Author: User
1, interpreted language: is the side of the source program to read the execution. The compiled language is executed after compiling the source code into the target code. You don't need to compile it later in the execution
2, Python is case-sensitive,
3,python is based on line indentation to distinguish between modules and functions, so the style of the code requirements are relatively high, rely on indentation to differentiate code levels, preferably with the TAB key indentation
4, the constant name all uppercase letters, by the underscore to connect. Variable names are best used in lowercase letters, without m and G, with the first letter of the class name capitalized, and the modules all in lowercase.
Comments in 5,python start with #
The string in the 6,python can be any string contained in "" or "", and the string is immutable
How to define a function in 7,python
#!/usr/bin/python
#Filename: func_local.py

def func (x):
print ' x is ', X
x = 2
print ' x is ', X

x = 50
Func (x)
print ' x is still ', X

8. How to make your own module
#!/usr/bin/python
#Filename: mymodule.py

Def sayhi ():
print ' Hi,this is my module '

Version = ' 0.1 '

#End of mymodule.py
Then save the file to the same directory where you called the program, or one of the directories in Sys.path, as shown below.
#!/usr/bin/python
#Filename: mymodule.py

Import MyModule

Mymodule.sayhi ()
print ' Version ', mymodule.version

9, using the list
shoplist[' apple ', ' mango ', ' carrot '
Len (shoplist)
For item in Shoplist
Print Item
Shoplist.append (' rice ')
Shoplist.sort ()
Olditem = shoplist[0]
10, using tuples
Tuples and lists are very similar, except that tuples and strings are not allowed to change.
Zoo = (' Wolf ', ' Elephant ', Penguin ')
New_zoo = (' monkey ', ' Dolphin ', zoo)
The Len (Zoo) Len function Gets the length of the tuple, which means that the tuple is also a sequence.
A single element in a tuple can be accessed by zoo[0].
Tuples are used with print statements as follows:
Age = 22
name = ' Swaroop '
Print '%s is%d years '% (name,age)
11, using a dictionary:
Dictionaries associate keys with values, the value of a key must be unique, and the key value pairs in the dictionary are not sequential.
D = {Key1:value,key2:value2}
AB = {' Wdy ': ' 71619 ', ' jxm ': ' 71510 ', ' ch ': ' 71432 '}
Add an element: ab[' zwj ']= ' zwj@71230 '
Delete an element: Del ab[' Wdy ']
12, using the sequence:
Lists, tuples, and strings are sequences, and the two main features of the sequence are the search operator and the slicing operator, which allows us to get a slice of the sequence.
shoplist[' apple ', ' mango ', ' carrot ', ' banana '
print ' Item 0 is ', shaoplist[0]
print ' item-1 is ', shoplist[-1] first in the countdown
print ' Item 1 to 3 is ', Shoplist[1:3] slice operator
13, create a class
Class statement to create a new class, and then add a parenthesis followed by a class name to create an instance.
CALSS Person:
def sayhi (self):
print ' Hello,how Are you '

p = Person
P.sayhi ()

The __init__ method of a class: This method runs immediately when an object of a class is created, and this method can do some initialization action on the object.
All of the class members (including data members) in Python are public, and all methods are valid, with the exception that the variable is prefixed with a double underline, so the variable is private and can only be used in that class or object.
Class Person:
def __init__ (self,name):
Self.name = Name
def sayhi (self)
print ' hello,my name is ', Self.name

p = person (' Wdy ')
P.sayhi ()

Inheritance of Classes
Class Schoolmember:
def __init__ (self,name,age):
Self.name = Name
Self.age = Age
print ' (initialized schoolmember:%s) ',%self.name

def tell (self):
print ' Name: '%s ' Age: '%s '% (self.name,self.age)

Class Teacher (Schoolmember):
def __init__ (self,name,age,salary):
schoolmember.__init__ (Self,name,age)
Self.salary = Salary
print ' (Initialized Teacher:%s) '%self.name

def tell (self):
Schoolmember.tell (self)
print ' Salary: '%d '%self.salary

t = Teacher (' Wdy ', 40,10000)

It's important in python that Python does not automatically call the constructor of the base class, it has to be specifically manually invoked, and in inheritance the Python entry is __init__,


14, Files
can open a file through the object of the file class, using the Read,readline of the file class, the latter write method to properly read and write a file, and finally call the Close method:
#!/usr/bin/python

Poem = ' \
This is a test
'

F = file (' Poem.txt ', ' W ') #以可写的方式打开poem. txt file
F.write (poem)
F.close ()

F = file (' poem.txt ', read)
while True:
line = F.readline ()
If Len (line) = = 0 
Break
Print Line
F.close ()

15, pickle
Pickle can store any Python object in a file, which can then be completely fetched, which is called a persistent storage object

16, handling exceptions
You can use a try ... except statement to handle the exception, and
Try finally is used to execute the following statement, regardless of what happens.

17, some other useful module
OS module, which contains some operating system related functions, such as the OS.GETCWD () function to get to the current working directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.