返回顶部
首页 > 资讯 > 后端开发 > Python >Python基础练习100题 ( 1~
  • 720
分享到

Python基础练习100题 ( 1~

基础Python 2023-01-31 07:01:10 720人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

大家好,好久不见,我最近在GitHub上发现了一个好东西,是关于夯实Python基础的100道题,原作者是在python2的时候创建的,闲来无事,非常适合像我一样的小白来练习 对于每一道题,解法都不唯一,我在这里仅仅是抛砖引玉,希望可以

大家好,好久不见,我最近在GitHub上发现了一个好东西,是关于夯实Python基础的100道题,原作者是在python2的时候创建的,闲来无事,非常适合像我一样的小白来练习

对于每一道题,解法都不唯一,我在这里仅仅是抛砖引玉,希望可以集合大家的智慧,如果哪道题有其他解法,希望可以在评论中留下大家宝贵的意见!每次我会更新10道题,一共会更新10篇,这也算是对我之前的文章一个总结啦,如果没有看到我之前有关Python的小白学习分享的同学们,可以戳下面连接查看哈:

  • Python 基础起步,写给同为小白的你
  • Python 进阶之路
  • Python pandas 之旅

如果大家想要和我联系,可以访问我的个人主页:

  • 我的个人主页

好啦,闲话少说,让我们开始今天的刷题之旅吧!

Question 1:

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.

解法一

for i in range(2000,3201):
    if i%7 == 0 and i%5!=0:
        print(i,end=',')
print("\b")

解法二

numbers = [str(x) for x in range(2000,3201) if (x%7==0) and (x%5!=0)]
print (','.join(numbers))

Question 2:

*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*

解法一

def fact(x):
    if x == 0:
        return 1
    return x * fact(x - 1)
x=int(input())
print(fact(x))

解法二


import math as ma
x=int(input())
print(ma.factorial(x))

解法三

from functools import reduce
from operator import mul

x=int(input())
print(reduce(mul,range(1,x+1)))

Question 3:

With a given integral number n, write a program to generate a dictionary that contains (i, i x i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.Suppose the following input is supplied to the program: 8

Then, the output should be:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}

解法一

n=int(input())
d=dict()
for i in range(1,n+1):
    d[i]=i*i
print(d)

解法二

n=int(input())
d={x:x*x for x in range(1,n+1)}
print(d)

Question 4:

Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.Suppose the following input is supplied to the program:
34,67,55,33,12,98
Then, the output should be:
['34', '67', '55', '33', '12', '98']
('34', '67', '55', '33', '12', '98')

解法一

values=input()
l=values.split(",")
t=tuple(l)
print(f"List of values : {l}")
print(f"Tuple of values : {t}")

Question 5:

Define a class which has at least two methods:

  • getString: to get a string from console input
  • printString: to print the string in upper case.

Also please include simple test function to test the class methods.


解法一


class InputOutString:
    def __init__(self):
        self.s = ""

    def getString(self):
        self.s = input()

    def printString(self):
        print(self.s.upper())
        
# Test

a = InputOutString()
a.getString()
a.printString()

Question 6:

Write a program that calculates and prints the value according to the given fORMula:

Q = Square root of [(2 C D)/H]

Following are the fixed values of C and H:

C is 50. H is 30.

D is the variable whose values should be input to your program in a comma-separated sequence.For example
Let us assume the following comma separated input sequence is given to the program:*

100,150,180
The output of the program should be:
18,22,24

解法一


import math
c=50
h=30
value = []
items= [x for x in input("Input numbers comma-separated:").split(',')]
for d in items:
    value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))

print (','.join(value))

Question 7:

Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i * j.

Note: i=0,1.., X-1; j=0,1,¡­Y-1. Suppose the following inputs are given to the program: 3,5

Then, the output of the program should be:

[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]

解法一

x,y = map(int,input().split(','))
lst = []

for i in range(x):
    tmp = []
    for j in range(y):     
        tmp.append(i*j)
    lst.append(tmp)
    
print(lst)

解法二


x,y = map(int,input().split(','))
lst = [[i*j for j in range(y)] for i in range(x)]  
print(lst)

Question 8:

Write a program that accepts a comma separated sequence of Words as input and prints the words in a comma-separated sequence after sorting them alphabetically.

Suppose the following input is supplied to the program:

without,hello,bag,world
Then, the output should be:
bag,hello,without,world

解法一

original_string = input("Input Text:")
l = original_string.split(",")
final_string = sorted(l,key=str)
print(','.join(final_string))

解法二


lst = input().split(',')
lst.sort()
print(",".join(lst))

Question 9:

Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized.

Suppose the following input is supplied to the program:

Hello world
Practice makes perfect
Then, the output should be:
HELLO WORLD
PRACTICE MAKES PERFECT

解法一


lines = []
while True:
    s = input()
    if s:
        lines.append(s.upper())  
    else:
        break;

for sentence in lines:
    print(sentence)

Question 10:

Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically.

Suppose the following input is supplied to the program:

hello world and practice makes perfect and hello world again
Then, the output should be:
again and hello makes perfect practice world

解法一

word = input().split()

for i in word:
    if word.count(i) > 1:    #count function returns total repeatation of an element that is send as argument
        word.remove(i)     # removes exactly one element per call

word.sort()
print(" ".join(word))

解法二



s = input("Input Text:")
words = [word for word in s.split(" ")]
print (" ".join(sorted(list(set(words)))))

我把前十道题的代码上传到github上啦,如果大家想看一下每道题的输出结果,可以点击以下链接下载:

  • Python 1-10题

我的运行环境Python 3.6+,如果你用的是Python 2.7版本,绝大多数不同就体现在以下3点:

  • raw_input()在python3中是input()
  • print需要加括号
  • fstring 可以换成.format(), 或者%s,%d

谢谢大家,我们下期见!希望各位朋友不要吝啬,把每道题的更高效的解法写在评论里,我们一起进步!!!

--结束END--

本文标题: Python基础练习100题 ( 1~

本文链接: https://lsjlt.com/news/192275.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • Python基础练习100题 ( 1~
    大家好,好久不见,我最近在Github上发现了一个好东西,是关于夯实Python基础的100道题,原作者是在Python2的时候创建的,闲来无事,非常适合像我一样的小白来练习 对于每一道题,解法都不唯一,我在这里仅仅是抛砖引玉,希望可以...
    99+
    2023-01-31
    基础 Python
  • python基础1习题练习
    python基础1习题练习: #encoding:utf-8 #1.实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败! name=input('name>>: ').strip...
    99+
    2023-01-31
    习题 基础 python
  • Python基础练习100题 ( 31
    昨天和大家分享了21-30题,今天继续来刷31~40题 Question 31: Define a function which can print a dictionary where the keys are number...
    99+
    2023-01-31
    基础 Python
  • Python基础练习100题 ( 41
    大家好,我又回来了,昨天和大家分享了31-40题,今天继续来看41~50题 Question 41: Write a program which can map() to make a list whose elements are s...
    99+
    2023-01-31
    基础 Python
  • Python基础练习100题 ( 61
    昨天和大家分享了51-60题,今天继续来刷61~70题 Question 61: The Fibonacci Sequence is computed based on the following formula: f(n)=0 if ...
    99+
    2023-01-31
    基础 Python
  • Python基础练习100题 ( 81
    昨天和大家分享了71-80题,今天继续来刷81~90题 Question 81: By using list comprehension, please write a program to print the list after r...
    99+
    2023-01-31
    基础 Python
  • Python基础练习100题 ( 71
    昨天和大家分享了61-70题,今天继续来刷71~80题 Question 71: Please write a program to output a random number, which is divisible by 5 and...
    99+
    2023-01-31
    基础 Python
  • Python基础练习100题 ( 51
    昨天和大家分享了41-50题,今天继续来刷51~60题 Question 51: Write a function to compute 5/0 and use try/except to catch the exceptions. ...
    99+
    2023-01-31
    基础 Python
  • Python基础练习100题 ( 21
    昨天和大家分享了前10道题,今天继续来刷21~30 Question 21: A robot moves in a plane starting from the original point (0,0). The robot can ...
    99+
    2023-01-31
    基础 Python
  • Python基础练习100题 ( 91
    昨天和大家分享了81-90题,今天继续来刷最后的91-100题 Question 91: Please write a program which accepts a string from console and print it ...
    99+
    2023-01-31
    基础 Python
  • Python基础练习100题 ( 11
    上一期和大家分享了前10道题,今天继续来刷11~20 Question 11: Write a program which accepts a sequence of comma separated 4 digit binary nu...
    99+
    2023-01-31
    基础 Python
  • 【Python基础】练习题
    # 练习题 ''' 1、简述编译型语言和解释性语言的区别,并且列出你知道哪些语言为编译型那些为解释型 编译型语言:每次编写完成后都要将其编译成二进制(0和1)文件 优点:运行速度快 ...
    99+
    2023-01-31
    练习题 基础 Python
  • python练习题1
    题目:输入某年某月某日,判断这一天是这一年的第几天? 分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊 情况,闰年且输入月份大于3时需考虑多加一天。 dateType= input('请输入年月日的格式为:...
    99+
    2023-01-31
    练习题 python
  • 【14】Python100例基础练习(1
    例1:有四个数字:1、2、3、4能组成多少个互不相同且无重复的数字的三位数?各是多少?审题:1.去重2.计算总数程序代码:方法1: dict=[] for in range(1,5): #i变量赋值 1 2 3 4 for j ...
    99+
    2023-01-31
    基础
  • Python--基础练习
    1. 在Linux电脑上安装python,ipython,pycharm专业版本软件;   2. 在Windows电脑上安装python3版本,并配置环境变量,确保Dos环境下运行脚本;   3. Linux下有多少种运行python的不同...
    99+
    2023-01-31
    基础 Python
  • 基础Python练习题有哪些
    本篇内容主要讲解“基础Python练习题有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“基础Python练习题有哪些”吧!1. 输入一个百分制成绩,要求输出成绩等级A、B、C、D、E,其中9...
    99+
    2023-06-25
  • python基础知识练习题(二)
    1、 有两个列表   l1 = [11, 22, 33]   l2 = [22, 33, 44]    a.获取内容相同的元素列表 li = []l1 = [11, 22, 33] l2 = [22, 33, 44] for v1 i...
    99+
    2023-01-31
    练习题 基础知识 python
  • Python 基础练习 PAT水题(四)
    #学习笔记#用以练习python基础#原题链接:https://www.patest.cn/contests/pat-b-practise/1050 本题要求将给定的N个正整数按非递增的顺序,填入“螺旋矩阵”。所谓“螺旋矩阵”,是指从左上角...
    99+
    2023-01-31
    基础 Python 水题
  • 100道python经典练习题
    链接:https://pan.baidu.com/s/1K0iuZKJukLoGQ8OBy7xq1Q 提取码:2s6q 链接长期有效,如有疑问,欢迎评论区交流。 ...
    99+
    2023-01-31
    练习题 经典 python
  • python练习集100题(21-40)
    题目21:两个乒乓球队进行比赛,各出3人。甲队为a,b,c三人,乙队为x,y,z三人。以抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x、z比,请编程找出三队比赛名单。first_list=['x','y','z']...
    99+
    2023-01-31
    python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作