{"id":2522,"date":"2020-08-08T15:02:26","date_gmt":"2020-08-08T15:02:26","guid":{"rendered":"https:\/\/idiagress.com\/vinaaypatil\/?p=2522"},"modified":"2023-10-28T06:08:42","modified_gmt":"2023-10-28T06:08:42","slug":"1-2-objects-and-variable","status":"publish","type":"post","link":"https:\/\/idiagress.com\/vinaaypatil\/2020\/08\/08\/1-2-objects-and-variable\/","title":{"rendered":"1.2 Objects and Variables"},"content":{"rendered":"\n<p>Before delving into this chapter, it&#8217;s crucial to recall how to run a fundamental Python program, whether you&#8217;re using Thonny or Atom. If you need a quick refresher, you can refer to the previous chapter for a step-by-step guide.<\/p>\n\n\n\n<p>In the world of Python, there&#8217;s a saying that &#8216;everything in Python is an object.&#8217; As we progress, we&#8217;ll dive deeper into this concept and gain a more profound understanding of it. But, let&#8217;s begin by exploring the fundamentals of data types.<\/p>\n\n\n\n<p>In the last chapter, we laid the foundation by creating a simple program with just one command. Now, let&#8217;s build upon that knowledge and take the next steps in our Python journey.<\/p>\n\n\n\n<div style=\"height:58px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Our single line program was as follows<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">print(2+3)<\/mark><\/p>\n\n\n\n<p>The program&#8217;s output was &#8216;<b>5<\/b>&#8216; <span class=\"Apple-converted-space\">, which is\u00a0 <\/span>the addition of the two numbers <span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>&#8216; <b>2+3<\/b>.&#8217;<\/p>\n\n\n\n<p>Let&#8217;s create a new Python program named &#8216;variables.py&#8217; and add a few more commands to it:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open your Python development environment, whether it&#8217;s Thonny or Atom.<\/li>\n\n\n\n<li>In the editor, create a new Python file and save it as &#8216;variables.py&#8217; within your designated project folder.<\/li>\n\n\n\n<li>Add the following lines of code to your &#8216;<strong>variables.py<\/strong>&#8216; file:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">print(2 + 3)\nprint('2 + 3')<\/mark><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>Save the file.<\/li>\n\n\n\n<li>Now, run the program by clicking the &#8216;Run&#8217; or &#8216;Execute&#8217; button in Thonny or using the terminal\/command prompt if you&#8217;re in Atom.<\/li>\n\n\n\n<li>Observe the program&#8217;s output<\/li>\n<\/ol>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/Strings_Variables_Idiagress_python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"505\" height=\"83\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/Strings_Variables_Idiagress_python.png\" alt=\"\" class=\"wp-image-2523\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/Strings_Variables_Idiagress_python.png 505w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/Strings_Variables_Idiagress_python-300x49.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/Strings_Variables_Idiagress_python-335x55.png 335w\" sizes=\"auto, (max-width: 505px) 100vw, 505px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is how Python executes the individual commands.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">Line No<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">Input<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">Output<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">1<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">print (2+3)<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">5<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">2<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">print (\u20182+3\u2019)<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">2+3<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The quotes (&#8216;<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>&#8216;) in the print command change how Python interprets what&#8217;s inside the &#8216;print&#8217; command.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n\n\n\n<p>At line no 1, the input is treated as numbers, and Python has proceeded to execute the addition&#8217;s mathematical operation.<\/p>\n\n\n\n<p>At line no 2, the input is treated as characters, including the addition sign, and prints them out as mere words.<\/p>\n\n\n\n<p>Within the inverted commas (quotes &#8216;<span class=\"Apple-converted-space\">&nbsp; <\/span>&#8216;), the characters&#8217; input is technically called a &#8216;String.&#8217;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python defines Characters (letters) data as &#8216;String.&#8217;<span class=\"Apple-converted-space\">&nbsp;<\/span><\/li>\n\n\n\n<li>Numbers are divided into two data types,\n<ul class=\"wp-block-list\">\n<li>&#8216;Integers&#8217; \u2014 when no decimals are involved,<span class=\"Apple-converted-space\">&nbsp;<\/span><\/li>\n\n\n\n<li>&#8216;Float&#8217; \u2014 when decimals are present.<span class=\"Apple-converted-space\">&nbsp;<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><b>Syntaxes for data types: <\/b>Let\u2019s understand this concept by using an example. Modify the program by adding the two commands below and run it in Python.<span class=\"Apple-converted-space\">&nbsp;<\/span><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span style=\"color: #268bd2; font-family: Courier; font-size: large;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: large;\">(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: large;\">2<\/span><span style=\"color: #657b83; font-family: Courier; font-size: large;\">+<\/span><span style=\"color: #d33682; font-family: Courier; font-size: large;\">3<\/span><span style=\"color: #657b83; font-family: Courier; font-size: large;\">)<br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: large;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: large;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: large;\">&#8216;2+3&#8217;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: large;\">)<br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: large;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: large;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: large;\">&#8216;2 plus 3&#8217;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: large;\">)<br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: large;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: large;\">(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: large;\">2<\/span><span style=\"color: #657b83; font-family: Courier; font-size: large;\"> plus <\/span><span style=\"color: #d33682; font-family: Courier; font-size: large;\">3<\/span><span style=\"color: #657b83; font-family: Courier; font-size: large;\">) <\/span><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Upon running this file, we get the following message from Python.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_syntax_error_python_idiagress.png\"><img loading=\"lazy\" decoding=\"async\" width=\"502\" height=\"130\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_syntax_error_python_idiagress.png\" alt=\"\" class=\"wp-image-2524\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_syntax_error_python_idiagress.png 502w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_syntax_error_python_idiagress-300x78.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_syntax_error_python_idiagress-335x87.png 335w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The program output indicates a &#8216;Syntax Error&#8217; while processing line 4. <strong>Syntax errors occur when the rules of a programming language are violated<\/strong>.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>As previously discussed, in Python, strings are defined within quotes (single or double), while numbers are defined without quotes. In line 4, we did not use single quotes to enclose the characters (2 plus 3) which makes Python interpret it as numbers. However, the line also contains the word &#8216;plus,&#8217; which doesn&#8217;t fit the expected syntax for a number.<\/p>\n\n\n\n<p>This violation of the syntax rules results in an error. To resolve this error and allow the program to run smoothly, we can simply remove line 4. Doing so ensures that the program adheres to Python&#8217;s syntax rules without any issues.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/correcting_syntax_error_python_course_idiagress.png\"><img loading=\"lazy\" decoding=\"async\" width=\"517\" height=\"101\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/correcting_syntax_error_python_course_idiagress.png\" alt=\"\" class=\"wp-image-2525\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/correcting_syntax_error_python_course_idiagress.png 517w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/correcting_syntax_error_python_course_idiagress-300x59.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/correcting_syntax_error_python_course_idiagress-335x65.png 335w\" sizes=\"auto, (max-width: 517px) 100vw, 517px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><b><span class=\"Apple-converted-space\">&nbsp;<\/span>A. Rules for Mathematical operations in Python<\/b><\/h2>\n\n\n\n<p>In introductory algebra, we&#8217;re familiar with the BODMAS rule (Brackets Off Division Multiplication Addition Subtraction), which governs the order of operations in mathematical expressions. Python follows a similar rule with a slight modification, using PEMDAS (Parentheses, Exponents, Multiplication, Division, Addition, Subtraction).<\/p>\n\n\n\n<p>In Python&#8217;s order of operations, when multiple operations share the same precedence, they are evaluated from left to right. This means that the operation that comes first from the left is solved first.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<p><strong>50 \u00f7 5 \u2022 5:<\/strong><\/p>\n\n\n\n<p>When we move from left to right, the division (50 \u00f7 5) occurs first and is solved first, resulting in 10. Then, the multiplication (10 \u2022 5) is performed, giving us an answer of 50.<\/p>\n\n\n\n<p><strong>With brackets:<\/strong><\/p>\n\n\n\n<p>Now, let&#8217;s consider the same set with brackets: 50 \u00f7 (5 \u2022 5). In this case, the brackets operation comes first, and (5 \u2022 5) equals 25. Then, the division (50 \u00f7 25) is solved, resulting in an answer of 2.<\/p>\n\n\n\n<p>Similar to its syntax for strings, Python has specific syntax rules to represent mathematical operations, ensuring that expressions are evaluated correctly following the PEMDAS rule<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Brackets Off, Exponential, (Division or Multiplication), (Addition or Subtraction)<\/strong><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span style=\"color: #000000; font-family: Futura; font-size: large;\"><b>No<\/b><\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\"><b>Mathematical Operation<\/b><\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\"><b>Mathematical Symbol<\/b><\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\"><b>Python Syntax<\/b><\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">1<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">Exponential<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">X Y<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">X * * Y<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">2<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">Division<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">X\u00f7Y<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">X\/Y<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">3<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">Multiplication<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">X \u2022 Y<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">X * Y<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">4<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">Addition<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">X+Y<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">X + Y<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">5<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">Substraction<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">X-Y<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: large;\">X &#8211; Y<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>To explore these mathematical expressions, create a new file (\u2018Math.py\u2019)<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span style=\"color: #268bd2; font-family: Courier; font-size: large;\">print<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: large;\">(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: large;\">50<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: large;\">\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: large;\">5<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: large;\"> * <\/span><span style=\"color: #d33682; font-family: Courier; font-size: large;\">5<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: large;\">)<br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: large;\">print<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: large;\">(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: large;\">50<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: large;\">\/(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: large;\">5<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: large;\">*<\/span><span style=\"color: #d33682; font-family: Courier; font-size: large;\">5<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: large;\">)) <\/span><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The output we get is,<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/first_math_program_operations_idiagress.png\"><img loading=\"lazy\" decoding=\"async\" width=\"488\" height=\"104\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/first_math_program_operations_idiagress.png\" alt=\"\" class=\"wp-image-2526\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/first_math_program_operations_idiagress.png 488w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/first_math_program_operations_idiagress-300x64.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/first_math_program_operations_idiagress-335x71.png 335w\" sizes=\"auto, (max-width: 488px) 100vw, 488px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"vw-infobox\"><div class=\"vw-infobox-inner\"><h4 class=\"vw-infobox-title\"><span>Exercise<\/span><\/h4><div class=\"vw-infobox-content\">Perform hand calculations for the equations and subsequently create a Python program to verify the solutions.<\/p>\n<ol>\n<li>33 + 9 \u00f7 3<\/li>\n<li>(10\u00f75) \u2022 (50\u00f710)<\/li>\n<li>10\u00f75\u202250\u00f710<\/li>\n<li>200*10+1000 &#8211; 2000\u00f72<\/li>\n<li>(((20\u00f75)+10)\u2022((30-6)-10))2<\/li>\n<\/ol>\n<p><\/div><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><b>B. Variables in Python<\/b><\/p>\n\n\n\n<p>In introductory algebra, we often encounter problems where we need to solve mathematical expressions. These problems typically present questions like: &#8216;If the value of &#8216;A&#8217; is 10, and &#8216;B&#8217; is 5, and C = A \u2022 B, then what is C&#8217;s value?&#8217;<\/p>\n\n\n\n<p>In these contexts, &#8216;A,&#8217; &#8216;B,&#8217; and &#8216;C&#8217; are referred to as &#8216;parameters&#8217; or &#8216;variables.&#8217; These variables represent quantities that can vary within the context of the mathematical problem.<\/p>\n\n\n\n<p>Now, the pivotal question is: How do these variables benefit us? Let&#8217;s illustrate this with an example. Consider the following equation:<\/p>\n\n\n\n<p><strong>Solve (22 \u00f7 7) \u2022 (5)\u00b2<\/strong><\/p>\n\n\n\n<p>This is a generic problem that can be easily solved. However, we can rewrite the same problem in a variable format:<\/p>\n\n\n\n<p><strong>C = (22 \u00f7 7) \u2022 (A)\u00b2<\/strong><\/p>\n\n\n\n<p>In this equation, &#8216;C&#8217; and &#8216;A&#8217; are our variables. If you look closely, you might notice a connection to basic geometry. This formula is reminiscent of the formula for the radius of a circle.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">Area of Circle = (22\u00f77) \u2022 (Radius)<sup>2<\/sup><span class=\"Apple-converted-space\">&nbsp; <\/span><\/pre>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Variables provide a powerful way to generalize and represent mathematical relationships. They allow us to create equations and formulas that can be applied to a range of scenarios, making mathematics a versatile and practical tool for problem-solving in various fields<\/p>\n\n\n\n<p>When we input a generalized formula into Python, we gain the ability to solve for different values, making our program interactive and adaptable. To do this, we need the capability to change the value of a variable, such as the radius in our example, interactively. The most effective way to achieve this is by prompting the user for input and storing that input in a variable. The command used for this purpose is &#8216;input.&#8217;<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">A=input(\"Enter radius\")<\/pre>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In Python, &#8216;A&#8217; is the variable name, and &#8216;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>input()<\/strong><\/mark>&#8216; is the command used to prompt the user for input.<\/p>\n\n\n\n<p>The message within the &#8216;input()&#8217; command, enclosed in quotes (&#8220;Enter radius&#8221;), serves as an informational prompt displayed to the user, indicating the type of input expected.<\/p>\n\n\n\n<p>The complete program for calculating the area of a circle can be expressed as follows<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">A=<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;Enter radius&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">)<br>C=(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">22<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">7<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">)(A**<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">2<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">)<br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(C)<\/span><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>When we run this program, the output is as follows<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/syntax_error_output_circle_py.png\"><img loading=\"lazy\" decoding=\"async\" width=\"629\" height=\"172\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/syntax_error_output_circle_py.png\" alt=\"\" class=\"wp-image-2571\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/syntax_error_output_circle_py.png 629w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/syntax_error_output_circle_py-300x82.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/syntax_error_output_circle_py-335x92.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/syntax_error_output_circle_py-600x164.png 600w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span class=\"Apple-converted-space\"><\/span>However, when we run this program, we encounter an error: &#8216;<strong>Type Error: Unsupported operand types for ** or pow(): &#8216;str&#8217; and &#8216;int.&#8217;<\/strong><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In this error message, &#8216;str&#8217; stands for &#8216;string&#8217; (representing the data type of &#8216;A&#8217;), and &#8216;int&#8217; stands for &#8216;integer&#8217; (the data type used in the mathematical operation). <\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\"><strong>The issue arises because the &#8216;input()&#8217; command, by default, treats the user&#8217;s input as a string<\/strong><\/mark>, and then, in the subsequent line, we attempt a mathematical operation (&#8216;A ** 2&#8217;) on this string. This results in an error.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>To resolve this issue, we must declare the data type we expect from the user&#8217;s input. We have two options: &#8216;integer&#8217; (for non-decimal numbers) or &#8216;float&#8217; (for decimal numbers). Given that our program involves a decimal term (22 \u00f7 7), it&#8217;s wise to use floats.<\/p>\n\n\n\n<p>Here&#8217;s how we declare the data type in the &#8216;input()&#8217; command:<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">A=<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">\u201cEnter radius&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">))<br>C=(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">22<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">7<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">)*(A**<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">2<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">)<br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(C)<\/span><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Encasing an expression in a float bracket,<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>float( )<span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>extracts a float value.<span class=\"Apple-converted-space\">&nbsp;<\/span><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/float_area_of_circle_python_idiagress.png\"><img loading=\"lazy\" decoding=\"async\" width=\"530\" height=\"84\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/float_area_of_circle_python_idiagress.png\" alt=\"\" class=\"wp-image-2528\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/float_area_of_circle_python_idiagress.png 530w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/float_area_of_circle_python_idiagress-300x48.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/float_area_of_circle_python_idiagress-335x53.png 335w\" sizes=\"auto, (max-width: 530px) 100vw, 530px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In the output above , we entered the radius as<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>&#8216; <b>23<\/b>&#8216;,<span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>and the value highlighted in the rectangle<span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>&#8216;<b>1662.57..<\/b>&#8216;<span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>is the circle&#8217;s area.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><b>C. Clarity and readability of Python Program<span class=\"Apple-converted-space\">&nbsp;<\/span><\/b><\/h3>\n\n\n\n<p>The &#8216;circle.py&#8217; program accomplishes its intended task by allowing users to input the value of the &#8216;radius&#8217; and obtain the &#8216;area&#8217; as output. However, consider someone who only views the output window; that person might become confused about the meaning of the value &#8216;1662.57.&#8217; Is it the circumference of a circle with a radius of 23? Is it the volume of a sphere with a radius of 23?<\/p>\n\n\n\n<p>To avoid such confusion, our program should provide clarity in a way that anyone viewing only the output can understand the program&#8217;s purpose without having to open the <strong>source code<\/strong>.<\/p>\n\n\n\n<p>The term &#8216;<strong>source code<\/strong>&#8216; refers to the original Python program we created, in this case, &#8216;circle.py.&#8217; Source code has its own requirement \u2013 it should be &#8216;<strong>readable<\/strong>.&#8217; <\/p>\n\n\n\n<p>Think about a situation where someone revisits your code five years down the line; the code should be so well-structured and documented that the person can easily comprehend its logic and flow. If the person needs to contact you for clarification, your code may be considered &#8216;unreadable.&#8217;<\/p>\n\n\n\n<p>To make code readable, we can use techniques like adding empty lines and incorporating comments into our program. In Python, we use the &#8216;#&#8217; symbol to include descriptive comments within the code. These comments provide explanations and context for each section, making it easier for both you and others to understand and maintain the code. Here is an example of it:<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&nbsp;<\/td><\/tr><tr><td><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Python program for finding area of circle with radius as an input<\/i><br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Asking user to input radius and storing it as datatype float<\/i><br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_r1 = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;Enter the value of radius :&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">))<br>_a1 = ((<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">22<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">7<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">)*(_r1 ** <\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">2<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">))<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># print area of circle with context<\/i><br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;For a circle of radius&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _r1, <\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;the circular area is: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _a1)<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The output is<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/readable_python_circle_program_idiagress.png\"><img loading=\"lazy\" decoding=\"async\" width=\"702\" height=\"81\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/readable_python_circle_program_idiagress.png\" alt=\"\" class=\"wp-image-2529\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/readable_python_circle_program_idiagress.png 702w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/readable_python_circle_program_idiagress-300x35.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/readable_python_circle_program_idiagress-335x39.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/readable_python_circle_program_idiagress-700x81.png 700w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/readable_python_circle_program_idiagress-600x69.png 600w\" sizes=\"auto, (max-width: 702px) 100vw, 702px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The comments prefaced by &#8216;<b>#&#8217;&nbsp;<\/b>are not processed by Python for the output and serve to make the code&nbsp;<b>&#8216;readable.&#8217;<\/b>&nbsp;In contrast, the print command&#8217;s context brings clarity in the output.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Variable Names in Python<\/h3>\n\n\n\n<p>n the context of our &#8216;circle.py&#8217; program, we created two versions of the code.<\/p>\n\n\n\n<p>The first version used variable names &#8216;A&#8217; and &#8216;C&#8217; to represent the radius and the area, respectively. However, in the revised program, we opted for more descriptive variable names, using &#8216;_r1&#8217; for the radius and &#8216;_a1&#8217; for the area.<\/p>\n\n\n\n<p>When it comes to naming variables in Python, there are some key rules to remember:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Variable names cannot begin with a number. They must start with a letter or an underscore.<\/li>\n\n\n\n<li>After the initial letter or underscore, variable names can contain letters, numbers, and underscores.<\/li>\n\n\n\n<li>Special symbols, aside from the underscore (&#8216;_&#8217;), are not allowed to be part of variable names.<\/li>\n<\/ol>\n\n\n\n<p>Following these rules ensures that your variable names are clear, informative, and in compliance with Python&#8217;s naming conventions. Descriptive variable names contribute to the clarity and readability of your code.&#8221;<\/p>\n\n\n<div class=\"vw-infobox\"><div class=\"vw-infobox-inner\"><h4 class=\"vw-infobox-title\"><span>Exercise<\/span><\/h4><div class=\"vw-infobox-content\"><\/p>\n<ol>\n<li>Get as input the length and breadth of a rectangle, and calculate its area.<\/li>\n<li>Get as input the inner and outer radius of a circular ring and print the ring area as the output.<\/li>\n<li>Create a program that receives the circle area as its input and prints circumference as an output. (Square root is exponential to the power 0.5, (25)**0.5 = 5)<\/li>\n<li>Calculate the volume of a cube by using the length of the cube as the input.<\/li>\n<li>The age of Anju is twice that of Sanju. Input the age of Sanju and print the age of Anju as the output.<\/li>\n<\/ol>\n<p><\/div><\/div><\/div>\n\n\n\n<p><a class='vw-button vw-button--purple ' href='https:\/\/idiagress.com\/vinaaypatil\/2020\/08\/05\/1-1-verifying-python-install\/' target='_self'>Prev Chapter<\/a> &nbsp; &nbsp;<a class='vw-button vw-button-- ' href='https:\/\/idiagress.com\/vinaaypatil\/first-steps-in-python\/' target='_self'>Course Homepage<\/a> &nbsp; &nbsp;<a class='vw-button vw-button--pink  vw-button--arrow' href='https:\/\/idiagress.com\/vinaaypatil\/2020\/08\/09\/1-3-linear-equations-using-python-algorithms\/' target='_self'>Next Chapter<\/a><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u201cAlways code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live\u201d<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Before delving into this chapter, it&#8217;s crucial to recall how to run a fundamental Python program, whether you&#8217;re using Thonny or Atom. If you need a quick refresher, you can refer to the previous chapter for a step-by-step guide. In the world of Python, there&#8217;s a saying that &#8216;everything in Python is an object.&#8217; As &#8230;<\/p>\n","protected":false},"author":2,"featured_media":2758,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[412],"tags":[407,405,408,409,406],"class_list":["post-2522","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-first-steps-in-python","tag-floats-in-python","tag-objects-in-python","tag-strings-in-python","tag-syntax-errors-in-python","tag-variables-in-python"],"_links":{"self":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/comments?post=2522"}],"version-history":[{"count":4,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2522\/revisions"}],"predecessor-version":[{"id":3705,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2522\/revisions\/3705"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/media\/2758"}],"wp:attachment":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/media?parent=2522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/categories?post=2522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/tags?post=2522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}