Boston Python Workshop 3/Data types
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