You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>This statement assigns the name <code>x</code> to the value <code>3</code>. Like a suitcase tag, the name <code>x</code> is bound to the value <code>3</code>.</p>
<p>Python first evaluates the expression on the right-hand side of the <code>=</code> assignment operator, then binds the name <code>x</code> to the resulting value.</p>
636
640
<p>The below statement re-assigns the name <code>x</code>. Think of this as moving the suitcase tag to a different suitcase.</p>
<p>Good style practices can involve comments, meaningful names, whitespace, markdown cells interspersed with code cells, etc. <ahref="https://inferentialthinking.com/chapters/03/2/Names.html">Ch 3.2</a> of <em>Inferential Thinking</em> describes meaningful names; we discuss comments below.</p>
676
680
<p><strong>Comments</strong> are used to explain what code does. Good programmers write code that is self-evident and use comments only where necessary.</p>
677
681
<p>In Python, you can write comments in the same line as code (“in-line” comments) using <code>#</code>:</p>
<p>Once you fix syntax errors, you may still encounter <strong>functionality errors</strong>, which can be errors caused during execution that leads to your program crashing. Here’s one common one:</p>
<p><strong>Concatenation operation</strong>: The <code>+</code> operator works differently on string data types. Instead of adding numerically, it “adds textually,” which is more formally called <strong>concatenation</strong>:</p>
<p><strong>Length function</strong>: There is one function not shown above that would be useful to you know, and that is <code>len(s)</code>, which takes a string argument and returns its length.</p>
<h2class="anchored" data-anchor-id="boolean-data-type">Boolean Data Type</h2>
613
617
<p>The Boolean data type (<code>bool</code>) has exactly two values: <code>True</code> and <code>False</code>. Note that boolean values are <em>not</em> strings!</p>
<p>We can also <strong>typecast</strong>, or convert values between data types. These typecasting functions take in one typed argument and return another typed argument, then return that value as a different type. The function name is generally the type. Note that data type conversion is only valid “when it makes sense.” We’ll talk about this more later.</p>
<divclass="sourceCode cell-code" id="cb11"><preclass="sourceCode python code-with-copy"><codeclass="sourceCode python"><spanid="cb11-1"><ahref="#cb11-1" aria-hidden="true" tabindex="-1"></a>x <spanclass="op">=</span><spanclass="dv">3</span><spanclass="co"># what type is x?</span></span>
635
639
<spanid="cb11-2"><ahref="#cb11-2" aria-hidden="true" tabindex="-1"></a><spanclass="bu">str</span>(x) <spanclass="co"># returns a value of type string</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
<divclass="sourceCode cell-code" id="cb13"><preclass="sourceCode python code-with-copy"><codeclass="sourceCode python"><spanid="cb13-1"><ahref="#cb13-1" aria-hidden="true" tabindex="-1"></a>s <spanclass="op">=</span><spanclass="st">"5"</span><spanclass="co"># what type is x?</span></span>
642
646
<spanid="cb13-2"><ahref="#cb13-2" aria-hidden="true" tabindex="-1"></a><spanclass="bu">float</span>(s) <spanclass="co"># what type is returned?</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
0 commit comments