Anonymous user
Boston Python Workshop 3/Data types: Difference between revisions
no edit summary
imported>Jesstess No edit summary |
imported>Jesstess No edit summary |
||
Line 1:
==Numbers: integers and floats==
* Integers don't have a decimal place.
====Math: addition, subtraction, multiplication, division====▼
* Floats have a decimal place.
<pre>▼
>>> type(1)▼
<type 'int'>▼
>>> type(1.0)▼
<type 'float'>▼
</pre>▼
<b>addition</b>: 2 + 2<br />
<b>subtraction</b>: 0 - 2<br />
<b>multiplication</b>: 2 * 3<br />
====Math: division====
<pre>
Line 15 ⟶ 26:
</pre>
* Integer
<pre>
Line 24 ⟶ 35:
</pre>
==
* String are surrounded by quotes.
▲<pre>
* Use triple-quotes (""") to create whitespace-preserving multi-line strings.
▲>>> type(1)
▲<type 'int'>
▲>>> type(1.0)
▲<type 'float'>
▲</pre>
<pre>
Line 39 ⟶ 44:
'Hello'
</pre>
<pre>
>>> print "
... The monetary component of the Nobel Prize
... was US $1.4 million."""
In 2009,
The monetary component of the Nobel Prize
was US $1.4 million.
</pre>
>>> print "Hello", "World", 1▼
====Types====▼
<pre>
Line 64 ⟶ 58:
<type 'str'>
</pre>
<b>String concatenation with '+'</b>: "Hello" + "World"<br />
<b>Printing strings with '+'</b>: print "Hello" + "World"<br />
==Booleans==
* There are two booleans, <code>True</code> and <code>False</code>.
* Use booleans to make decisions.
<pre>
>>> type(True)
<type 'bool'>
>>> type(False)
<type 'bool'>
</pre>
====Containment with 'in' and 'not in'====
<pr>
>>> "H" in "Hello"
True
>>> "
False
* <code>==</code> tests for equality
* <codE>!=</code> tests for inequality
* <code><</code>, <code><=</code>, <code>></code>, and <code>>=</code> have the same meaning as in math class
>>> 0 == 0
Line 107 ⟶ 102:
</pre>
====Use with if/else blocks====
▲<code><</code>, <code><=</code>, <code>></code>, and <code>>=</code> have the same meaning as in math class:
* When Python encounters the <code>if</code> keyword, it evaluates the expression following the keyword and before the colon. If that expression is <code>True</code>, Python executes the code in the indented code block under the if line. If that expression is <code>False</code>, Python skips over the code block.
<pre>
temperature = 32
if temperature > 60 and temperature < 75:
print "It's nice and cozy in here!"
else:
print "Too extreme for me."
</pre>
|