Boston Python Workshop 3/Data types

From OpenHatch wiki
Revision as of 15:28, 12 June 2011 by imported>Jesstess (→‎Strings)

Numbers: integers and floats

Addition

>>> 2 + 2
4

Subtraction

>>> 0 - 2
-2

Multiplication

>>> 2 * 3
6

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

Remainder

>>> 4 % 2
0
>>> 4 % 3
1

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'>