{"id":237,"date":"2023-02-10T10:38:51","date_gmt":"2023-02-10T10:38:51","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=237"},"modified":"2024-06-14T08:49:23","modified_gmt":"2024-06-14T07:49:23","slug":"data-types","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-1\/data-types\/","title":{"rendered":"Data types"},"content":{"rendered":"\n<p>Every variable has a data type. As the name suggests, this controls the type of data that is stored within the variable. Here are some of the simple data types:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">String<\/h2>\n\n\n\n<p>A string is a sequence of Unicode characters that may be a combination of letters, numbers, and special symbols. They are enclosed in either single or double quotes. It doesn\u2019t matter which you use but the start and end quote must be the same.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string_with_double_quotes = \"I am a string enclosed in double quotes\"\nstring_with_single_quotes = 'I am a string enclosed in single quotes'<\/code><\/pre>\n\n\n\n<p>You can use the other type of quote within your string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string_with_double_quotes = \"Isn't it nice to use apostrophes?\"\nstring_with_single_quotes = 'They said to me \"No apostrophes\" in this one'<\/code><\/pre>\n\n\n\n<p>If you do need to use the same type of quote, then you must escape it with a backslash.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string_with_double_quotes = \"You would need to escape \\\"Quotes\\\" in this string\"\nstring_with_single_quotes = 'It\\'s required to escape apostrophes in this string'<\/code><\/pre>\n\n\n\n<p>You can join multiple strings together using a process called concatenation. To do this in Python you use the + sign.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string_start = \"Hello\"\nstring_end = \"World\"\ncomplete_string = string_start + \" \" + string_end<\/code><\/pre>\n\n\n\n<p>In the example above we are joining three strings together. Two of them a contained in the variables string_start and string_end, but there is also a space stored as a raw string placed between the two variables.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Integer<\/h2>\n\n\n\n<p>Integers are whole numbers without a decimal point and can be positive or negative.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>number1 = 13\nnumber2 = -203\nplayer_health = 5<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Float<\/h2>\n\n\n\n<p>Floats or floating point numbers are numbers that contain a decimal point and can be either positive or negative. They can also use scientific notation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>number1 = 7.502\nnumber2 = 9.8e3\nplayer_speed = 10.5<\/code><\/pre>\n\n\n\n<p>Python can readily convert between the numeric types so you don\u2019t have to worry about mixing them in an equation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>total = 10 \/ 2.5<\/code><\/pre>\n\n\n\n<p>You can readily convert between the numeric data types using built in functions. We&#8217;ll cover functions later on in the course.<\/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\" style=\"flex-basis:50%\">\n<p>The code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>number1 = 12.0\nnumber2 = int(number1)\nnumber3 = float(number2)\nprint(number1)\nprint(number2)\nprint(number3)<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<p>Would give an output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>12.0\n12\n12.0<\/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\">Boolean<\/h2>\n\n\n\n<p>Boolean data types are either True or False. The capitalisation of True and False is important as these are reserved Python key values.<\/p>\n\n\n\n<p>Just as with the numeric data types you should always wrap a Boolean variable in a str() function if displaying it on the screen or saving it in a text file.<\/p>\n\n\n\n<p>Booleans are really useful to set as flags showing the state of a program or object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>boolean_variable_1 = True\nboolean_variable_2 = False\nis_player_jumping = True<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">List<\/h2>\n\n\n\n<p>A list can be used to store multiple variables of any type.&nbsp; This is useful if you want to organise related data.<\/p>\n\n\n\n<p>The list can contain a mix of data types.<\/p>\n\n\n\n<p>You can access an individual value in a list using its index. This is a number that&nbsp;<strong>starts at 0&nbsp;<\/strong>for the first entry and then increments by 1 for each additional value.<\/p>\n\n\n\n<p>You can make an empty list to be filled later. To add an element to an existing array you can use the append function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>animals = &#091;\"cat\", \"dog\", \"rabbit\"]\nchosen_animal = animals&#091;1]\nshopping_list = &#091;]\nshopping_list.append(\"Apples\")<\/code><\/pre>\n\n\n\n<p>Python stores a string as a list. So you can access individual characters of the string in the same way that you would a list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text_to_display = \"Hello World!\"\nprint(text_to_display&#091;4])<\/code><\/pre>\n\n\n\n<p>This would print \u201co\u201d as it is the fifth character in the string. Remember indexes start at 0.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Dictionary<\/h2>\n\n\n\n<p>A dictionary is like a list but uses a key-value pair for each entry. This allows you to set a unique key for each entry. Dictionaries use curly braces instead of the square brackets used by lists.<\/p>\n\n\n\n<p>You can access an individual value in a dictionary using their key.<\/p>\n\n\n\n<p>You can make an empty dictionary which can be filled later.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ages = {'JDoe': 29, 'MSmith': 41, 'TChan': 30}\nchosen_person = ages&#091;'MSmith']\nexam_grades = {}<\/code><\/pre>\n\n\n\n<p>To add entries into a blank list you can either set it using their key, or use the update method to pass in a dictionary of values to add. For example:<\/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>exam_grades = {}\nprint(exam_grades)\nexam_grades&#091;'JDoe'] = 85\nprint(exam_grades)\nexam_grades.update({'MSmith': 67, 'BMacleod': 81})\nprint(exam_grades)<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>Would give an output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{}\n{'JDoe': 85}\n{'JDoe': 85, 'MSmith': 67, 'BMacleod': 81}<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<p>What data type would you use to store each of the following?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Exam mark between 0 \u2013 100<\/li>\n\n\n\n<li>A vehicle\u2019s number plate<\/li>\n\n\n\n<li>Caf\u00e9 menu with prices<\/li>\n\n\n\n<li>Book titles at a library<\/li>\n\n\n\n<li>Forward velocity of a vehicle in MPH<\/li>\n<\/ul>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-1\/the-print-function\/\" data-type=\"page\" data-id=\"249\">Next: The print function<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every variable has a data type. As the name suggests, this controls the type of data that is stored within the variable. Here are some of the simple data types: String A string is a sequence of Unicode characters that may be a combination of letters, numbers, and special symbols. They are enclosed in either&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-1\/data-types\/\">Continue reading <span class=\"screen-reader-text\">Data types<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":14,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-237","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/237","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=237"}],"version-history":[{"count":16,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/237\/revisions"}],"predecessor-version":[{"id":1381,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/237\/revisions\/1381"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/14"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}