Python Fundamentals Tutorial: Types

2. Types

2.1. Strings

String literals can be defined with any of single quotes ('), double quotes (") or triple quotes (''' or """). All give the same result with two important differences.

  1. If you quote with single quotes, you do not have to escape double quotes and vice-versa.
  2. If you quote with triple quotes, your string can span multiple lines.
>>> 'hello' + " " + '''world'''
'hello world'

2.2. Integers

Integer literals are created by any number without a decimal or complex component.

>>> 1 + 2
3

2.2.1. Integer Division

Some programming tasks make extensive use of integer division and Python behaves in the expected manner.

>>> 10 / 3
3
>>> 10 % 3
1
>>> divmod(10, 3)
(3, 1)

2.3. Floats

Float literals can be created by adding a decimal component to a number.

>>> 1.0 / .99
1.0101010101010102

2.4. Complex

Complex literals can be created by using the notation x + yj where x is the real component and y is the imaginary component.

>>> 1j * 1j
(-1+0j)