{"id":329498,"date":"2023-09-18T14:00:00","date_gmt":"2023-09-18T14:00:00","guid":{"rendered":"https:\/\/realpython.com\/python-operators-expressions\/"},"modified":"2023-09-18T14:00:00","modified_gmt":"2023-09-18T14:00:00","slug":"operators-and-expressions-in-python","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/09\/18\/operators-and-expressions-in-python\/","title":{"rendered":"Operators and Expressions in Python"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<div>\n<p>In Python, <strong>operators<\/strong> are special symbols, combinations of symbols, or keywords that designate some type of computation. You can combine objects and operators to build <strong>expressions<\/strong> that perform the actual computation. So, operators are the building blocks of expressions, which you can use to manipulate your data. Therefore, understanding how operators work in Python is essential for you as a programmer.<\/p>\n<p>In this tutorial, you\u2019ll learn about the operators that Python currently supports. You\u2019ll also learn the basics of how to use these operators to build expressions.<\/p>\n<p><strong>In this tutorial, you\u2019ll:<\/strong><\/p>\n<ul>\n<li>Get to know Python\u2019s <strong>arithmetic operators<\/strong> and use them to build <strong>arithmetic expressions<\/strong><\/li>\n<li>Explore Python\u2019s <strong>comparison<\/strong>, <strong>Boolean<\/strong>, <strong>identity<\/strong>, and <strong>membership<\/strong> operators<\/li>\n<li>Build <strong>expressions<\/strong> with comparison, Boolean, identity, and membership operators<\/li>\n<li>Learn about Python\u2019s <strong>bitwise<\/strong> operators and how to use them<\/li>\n<li>Combine and repeat sequences using the <strong>concatenation<\/strong> and <strong>repetition<\/strong> operators<\/li>\n<li>Understand the <strong>augmented assignment<\/strong> operators and how they work<\/li>\n<\/ul>\n<p>To get the most out of this tutorial, you should have a basic understanding of Python programming concepts, such as <a href=\"https:\/\/realpython.com\/python-variables\/\">variables<\/a>, <a href=\"https:\/\/realpython.com\/python-assignment-operator\/\">assignments<\/a>, and built-in <a href=\"https:\/\/realpython.com\/python-data-types\/\">data types<\/a>.<\/p>\n<div class=\"alert alert-warning\" role=\"alert\">\n<p><strong markdown=\"1\">Free Bonus:<\/strong> <a href=\"https:\/\/realpython.com\/bonus\/python-operators-expressions-cheat-sheet\/\" class=\"alert-link\" data-toggle=\"modal\" data- data-focus=\"false\" markdown=\"1\">Click here to download your comprehensive cheat sheet<\/a> covering the various operators in Python.<\/p>\n<\/div>\n<div class=\"alert alert-primary\" role=\"alert\">\n<p><strong><i class=\"fa fa-graduation-cap\" aria-hidden=\"true\"><\/i> Take the Quiz:<\/strong> Test your knowledge with our interactive \u201cPython Operators and Expressions\u201d quiz. Upon completion you will receive a score so you can track your learning progress over time:<\/p>\n<p class=\"text-center my-2\"><a class=\"btn btn-primary\" href=\"https:\/\/realpython.com\/quizzes\/python-operators-expressions\/\" >Take the Quiz \u00bb<\/a><\/p>\n<\/div>\n<h2 id=\"getting-started-with-operators-and-expressions\">Getting Started With Operators and Expressions<a class=\"headerlink\" href=\"https:\/\/realpython.com\/python-operators-expressions\/#getting-started-with-operators-and-expressions\" title=\"Permanent link\"><\/a><\/h2>\n<p>In programming, an <strong>operator<\/strong> is usually a symbol or combination of symbols that allows you to perform a specific operation. This operation can act on one or more <strong>operands<\/strong>. If the operation involves a single operand, then the operator is <strong>unary<\/strong>. If the operator involves two operands, then the operator is <strong>binary<\/strong>.<\/p>\n<p>For example, in Python, you can use the minus sign (<code>-<\/code>) as a unary operator to declare a negative number. You can also use it to subtract two numbers:<\/p>\n<div class=\"highlight python repl\"><span class=\"repl-toggle\" title=\"Toggle REPL prompts and output\">&gt;&gt;&gt;<\/span><\/p>\n<pre><span><\/span><code><span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"o\">-<\/span><span class=\"mf\">273.15<\/span>\n<span class=\"go\">-273.15<\/span>\n\n<span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"mi\">5<\/span> <span class=\"o\">-<\/span> <span class=\"mi\">2<\/span>\n<span class=\"go\">3<\/span>\n<\/code><\/pre>\n<\/div>\n<p>In this code snippet, the minus sign (<code>-<\/code>) in the first example is a unary operator, and the number <code>273.15<\/code> is the operand. In the second example, the same symbol is a binary operator, and the numbers <code>5<\/code> and <code>2<\/code> are its left and right operands.<\/p>\n<p>Programming languages typically have operators built in as part of their syntax. In many languages, including Python, you can also create your own operator or modify the behavior of existing ones, which is a powerful and advanced feature to have.<\/p>\n<p>In practice, operators provide a quick shortcut for you to manipulate data, perform mathematical calculations, compare values, run <a href=\"https:\/\/realpython.com\/python-boolean\/\">Boolean<\/a> tests, assign values to variables, and more. In Python, an operator may be a symbol, a combination of symbols, or a <a href=\"https:\/\/realpython.com\/python-keywords\/\">keyword<\/a>, depending on the type of operator that you\u2019re dealing with.<\/p>\n<p>For example, you\u2019ve already seen the subtraction operator, which is represented with a single minus sign (<code>-<\/code>). The equality operator is a double equal sign (<code>==<\/code>). So, it\u2019s a combination of symbols:<\/p>\n<div class=\"highlight python repl\"><span class=\"repl-toggle\" title=\"Toggle REPL prompts and output\">&gt;&gt;&gt;<\/span><\/p>\n<pre><span><\/span><code><span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"mi\">42<\/span> <span class=\"o\">==<\/span> <span class=\"mi\">42<\/span>\n<span class=\"go\">True<\/span>\n<\/code><\/pre>\n<\/div>\n<p>In this example, you use the Python equality operator (<code>==<\/code>) to compare two numbers. As a result, you get <a href=\"https:\/\/docs.python.org\/3\/library\/constants.html#True\"><code>True<\/code><\/a>, which is one of Python\u2019s Boolean values.<\/p>\n<p>Speaking of Boolean values, the Boolean or logical operators in Python are keywords rather than signs, as you\u2019ll learn in the section about <a href=\"https:\/\/realpython.com\/python-operators-expressions\/#boolean-operators-and-expressions-in-python\">Boolean operators and expressions<\/a>. So, instead of the odd signs like <code>||<\/code>, <code>&amp;&amp;<\/code>, and <code>!<\/code> that many other programming languages use, Python uses <code>or<\/code>, <code>and<\/code>, and <code>not<\/code>.<\/p>\n<p>Using keywords instead of odd signs is a really cool design decision that\u2019s consistent with the fact that Python loves and encourages <a href=\"https:\/\/pep20.org\/#readability\">code\u2019s readability<\/a>.<\/p>\n<p>You\u2019ll find several categories or groups of operators in Python. Here\u2019s a quick list of those categories:<\/p>\n<ul>\n<li><strong>Assignment<\/strong> operators<\/li>\n<li><strong>Arithmetic<\/strong> operators<\/li>\n<li><strong>Comparison<\/strong> operators<\/li>\n<li><strong>Boolean<\/strong> or logical operators<\/li>\n<li><strong>Identity<\/strong> operators<\/li>\n<li><strong>Membership<\/strong> operators<\/li>\n<li><strong>Concatenation<\/strong> and <strong>repetition<\/strong> operators<\/li>\n<li><strong>Bitwise<\/strong> operators<\/li>\n<\/ul>\n<p>All these types of operators take care of specific types of computations and data-processing tasks. You\u2019ll learn more about these categories throughout this tutorial. However, before jumping into more practical discussions, you need to know that the most elementary goal of an operator is to be part of an <a href=\"https:\/\/docs.python.org\/3\/glossary.html#term-expression\">expression<\/a>. Operators by themselves don\u2019t do much:<\/p>\n<div class=\"highlight python repl\"><span class=\"repl-toggle\" title=\"Toggle REPL prompts and output\">&gt;&gt;&gt;<\/span><\/p>\n<pre><span><\/span><code><span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"o\">-<\/span>\n  File <span class=\"nb\">\"&lt;input&gt;\"<\/span>, line <span class=\"m\">1<\/span>\n<span class=\"w\">    <\/span><span class=\"o\">-<\/span>\n<span class=\"w\">    <\/span><span class=\"pm\">^<\/span>\n<span class=\"gr\">SyntaxError<\/span>: <span class=\"n\">incomplete input<\/span>\n\n<span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"o\">==<\/span>\n  File <span class=\"nb\">\"&lt;input&gt;\"<\/span>, line <span class=\"m\">1<\/span>\n<span class=\"w\">    <\/span><span class=\"o\">==<\/span>\n<span class=\"w\">    <\/span><span class=\"pm\">^^<\/span>\n<span class=\"gr\">SyntaxError<\/span>: <span class=\"n\">incomplete input<\/span>\n\n<span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"ow\">or<\/span>\n  File <span class=\"nb\">\"&lt;input&gt;\"<\/span>, line <span class=\"m\">1<\/span>\n<span class=\"w\">    <\/span><span class=\"ow\">or<\/span>\n<span class=\"w\">    <\/span><span class=\"pm\">^^<\/span>\n<span class=\"gr\">SyntaxError<\/span>: <span class=\"n\">incomplete input<\/span>\n<\/code><\/pre>\n<\/div>\n<p>As you can see in this code snippet, if you use an operator without the required operands, then you\u2019ll get a <a href=\"https:\/\/realpython.com\/invalid-syntax-python\/\">syntax error<\/a>. So, operators must be part of expressions, which you can build using Python objects as operands.<\/p>\n<p>So, what is an expression anyway? Python has <a href=\"https:\/\/docs.python.org\/3\/reference\/simple_stmts.html\">simple<\/a> and <a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html\">compound<\/a> statements. A simple statement is a construct that occupies a single <a href=\"https:\/\/docs.python.org\/3\/reference\/lexical_analysis.html#logical-lines\">logical line<\/a>, like an assignment statement. A compound statement is a construct that occupies multiple logical lines, such as a <a href=\"https:\/\/realpython.com\/python-for-loop\/\"><code>for<\/code> loop<\/a> or a <a href=\"https:\/\/realpython.com\/python-conditional-statements\/\">conditional<\/a> statement. An <strong>expression<\/strong> is a simple statement that produces and returns a value.<\/p>\n<\/div>\n<h2><a href=\"https:\/\/realpython.com\/python-operators-expressions\/?utm_source=realpython&#038;utm_medium=rss\">Read the full article at https:\/\/realpython.com\/python-operators-expressions\/ \u00bb<\/a><\/h2>\n<hr \/>\n<p><em>[ Improve Your Python With ? Python Tricks ? \u2013 Get a short &amp; sweet Python Trick delivered to your inbox every couple of days. <a href=\"https:\/\/realpython.com\/python-tricks\/?utm_source=realpython&amp;utm_medium=rss&amp;utm_campaign=footer\">&gt;&gt; Click here to learn more and see examples<\/a> ]<\/em><\/p>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In Python, operators are special symbols, combinations of symbols, or keywords that designate some type of computation. You can combine objects and operators to build expressions that perform the actual computation. So, operators are the building blocks of expressions.<\/p>\n<\/div>","protected":false},"author":2055,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Operators and Expressions in Python - ITTeacherITFreelance.hk","description":"In Python, operators are special symbols, combinations of symbols, or keywords that designate some type of computation. You can combine objects and operators to"},"footnotes":""},"categories":[10700],"tags":[],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329498"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2055"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329498"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329498\/revisions"}],"predecessor-version":[{"id":329499,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329498\/revisions\/329499"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}