The way of learning python 21-30 questions

Keywords: Python Pycharm crawler

**We have updated 1-20 questions for you. We have been learning python for half a year. We first understand these basic questions, which is of great benefit to the improvement of your ability. Here we update 21-30 questions, and later we will update some basic knowledge of python. I hope you will pay more attention.
21. Questions:
The robot moves in the plane from the origin (0,0). The robot can move up, down, left and right as long as the number of steps is given. The robot trajectory is as follows:
UP 5
DOWN 3
LEFT 3
RIGHT 2
¡­
The number after the direction is the number of steps. Please write a program to calculate the distance from the current position after a series of motions and the origin. If the distance is a floating point number, only the nearest integer is printed.

example:

If the following tuples are used as input to the program:
UP 5
DOWN 3
LEFT 3
RIGHT 2
¡­
Then, the output of the program should be:
2

import math
pos = [0,0]
while True:
    s = input('Input:')
    if not s:
        break
    movement = s.split(" ")#Divided by spaces
    direction = movement[0]
    steps = int(movement[1])
    if direction=="UP":
        pos[0]+=steps
    elif direction=="DOWN":
        pos[0]-=steps
    elif direction=="LEFT":
        pos[1]-=steps
    elif direction=="RIGHT":
        pos[1]+=steps
    else:
        pass

print(int(round(math.sqrt(pos[1]**2+pos[0]**2))))

22. Question:
Write a program to calculate the frequency of words from input. The output should be output after sorting the keys alphabetically.

Suppose the following inputs are provided 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

ls = input ("please input:").split()

digit = []
digitchar = []
upper = []
lower = []
for i in ls:
    if i.isalpha():            
        if i.islower():           
            lower.append(i)
        else:    
            upper.append(i)
    elif i.isdigit():
        digit.append(i)
    else:
        digitchar.append(i)
tol = []
for i in sorted(set(digit)):
    tol.append(i)
for i in sorted(set(digitchar)):
    tol.append(i)
for i in sorted(set(upper)):
    tol.append(i)
for i in sorted(set(lower)):
    tol.append(i)
for z in tol:
    print (str(z)+":"+str(tol.count(z)))

23. Question:
Write a method of calculating the square value of a number

ls = input ("please input :")
for i in ls:
    j= int(i)**2
print (j)

24. Question:
Python has many built-in functions. If you don't know how to use it, you can read the documentation online or find some books. However, python has a built-in document function for each built-in function.

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

And add documents for your functions

There's not much more here, abs()Absolute value, int()Integer, input()input

25. Question:
Define a class that has a class parameter and the same instance parameter.

class mytest():
    def test(self):
        self.x = input ("please input:")
        print (self.x)
aaa=mytest()
aaa.test()

26. Question:
Define a function that can calculate the sum of two numbers.

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

print(SumFunction(1,2))

27. Question:
Defines a function that converts an integer to a string and prints it in the console.

def printValue(n):
	print str(n)

printValue(1997)

28. Question:
Define a function that can receive two integers in string form, calculate their sum, and then print them out in the console.
(note that the difference from the previous one is that the input here is a string type)

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

printValue("5","8") #13

29. Question:
Define a function that can take two strings as input, connect them, and print them out in the console.
(addition and subtraction of int type and addition and subtraction of string should be noted by beginners)

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

printValue("2","50") #250

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

def function(x,y):
    if len(x)>len(y):
        print (x)
    elif len(x)<len(y):
        print (y)
    else:
        print (x)
        print (y)
x = input("input x:")
y = input("input y:")
function(x,y)

Here we update 21-30 questions. The code can run. I don't know how to leave a message in the comment area. I hope you can pay more attention and make progress together!

Posted by JayFM on Tue, 21 Sep 2021 17:45:51 -0700