1.Python语言是开源,简单主义思想,既支持面向过程编程也支持面向对象的编程,并且丰富的库。
2.常量:
数:整数(int)、浮点数(float)和复数:如(-5+4j)和(2.3-4.6j)
字符串:注意python预言中双引号和单引号用法含义完全一样
3.数据结构:
列表:例子shoplist = ['apple', 'mango', 'carrot', 'banana'],常用方法len(),append(),sort()等
元组:例子zoo = ('python', 'elephant', 'penguin'),元组和列表十分类似,只不过元组和字符串一样是不可变的即你不能修改元组,但和列表都能通过检索获取内容shoplist[0],zoo[0],new_zoo = ('monkey', 'camel', zoo),new[2][2]
字典:键值对,记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对它们排序。常用方法:item()
ab = { 'Swaroop' : 'swaroop@swaroopch.com',
'Larry' : 'larry@wall.org',
'Matsumoto' : 'matz@ruby-lang.org',
'Spammer' : 'spammer@hotmail.com'
}
for name, address in ab.items():
print('Contact {0} at {1}'.format(name, address))
#添加键值对
ab['Guido'] = 'guido@python.org'
#删除键值对
del ab['Spammer']
集合:bri = set(['brazil', 'russia', 'india']),集合是没有顺序的简单对象的聚集。当在聚集中一个对象的存在比其顺序或者出现的次数重要时使用集合。使用集合,可以检查是否是成员,是否是另一个集合的子集,得到两个集合的交集等等
4.控制流:在Python 中有三种控制流语句——if、for 和while
5.面向对象编程:
self方法:MyObject.method(arg1, arg2) 的时候,这会由Python 自动转为MyClass.method(MyObject, arg1, arg2)—— 这就是self 的原理了。
__init__方法:__init__ 方法相当于C++,Java,C# 中的构造函数,例子
class Person:
def __init__(self, name):
self.name = name
def sayHi(self):
print('Hello, my name is', self.name)
p = Person('Swaroop')
p.sayHi()
子类、父类:如果它不能在导出类中找到对应的方法,它才开始到基本类中逐个查找。
基本类是在类定义的时候
例子:
1 #!/usr/bin/python
2 # Filename: inherit.py
3
4 class SchoolMember:
5 '''Represent any school member.'''
6 def __init__(self,name,age):
7 self.name = name
8 self.age = age
9 print('(Initialize SchoolMember:{0})'.format(self.name))
10
11 def tell(self):
12 '''Tell my details.'''
13 print('Name:"{0}" Age:"{1}"'.format(self.name,self.age),end
='')
14
15 class Teacher(SchoolMember):
16 '''Repressent a teacher.'''
17 def __init__(self,name,age,salary):
18 SchoolMember.__init__(self,name,age)
19 self.salary = salary
20 print('(Initialized Teacher:{0})'.format(self.name))
21
22 def tell(self):
23 SchoolMember.tell(self)
24 print('Salary:"{0:d}"'.format(self.salary))
25
26 class Student(SchoolMember):
27 '''Represents a student'''
28 def __init__(self,name,age,marks):
29 SchoolMember.__init__(self,name,age)
30 self.marks = marks
31 print('(Initialized Student:{0})'.format(self.name))
32
33 def tell(self):
34 SchoolMember.tell(self)
35 print('Marks:"{0:d}"'.format(self.marks))
36
37 t = Teacher('Mrs.Shrividya',30,30000)
38 s = Student('Swaroop',25,75)
42 members = [t,s]
43 for member in members:
44 member.tell() # work for both Teacher and Students
输出:
1 $ python inherit.py
2 (Initialize SchoolMember:Mrs.Shrividya)
3 (Initialized Teacher:Mrs.Shrividya)
4 (Initialize SchoolMember:Swaroop)
5 (Initialized Student:Swaroop)
6
7 Name:"Mrs.Shrividya" Age:"30"Salary:"30000"
8 Name:"Swaroop" Age:"25"Marks:"75"
6.异常
1 class ShortInputException(Exception):
2 '''A user-defined exception class'''
3 def __init__(self, length,atleast):
4 Exception.__init__(self)
5 self.length = length
6 self.atleast = atleast
7
8 try:
9 text = input('Enter something-->')
10 if len(text) < 3:
11 raise ShortInputException(len(text),3)
12 #other work can continue as usual here
13 except EOFError:
14 print('Why did you do an EOF on me')
15 except ShortInputException as ex:
16 print('ShortInputException The input was {0} long, excepted
\
17 atleast {1}'.format(ex.length, ex.atleast))
18 else:
19 print('No exception was raised.')
输出:
1 $ python raising.py
2 Enter something --> a
3 ShortInputException: The input was 1 long, expected at least 3
4
5 $ python raising.py
6 Enter something --> abc
7 No exception was raised.