3.
a:
while True:
s = raw_input('变量名为:')
if s == 'exit':
print '退出'
break
#判断是否由字母或下划线组成
if s[0].isalpha() or s[0] == '_':
for i in s[1:]:
if not (i.isalnum() or i == '_'):
print '%s变量名不合法 ' % s
break
else:
print '%s 变量名合法' % s
else:
print '%s变量名不合法' % s
b:
count = 0
for i in range(1, 100):
if i % 2 == 0:
count -= i
else:
count += i
print count
c:
num = range(1, 13)
i = 1
while i < 13:
if (i == 6 or i == 10):
print ' ',
else:
print i,
i += 1
h:
li = ['frdgrfgdsHHJJ', 'cdfregHHHJDGF']
new_li = []
for i in li:
new_li.append(i.lower())
print(new_li)
7.
li=['happy','lucky','linux']
print len(li)
li.append('seven')
print li
li.insert(0,'Tony')
print li
li[1]='Kelly'
print li
print li.pop(1)
print li
li.pop(2)
print li
print li[::-1]
print li
8.
dic = {'k1': 'v1', 'k2': 'v2', 'k3': [11, 22, 33]}
# 1.
for i in dic:
print(i)
# 2.
for v in dic.values():
print(v)
# 3.
for k in dic:
print(k, dic[k])
# 4.
dic['k1'] = 'harry'
print(dic)
# 5.
dic['k3'].append('44')
print(dic)
# 6.
dic['k3'].insert(0, 18)
print(dic)
9.
s = raw_input('请输入英文句子:')
s_new = s.split()
dict = {}
for item in s_new:
if item not in dict:
dict[item] = 1
else:
dict[item] += 1
print dict
10.
def fun(set):
bigger = []
smaller = []
for i in set:
if i < 66:
smaller.append(i)
else:
bigger.append(i)
dic = {'k2': smaller, 'k1': bigger, 'k2': smaller}
print(dic)
list = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
fun(list)
12.
i=1
while i<=9:
j=1
while j<=i:
print '%d*%d=%d\t'%(i,j,i*j),
j+=1
print ''
i+=1
13.
num1 = int(raw_input('请输入第一个数:'))
num2 = int(raw_input('请输入第二个数:'))
min_num = min(num1, num2)
for i in range(1, min_num + 1):
if num1 % i == 0 and num2 % i == 0:
biger_count = i
smaller_count = (num1 * num2) / biger_count
print '最大公约数为:%d' % biger_count
print '最小公倍数为:%d' % smaller_count
21.
class People(object):
__name = 'luffy'
__age = 18
pl = People()
print(pl.__name, pl.__name)
# 出现报错,因为私有属性不允许直接访问
22.
class Parent(object):
x = 1
class Child1(Parent):
pass
class Child2(Parent):
pass
print(Parent.x, Child1.x, Child2.x)
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)
Parent.x = 3
print(Parent.x, Child1.x, Child2.x)
23.
class Person(object):
def __init__(self, name):
self.name = name
def buy_car(self, car):
print '%s 宝马BMW 4s店买%s' % (self.name, car)
joker = Person('joker')
joker.buy_car('BMW X7')
class B:
def handle(self):
print '喵喵'
class A(B):
def handle(self):
B.handle(self)
new = A()
new.handle()
25.
class Student(object):
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
Student.count += 1
@staticmethod
def count_student():
print '共实例%d个对象' % Student.count
bob = Student('bob', 19)
Jenny = Student('Jenny', 18)
Danny = Student('Danny', 19)
liming = Student('liming', 20)
Student.count_student()
26.
class Student(object):
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
Student.count += 1
@staticmethod
def count_student():
print '共实例%d个对象' % Student.count
bob = Student('bob', 19)
Jenny = Student('Jenny', 18)
Danny = Student('Danny', 19)
liming = Student('liming', 20)
Student.count_student()