马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
【Python编程基础练习】 Python编程基础练习100题学习记录第一期(1~10)
【导语】本文是 GitHub 热门项目“100+ Python challenging programming exercises for Python 3”的学习记录第一期(1~10 题),作者逐题给出带注释的代码并修正了原题不妥之处,适合 Python 初学者以练促学、巩固基础。
核心观点
- 项目定位:基于 GitHub 的 Python 100+ 编程练习题,作者做学习记录并加注释;6~17 题为 level1、18-22 为 level3,其余为 level1
- 第 1 题:输出 2000~3200 间能被 7 整除但不能被 5 整除的数,用 range+if 筛选,结果以逗号分隔单行打印(列表元素需先转字符串)
- 第 2 题:计算给定数字的阶乘,演示 while 循环累乘与 input 控制台输入的处理方式
- 学习建议:作者建议 Python 编程尽量使用英文、工作时基本用英语,并提示部分原题有误已做修正
适用场景
适用于 Python 初学者刷题巩固基础、编程练习教学与自学路线规划场景,适合入门者与编程教师参考。
关键信息
(练习记录,文中 1~10 题带注释代码与题目原文见正文代码块。)
1.此为GitHub项目的学习记录,记录着我的思考,代码基本都有注释。
2.可以作为Python初学者巩固基础的绝佳练习,原题有些不妥的地方我也做了一些修正。
3.建议大家进行Python编程时使用英语,工作时基本用英语。
4.6~17题为level1难度,18-22题为level3难度,其余都为level1难度。
项目名称:
100+ Python challenging programming exercises for Python 3
- #!usr/bin/env Python3.9 # linux环境运行必须写上
- # -*- coding:UTF-8 -*- # 写一个特殊的注释来表明Python源代码文件是unicode格式的
- """Question:
- Write a program which will find all such numbers
- which are divisible by 7 but are not a multiple of 5,
- between 2000 and 3200 (both included).
- The numbers obtained should be printed
- in a comma-separated sequence on a single line."""
- '''Hints: Consider use range(#begin, #end) method'''
- l1 = [] # 建立空列表
- for i in range(2000, 3201): # for循环寻找能被7整除但不能被5整除的数字
- if (i % 7 == 0) and (i % 5 != 0):
- l1.append(str(i)) # 列表里的数据需要是字符串
- print(','.join(l1)) # 在一行显示,以逗号隔开
复制代码 [code]#!usr/bin/env Python3.9# -*- coding:UTF-8 -*-"""Question:Write a program which can compute the factorial of a given numbers.The results should be printed in a comma-separated sequence on a single line.Suppose the following input is supplied to the program: 8 Then, the output should be: 40320"""'''Hints: In case of input data being supplied to the question, it should be assumed to be a console input.'''i = 1result = 1t = int(input('请输入一个数字: '))while i |