iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python基础练习100题 ( 21
  • 928
分享到

Python基础练习100题 ( 21

基础Python 2023-01-31 08:01:59 928人浏览 泡泡鱼

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

摘要

昨天和大家分享了前10道题,今天继续来刷21~30 Question 21: A robot moves in a plane starting from the original point (0,0). The robot can

昨天和大家分享了前10道题,今天继续来刷21~30

Question 21:

A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:
UP 5
DOWN 3
LEFT 3
RIGHT 2
The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer.
Example:
If the following tuples are given as input to the program:
UP 5
DOWN 3
LEFT 3
RIGHT 2
Then, the output of the program should be:
2

解法一

import  math

x,y = 0,0
while True:
    s = input().split()
    if not s:
        break
    if s[0]=='UP':                  # s[0] indicates command
        x-=int(s[1])                # s[1] indicates unit of move
    if s[0]=='DOWN':
        x+=int(s[1])
    if s[0]=='LEFT':
        y-=int(s[1])
    if s[0]=='RIGHT':
        y+=int(s[1])
                                    # N**P means N^P
dist = round(math.sqrt(x**2 + y**2))  # euclidean distance = square root of (x^2+y^2) and rounding it to nearest integer
print(dist)

Question 22:

Write a program to compute the frequency of the Words from the input. The output should output after sorting the key alphanumerically.

Suppose the following input is supplied to the program:

New to python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.
Then, the output should be:
2:2
3.:1
3?:1
New:1
Python:5
Read:1
and:1
between:1
choosing:1
or:2
to:1

解法一

ss = input().split()
word = sorted(set(ss))     # split words are stored and sorted as a set

for i in word:
    print("{0}:{1}".fORMat(i,ss.count(i)))

解法二

ss = input().split()
dict = {}
for i in ss:
    i = dict.setdefault(i,ss.count(i))   

dict = sorted(dict.items())            
                                         
for i in dict:
    print("%s:%d"%(i[0],i[1]))

解法三

from collections import Counter

ss = input().split()
ss = Counter(ss)         # returns key & frequency as a dictionary
ss = sorted(ss.items())  # returns as a tuple list

for i in ss:
    print("%s:%d"%(i[0],i[1]))

Question 23:

Write a method which can calculate square value of number

解法一

def square(num):
    return num ** 2

print(square(2))
print(square(3))

解法二

n=int(input())
print(n**2)

Question 24:

Python has many built-in functions, and if you do not know how to use it, you can read document online or find some books. But Python has a built-in document function for every built-in functions.

Please write a program to print some Python built-in functions documents, such as abs(), int(), raw_input()

And add document for your own function

解法一

print (abs.__doc__)
print (int.__doc__)

def square(num):
    '''
 Return the square value of the input number.
 The input number must be integer.
 '''
    return num ** 2

print (square(2))
print (square.__doc__)

Question 25:

Define a class, which have a class parameter and have a same instance parameter.

解法一

class Car:
    name = "Car"

    def __init__(self,name = None):
        self.name = name

honda=Car("Honda")
print("%s name is %s"%(Car.name,honda.name))

toyota=Car()
toyota.name="Toyota"
print("%s name is %s"%(Car.name,toyota.name))

解法二

class Person:
    # Define the class parameter "name"
    name = "Person"
    
    def __init__(self, name = None):
        # self.name is the instance parameter
        self.name = name

jeffrey = Person("Jeffrey")
print ("{0} name is {1}".format(Person.name, jeffrey.name))

nico = Person()
nico.name = "Nico"
print (f"{Person.name} name is {nico.name}")

Question 26:

Define a function which can compute the sum of two numbers.

解法一

sum = lambda n1,n2 : n1 + n2      # here lambda is use to define little function as sum
print(sum(1,2))

解法二

def SumFunction(number1, number2):
    return number1 + number2

print SumFunction(1,2)

Question 27:

Define a function that can convert a integer into a string and print it in console.

解法一

def printValue(n):
    print (str(n))

printValue(3)

解法二

conv = lambda x : str(x)
n = conv(10)
print(n)
print(type(n))  

Question 28:

Define a function that can receive two integer numbers in string form and compute their sum and then print it in console.

解法一

def printValue(s1,s2):
    print int(s1) + int(s2)
printValue("3","4") #7

解法二

sum = lambda s1,s2 : int(s1) + int(s2)
print(sum("10","45"))      # 55

Question 29:

Define a function that can accept two strings as input and concatenate them and then print it in console.

解法一

def printValue(s1,s2):
    print s1 + s2

printValue("3","4") #34

解法二

sum = lambda s1,s2 : s1 + s2
print(sum("10","45"))        # 1045

Question 30:

Define a function that can accept two strings as input and print the string with maximum length in console. If two strings have the same length, then the function should print all strings line by line.

解法一

def printVal(s1,s2):
    len1 = len(s1)
    len2 = len(s2)
    if len1 > len2:
        print(s1)
    elif len1 < len2:
        print(s2)
    else:
        print(s1)
        print(s2)

s1,s2=input().split()
printVal(s1,s2)

这十道题的代码在我的GitHub上,如果大家想看一下每道题的输出结果,可以点击以下链接下载:

  • Python 21-30题

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

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

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

--结束END--

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

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

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

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 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题 ( 1~
    大家好,好久不见,我最近在Github上发现了一个好东西,是关于夯实Python基础的100道题,原作者是在Python2的时候创建的,闲来无事,非常适合像我一样的小白来练习 对于每一道题,解法都不唯一,我在这里仅仅是抛砖引玉,希望可以...
    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
  • 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题 ( 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题 ( 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题 ( 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基础练习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题 ( 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基础】练习题
    # 练习题 ''' 1、简述编译型语言和解释性语言的区别,并且列出你知道哪些语言为编译型那些为解释型 编译型语言:每次编写完成后都要将其编译成二进制(0和1)文件 优点:运行速度快 ...
    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--基础练习
    1. 在Linux电脑上安装python,ipython,pycharm专业版本软件;   2. 在Windows电脑上安装python3版本,并配置环境变量,确保Dos环境下运行脚本;   3. Linux下有多少种运行python的不同...
    99+
    2023-01-31
    基础 Python
  • 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 水题
  • 基础Python练习题有哪些
    本篇内容主要讲解“基础Python练习题有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“基础Python练习题有哪些”吧!1. 输入一个百分制成绩,要求输出成绩等级A、B、C、D、E,其中9...
    99+
    2023-06-25
  • python基础学习21----进程
    python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程。 进程与线程的使用有很多相似之处,有关线程方面的知识请参考https://www.cnblogs.com/sfen...
    99+
    2023-01-30
    进程 基础 python
  • python基础练习_1.1
    练习_1.1练习题目:    1 打印九九乘法表     2 打印下方菱形     3 打印100以内的斐波那契数列     4 求斐波那契数列第101项     5 求10万内的所有质数        *          ***    ...
    99+
    2023-01-31
    基础 python
  • 100道python经典练习题
    链接:https://pan.baidu.com/s/1K0iuZKJukLoGQ8OBy7xq1Q 提取码:2s6q 链接长期有效,如有疑问,欢迎评论区交流。 ...
    99+
    2023-01-31
    练习题 经典 python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作