{"id":361,"date":"2023-02-13T17:18:22","date_gmt":"2023-02-13T17:18:22","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=361"},"modified":"2024-06-06T10:12:23","modified_gmt":"2024-06-06T09:12:23","slug":"built-in-functions","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-2\/built-in-functions\/","title":{"rendered":"Built in functions"},"content":{"rendered":"\n<p>Python has many built in functions to make programming easier.&nbsp; You have already encountered some them like int(),float() or list() when converting data types, range() for making a list of integers, along with print() in a lot of the examples.<\/p>\n\n\n\n<p>Here are some more details for some of them along with a few of the more common ones that you will use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">print<\/h2>\n\n\n\n<p>The print function will take any number of values separated by commas and prints them to the screen as a continuous line.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># print using a single value\nprint(\"This is to be shown to the user\")\n\n# print using multiple values\nname = \"John\"\nage = \"20\"\nprint(\"Hello\", name, \"you are\", age)<\/code><\/pre>\n\n\n\n<p>You can use triple quotes to do multiple lines of text including new lines. You can also include single and double quotes within a triple quote print.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># print using triple quotes\nprint(\"\"\"This ia a multi line\r\nquote block, you can include quotes like '\r\nand \" within it.\r\n\"\"\")<\/code><\/pre>\n\n\n\n<p>The f print method allows you to include variables within your print statement. This is useful if you are needing a lot of data printed out.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># print substituting values\nuser = \"John Doe\"\r\nlocation = \"New York\"\r\nprint(f\"The user {user} is currently in {location}\")<\/code><\/pre>\n\n\n\n<p>Here the print statement is replacing any curly bracket value with the matching variable contents.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">range<\/h2>\n\n\n\n<p>The range function generates a list of integers. This is particularly useful in loops.<br>If provided with only one parameter the range function counts from 0 until it hits that value e.g.<br>range(5) generates:<\/p>\n\n\n\n<p>0 1 2 3 4<\/p>\n\n\n\n<p>If two parameters are provides however the function assigns them as the beginning and ending values. So range(0, 5) is the same as just range(5)<\/p>\n\n\n\n<p>So range(4,9) would generate:<\/p>\n\n\n\n<p>4 5 6 7 8<\/p>\n\n\n\n<p>If a third parameter is provided then it is used as the step value. So range(5, 20, 5) would generate:<\/p>\n\n\n\n<p>5 10 15<\/p>\n\n\n\n<p>As such you can never give the step argument a value of 0.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>The code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>test_list = range(5)\nfor value in test_list:\n    print(value)\nprint(\"\")\ntest_list2 = range(4, 9)\nfor value in test_list2:\n    print(value)\nprint(\"\")\ntest_list3 = range(0, 20,  5)\nfor value in test_list3:\n    print(value)\nprint(\"\")<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>Gives an output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n2\n3\n4\n\n4\n5\n6\n7\n8\n\n0\n5\n10\n15\n<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">input<\/h2>\n\n\n\n<p>The input function allows the program to receive a keyboard input. When it is called the program halts until an input is submitted. This function then outputs a string containing the data that was entered on the keyboard.<\/p>\n\n\n\n<p>This function has an optional parameter which accepts a string that is shown as a prompt to the user. This saves the developer from having to use a separate print function to explain what the input is asking for.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = input(\"Please enter your name? \")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">len<\/h2>\n\n\n\n<p>The len function returns the length of an object that you pass into the function. If the object is a list it will tell you how many entries there are in it. If it is a string then it will show how many characters are in it.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>The code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>test_list = &#091;34, 91, 2, 920]\nprint(len(test_list))\nprint(\"\")\ntest_string = \"Hello World!\"\nprint(len(test_string))<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>Gives an output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>4\n\n12<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">type<\/h2>\n\n\n\n<p>The type function returns the data type of the argument. This is useful when you are unsure of the data type or dealing with user created data types such as classes.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>The code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>test = \"Hello World!\"\nprint(type(test))\nprint(\"\")\ntest_2 = 9043.01\nprint(type(test_2))\nprint(\"\")\ntest_3 = &#091;\"cat\", \"dog\", \"rabbit\"]\nprint(type(test_3))<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>Gives an output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;class 'str'&gt; \n\n&lt;class 'float'&gt; \n\n&lt;class 'list'&gt;<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-2\/challenge-5\/\" data-type=\"page\" data-id=\"370\">Next: Challenge 5<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python has many built in functions to make programming easier.&nbsp; You have already encountered some them like int(),float() or list() when converting data types, range() for making a list of integers, along with print() in a lot of the examples. Here are some more details for some of them along with a few of the&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-2\/built-in-functions\/\">Continue reading <span class=\"screen-reader-text\">Built in functions<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":16,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-361","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/users\/5710"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/comments?post=361"}],"version-history":[{"count":8,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/361\/revisions"}],"predecessor-version":[{"id":1375,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/361\/revisions\/1375"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/16"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}