# Python simple exercise: 11 questions

## 1. Calculate the sum of all numbers in a string

```def numsum(s):
sum=0 #Define variables and prepare to record the sum of numbers
for i in range(len(s)): #Traversal string
if s[i]>='0' and s[i]<='9': #If the character at i is a character
sum= sum + int(s[i]) #Convert characters to int and sum
return sum

print(numsum(s))
```

## 2. String output in reverse order

```a = input('Please enter a string')
print('The output in reverse order is:')
print(a[::-1])
def nx(s):
sum='' #Define an empty string
for i in reversed(range(len(s))): #i traversal in reverse order
sum=sum+s[i] #Make separate strings
return sum
print(nx(s))
```

## 3. Write a program, input a list containing several integers, and output the maximum value in the list. For example, input [1, 2, 3, 4, 5888] and output 888

```def max(list):
a= list[0]
for i in range(0,len(list)):
if list[i]>list[0]:
a=list[i]
return a
print('The maximum number is:'+max(a))
#For a given digital input:
#1 2 3 4
#The way to convert it to a list is:
#li =list(map(int,input().split()))
```

## 4. Write a program, input a list containing several integers, convert all integers in the list into strings, and then output a list containing these strings. For example, input [1, 2, 3, 4, 5888] and output ['1', '2', '3', '4', '5', '888'].

```def question4():
li=list(map(int,input('Please enter a string of integers separated by spaces:').split(',')))
print('Convert to list:',[str(i) for i in li])
question4()
```

## 5. Write a program, input a list containing several arbitrary data, and output a list composed of elements equivalent to True in the list. For example, input [1, 2, 0, None, False, 'a'] and output [1, 2, 'a'].

```def question5():
li = list(input('Please enter a string separated by spaces:').split(','))
a=[]
for i in li :
if i!=0 and i!='None' and i!='none' and i!='False' and i!='false':
a.append(i)
print('Equivalent to True The elements are:',a)
question5()
```

## 6. Write a program, input a list containing several natural numbers, and output the average value of these natural numbers.

```def question6():
li = list(map(int,input('Please enter a string of natural numbers separated by spaces:').split()))
sumn=0
for i in li:
sumn+=i
avg=sumn/len(li)
print('The average is:%.3f'%avg)
question6()
```

## 7. Write a program, input a list containing several natural numbers, and output a new list of these natural numbers in descending order.

```def question7():
li = list(map(int,input('Please enter a string of natural numbers separated by spaces:').split()))
for i in range(0,len(li)):
for j in range(i,len(li)):
if li[i] < li[j]:
temp=li[i]
li[i]=li[j]
li[j]=temp
print('In descending order:',li)
question7()
```

## 8. Write a program, input a list containing several natural numbers, and output a new list. Each element in the new list is the number of digits of each natural number in the original list. For example, input [1888, 9923456] and output [1, 3, 2, 5].

```def question8():
li = list(map(int,input('Please enter a string of natural numbers separated by spaces:').split()))
m=[]
n=0
for i in li :
m.append(len(str(i)))
n+=1
print('The new list is:',m)
question8()
```

## 9. Write a program, input a list containing several real numbers, and output the real number with the largest absolute value. For example, input [- 8, 64, 3.5, - 89], output - 89.

```def question9():
li = list(map(eval,input('Please enter a string of real numbers separated by spaces:').split()))
maxn=li[0]
for i in li:
if abs(maxn)<abs(i):
maxn=i
print('The maximum absolute value is:',maxn)
question9()
```

## 10. Write a program, input a list containing several integers, and output the product of these integers. For example, input [- 2, 3, 4], output - 24.

```def question10():
li =list(map(eval,input('Please enter a string of integers separated by spaces:').split()))
product=1;
for i in li:
product=product*i
print('The product is:'.product)

question10()
```

## 11. The two input vectors are regarded as the inner product of the two input vectors.

```def question11():
li1 =list(map(eval,input('Please enter the first string of real numbers separated by spaces').split()))
li2 =list(map(eval,input('Please enter the second string of real numbers separated by spaces:').split()))
m=[]
for i in range(0,len[li1]):
m.append(li1[i]*li2[i])
print('The inner product is:',m)
question11()
```

Tags: Python

Posted by jredwilli on Mon, 16 May 2022 02:09:03 +0300