Strings, Numbers and Booleans

Strings

Python has strings, which are written using either single or double quotes.


In [1]:
"This is a string!!!"


Out[1]:
'This is a string!!!'

In [2]:
'This is also a string!!!'


Out[2]:
'This is also a string!!!'

In [3]:
"This string contains single 'quotation' marks!!!"


Out[3]:
"This string contains single 'quotation' marks!!!"

In [4]:
'This string contains double "quotation" marks!!!'


Out[4]:
'This string contains double "quotation" marks!!!'

Numbers

Python has two types of numbers, integers and floats, which are analogous to mathematical integers and real number respectively.

Integers


In [5]:
7


Out[5]:
7

In [6]:
42


Out[6]:
42

Integers support all of the operations you would expect:

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulo

In [7]:
5 + 3


Out[7]:
8

In [8]:
5 - 3


Out[8]:
2

In [9]:
5 * 3


Out[9]:
15

In [10]:
5 / 3 # This is weird. More on this later.


Out[10]:
1

In [11]:
5 % 3 # The remainder of 5 / 3


Out[11]:
2

Floats (Floating-Point Numbers)


In [12]:
7.


Out[12]:
7.0

In [13]:
42.


Out[13]:
42.0

In [14]:
1.5


Out[14]:
1.5

Floats support the same operations as integers.

Gotchas

One thing to look out for is division involving integers.


In [15]:
3/4


Out[15]:
0

Mathematically this should be 0.75, but as the computation is using integers the result rounds down. Using floats on the other hand yields more accurate result.


In [16]:
3./4.


Out[16]:
0.75

Note that just using floats isn't enough to give you a mathematically correct answer. For example


In [17]:
0.1 + 0.2


Out[17]:
0.30000000000000004

On a computer, floating-point numbers are represented using a finite number of bits (ie. memory). This means that not every real number has a floating-point number associated with it, meaning that computers need to perform rounding to represent floating-point numbers and the results of computations on floating-point numbers. This is a source of error that needs to be taken into account when performing numerical computations.

For more details look here.

Computations with Mixed Number Types

In computations involving integers and floats, the results will be "upgraded" to floats.


In [18]:
x = 1
y = 2.
z = x + y
print(type(x))
print(type(y))
print(type(z))


<type 'int'>
<type 'float'>
<type 'float'>

In [19]:
3./4


Out[19]:
0.75

Booleans

Booleans represent true or false values.


In [20]:
True


Out[20]:
True

In [21]:
False


Out[21]:
False

They support the kinds of operations you would expect from boolean logic.


In [22]:
True and False


Out[22]:
False

In [23]:
True or False


Out[23]:
True

In [24]:
not True


Out[24]:
False

Booleans can be generated from equality comparisons.


In [25]:
1 == 2 # Check equality.


Out[25]:
False

In [26]:
1 != 2 # Checks non-equality


Out[26]:
True

In [27]:
2 > 4 # Size comparison


Out[27]:
False

In [28]:
"zebra" == "zebra" # Comparing strings


Out[28]:
True

In [29]:
(1, 2, 3) == (1, 2, 3) # Comparing tuples


Out[29]:
True