【Python小练习(一)】教程文章相关的互联网学习教程文章

python3练习100题——026【代码】

原题链接:http://www.runoob.com/python/python-exercise-example26.html题目:利用递归方法求5!。是25题递归方式的简化版所以很容易。我的代码:def prod(x):if x==1:return 1else:return x*prod(x-1)print(prod(5)) 原文:https://www.cnblogs.com/drifter/p/9185903.html

python3练习100题——020【代码】

原题链接:http://www.runoob.com/python/python-exercise-example20.html题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?我的代码:def ball():times=int(input("Hou many times the ball hit the floor?"))h=100.0record=[]      length=100for i in range(0,times):h=h/2record.append(h)for i in record[:-1]:length += i*2print(length)pri...

Python Codecademy 练习:去掉字符串中的元音字母【代码】

1def anti_vowel(text):2 out=[]3 mystring=list(text)4for i in mystring:5if i notin ["a","e","i","o","u","A","E","I","O","U"]:6 out.append(i)7print("".join(out))8 910 testing=input("请输入字符串:") 11 anti_vowel(testing) 第一次使用remove方法,直接删除list中的元音字母,但是调试时发现去除字母后,list元素的位置发生变化,再次遍历的时候可能会漏掉,于是使用append方法,将不是元音的字...

Python入门小练习 002 批量下载网页链接中的图片【代码】

我们常常需要下载网页上很多喜欢的图片,但是面对几十甚至上百张的图片,一个一个去另存为肯定是个很差的体验。我们可以用urllib包获取html的源码,再以正则表达式把匹配的图片链接放入一个list中,使用for循环来依次下载list中的链接。 import re import urllib a = raw_input("Please input a URL: ") s = urllib.urlopen(a) s2 = s.read()def image(s2):reg = r‘src="(.*?\.jpg)" pic_ext‘compile_reg = re.compile(reg)imag...

python 练习题:使用迭代查找一个list中最小和最大值,并返回一个tuple【代码】

# -*- coding: utf-8 -*-# 请使用迭代查找一个list中最小和最大值,并返回一个tuplefrom collections import Iterabledef findMinAndMax(L):if len(L) == 0:return (None,None)if isinstance(L,Iterable) == True:min = L[0]max = L[0]for x in L:if x > max:max = xif x < min:min = xreturn (min,max)# 测试 if findMinAndMax([]) != (None, None):print(‘测试失败!‘) elif findMinAndMax([7]) != (7, 7):print(‘测试失败!‘)...

Python基础练习-迭代器【图】

原文:http://www.cnblogs.com/chenqizhou/p/7019787.html

Python week1-练习1登陆接口【代码】

文件下载:practise1.tar练习一:输入用户名密码认证成功后现实欢迎信息输错三次后锁定#!/usr/bin/env python #Author:Austin name = input("Please input user name:")deny_file = open("deny.txt","r") line = deny_file.readline()[:-1] while line:if line == name:print("Sorry, the user has locked!")exit()line = deny_file.readline()[:-1] deny_file.close()password = input("Password:")f = open("passwd.txt","r") li...

Python基础练习(二)笔趣看《伏天氏》全文章节爬取【代码】【图】

大家如果觉得有帮助的话,可以关注我的知乎https://www.zhihu.com/people/hdmi-blog/posts,里面有写了一些我学习爬虫的练习~今天我们想要爬取的是笔趣看小说网上的网络小说,并将其下载,保存为文件。 运行平台:WindowsPython版本:Python3.6IDE:Sublime Text 其他:Chrome浏览器 步骤1:通过Chrome浏览器检查元素步骤2:获取单个页面HTML文本步骤3:解析HTML文本并获取所有章节的名称和链接步骤4:获取每个章节的文本并简单修改...

python练习册 每天一个小程序 第0010题【代码】

# -*-coding:utf-8-*-‘‘‘ 题目描述:使用 Python 生成类似于下图中的字母验证码图片思路:运用PIL库加random 随机字母进行生成‘‘‘import random import string from PIL import Image, ImageDraw, ImageFont, ImageFilterdef rnword():return random.choice(string.letters)def color():return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))def color2():return (random.randint(32, 127), ...

Python基础综合练习【代码】【图】

# coding=utf-8 import turtle# 画五角星的方法 def drawPentagram(x):turtle.begin_fill()turtle.color(‘yellow‘)for i in range(5):turtle.forward(x)turtle.right(144)turtle.end_fill()def gotoPoint(x,y,z):turtle.penup()turtle.setheading(0)turtle.goto(x,y)turtle.right(z)turtle.pendown()#length = 540 length = int(input(‘请输入国旗长度:‘)) width=length/3*2 bigDiameter=width*0.3 smallDiameter=width*0.1 t...

Python 练习实例45【图】

Python 练习实例45题目:统计 1 家电维修到 100 之和。程序分析:无程序源代码: 以上实例输出结果为: 原文:https://www.cnblogs.com/danjiu/p/12468784.html

Python 练习册,每天一个小程序 -- 0001题【代码】

继续做题:第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?分析问题:一般来说,公司在搞活动的时候都会有批量的激活码放出,一般激活码的格式都是 xxxxx-xxxxx...的格式,并且是随机生成的,我这里使用到的是 python的random模块。解决问题:简单的实现如下:#!/usr/bin/env python # -*- coding: utf-8 -*- import rand...

python之基础练习30题【代码】

题目:1,九九乘法表2,手动输入一个字符串,打散放进一个列表,小写字母从大到小排列序,大写字母保持不变3,li=[1,2,3,4,5,6,7,8,8]组成多少个互不相同且不重复的两位数4,计算1+2+3...+98+99+100,并打印出计算公式 5.列表[‘alex‘,‘egon‘,‘yuan‘,‘wusir‘,‘666‘] 1.把666替换成999# 2.获取"yuan"索引# 3.假设不知道前面有几个元素,分片得到最后的三个元素6.切割字符串"luffycity"为"luffy","city"7.求1~100间...

python练习题:利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法【代码】

方法一:# -*- coding: utf-8 -*-# 利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法:def trim(s):while s[:1] == ‘ ‘:s = s[1:]while s[-1:] == ‘ ‘:s = s[0:-1]return s# 测试: if trim(‘hello ‘) != ‘hello‘:print(‘测试失败!‘) elif trim(‘ hello‘) != ‘hello‘:print(‘测试失败!‘) elif trim(‘ hello ‘) != ‘hello‘:print(‘测试失败!‘) elif trim(‘ hello...

Leetcode练习(Python):链表类:第92题:反转链表 II:反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。

题目:反转链表 II:反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。说明:1 ≤ m ≤ n ≤ 链表长度。示例:输入: 1->2->3->4->5->NULL, m = 2, n = 4输出: 1->4->3->2->5->NULL思路:思路较简单,找到规律就好。程序:# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution:...