Python Functions Exercises

Exercise 1

Kilometres to miles converter

Write a function that takes kilometres as a parameter ,converts it into miles and returns the result

 

 

 

2

Divisible by 11 checker

Write a function that takes an integer as an argument and returns whether it is divisible by 11 or not.

 

 

 

3

Highest number

Write a function that takes 2 numbers as arguments and returns the highest of the 2 numbers.

 

 

 

4

Hexagon area calculator

Write a function that takes the length of a side of a regular hexagon as a parameter and returns the area of the hexagon.

Hint: Video for area of a hexagon calculation

 

5

Days til Christmas

Write a function that calculates how many days it is until Christmas and returns the number of days.

 

You may want to use the Datetime module to help you.

 

 

6

Is a palindrome?

Create a function that checks to see if a given string is a palindrome or not.

 

 

You might want to take a look at the String methods video for some help.

 

 

7

Fuel cost calculator

Write a function that takes a distance as a required argument, mpg (default 50 mpg) and fuel costs (default $1 a litre) as optional arguments. The function should return the cost in dollars.

8

Most common character

Create a function that takes a string as a parameter and the returns the most common character in that string.

 

 

 

 

9

Is a prime number?

Write a function that takes an integer and checks if it is a prime number or not, returning a Boolean.

 

 

 

10

All Prime numbers

Write a function that takes an integer value as an argument and returns a list of all prime numbers up to that number.

Hint: You might want to use modulo

 

 

11

Highest Common Factor

Write a function that takes 2 integer values and returns the highest common factor of the numbers.

 

12

Date checker

Write a function that takes a date in string format DD/MM/YYYY and checks to see if it is a valid date and in the correct format.

Your function should take account of leap years.

 

You may want to look at the date conversion video for help.

 

13

Postcode Checker

Write a function that takes a post code as an argument and returns whether it is a valid post code. 

Valid format:

[2 letters]  [1 or 2 Digits] [Space] [1 Digit] [2 letters]

(This is a slight simplification from the real UK postcode system to make the challenge a little easier – otherwise you would need to use a pretty complicate regular expression)

 

Hint: You might want to use on or more of the follow:

 

14

Check Valid Email Address

Write a function that takes an email address as an argument and returns True/False depending on whether it is a valid email address.

Check rules:

  • Must contain at least 1 character before the at symbol
  • Must contain an @ symbol
  • Must have at-least 1 character after the @ symbol and before the period(.)
  • Must contain at least 1 character after the last period(.).
  • Maximum 256 characters
  • Must start with a letter or a number

15

Caesar Cipher Encryptor / Decryptor

Create a function that takes some text(String) and a Shift(Integer)and then encrypts the text using the Caesar Cipher algorithm, returning the encrypted text.

Create a second function to decrypt an encrypted string, using the same input parameters and returning the decrypted text.