相信有很多人都在努力地自学着Python,但自学过程中总是看懂了却不会做。这就像学生时期老师讲过了,觉得听懂了,但一看题就懵了。
在自学任何一门软件或是编程语言的过程中,最重要的是结合教程去进行实践训练,毕竟“实践才是检验真理的唯一标准”。
今天给大家带来Python从初级入门到精进的练习实例共100例,每篇五道题保持篇幅不会过长,大家练习也循序渐进、松弛有度。并且还会附上题目程序的的源代码,供大家参考,大家初期可以“照猫画虎”去发现其编程的目的及规律,从而更好地理解。
Python 练习实例86
题目:两个字符串连接程序。
程序分析:无。
程序源代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
a = "acegikm"
b = "bdfhjlnpq"
# 连接字符串
c = a + b
print c
Python 练习实例87
题目:回答结果(结构体变量传递)。
程序分析:无。
程序源代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
class student:
x = 0
c = 0
def f(stu):
stu.x = 20
stu.c = 'c'
a= student()
a.x = 3
a.c = 'a'
f(a)
print a.x,a.c
Python 练习实例88
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
程序分析:无。
程序源代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
n = 1
while n <= 7:
a = int(raw_input('input a number:\n'))
while a < 1 or a > 50:
a = int(raw_input('input a number:\n'))
print a * '*'
n += 1
Python 练习实例89
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
程序分析:无。
程序源代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from sys import stdout
if __name__ == '__main__':
a = int(raw_input('输入四个数字:\n'))
aa = []
aa.append(a % 10)
aa.append(a % 100 / 10)
aa.append(a % 1000 / 100)
aa.append(a / 1000)
for i in range(4):
aa[i] += 5
aa[i] %= 10
for i in range(2):
aa[i],aa[3 - i] = aa[3 - i],aa[i]
for i in range(3,-1,-1):
stdout.write(str(aa[i]))
Python 练习实例90
题目:列表使用实例。
程序分析:无。
程序源代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#list
#新建列表
testList=[10086,'中国移动',[1,2,4,5]]
#访问列表长度
print len(testList)
#到列表结尾
print testList[1:]
#向列表添加元素
testList.append('i\'m new here!')
print len(testList)
print testList[-1]
#弹出列表的最后一个元素
print testList.pop(1)
print len(testList)
print testList
#list comprehension
#后面有介绍,暂时掠过
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print matrix
print matrix[1]
col2 = [row[1] for row in matrix]#get a column from a matrix
print col2
col2even = [row[1] for row in matrix if row[1] % 2 == 0]#filter odd item
print col2even
祝大家Python学习顺利!没有 B U G~