Boston Python Workshop 3/Data types: Difference between revisions
Content added Content deleted
imported>Jesstess No edit summary |
imported>Jesstess No edit summary |
||
Line 1: | Line 1: | ||
==Numbers: integers and floats== |
==Numbers: integers and floats== |
||
* Integers don't have a decimal place. |
|||
⚫ | |||
* Floats have a decimal place. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
<b>addition</b>: 2 + 2<br /> |
<b>addition</b>: 2 + 2<br /> |
||
<b>subtraction</b>: 0 - 2<br /> |
<b>subtraction</b>: 0 - 2<br /> |
||
<b>multiplication</b>: 2 * 3<br /> |
<b>multiplication</b>: 2 * 3<br /> |
||
<b>division:</b><br /> |
|||
====Math: division==== |
|||
<pre> |
<pre> |
||
Line 15: | Line 26: | ||
</pre> |
</pre> |
||
Integer |
* Integer division produces an integer. You need a number that knows about the decimal point to get a decimal out of division: |
||
<pre> |
<pre> |
||
Line 24: | Line 35: | ||
</pre> |
</pre> |
||
== |
==Strings== |
||
* String are surrounded by quotes. |
|||
⚫ | |||
* Use triple-quotes (""") to create whitespace-preserving multi-line strings. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
==Strings== |
|||
<pre> |
<pre> |
||
Line 39: | Line 44: | ||
'Hello' |
'Hello' |
||
</pre> |
</pre> |
||
====String concatenation==== |
|||
<pre> |
<pre> |
||
>>> print " |
>>> print """In 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> |
</pre> |
||
====Printing strings==== |
|||
<pre> |
|||
>>> print "Hello" |
|||
Hello |
|||
>>> print "Hello", "World" |
|||
Hello World |
|||
⚫ | |||
Hello World 1 |
|||
</pre> |
|||
⚫ | |||
<pre> |
<pre> |
||
Line 64: | Line 58: | ||
<type 'str'> |
<type 'str'> |
||
</pre> |
</pre> |
||
<b>String concatenation with '+'</b>: "Hello" + "World"<br /> |
|||
<b>Printing strings with '+'</b>: print "Hello" + "World"<br /> |
|||
⚫ | |||
==Booleans== |
==Booleans== |
||
* There are two booleans, <code>True</code> and <code>False</code>. |
|||
* Use booleans to make decisions. |
|||
<pre> |
<pre> |
||
>>> True |
>>> type(True) |
||
<type 'bool'> |
|||
True |
|||
>>> False |
>>> type(False) |
||
<type 'bool'> |
|||
False |
|||
</pre> |
</pre> |
||
==Containment== |
====Containment with 'in' and 'not in'==== |
||
<pr> |
|||
>>> "H" in "Hello" |
>>> "H" in "Hello" |
||
True |
True |
||
>>> " |
>>> "a" not in ["a", "b", "c"] |
||
>>> "a" in ["a", "b", "c"] |
|||
True |
|||
>>> "x" in ["a", "b", "c"] |
|||
False |
False |
||
⚫ | |||
<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 |
|||
⚫ | |||
>>> 0 == 0 |
>>> 0 == 0 |
||
Line 107: | Line 102: | ||
</pre> |
</pre> |
||
====Use with if/else blocks==== |
|||
⚫ | |||
* 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> |
<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> |
</pre> |
Revision as of 15:48, 12 June 2011
Numbers: integers and floats
- Integers don't have a decimal place.
- Floats have a decimal place.
>>> type(1) <type 'int'> >>> type(1.0) <type 'float'>
Math: addition, subtraction, multiplication
addition: 2 + 2
subtraction: 0 - 2
multiplication: 2 * 3
Math: division
>>> 4 / 2 2 >>> 1 / 2 0
- Integer division produces an integer. You need a number that knows about the decimal point to get a decimal out of division:
>>> 1.0 / 2 0.5 >>> float(1) / 2 0.5
Strings
- String are surrounded by quotes.
- Use triple-quotes (""") to create whitespace-preserving multi-line strings.
>>> "Hello" 'Hello'
>>> print """In 2009, ... 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.
>>> type("Hello") <type 'str'>
String concatenation with '+': "Hello" + "World"
Printing strings with '+': print "Hello" + "World"
Printing strings with ',': print "Hello", "World", 1
Booleans
- There are two booleans,
True
andFalse
. - Use booleans to make decisions.
>>> type(True) <type 'bool'> >>> type(False) <type 'bool'>
Containment with 'in' and 'not in'
<pr> >>> "H" in "Hello" True >>> "a" not in ["a", "b", "c"] False
Equality
==
tests for equality!=
tests for inequality<
,<=
,>
, and>=
have the same meaning as in math class.
>>> 0 == 0 True >>> 0 == 1 False
"a" != "a"
"a" != "A"
Use with if/else blocks
- When Python encounters the
if
keyword, it evaluates the expression following the keyword and before the colon. If that expression isTrue
, Python executes the code in the indented code block under the if line. If that expression isFalse
, Python skips over the code block.
temperature = 32 if temperature > 60 and temperature < 75: print "It's nice and cozy in here!" else: print "Too extreme for me."