Boston Python Workshop 3/Data types: Difference between revisions
Content added Content deleted
imported>Jesstess |
imported>Jesstess No edit summary |
||
Line 1: | Line 1: | ||
==Numbers: integers and floats== |
==Numbers: integers and floats== |
||
====Math: addition, subtraction, multiplication, division==== |
|||
====Addition==== |
|||
<b>addition</b>: 2 + 2<br /> |
|||
⚫ | |||
<b>subtraction</b>: 0 - 2<br /> |
|||
⚫ | |||
<b>multiplication</b>: 2 * 3<br /> |
|||
4 |
|||
<b>division:</b><br /> |
|||
⚫ | |||
====Subtraction==== |
|||
⚫ | |||
⚫ | |||
-2 |
|||
⚫ | |||
====Multiplication==== |
|||
⚫ | |||
⚫ | |||
6 |
|||
⚫ | |||
====Division==== |
|||
<pre> |
<pre> |
||
Line 38: | Line 22: | ||
>>> float(1) / 2 |
>>> float(1) / 2 |
||
0.5 |
0.5 |
||
⚫ | |||
====Remainder==== |
|||
⚫ | |||
⚫ | |||
0 |
|||
>>> 4 % 3 |
|||
1 |
|||
</pre> |
</pre> |
||
Line 88: | Line 63: | ||
>>> type("Hello") |
>>> type("Hello") |
||
<type 'str'> |
<type 'str'> |
||
⚫ | |||
==Booleans== |
|||
⚫ | |||
⚫ | |||
True |
|||
⚫ | |||
False |
|||
⚫ | |||
==Containment== |
|||
>>> "H" in "Hello" |
|||
True |
|||
>>> "X" in "Hello" |
|||
>>> "a" in ["a", "b", "c"] |
|||
True |
|||
>>> "x" in ["a", "b", "c"] |
|||
False |
|||
⚫ | |||
"a" not in "abcde" |
|||
⚫ | |||
⚫ | |||
"Perl" not in "Boston Python Workshop" |
|||
⚫ | |||
==Equality== |
|||
⚫ | |||
True |
|||
⚫ | |||
False |
|||
⚫ | |||
"a" != "a" |
|||
</pre> |
|||
<pre> |
|||
"a" != "A" |
|||
</pre> |
|||
<code><</code>, <code><=</code>, <code>></code>, and <code>>=</code> have the same meaning as in math class: |
|||
<pre> |
|||
1 > 0 |
|||
</pre> |
|||
<pre> |
|||
2 >= 3 |
|||
</pre> |
|||
<pre> |
|||
-1 < 0 |
|||
</pre> |
|||
<pre> |
|||
.5 <= 1 |
|||
</pre> |
</pre> |
Revision as of 15:33, 12 June 2011
Numbers: integers and floats
Math: addition, subtraction, multiplication, division
addition: 2 + 2
subtraction: 0 - 2
multiplication: 2 * 3
division:
>>> 4 / 2 2 >>> 1 / 2 0
Integer divison 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
Types
>>> type(1) <type 'int'> >>> type(1.0) <type 'float'>
Strings
>>> "Hello" 'Hello'
String concatenation
>>> print "Hello" + "World" HelloWorld
Printing strings
>>> print "Hello" Hello >>> print "Hello", "World" Hello World >>> print "Hello", "World", 1 Hello World 1
Types
>>> type("Hello") <type 'str'>
Booleans
>>> True True >>> False False
Containment
>>> "H" in "Hello" True >>> "X" in "Hello" >>> "a" in ["a", "b", "c"] True >>> "x" in ["a", "b", "c"] False
"a" not in "abcde"
"Perl" not in "Boston Python Workshop"
Equality
>>> 0 == 0 True >>> 0 == 1 False
"a" != "a"
"a" != "A"
<
, <=
, >
, and >=
have the same meaning as in math class:
1 > 0
2 >= 3
-1 < 0
.5 <= 1