{"id":3316,"date":"2017-07-30T18:10:39","date_gmt":"2017-07-30T16:10:39","guid":{"rendered":"https:\/\/codingexplained.com\/?p=3316"},"modified":"2017-07-30T18:10:39","modified_gmt":"2017-07-30T16:10:39","slug":"basic-math-operators-in-python","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python","title":{"rendered":"Basic Math Operators in Python"},"content":{"rendered":"<p>So far, we haven&#8217;t really done anything dynamically yet; we declared and initialized variables and output their values. That&#8217;s about to change, because now we are going to be working a bit with the basic math operators that Python provides.<\/p>\n<p>Python supports all of the math operations that you would expect. The basic ones are addition, subtraction, multiplication, and division. Other ones include the exponentiation and modulo operators, which you will see in a moment.<\/p>\n<h2>Addition &#038; Subtraction<\/h2>\n<p>Let&#8217;s start out simple and add two numbers together, store the result in a variable and print out the result.<\/p>\n<pre><code class=\"python\">a = 4\r\nb = 2\r\nc = a + b\r\nprint(c)<\/code><\/pre>\n<p>Even though we used variables for the addition, we could just as well have entered numbers instead.<\/p>\n<p>As I am sure you can imagine, the above prints out the number <span class=\"code\">6<\/span>.<\/p>\n<h2>Division<\/h2>\n<p>Let&#8217;s now try to divide <span class=\"code\">c<\/span> by two and print out the result.<\/p>\n<pre><code class=\"python\">print(c \/ 2)<\/code><\/pre>\n<p>Notice how I printed out the result of the division \u2014 being an expression \u2014 directly, instead of storing the result within a variable first. When printing out values, we are using a <em>function<\/em> named <span class=\"code\">print<\/span>. We haven&#8217;t talked about functions yet, so we will get back to that later. The point is that this function takes an <em>argument<\/em>. For this argument, we can pass in a value directly, such as &#8220;Hello&#8221;, 22, or a mathematical expression as in this case. We will get back to all of this later, so don&#8217;t worry if that went over your head.<\/p>\n<p>Either way, the above line of code prints out the number <span class=\"code\">3.0<\/span>. Note that this number is actually of the type <span class=\"code\">float<\/span> \u2014 being numbers with decimals \u2014 instead of <span class=\"code\">int<\/span>. In this case we are dividing two integers (i.e. whole numbers), but that might not necessarily be the case. So the fact that divisions always result in floating point numbers is just convenient for us in case we need to perform some further operations on the number.<\/p>\n<h2>Multiplication<\/h2>\n<p>There is nothing special about multiplication in Python; we just use an asterisk (<span class=\"code\">*<\/span>) in the same way we did with addition, subtraction, and division.<\/p>\n<pre><code class=\"python\">print(2 * 2)<\/code><\/pre>\n<p>I&#8217;m sure it comes as no surprise to you that the above outputs <span class=\"code\">4<\/span>. Note that if we multiply two integers, the result is of the data type <span class=\"code\">int<\/span>. If we multiply by at least one decimal number, the result is a <span class=\"code\">float<\/span> value.<\/p>\n<h2>Exponential Operator<\/h2>\n<p>Apart from the most common mathematical operators \u2014 being <span class=\"code\">+<\/span>, <span class=\"code\">&#8211;<\/span>, <span class=\"code\">*<\/span> and <span class=\"code\">\/<\/span>) \u2014 Python also provides a handy operator for working with <em>exponents<\/em>. Consider the following example.<\/p>\n<pre><code class=\"python\">print(2 ** 10)<\/code><\/pre>\n<p>This raises 2 to the power of 10, also noted as 2<sup>10<\/sup>, where 10 is the exponent. Without getting too much into math, this is the equivalent of multiplying 2 by itself 10 times, i.e. <span class=\"code\">2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2<\/span>. As you can see, using the exponential operator is much more convenient than typing all of that out manually. The result of the above is <strong>1024<\/strong>.<\/p>\n<h2>Modulo Operator<\/h2>\n<p>Another math operator, is the <em>modulo<\/em> operator. What this operator does, is that it performs a division and then returns the remainder of the division. Consider the following examples.<\/p>\n<pre><code class=\"python\">print(4 % 2)\r\nprint(4 % 3)<\/code><\/pre>\n<p>The first line outputs 0. What happens is that 4 is divided by 2, leaving a remainder of 0. The second line divides 4 by 3, which leaves a remainder of 1. Likewise, the expression <span class=\"code\">105 % 10<\/span> gives us the integer 5. Hopefully that makes sense.<\/p>\n<p>So what is the point of this operator? Typically you are not interested in the remainder itself, but just determining whether one number is divisible by another number. We will get back to examples of this at a later point, but a simple example could be to highlight every second row of a table by checking if the current row number is divisible by two (i.e. the remainder is zero). Again, more on that later.<\/p>\n<h2>Floor Division<\/h2>\n<p>The last math operator that we will look at, is the <em>floor division<\/em> operator. Suppose that we divide 10 by 3, for instance. This division yields a floating point number (i.e. a decimal number) of 3.33 (I reduced the number of decimals for brevity). When using floor division, the result of a division is rounded <em>down<\/em> to the nearest integer value, i.e. to a whole number. Consider the following example, where the floor division is denoted by two slashes, i.e. <span class=\"code\">\/\/<\/span>.<\/p>\n<pre><code class=\"python\">print(10 \/\/ 3)<\/code><\/pre>\n<p>The division itself results in the decimal number 3.33 (again, the actual result produces more decimals). This number is then rounded down to the nearest full number, being 3. So the result of the above is the integer 3.<\/p>\n<p>But what if the division results in a negative number? Suppose that we divide -11 by 3, which gives -3.66. What would the result of the following then be?<\/p>\n<pre><code class=\"python\">print(-11 \/\/ 3)<\/code><\/pre>\n<p>The result would actually be -4. That&#8217;s because the number is rounded <em>down<\/em>, and since -3.66 is below zero, rounding down to the nearest integer (or full number) means rounding down to -4. So if the result of the division is less than zero, we <em>round away from zero<\/em>, so to speak.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"series\">Part 5 of 5 in the <a href=\"https:\/\/codingexplained.com\/tutorial\/python-for-beginners\" class=\"series-196\" title=\"Python for Beginners\">Python for Beginners<\/a> series<\/div><p>So far, we haven&#8217;t really done anything dynamically yet; we declared and initialized variables and output their values. That&#8217;s about to change, because now we are going to be working a bit with the basic math operators that Python provides. Python supports all of the math operations that you would expect. The basic ones are&hellip; <a href=\"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python\" class=\"more-link\">read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[197],"tags":[198],"series":[196],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Basic Math Operators in Python<\/title>\n<meta name=\"description\" content=\"Introduction to the basic math operators available in Python. Learn how to work with basic math operators in Python, incl. exponents and floor division.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Basic Math Operators in Python\" \/>\n<meta property=\"og:description\" content=\"Introduction to the basic math operators available in Python. Learn how to work with basic math operators in Python, incl. exponents and floor division.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python\" \/>\n<meta property=\"og:site_name\" content=\"Coding Explained\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:published_time\" content=\"2017-07-30T16:10:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"444\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Bo Andersen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:site\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bo Andersen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python\",\"url\":\"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python\",\"name\":\"Basic Math Operators in Python\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2017-07-30T16:10:39+00:00\",\"dateModified\":\"2017-07-30T16:10:39+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"Introduction to the basic math operators available in Python. Learn how to work with basic math operators in Python, incl. exponents and floor division.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Basic Math Operators in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codingexplained.com\/#website\",\"url\":\"https:\/\/codingexplained.com\/\",\"name\":\"Coding Explained\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codingexplained.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\",\"name\":\"Bo Andersen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"caption\":\"Bo Andersen\"},\"description\":\"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!\",\"sameAs\":[\"https:\/\/codingexplained.com\",\"https:\/\/www.facebook.com\/codingexplained\",\"https:\/\/www.linkedin.com\/in\/ba0708\",\"https:\/\/twitter.com\/codingexplained\",\"https:\/\/www.youtube.com\/c\/codingexplained\"],\"url\":\"https:\/\/codingexplained.com\/author\/andy\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Basic Math Operators in Python","description":"Introduction to the basic math operators available in Python. Learn how to work with basic math operators in Python, incl. exponents and floor division.","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:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python","og_locale":"en_US","og_type":"article","og_title":"Basic Math Operators in Python","og_description":"Introduction to the basic math operators available in Python. Learn how to work with basic math operators in Python, incl. exponents and floor division.","og_url":"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2017-07-30T16:10:39+00:00","og_image":[{"width":1200,"height":444,"url":"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png","type":"image\/png"}],"author":"Bo Andersen","twitter_card":"summary_large_image","twitter_creator":"@codingexplained","twitter_site":"@codingexplained","twitter_misc":{"Written by":"Bo Andersen","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python","url":"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python","name":"Basic Math Operators in Python","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2017-07-30T16:10:39+00:00","dateModified":"2017-07-30T16:10:39+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"Introduction to the basic math operators available in Python. Learn how to work with basic math operators in Python, incl. exponents and floor division.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/python\/basic-math-operators-in-python#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Basic Math Operators in Python"}]},{"@type":"WebSite","@id":"https:\/\/codingexplained.com\/#website","url":"https:\/\/codingexplained.com\/","name":"Coding Explained","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codingexplained.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d","name":"Bo Andersen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","caption":"Bo Andersen"},"description":"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!","sameAs":["https:\/\/codingexplained.com","https:\/\/www.facebook.com\/codingexplained","https:\/\/www.linkedin.com\/in\/ba0708","https:\/\/twitter.com\/codingexplained","https:\/\/www.youtube.com\/c\/codingexplained"],"url":"https:\/\/codingexplained.com\/author\/andy"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3mJkW-Ru","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/3316"}],"collection":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/comments?post=3316"}],"version-history":[{"count":9,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/3316\/revisions"}],"predecessor-version":[{"id":3325,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/3316\/revisions\/3325"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=3316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=3316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=3316"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=3316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}