【Python基础练习题100例(Python 3.x)】教程文章相关的互联网学习教程文章

python练习题【代码】

1、使?while循环输出 1 2 3 4 5 6 8 9 10 count = 1 while count <= 10 :if count == 7 :count +=1continueprint(count,end=" ")count +=1num = 1 while num <= 10 :print(num,end=" ")num += 1 #num + 1 2、求1-100的所有数的和 num = 1 h = 0 while num <= 100 :h = h + numnum += 1 print(h) 3、输出 1-100 内的所有奇数 num = 1 while num <= 100 :if num % 2 == 1:print(num,end=" ")else:passnum +=1 4、输出 1-100 内的所...

python循环练习题(九九乘法表、寻找水仙花数、寻找完美数、猜数字游戏、百钱买百鸡)【代码】

猜数字游戏 计算机出一个1~100之间的随机数由人来猜 计算机根据人猜的数字分别给出提示大一点/小一点/猜对了,并继续输入 关于这一题,因为不知道循环的次数和迭代对象,所以选择while循环 1、首先用random.randint定义一个随机数 2、第一次写的时候把输入的答案放在循环外面,后来想了一下,如果要根据提示再继续输入的话就必须把要每次输入的数字放入循环中,否则无法循环输入 3、break在循环里面的作用很重要,注意思考该把他放...

Python练习题079:字符串排序【代码】【图】

实现 if __name__ == '__main__':str1 = input('input string:\n')str2 = input('input string:\n')str3 = input('input string:\n')print(str1,str2,str3)if str1 > str2 : str1,str2 = str2,str1if str1 > str3 : str1,str3 = str3,str1if str2 > str3 : str2,str3 = str3,str2print('\n after being sorted.\n')print(str1,str2,str3)运行结果:

Python练习题085:几个9能整除输入的奇数【代码】

题目 输入一个奇数,然后判断最少几个 9 除于该数的结果为整数。 分析 逐个判断,不能整除则增加9的个数。增加的时候只要扩大10倍再加9即可。 实现 n=int(input("plz input odd num:")) a=9 while a%n!=0:a=a*10+9 print("{}能够除尽{}".format(a,n))

Python练习题096:统计字符个数【代码】

题目 计算字符串中子串出现的次数。 实现 if __name__=="__main__":str1=input("plz input string1:")str2=input("plz input string2:")print(str1.count(str2))

python3 day07 练习题【代码】

1、new方法和init方法执行的执行顺序?答:先执行new方法,开辟内存,创建对象,再执行init # class Person(): # def __init__(self, name, age): # self.name = name # self.age = age # print("我是init方法.") # # def __new__(cls, *args, **kwargs): # print("我是new方法.") # return object.__new__(cls) # # p = Person("lily", 18) # p2 = Person("lucy", 20) # print(id(p)...

Python的早期练习题-2【代码】

练习 1、- 使用列表推导式找出单词长度大于n的单词 ls = ["google","apple","hello","battle"] n = 5for item in ls:if len(item) > n:print(item)google battle2、- 使用列表推导式寻找两个列表中的相同元素 ls1 = ["google","apple","hello","battle"] ls2 = ["gooogle","dpple","hello","baattle"]for item1 in ls1:for item2 in ls2:if item1 == item2 :print(item1)hellols1 = ["google","apple","hello","battle"] ls2 = ["g...

python3 练习题 day05【代码】

1、计算两个格式化时间之间差了多少年月日时分秒 # import time # # def diff_date(date_str1, date_str2): #方法一 # diff_second = int(abs(time.mktime(time.strptime(date_str1, "%Y-%m-%d %H:%M:%S")) - # 取两个时间的秒差值 # time.mktime(time.strptime(date_str2, "%Y-%m-%d %H:%M:%S")))) # struct_time = time.localtime(diff_second) # 结构化时间 # diff_year = struct...

python 练习题【代码】【图】

长方形 height = int(input("please you want to height:")) width = int(input("please you want to width:")) num_height = 0 while height > num_height:num_width = 1while width > num_width:num_width += 1print("*",end="")print("*")num_height += 1View Code 乘法表 num = 1 while num <= 9:every = 1while num >= every:print(str(num) + "*" + str(every) + " = " + str(num*every),end=" ")every += 1print()num +=...

【Python、练习题】自定义栈,并实现O(1)查找最值方法【代码】

以查找最小值为例 class MyStack:def __init__(self):self.values = []self.mins = []def push(self, ins):if not self.mins:self.mins.append((0, ins))elif ins < self.mins[-1][1]:self.mins.append((len(self.values), ins))self.values.append(ins)def pop(self):if self.mins and len(self.values)-1 == self.mins[-1][0]:self.mins.pop()return self.values.pop() if self.values else None# def top(self):# return se...

Python中封装、继承、多态的练习题【代码】【图】

一. 1.房子有户型,总面积和家具名称列表 新房子没有任何的家具 2.家具有名字和占地面积,其中 床:占4平米 衣柜:占2平米 餐桌:占1.5平米 3.将以上三件家具添加到房子中 4.打印房子时,要求输出:户型,总面积,剩余面积,家具名称列表 源代码 class Furniture(): def __init__(self,name,area):self.name = nameself.area = areadef __str__(self):return '[%s]占地: %.2f' %(self.name,self.area)class House():def __init__(se...

Python基础练习题100例(Python 3.x)【代码】【图】

1:题目:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。 程序源代码: 1 for i in range(1, 5): 2 for j in range(1, 5): 3 for k in range(1, 5): 4 if (i != k) and (i != j) and (j != k): 5 print(i, j, k)View Code 以上实例输出结果为:1 1 2 ...

python六十四课——高阶函数练习题(二)【代码】

总结:高阶函数以及匿名函数之间的配合使用from functools import reduce #模块一:lambda和filter的结合使用 #lt = [1,2,3,4,5,6,7,8,9] --> [3,6,9]] lt = [1,2,3,4,5,6,7,8,9] print(list(filter(lambda x:x%3==0,lt)))#模块二:lambda和map的结合使用 #容器/序列对象:range对象 mo=map(lambda x:x**2,range(5)) print(list(mo))#模块三: 在模块二的基础上扩展功能:range(10) 过滤以后保留的数据范围为:(5,50)之间 mo=map(...

python六十四课——高阶函数练习题(三)【代码】

案例五:求两个列表元素的和,返回新列表lt1 = [1,2,3,4]lt2 = [5,6]效果:[6,8,10,12]lt1=[1,2,3,4] lt2=[5,6] print(list(map(lambda x,y:x+y,lt1,lt2)))案例六:求字符串中每个单词的长度效果:[7,2,8]content = Welcome To ShangHai#切割是关键,因为一切就是列表了,按照什么切呢?直接split()切content=Welcome To ShangHai word_list=content.split() mo=map(len,word_list) print(list(mo),type(mo))

python练习题-day18【代码】

1、匹配一行文字中的所有开头的字母内容 import re s="i love you not because of who you are, but because of who i am when i am with you"import re content=re.findall(r"\b\w",s) print(content)2、匹配一行文字中的所有开头的数字内容 import re s="i love you not because 12sd 34er 56df e4 54434"import re s="i love you not because 12sd 34er 56df e4 54434" ret=re.findall(r"\b\d+",s) print(ret)3、匹配一行文字...