46 Simple Python Exercises 1-5 questions, pythonexercises
The original question and answer will be posted. The answer is not optimal, and it also reflects my learning process. If there is time, the optimized code will be updated.
This is version 0.45 of a collection of simple Python exercises constructed (but in between cases only found and collected) by Torbjörn Lager (torbjorn.lager@ling.gu.se ). most of them involve characters, words and phrases, rather than numbers, and are therefore suitable for students interested in language rather than math.
- Define a function max () that takes two numbers as arguments and returns the largest of them. use the if-then-else construct available in Python. (It is true that Python has the max () function built in, but writing it yourself is nevertheless a good exercise .)
#Define a function max() that takes two numbers as# arguments and returns the largest of them.# Use the if-then-else construct available in Python.# (It is true that Python has the max() function built in,# but writing it yourself is nevertheless a good exercisedef max(number1, number2): #number1=input('input number1:') #number2=input('input number2:') if number1>number2: return print('the larger one is:{}'.format(number1)) elif number2>number1: return print('the larger one is:{}'.format(number2)) else: return print('number1=number2')max(1,2)max(3,2)max(2,2)
- Define a function max_of_three () that takes three numbers as arguments and returns the largest of them.
#Define a function max_of_three() that takes three numbers# as arguments and returns the largest of themdef max_of_three(a,b,c): #a = input('input a:') #b = input('input b:') #c = input('input c:') if a>b: if a>c: return print('the larger one is a: {}'.format(a)) elif c>a: return print('the larger one is c:{}'.format(c)) elif a==c: return print('the larger one is a,c:{}'.format(c)) elif a<b: if b>c: return print('the larger one is b: {}'.format(b)) elif b<c: return print('the larger one is c:{}'.format(c)) elif b == c: return print('the larger one is b,c:{}'.format(c)) elif a==b: if a>c: return print('the larger one is a,b: {}'.format(a)) if a<c: return print('the larger one is c:{}'.format(c)) if a==c: return print('a=b=c')max_of_three(1,2,3)max_of_three(2,2,3)max_of_three(1,3,3)max_of_three(3,2,3)max_of_three(2,3,2)max_of_three(1,3,2)max_of_three(1,3,3)max_of_three(3,2,1)max_of_three(3,3,1)max_of_three(3,2,2)max_of_three(2,2,2)
- Define a function that computes the length of a given list or string. (It is true that Python has the len () function built in, but writing it yourself is nevertheless a good exercise .)
#Define a function that computes the length of a given list or string.# (It is true that Python has the len() function built in,# but writing it yourself is nevertheless a good exercise.)#a=len(['num',2,3,4])#print(a)def len(words): #words=input('input list or string:') #print(words) #print(type(words)) i=0 for word in words: #print(word) i=i+1 print(i)len([1,2,3,'num','a'])len('asdfghjkl')len('hjfjsd,fdf')
- Write a function that takes a character (I. e. a string of length 1) and returns True if it is a vowel, False otherwise.
#Write a function that takes a character (i.e. a string of length 1)# and returns True if it is a vowel, False otherwise.def vowelt(): vowel = ['a', 'e', 'i', 'o', 'u'] ch=input('input a string of length 1: ') if len(ch)==1: if ch in vowel: return True else: return Falseif vowelt(): print('a')else: print('b')
- Write a function translate () that will translate a text into "rövarspr into ket" (Swedish for "robber's language "). that is, double every consonant and place an occurrence of "o" in. for example, translate ("this is fun") shocould return the string "tothohisos isos fofunon ".
# Write a function translate() that will translate a text into "rövarspråket"# (Swedish for "robber's language"). That is,# double every consonant and place an occurrence of "o" in between.# For example, translate("this is fun") should return the string "tothohisos isos fofunon".def robber(robber): vowel = ['a', 'e', 'i', 'o', 'u'] robb=[] for rob in robber: if rob in vowel: robb.append(rob) elif rob==" ": robb.append(rob) else: robb.append((rob+'o'+rob)) return(''.join(robb))b=input('input a text: ')a=robber(b)print(a)