python考试编程题

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

python考试编程题_第1张图片

b:
count = 0
for i in range(1, 100):
    if i % 2 == 0:
        count -= i
    else:
        count += i
print count

 

python考试编程题_第2张图片

c:
num = range(1, 13)
i = 1
while i < 13:
    if (i == 6 or i == 10):
        print ' ',
    else:
        print i,
    i += 1

python考试编程题_第3张图片

h:

li = ['frdgrfgdsHHJJ', 'cdfregHHHJDGF']
new_li = []
for i in li:
    new_li.append(i.lower())

print(new_li)

python考试编程题_第4张图片

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

python考试编程题_第5张图片

8.

dic = {'k1': 'v1', 'k2': 'v2', 'k3': [11, 22, 33]}

# 1.

for i in dic:
     print(i)

python考试编程题_第6张图片

# 2.
 for v in dic.values():
     print(v)

python考试编程题_第7张图片

 # 3.
 for k in dic:
    print(k, dic[k])

python考试编程题_第8张图片

 # 4.
 dic['k1'] = 'harry'
 print(dic)

python考试编程题_第9张图片

 # 5.
 dic['k3'].append('44')
 print(dic)

python考试编程题_第10张图片

# 6.
dic['k3'].insert(0, 18)
print(dic)

python考试编程题_第11张图片

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

python考试编程题_第12张图片

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)

python考试编程题_第13张图片

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

python考试编程题_第14张图片

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

python考试编程题_第15张图片

21.

class People(object):
    __name = 'luffy'
    __age = 18


pl = People()
print(pl.__name, pl.__name)

# 出现报错,因为私有属性不允许直接访问

python考试编程题_第16张图片

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)

python考试编程题_第17张图片

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')

 

python考试编程题_第18张图片

class B:
    def handle(self):
        print '喵喵'


class A(B):
    def handle(self):
        B.handle(self)


new = A()
new.handle()

python考试编程题_第19张图片

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()

python考试编程题_第20张图片

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()

python考试编程题_第21张图片

 

 

你可能感兴趣的:(python考试编程题)