{"id":123,"date":"2023-06-02T19:23:26","date_gmt":"2023-06-02T19:23:26","guid":{"rendered":"http:\/\/python.garden\/index.php\/2023\/06\/02\/variables-data-types-and-operators\/"},"modified":"2023-06-02T19:36:06","modified_gmt":"2023-06-02T19:36:06","slug":"variables-data-types-and-operators","status":"publish","type":"post","link":"https:\/\/python.garden\/index.php\/2023\/06\/02\/variables-data-types-and-operators\/","title":{"rendered":"Chapter 1.3: Variables, Data Types, and Operators"},"content":{"rendered":"\n<p>In the last chapter, we learned how to create a simple Python program. We will now delve deeper into the fundamentals of Python by discussing variables, data types, and operators.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Variables<\/h2>\n\n\n\n<p>Variables in Python are used to store information that can be referenced and manipulated in our programs. They are created by assigning a value to a label with the <code>=<\/code> operator.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">greeting = &quot;Hello, Python!&quot;\nprint(greeting)<\/pre><\/div>\n\n\n\n<p>Output:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">Hello, Python!<\/pre><\/div>\n\n\n\n<p>In this example, <code>greeting<\/code> is a variable that holds the string &#8220;Hello, Python!&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Data Types<\/h2>\n\n\n\n<p>Python has several built-in data types that we can use to structure and manipulate our data. Here are some of the most commonly used ones:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Integers:<\/strong> Whole numbers. Example: <code>5<\/code><\/li>\n\n\n\n<li><strong>Floats:<\/strong> Decimal numbers. Example: <code>5.0<\/code><\/li>\n\n\n\n<li><strong>Strings:<\/strong> A sequence of characters. Example: <code>\"Hello, Python!\"<\/code><\/li>\n\n\n\n<li><strong>Booleans:<\/strong> True or false values. Example: <code>True<\/code><\/li>\n\n\n\n<li><strong>Lists:<\/strong> Ordered, mutable sequences. Example: <code>[1, 2, 3]<\/code><\/li>\n\n\n\n<li><strong>Tuples:<\/strong> Ordered, immutable sequences. Example: <code>(1, 2, 3)<\/code><\/li>\n\n\n\n<li><strong>Dictionaries:<\/strong> Unordered key-value pairs. Example: <code>{\"name\": \"Python\", \"year\": 1991}<\/code><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Operators<\/h2>\n\n\n\n<p>Operators are symbols that carry out operations on one or more operands. Python has several types of operators:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Arithmetic Operators:<\/strong> Used with numeric values to perform common mathematical operations.<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\"># Addition (+)\nprint(5 + 2)  # Output: 7\n\n# Subtraction (-)\nprint(5 - 2)  # Output: 3\n\n# Multiplication (*)\nprint(5 * 2)  # Output: 10\n\n# Division (\/)\nprint(5 \/ 2)  # Output: 2.5\n\n# Floor Division (\/\/)\nprint(5 \/\/ 2) # Output: 2\n\n# Modulus (%)\nprint(5 % 2)  # Output: 1\n\n# Exponentiation (**)\nprint(5 ** 2) # Output: 25<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Assignment Operators:<\/strong> Used to assign values to variables.<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">x = 5  # x is now 5\nx += 3 # equivalent to x = x + 3, so x is now 8\nx -= 2 # equivalent to x = x - 2, so x is now 6\n# Similarly, there are *=, \/=, \/\/=, %=, **= operators<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Comparison Operators:<\/strong> Used to compare values. The result of a comparison is a boolean (<code>True<\/code> or <code>False<\/code>).<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">print(5 &gt; 2)   # Output: True\nprint(5 &lt; 2)   # Output: False\nprint(5 == 2)  # Output: False\nprint(5 != 2)  # Output: True\nprint(5 &gt;= 2)  # Output: True\nprint(5 &lt;= 2)  # Output: False<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><strong>Logical Operators:<\/strong> Used to combine conditional statements.<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">print(True and False)  # Output: False\nprint(True or False)   # Output: True\nprint(not True)        # Output: False<\/pre><\/div>\n\n\n\n<p>In the next chapter, we&#8217;ll explore control structures and learn how to use these operators to influence the flow of our programs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>That&#8217;s a basic overview of variables, data types,<\/p>\n\n\n\n<p>and operators in Python! With this knowledge, you&#8217;re well on your way to writing more complex and powerful Python programs.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last chapter, we learned how to create a simple Python program. We will now delve deeper into the fundamentals of Python by discussing variables, data types, and operators.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[94,95,98],"tags":[],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-introduction-to-python-2","category-introduction-to-python-introduction-to-python-2","category-variables-data-types-and-operators"],"featured_image_src":null,"author_info":{"display_name":"shababdoo","author_link":"https:\/\/python.garden\/index.php\/author\/shababdoo\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":0,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"wp:attachment":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}