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>
 
====Math: addition, subtraction, multiplication, division====
 
<b>addition</b>: 2 + 2<br />
<b>subtraction</b>: 0 - 2<br />
<b>multiplication</b>: 2 * 3<br />
 
<b>division:</b><br />
====Math: division====
 
<pre>
Line 15 ⟶ 26:
</pre>
 
* Integer divisondivision produces an integer. You need a number that knows about the decimal point to get a decimal out of division:
 
<pre>
Line 24 ⟶ 35:
</pre>
 
====Types==Strings==
 
* 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>
 
==Strings==
 
<pre>
Line 39 ⟶ 44:
'Hello'
</pre>
 
====String concatenation====
 
<pre>
>>> print "Hello""In + "World"2009,
... The monetary component of the Nobel Prize
HelloWorld
... was US $1.4 million."""
In 2009,
The monetary component of the Nobel Prize
was US $1.4 million.
</pre>
 
====Printing strings====
 
<pre>
>>> print "Hello"
Hello
>>> print "Hello", "World"
Hello World
>>> print "Hello", "World", 1
Hello World 1
</pre>
 
====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 />
<b>Printing strings with ','</b>>: print "Hello", "World", 1<br />
 
==Booleans==
 
* There are two booleans, <code>True</code> and <code>False</code>.
* Use booleans to make decisions.
 
<pre>
>>> type(True)
<type 'bool'>
True
>>> type(False)
<type 'bool'>
False
</pre>
 
====Containment with 'in' and 'not in'====
 
<pr>
>>> "H" in "Hello"
True
>>> "Xa" not in ["a", "b", "Helloc"]
>>> "a" in ["a", "b", "c"]
True
>>> "x" in ["a", "b", "c"]
False
 
====TypesEquality====
<pre>
"a" not in "abcde"
</pre>
 
<pre>
"Perl" not in "Boston Python Workshop"
</pre>
 
* <code>==</code> tests for equality
==Equality==
* <codE>!=</code> tests for inequality
* <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, and <code>&gt;=</code> have the same meaning as in math class:.
 
>>> 0 == 0
Line 107 ⟶ 102:
</pre>
 
====Use with if/else blocks====
<code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, and <code>&gt;=</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>
1 > 0
</pre>
 
<pre>
2 >= 3
</pre>
 
<pre>
-1 < 0
</pre>
 
<pre>
temperature = 32
.5 <= 1
if temperature > 60 and temperature < 75:
print "It's nice and cozy in here!"
else:
print "Too extreme for me."
</pre>
Anonymous user