{"id":1192,"date":"2020-09-03T02:07:34","date_gmt":"2020-09-03T02:07:34","guid":{"rendered":"http:\/\/learnlearn.uk\/python\/?page_id=1192"},"modified":"2023-11-20T05:41:21","modified_gmt":"2023-11-20T05:41:21","slug":"python-functions-exercises","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/","title":{"rendered":"Python Functions Exercises"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Exercise 1<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 1 &#8211; Kilometers\u00a0to miles\u00a0converter<\/h3>\n<p>Write a function called <strong>km_to_miles<\/strong> that takes kilometers as a parameter, converts it into miles and returns the result.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">2<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 2 &#8211; Divisible by 11 checker<\/h3>\n<p>Write a function called <strong>is_divisable_by_11<\/strong> that takes an integer as an parameter and returns whether it is divisible by 11 or not.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">3<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 3 &#8211; Highest number<\/h3>\n<p>Write a function called <strong>get_highest<\/strong> that takes 2 numbers as parameters and returns the highest of the 2 numbers.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">4<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 4 &#8211; Hexagon area calculator<\/h3>\n<p>Write a function called <strong>hexagon_area<\/strong> that takes the length of a side of a regular hexagon as a parameter and returns the area of the hexagon.<\/p>\n<p><a href=\"https:\/\/youtu.be\/hNQpKUTyKb8\">Hint: Video for area of a hexagon calculation<\/a><\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">5<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 5 &#8211; Sleeps until Christmas<\/h3>\n<p>Write a function called <strong>sleeps_until_xmas<\/strong> that calculates how many days it is until Christmas and returns the number of sleeps left until the big day.<\/p>\n<p>&nbsp;<\/p>\n<p>You may want to use the<a href=\"https:\/\/learnlearn.uk\/python\/using-date-time-python\/\"> Datetime module<\/a> to help you.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">6<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 6 &#8211; Is a palindrome?<\/h3>\n<p>Create a function called<strong> is_palindrone<\/strong> that checks to see if a given string is a palindrome or not.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>You might want to take a look at the <a href=\"https:\/\/www.youtube.com\/watch?v=P6pKOPhUWf0\">String methods video for<\/a> some help.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">7<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 7 &#8211; Fuel cost calculator<\/h3>\n<p>Write a function called <strong>fuel_cost<\/strong> that takes a distance as a required argument, mpg (default 50 mpg) and fuel cost (default $1 a litre) as optional arguments. The function should return the cost in dollars.<\/p>\n\n<\/div><h2 class=\"tabtitle\">8<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 8 &#8211; Most common character<\/h3>\n<p>Create a function called <strong>most_common_char<\/strong> that takes a string as a argument and the returns the most common character in that string.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">9<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 9 &#8211; Is a prime number?<\/h3>\n<p>Write a function called <strong>is_prime<\/strong> that takes an integer as an argument and checks if it is a prime number or not, returning a Boolean.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">10<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 10- All Prime numbers<\/h3>\n<p>Write a function called <strong>primes<\/strong> that takes an integer value as an argument and returns a list of all prime numbers up to that number.<\/p>\n<p><span style=\"font-weight: 400;\">Hint: You might want to use <\/span><a href=\"https:\/\/www.educative.io\/edpresso\/what-is-a-modulo-operator-in-python\"><span style=\"font-weight: 400;\">modulo <\/span><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">11<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 11 &#8211; Highest Common Factor<\/h3>\n<p>Write a function called<strong> hcf<\/strong> that takes 2 integer values and returns the highest common factor of the numbers.<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">12<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 11 &#8211; Valid Date<\/h3>\n<p>Write a function called <strong>is_valid_date<\/strong> 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.<\/p>\n<p>Your function should take account of leap years.<\/p>\n<p>&nbsp;<\/p>\n<p>You may want to look at the <a href=\"https:\/\/www.youtube.com\/watch?v=zj9nuXhboAY\">date conversion video<\/a> for help.<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">13<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 13 &#8211; Postcode Checker<\/h3>\n<p><span style=\"font-weight: 400;\">Write a function called <strong>is_valid_postcode<\/strong> that takes a post code as an argument and returns whether it is a valid post code.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Valid format:<\/span><\/p>\n<p><strong>[2 letters]\u00a0 [1 or 2 Digits] [Space] [1 Digit] [2 letters]<\/strong><\/p>\n<p><span style=\"font-weight: 400;\">(This is a slight simplification from the real UK postcode system to make the challenge a little easier &#8211; otherwise you would need to use a <a href=\"https:\/\/stackoverflow.com\/questions\/164979\/regex-for-matching-uk-postcodes\">pretty complicate regular expression<\/a>)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Hint: You might want to use on or more of the follow:<\/span><\/p>\n<ul>\n<li><a href=\"https:\/\/www.w3schools.com\/python\/ref_string_partition.asp#:~:text=The%20partition()%20method%20searches,the%20part%20after%20the%20string.\"><span style=\"font-weight: 400;\">.partition string method,<\/span><\/a><\/li>\n<li><a href=\"https:\/\/www.w3schools.com\/python\/ref_string_split.asp\">.split() string method<\/a><\/li>\n<li><a href=\"https:\/\/www.w3schools.com\/python\/gloss_python_string_slice.asp\">string slicing<\/a><\/li>\n<li><a href=\"https:\/\/www.w3schools.com\/python\/ref_string_isdigit.asp\">isdigit()<\/a>, <a href=\"https:\/\/www.w3schools.com\/python\/ref_string_isalpha.asp\">isalpha()<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">14<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 14 &#8211; Check Valid Email Address<\/h3>\n<p>Write a function called <strong>is_valid_email\u00a0<\/strong> that takes an email address as an argument and returns True\/False depending on whether it is a valid email address.<\/p>\n<p>Check rules:<\/p>\n<ul>\n<li>Must contain at least 1 character before the at symbol<\/li>\n<li>Must contain an @ symbol<\/li>\n<li>Must have at-least 1 character after the @ symbol and before the period(.)<\/li>\n<li>Must contain at least 1 character after the last period(.).<\/li>\n<li>Maximum 256 characters<\/li>\n<li>Must start with a letter or a number<\/li>\n<\/ul>\n\n<\/div><h2 class=\"tabtitle\">15<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 15 &#8211; Caesar Cipher Encryptor \/ Decryptor<\/h3>\n<p>Create a function called <strong>encrypt<\/strong> that takes some text(String) and a Shift(Integer)and then encrypts the text using the Caesar Cipher algorithm, returning the encrypted text.<\/p>\n<p>Create a second function to decrypt an encrypted string, using the same input parameters and returning the decrypted text.<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">16<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions Challenge 16 &#8211; Find all palindromes<\/h3>\n<p>Create a function called <strong>get_palindrone<\/strong>s that is given a string and returns a list of all the palindromes in the string.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">17<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions challenge 17 &#8211; Sort Competitors List<\/h3>\n<p>Create a function that takes a 2D list of competitor names and league (e.g\u00a0 \u00a0 Bob Jones, &#8220;Bronze&#8221;) as well as an optional parameter &#8220;Reverse&#8221;.<\/p>\n<p>The function should return the list sorted by league. (the league senority is Bronze, Silver, Gold, Ruby, Diamond), reversed if the optional reversed parameter is set to true.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Functions Challenge 1 &#8211; Kilometers\u00a0to miles\u00a0converter Write a function called km_to_miles that takes kilometers as a parameter, converts it into miles and returns the result. &nbsp; &nbsp; &nbsp; Functions Challenge 2 &#8211; Divisible by 11 checker Write a function called is_divisable_by_11 that takes an integer as an parameter and returns whether it is divisible by&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Python Functions Exercises<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"on","neve_meta_content_width":84,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Functions Exercises - Python<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Functions Exercises - Python\" \/>\n<meta property=\"og:description\" content=\"Functions Challenge 1 &#8211; Kilometers\u00a0to miles\u00a0converter Write a function called km_to_miles that takes kilometers as a parameter, converts it into miles and returns the result. &nbsp; &nbsp; &nbsp; Functions Challenge 2 &#8211; Divisible by 11 checker Write a function called is_divisable_by_11 that takes an integer as an parameter and returns whether it is divisible by&hellip;&nbsp;Read More &raquo;Python Functions Exercises\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/\" \/>\n<meta property=\"og:site_name\" content=\"Python\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-20T05:41:21+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/\",\"url\":\"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/\",\"name\":\"Python Functions Exercises - Python\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#website\"},\"datePublished\":\"2020-09-03T02:07:34+00:00\",\"dateModified\":\"2023-11-20T05:41:21+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Python Unit Home\",\"item\":\"https:\/\/learnlearn.uk\/python\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Functions Exercises\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#website\",\"url\":\"https:\/\/learnlearn.uk\/python\/\",\"name\":\"Python\",\"description\":\"Programming\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/python\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#organization\",\"name\":\"Python\",\"url\":\"https:\/\/learnlearn.uk\/python\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png\",\"width\":710,\"height\":98,\"caption\":\"Python\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Functions Exercises - Python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/","og_locale":"en_GB","og_type":"article","og_title":"Python Functions Exercises - Python","og_description":"Functions Challenge 1 &#8211; Kilometers\u00a0to miles\u00a0converter Write a function called km_to_miles that takes kilometers as a parameter, converts it into miles and returns the result. &nbsp; &nbsp; &nbsp; Functions Challenge 2 &#8211; Divisible by 11 checker Write a function called is_divisable_by_11 that takes an integer as an parameter and returns whether it is divisible by&hellip;&nbsp;Read More &raquo;Python Functions Exercises","og_url":"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/","og_site_name":"Python","article_modified_time":"2023-11-20T05:41:21+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/","url":"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/","name":"Python Functions Exercises - Python","isPartOf":{"@id":"https:\/\/learnlearn.uk\/python\/#website"},"datePublished":"2020-09-03T02:07:34+00:00","dateModified":"2023-11-20T05:41:21+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/python\/python-functions-exercises\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Python Unit Home","item":"https:\/\/learnlearn.uk\/python\/"},{"@type":"ListItem","position":2,"name":"Python Functions Exercises"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/python\/#website","url":"https:\/\/learnlearn.uk\/python\/","name":"Python","description":"Programming","publisher":{"@id":"https:\/\/learnlearn.uk\/python\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/python\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/python\/#organization","name":"Python","url":"https:\/\/learnlearn.uk\/python\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png","contentUrl":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png","width":710,"height":98,"caption":"Python"},"image":{"@id":"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/python\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"Functions Challenge 1 &#8211; Kilometers\u00a0to miles\u00a0converter Write a function called km_to_miles that takes kilometers as a parameter, converts it into miles and returns the result. &nbsp; &nbsp; &nbsp; Functions Challenge 2 &#8211; Divisible by 11 checker Write a function called is_divisable_by_11 that takes an integer as an parameter and returns whether it is divisible by&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/1192"}],"collection":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/comments?post=1192"}],"version-history":[{"count":25,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/1192\/revisions"}],"predecessor-version":[{"id":1476,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/1192\/revisions\/1476"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/media?parent=1192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}