1.2 python基本操作
1.数据类型
整数,浮点数,真值,字符串
python 对于数据类型可以不要特别申明,数据类型有python自动决定的。
a=10;
print(type(a))
2.输入、输出
raw_input
print 支持字符串通过“+”进行添加
name=raw_input("please enter your name")
print "hello!"+str(name)
3.(这个部分需要添加一些知识点)
序列:元组、列表 集合、字典
s1 = (2, 1.3, 'love', 5.6, 9, 12, False) #元组
s2 = [True, 5, 'smile'] #列表
d = {'name':'alexzhou','sex':'man'} #字典
s = set('alexzhou') #集合
关于列表的一些使用:
list.append();list.extend();list.insert(i,x);
4.python 简单数学运算
- 数学 +, -, *, /, **, %
- 判断 ==, !=, >, >=, <, <=, in
- 逻辑 and, or, not
5.循环
# break using
# if the cycle have "break",cycle will jump out!
count = 0
for letter in 'Snow!':
print 'Letter # ' + str(count) + ' is ' + str(letter)
count += 1
#break
print count
while 循环
x = 25
epsilon = 0.01
step = 0.1
guess = 0.0
while abs(guess**2-x) >= epsilon:
if guess <= x:
guess += step
else:
break
if abs(guess**2 - x) >= epsilon:
print 'failed'
else:
print 'succeeded: ' + str(guess)
6.函数
def iterPower(base, exp):
result = base
if exp == 0:
return 1
while exp >1:
exp -= 1
result = result * base
return result
6.1用迭代方式实现
def recurPower(base, exp):
'''
base: int or float.
exp: int >= 0
returns: int or float, base^exp
'''
if exp == 0:
return 1
elif exp == 1:
return base
return base * recurPower(base,exp-1)
6.2 类似回文
def semordnilap(str1, str2):
'''
str1: a string
str2: a string
returns: True if str1 and str2 are semordnilap;
False otherwise.
'''
# Your code here
if len(str1) != len(str2):
return False
elif len(str1) == 1 or len (str2) == 1:
return False
elif str1[0] != str2[-1]:
return False
elif str1[len(str1)-1] == str2[-len(str2)]:
return True
else:
return semordnilap(str1[1:],str2[:-1])
7.面向对象(lecture12-4-1)
class A(object):
def __init__(self):
self.a = 1
def x(self):
print "A.x"
def y(self):
print "A.y"
def z(self):
print "A.z"
class B(A):
def __init__(self):
A.__init__(self)
self.a = 2
self.b = 3
def y(self):
print "B.y"
def z(self):
print "B.z"
class C(object):
def __init__(self):
self.a = 4
self.c = 5
def y(self):
print "C.y"
def z(self):
print "C.z"
class D(C, B):
def __init__(self):
C.__init__(self)
B.__init__(self)
self.d = 6
def z(self):
print "D.z"