{"id":5743,"date":"2022-02-12T12:35:04","date_gmt":"2022-02-12T11:35:04","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=5743"},"modified":"2026-01-24T11:26:20","modified_gmt":"2026-01-24T10:26:20","slug":"variables","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/02\/12\/variables\/","title":{"rendered":"Variables"},"content":{"rendered":"<p>1. A <strong>variable<\/strong> is a named location reserved to store values in the memory. A variable is created or initialized automatically when you assign a value to it for the first time. (2.1.4.1)<\/p>\n<p><!--more--><\/p>\n<p>2. Each variable must have a unique name &#8211; an <strong>identifier<\/strong>. A legal identifier name must be a non-empty sequence of characters, must begin with the underscore(<code>_<\/code>), or a letter, and it cannot be a Python keyword. The first character may be followed by underscores, letters, and digits. Identifiers in Python are case-sensitive. (2.1.4.1)<\/p>\n<p>&nbsp;<\/p>\n<p>3. Python is a <strong>dynamically-typed<\/strong> language, which means you don&#8217;t need to <i>declare<\/i> variables in it.\u00a0 To assign values to variables, you can use a simple assignment operator in the form of the equal (<code>=<\/code>) sign, i.e.,<\/p>\n<pre class=\"lang:default decode:true\">var = 1<\/pre>\n<p>&nbsp;<\/p>\n<p>4. You can also use <strong>compound assignment operators<\/strong> (shortcut operators) to modify values assigned to variables, e.g., <code><\/code><\/p>\n<pre class=\"lang:default decode:true\">var += 1\r\nvar \/= 5 * 2<\/pre>\n<p>&nbsp;<\/p>\n<p>5. You can assign new values to already existing variables using the assignment operator or one of the compound operators, e.g.:<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">var = 2\r\nprint(var)\r\n\r\nvar = 3\r\nprint(var)\r\n\r\nvar += 1\r\nprint(var)<\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>6. You can combine text and variables using the <code>+<\/code> operator, and use the <code>print()<\/code> function to output strings and variables, e.g.:<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">var = \"007\"\r\nprint(\"Agent \" + var)<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<div><\/div>\n<div>\n<div class=\"container\">\n<div class=\"row\">\n<div class=\"small-12 large-6 columns\">\n<h1>Shortcut operators<\/h1>\n<p>It&#8217;s time for the next set of operators that make a developer&#8217;s life easier. Very often, we want to use one and the same variable both to the right and left sides of the <code>=<\/code> operator. For example, if we need to calculate a series of successive values of powers of 2, we may use a piece like this:<\/p>\n<pre class=\"lang:default decode:true \">x = x * 2<\/pre>\n<p>You may use an expression like this if you can&#8217;t fall asleep and you&#8217;re trying to deal with it using some good, old-fashioned methods:<\/p>\n<pre class=\"lang:default decode:true \">sheep = sheep + 1<\/pre>\n<p>Python offers you a shortened way of writing operations like these, which can be coded as follows:<\/p>\n<pre class=\"lang:default decode:true\">x *= 2\r\nsheep += 1<\/pre>\n<\/div>\n<div class=\"small-12 large-6 columns\">\n<p>Let&#8217;s try to present a general description for these operations.<\/p>\n<p>If <code>op<\/code> is a two-argument operator (this is a very important condition) and the operator is used in the following context:<\/p>\n<pre class=\"lang:default decode:true\">variable = variable op expression<\/pre>\n<p>It can be simplified and shown as follows:<\/p>\n<pre class=\"lang:default decode:true\">variable op= expression<\/pre>\n<p>Take a look at the examples below. Make sure you understand them all.<\/p>\n<pre class=\"lang:default decode:true \">i = i + 2 * j \u21d2 i += 2 * j\r\n\r\nvar = var \/ 2 \u21d2 var \/= 2\r\n\r\nrem = rem % 10 \u21d2 rem %= 10\r\n\r\nj = j - (i + var + rem) \u21d2 j -= (i + var + rem)\r\n\r\nx = x ** 2 \u21d2 x **= 2<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<div>\n<p><strong>Swaping the values of two variables<\/strong><\/p>\n<p>Take a look at the snippet:<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">variable_1 = 1\r\nvariable_2 = 2\r\n\r\nvariable_2 = variable_1\r\nvariable_1 = variable_2<\/pre>\n<\/div>\n<\/div>\n<p>If you do something like this, you would <strong>lose the value previously stored<\/strong> in <code>variable_2<\/code>. Changing the order of the assignments will not help. You need a <strong>third variable that serves as an auxiliary storage<\/strong>.<\/p>\n<p>This is how you can do it:<\/p>\n<pre class=\"lang:default decode:true\">variable_1 = 1\r\nvariable_2 = 2\r\nauxiliary = variable_1\r\nvariable_1 = variable_2\r\nvariable_2 = auxiliary<\/pre>\n<p>Python offers a more convenient way of doing the swap &#8211; take a look:<\/p>\n<pre class=\"lang:default decode:true\">variable_1 = 1\r\nvariable_2 = 2\r\nvariable_1, variable_2 = variable_2, variable_1<\/pre>\n<\/div>\n<div><\/div>\n<div>\n<p><em>Exercise 1<\/em><\/p>\n<p>What is the output of the following snippet?<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true \">var = 2\r\nvar = 3\r\nprint(var)<\/pre>\n<div class=\"ace_line\"><\/div>\n<\/div>\n<\/div>\n<p id=\"sol\"><code class=\"codep \">3<\/code><\/p>\n<p><em>Exercise 2<\/em><\/p>\n<p>Which of the following variable names are <u>illegal<\/u> in Python?<\/p>\n<pre class=\"lang:default decode:true \">my_var\r\nm\r\n101\r\naverylongvariablename\r\nm101\r\nm 101\r\nDel\r\ndel<\/pre>\n<p>&nbsp;<\/p>\n<p id=\"sol2\"><code class=\"codep \">my_var<br \/>\nm<br \/>\n101 # incorrect (starts with a digit)<br \/>\naverylongvariablename<br \/>\nm101<br \/>\nm 101 # incorrect (contains a space)<br \/>\nDel<br \/>\ndel # incorrect (is a keyword)<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 3<\/em><\/p>\n<p>What is the output of the following snippet?<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true \">a = '1'\r\nb = \"1\"\r\nprint(a + b)<\/pre>\n<div class=\"ace_line\"><\/div>\n<\/div>\n<\/div>\n<p id=\"sol3\"><code class=\"codep \">11<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 4<\/em><\/p>\n<p>What is the output of the following snippet?<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true \">a = 6\r\nb = 3\r\na \/= 2 * b\r\nprint(a)<\/pre>\n<div class=\"ace_line\"><\/div>\n<\/div>\n<\/div>\n<p id=\"sol4\"><code class=\"codep \">1.0<\/code><\/p>\n<p>2 * b = 6<br \/>\na = 6 \u2192 6 \/ 6 = 1.0<\/p>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<h2><\/h2>\n<h2>Ex. 5<\/h2>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>Which of the following <strong>variable names<\/strong> are <strong>illegal<\/strong> in Python?<\/p>\n<p>(Select two answers)<\/p>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>False<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>print<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>_a&amp;b<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>Print<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><\/div>\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct selection<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>False<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\"><\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct selection<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>_a&amp;b<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div><\/div>\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<p><label class=\"ud-heading-md ud-form-label ud-heading-sm\" for=\"form-group--567\"><\/label>In Python, variable names can only contain <strong>lowercase<\/strong> or <strong>uppercase<\/strong> <strong>letters<\/strong> (<code><strong>a-z<\/strong><\/code><strong>, <\/strong><code><strong>A-Z<\/strong><\/code>), <strong>digits<\/strong> (<code><strong>0-9<\/strong><\/code>) and <strong>underscore<\/strong> (<code><strong>_<\/strong><\/code>). Also, variable names cannot begin with a <strong>number<\/strong> and are <strong>case-sensitive<\/strong>. Finally, <strong>keywords<\/strong>, like <code><strong>for<\/strong><\/code> or <code><strong>if<\/strong><\/code>, cannot be used as variable names, because they are Python reserved words.<\/p>\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>Under these rules, <code><strong>print<\/strong><\/code> is a <strong>legal<\/strong> name because it is not a reserved word, but the name of a <strong>Python 3 built-in<\/strong> <strong>function<\/strong>.<\/li>\n<\/ul>\n<ul>\n<li><code><strong>Print<\/strong><\/code> is <strong>correct<\/strong> because it only uses allowed letters.<\/li>\n<\/ul>\n<ul>\n<li>The name <code><strong>_a&amp;b<\/strong><\/code> causes a <code><strong>SyntaxError<\/strong><\/code> exception because it contains the <strong>illegal<\/strong> <code><strong>&amp;<\/strong><\/code> character.<\/li>\n<\/ul>\n<ul>\n<li><code><strong>False<\/strong><\/code> is not ok because it is a Python reserved <strong>keyword<\/strong>.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 6<\/p>\n<p>What is the expected output of the following code ?<\/p>\n<pre class=\"lang:default decode:true \">    y = 'R2D2'\r\n    def my_func(x):\r\n        global y\r\n        y = x\r\n        return y\r\n     \r\n    x = 'Yoda'\r\n    my_func('C3PO')\r\n    print(y)<\/pre>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>A.\u00a0<code>Yoda<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>B. <code>R2D2<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">C. <\/span><code>C3PO<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>D. The code is erroneous.<\/p>\n<\/div>\n<p><em>Explanation<strong>:<\/strong><\/em><\/p>\n<p>When you create a variable inside a function, that variable has a <strong>local scope<\/strong>, and can only be used inside that function.<\/p>\n<p>With the\u00a0 <code>global<\/code> keyword, the scope of that variable will be extended to <strong>outside <\/strong>the function too.<\/p>\n<p>The call to <code>my_func('C3PO')<\/code>\u00a0 will assign <code>'C3PO'<\/code> to the <strong>global <\/strong>variable <code>y<\/code> and <code>print(y)<\/code> will return <code>C3PO<\/code>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\">\n<h2>Scenario<\/h2>\n<p>Miles and kilometers are units of length or distance.<\/p>\n<p>Bearing in mind that <code>1<\/code> mile is equal to approximately <code>1.61<\/code> kilometers, complete the program in the editor so that it converts:<\/p>\n<ul>\n<li>miles to kilometers;<\/li>\n<li>kilometers to miles.<\/li>\n<\/ul>\n<p>Do not change anything in the existing code. Write your code in the places indicated by <code>###<\/code>. Test your program with the data we&#8217;ve provided in the source code.<\/p>\n<p>Pay particular attention to what is going on inside the <code>print()<\/code> function. Analyze how we provide multiple arguments to the function, and how we output the expected data.<\/p>\n<p>Note that some of the arguments inside the <code>print()<\/code> function are strings (e.g., <code>\"miles is\"<\/code>, whereas some other are variables (e.g., <code>miles<\/code>).<\/p>\n<\/div>\n<pre class=\"lang:default decode:true \">kilometers = 12.25\r\nmiles = 7.38\r\n\r\nmiles_to_kilometers = 1.61 * miles\r\nkilometers_to_miles = kilometers \/ 1.61\r\n\r\nprint(miles, \"miles is\", round(miles_to_kilometers, 2), \"kilometers\")\r\nprint(kilometers, \"kilometers is\", round(kilometers_to_miles, 2), \"miles\")<\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h2>Scenario<\/h2>\n<p>Take a look at the code in the editor: it reads a <code>float<\/code> value, puts it into a variable named <code>x<\/code>, and prints the value of a variable named <code>y<\/code>. Your task is to complete the code in order to evaluate the following expression:<\/p>\n<p><span style=\"font-size: 12pt;\"> 3x<sup>3<\/sup> &#8211; 2x<sup>2<\/sup> + 3x &#8211; 1 <\/span><\/p>\n<p>The result should be assigned to <code>y<\/code>.<\/p>\n<p>Remember that classical algebraic notation likes to omit the multiplication operator &#8211; you need to use it explicitly. Note how we change data type to make sure that <code>x<\/code> is of type <code>float<\/code>.<\/p>\n<p>Keep your code clean and readable, and test it using the data we&#8217;ve provided, each time assigning it to the <code>x<\/code> variable (by hardcoding it). Don&#8217;t be discouraged by any initial failures. Be persistent and inquisitive.<\/p>\n<p>&nbsp;<\/p>\n<h2>Test Data<\/h2>\n<p>Sample input<\/p>\n<p><code class=\"codep \">x = 0<br \/>\nx = 1<br \/>\nx = -1<\/code><\/p>\n<p>Expected Output<\/p>\n<p><code class=\"codep \">y = -1.0<br \/>\ny = 3.0<br \/>\ny = -9.0<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Solution:<\/p>\n<pre class=\"lang:default decode:true \">x = -1\r\nx = float(x)\r\n\r\ny = 3*x**3 - 2*x**2 + 3*x - 1\r\n\r\nprint(\"y =\", y)<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. A variable is a named location reserved to store values in the memory. A variable is created or initialized automatically when you assign a value to it for the first time. (2.1.4.1)<\/p>\n","protected":false},"author":2,"featured_media":5936,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5743"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=5743"}],"version-history":[{"count":16,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5743\/revisions"}],"predecessor-version":[{"id":6286,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5743\/revisions\/6286"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media\/5936"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=5743"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=5743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}