--Originally published at Not a Programming Blog
1. 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.)
2. Define a function max_of_three()
that takes three numbers as arguments and returns the largest of them.
3. 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.)
4. Write a function that takes a character (i.e. a string of length 1) and returns True
if it is a vowel, False
otherwise.
5. 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"
.
6. Define a function sum()
and a function multiply()
that sums and multiplies (respectively) all the numbers in a list of numbers. For example, sum([1, 2, 3, 4])
should return 10
, and multiply([1, 2, 3, 4])
should return 24
.
7. Define a function reverse()
that computes the reversal of a string. For example, reverse("I am testing")
should return the string "gnitset ma I"
.
8. Define a function is_palindrome()
that recognizes palindromes (i.e. words that look the same written backwards). For example, is_palindrome("radar")
should return True
.
9. Write a function is_member()
that takes a value (i.e. a number, string, etc) x
and a list of values a
, and returns True
if x
is a member of a
, False
otherwise. (Note that this is exactly what
Continue reading "46 Simple Python Exercises (0-10)" →