Basic built-in data types
In line with its philosophy of keeping things simple, Python offers a much more stripped down collection of basic data types compared to Java. The following are the main basic Python data types:
- Numbers:
int
,float
,complex
- Booleans:
bool
- Strings:
str
That’s mainly it! (There’s also a bytes
type, but you probably will not be using it much).
Also, a definition: literal is a succint and ‘direct’ way to write a value, e.g. 558
, 2.6
, "hello"
, True
.
Numbers
Numbers can be
int
(integers)float
(floating points) - these are actually equivalent todouble
precision in Javacomplex
(complex numbers)
There is no fine-grained distinction e.g. short
, long
, and double
. An int
is an int
, a float
is a float
. As simple as that.
Try typing these into an interactive Python interpreter and see what you get:
>>> type(42)
>>> type(3.412)
>>> type(3.2e-12)
>>> type(1+2j)
Booleans
Booleans (bool
) can be either True
or False
. Note that these are Capitalised and not lowercase as in Java. Try these out!
>>> type(True)
>>> type(False)
Strings
Strings (str
) are a sequence of characters.
In Python, you surround string literals with either '
single quotes'
, "
double quotes"
, '''
triple quotes'''
or """
triple double-quotes"""
.
Try these:
>>> 'Is Python that easy?'
>>> "Is Python that easy?"
>>> '''Is Python that easy?'''
>>> """Is Python that easy?"""
All four are equivalent. So 'my string'
is identical to "my string"
.
You can also write multiline strings with '''
triple quotes'''
or """
triple double-quotes"""
. The whitespaces (spaces, newlines) are retained.
>>> ''' Never gonna give you up,
... Never gonna let you down,
... Never gonna run around and desert you.
... '''
Note: There is no char
type in Python. Just use str
(of length 1)!