少儿学python技术文章
这篇文章主要知识点是关于python、练习、少儿编程类的内容,如果大家想对相关知识点有系统深入的学习,可以参阅以下电子书
巩固练习里边的内容是供你尝试的。如果你觉得做不出来,可以暂时跳过,过段时间再回来做。
对于这个习题,试试下面几项。
1.让你的脚本再多打印一行。
2.让你的脚本只打印其中一行。
3.在一行的起始位置放一个“#”字符。它的作用是什么,自己研究一下。
从现在开始,除非特别情况,否则我将不再解释每个习题的工作原理了。
警告
#(octothorpe)有很多的英文名字,如pound(英镑符)、hash(电话的#键)、mesh(网)等。选一个你觉得酷的用就行了。
常见问题回答
这部分包含了学生做这个习题时遇到的真实问题。
我可不可以使用IDLE?
不行。你应该使用macOS的Terminal或者Windows的PowerShell,和我这里演示的一样。如果你不知道如何用它们,可以去阅读附录。
怎样让编辑器显示不同颜色?
编辑之前先将文件保存为.py格式,如ex1.py,后面编辑时你就可以看到各种颜色了。
运行ex1.py时看到SyntaxError: invalid syntax。
你也许已经运行了Python,然后又在Python环境下运行了一遍Python。关掉并重启终端,重来一遍,只键入python3.6 ex1.py就可以了。
遇到出错消息can’t open file 'ex1.py': [Errno 2] No such file or directory。
你需要在自己创建文件的目录下运行命令。确保你事先使用cd命令进入了这层目录下。假如你的文件保存在lpthw/ex1.py下面,那你需要先执行cd lpthw/,再运行python3.6 ex1.py。如果你不明白该命令的意思,那就去看看附录。
我的文件无法运行,它直接回到了提示符,没有任何输出。
很有可能是你把ex1.py文件中的代码做了字面理解,认为print("Hello World!")就是让你在文件中打印"Hello World!",于是你没有键入print。你的代码应该和我的一模一样才可以。
利用*字典*输出目录,可以选择目录进入,可以回退、退出!
#conding:utf8 |
作业二
list的小练习:实现购物车功能
product_list = [[ |
作业三
冒泡算法小练习
a_list = [ |
作业四
插入算法小练习
实现方法一:
a_list = [ |
实现方法二:
arr1 = [ |
做业五
打印99乘法表
#! /usr/bin/python # Filename : table_9x9.py print '\n9x9 Table\n' for i in range(1, 10) : for j in range(1, i+1) : print j, 'x', i, '=', j*i, '\t', # print '%d x %d = %d\t' %(j, i, j*i), print '\n' print '\nDone!' 关于该程序的说明: 1. 第一行是特殊注释行,称为组织行,用来告诉GNU/Linux系统应该使用哪个解释器来执行该程序。 2. 第二行至第四行都是一般的注释行,用来说明一些信息的(如文件名,作者,时间等)。 3. 第六行打印一个字符串。 4. 第八行i取值范围为1, 2, 3, 4, 5, 6, 7, 8, 9 5. 第九行j取值范围为1, 2, ..., i 6. 第十行和第十一行效果一样,最后的逗号都是用来取消自动换行的。 7. 第十二行作用是在每个内层for循环结束后换行,即在输出完九九乘法表一行后换行。 8. 第十四行打印一个字符串。 小结:通过这个程序熟悉print的用法,for循环的用法,以及range函数的特点。 |
课堂中提到的方法:
for i in range(1,10):
output = ''
for j in range(1,i+1):
output+= "%s*%s=%2s "%(i,j,i*j)
print output
---------------------------------------------------
作业六:
取一个列表中前两位大的数字
#!/usr/bin/python |
利用单循环来同事去除两个两个数。
作业七
二分查找法(利用递归函数)
num_list = [ |
作业八
利用函数完成用户的注册登录功能
#!/usr/bin/python
#encoding: utf-8
import getpass
user_list = {}
def new_user():
username = raw_input('please input your new name:')
if username in user_list:
print '%s is already exits!!' %username
else:
passwd = getpass.getpass() #已密码不显示的方式输入。
user_list[username] = passwd
def old_user():
username = raw_input('please input your name:')
passwd = raw_input('please input your passwd:')
if username in user_list:
if user_list[username] == passwd:
print 'login successfull!!'
else:
print 'login fail!!'
else:
print 'login fail!!!'
CMD = {'n' : new_user , 'o' : old_user}
def main_menu():
ppp = '''
新用户注册:n
老用户登录:o
推出:q
'''
while True:
try:
choice = raw_input(ppp)
except (KeyboardInterrupt, EOFError): #捕捉ctrl+ D 的错误输出。
choice = 'q'
if choice not in 'noq':
print 'please input again'
continue
if choice == 'q':
break
CMD[choice]() #通过取字典的key,选择来执行函数
if __name__ == '__main__':
main_menu()
作业九 |
log日志前十排序,其中包括重复的。利用函数简单的html输出
#!/usr/bin/python #coding:utf8 def openFile(log): with open(log) as f: content = f.readlines() return content def countLine(fn): content = fn count_dict = {} for line in content: line = line.split(' ') # string 通过split 转换为list local_ip,uri,code = line[0],line[6],line[8]
#利用元组来作为key,然后统计数量
count_dict[(local_ip,uri,code)] = count_dict.get((local_ip,uri,code),0) + 1
#这个排序的功能网上copy的,不了解原理
count_dict = sorted(count_dict.items() ,key=lambda item:item[1],reverse=True) return count_dict def detailLine(fn): count_dict = fn #下面进行重复行计数为一行。 n = 1 #取行计数 num = 0 #用于判断'统计数量'是否出现一样的 # print ('-----IP------------------URI----------CODE---count---') # for i in count_dict: # if n <= 10: # 取前十 # if i[1] != num: # print('%-10s%20s%5s%10s ---->no%s' % (i[0][0], i[0][1], i[0][2], i[1], n)) # num = i[1] # n += 1 # elif i[1] == num: # print('%-10s%20s%5s%10s' % (i[0][0], i[0][1], i[0][2], i[1])) with open('count.html','w') as f: res = "<table border='1'>" res += '<tr><td>IP</td><td>URI</td><td>CODE</td><td>COUNT</td></tr>' for i in count_dict: if n <= 10: if i[1] != num: res += '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>'% (i[0][0], i[0][1], i[0][2], i[1]) num = i[1] n += 1 elif i[1] == num: res += '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>' % (i[0][0], i[0][1], i[0][2], i[1]) res +='</table>' f.write(res) def main(): first = openFile('access.log') second = countLine(first) detailLine(second) if __name__ == '__main__': main()
作业十, |
生成随机验证码:
方法一: import string,random source = string.digits + string.ascio_lowercase print ("".join(random.sample(source,6))) 方法二: checkcode = '' for i in range(6): current = random.randrange(0,6) if current != i: temp = chr(random.randint(65,90)) else: temp = random.randint(0-9) checkcode += str(temp) print(checkcode)
以上就是Python的一些小练习的详细内容,更多请关注码农之家其它相关文章!
以上就是本次给大家分享的关于少儿学python的全部知识点内容总结,大家还可以在下方相关文章里找到计算机专业当少儿编程老、 傲梦青少儿编程费用 、 现在少儿编程培训好做吗、 网络儿童编程班哪个好 、 儿童学编程从什么语言开、 等少儿学python相关文章进一步学习,感谢大家的阅读和支持。
上一篇:学习python的第一个程序
下一篇:没有了
展开 +
收起 -
Copyright 2018-2021 www.xz577.com 码农之家
版权投诉 / 书籍推广:520161757@qq.com
python 表达式和语句及for、while循环练习实例
Python中表达式和语句及for、while循环练习 1)表达式 常用的表达式操作符:x + y, x - yx * y, x / y, x // y, x % y逻辑运算:x or y, x and y, not x成员关系运算:x in y, x not in y对象实例测试:x is y, x not is y比较运算:x y, x y, x = y, x = y, x == y, x != y位运算:x | y, x lt; y, x y一元运算:-x, +x, ~x:幂运算:x ** y索引和分片:x[i], x[i:j], x[i:j:stride]调用:x(...)取属性: x.attribute元组:(...)序列:[...]字典:{...}三元选择表达式:x if y else z匿名函数:lambda args: expression生成器函数发送协议:yield x 运算优先级:(...), [...], {...}s[i], s[i:j]s.attributes(...)+x, -x, ~xx ** y*, /, //, %+, -, lt;, =, , =, ==, !=is, not isin, not innotandorlambda 2)语句: 赋值语句 调用 print: 打印对象 if/elif/else: 条件判断 for/else: 序列迭代 while/else: 普通循环 pass: 占位符 break: continue def return yield global: 命名空间 raise: 触发异常 import: fro……
python基本语法练习实例
1、打印九九乘法表 #只打印结果for i in range(1,10): for j in range(1,i+1): print(i*j,end=" ") print() #打印算数表达式for i in range(1,10): for j in range(1,i+1): print("{0}*{1} = {2:2}".format(j,i,i*j),end=" ") print() 1*1 = 1 1*2 = 2 2*2 = 4 1*3 = 3 2*3 = 6 3*3 = 9 1*4 = 4 2*4 = 8 3*4 = 12 4*4 = 16 1*5 = 5 2*5 = 10 3*5 = 15 4*5 = 20 5*5 = 25 1*6 = 6 2*6 = 12 3*6 = 18 4*6 = 24 5*6 = 30 6*6 = 36 1*7 = 7 2*7 = 14 3*7 = 21 4*7 = 28 5*7 = 35 6*7 = 42 7*7 = 49 1*8 = 8 2*8 = 16 3*8 = 24 4*8 = 32 5*8 = 40 6*8 = 48 7*8 = 56 8*8 = 64 1*9 = 9 2*9 = 18 3*9 = 27 4*9 = 36 5*9 = 45 6*9 = 54 7*9 = 63 8*9 = 72 9*9 = 81 #将打印矩阵转置一下for i in range(1,10): print(" " *10*(i-1), end = " ") for j in range(i,10): print("{0}*{1} = {2:2}".format(i,j,i*j),end=" ") print() 1*1 = 1 1*2 = 2 1*3 = 3 1*4 = 4 1*5 = 5 1*6 = 6 1*7 = 7 1*8 = 8 1*9 = 9 2*2 = 4 2*3 = 6 2*4 = 8 2*5 = 10 2*6 = 12 2*7 = 14 2*8 = 16 2*9 = 18 3*3 = 9 3*4 = 12 3*5 = 15 3*6 = 18 3*7 = 21 3*8 = 24 3*9 = 27 4*4 = ……
分享一个python练习题实例
1、有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?2、企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?3、一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?4、输入某年某月某日,判断这一天是这一年的第几天?5、输入三个整数x,y,z,请把这三个数由小到大输出。6、斐波那契数列。7、将一个列表的数据复制到另一个列表中……
详解用Python练习画个美队盾牌
0 环境 Python版本:3.6.6 操作系统:Mac OS Mojave 10.14.2 1 引言 最近我媳妇每天晚上吃饭时候也拿手机看,上厕所也在看。 看着看着还会笑?WTF?你在干嘛呢? 没错,她在看美队…… 这男人比我帅?…… 比我有钱?…… 还是比我有腔调?…… 答案很明显,我相信你们的眼睛都是雪亮的。 那问题来了,作为一个男人,怎么能忍?虽然是个明星,虽然是假的,虽然已经挽回不了败局了……那我就送个美队的盾牌给你吧…… 这篇文章不是Turtle的入门篇,所以关于基本的画笔使用小胖不在这篇里赘述了。有兴趣的可以留言,我会根据你们的反馈来写一篇或者一个系列。 2 实战 先来看一个效果图: 用过Turtle的都知道,画笔是需要抬手和落笔的。这一点一定要理解,因为计算机是很死板的,你的每一个动作都必须告诉他,包括GC。 所以你的画笔一旦落下,经过的……