Basic Python Datatypes#
Python is a dynamically typed language – this means that you don’t need to specify ahead of time what kind of data you are going to store in a variable. Nevertheless, there are some core datatypes that we need to become familiar with as we use the language.
The first set of datatypes are similar to those found in other languages (like C/C++ and Fortran): floating point numbers, integers, and strings.
The next set of datatypes are containers. In python, unlike some languages, these are built into the language and make it very easy to do complex operations. We’ll look at these later.
Some examples come from the python tutorial: http://docs.python.org/3/tutorial/
integers#
Integers are numbers without a decimal point. They can be positive or negative. Most programming languages use a finite-amount of memory to store a single integer, but in python will expand the amount of memory as necessary to store large integers.
The basic operators, +
, -
, *
, and /
work with integers
2+2+3
7
2*-4
-8
Unlike in C++ and Fortran, in python, dividing 2 integers results in a float.
1/2
0.5
To get an integer result, we can use the // operator.
1//2
0
Python is a dynamically-typed language—this means that we do not need to declare the datatype of a variable before initializing it.
Here we’ll create a variable (think of it as a descriptive label that can refer to some piece of data). The =
operator assigns a value to a variable.
a = 1
b = 2
Functions operate on variables and return a result. Here, print()
will output to the screen.
print(a+b)
3
print(a*b)
2
Note that variable names are case sensitive, so a and A are different
A = 2048
print(a, A)
1 2048
Tip
Python has some built in help (and Jupyter/ipython has even more)
try doing:
help(a)
alternately (only in Jupyter), try:
a?
Another function, type()
returns the data type of a variable
type(a)
int
Note in languages like Fortran and C++, you specify the amount of memory an integer can take (usually 2 or 4 bytes). This puts a restriction on the largest size integer that can be represented.
Python will adapt the size of the integer so you don’t overflow
a = 12345678901234567890123456789012345123456789012345678901234567890
print(a)
print(a.bit_length())
print(type(a))
12345678901234567890123456789012345123456789012345678901234567890
213
<class 'int'>
floating point#
when operating with both floating point and integers, the result is promoted to a float.
1. + 2
3.0
but note the special integer division operator
1.//2
0.0
We’ll discuss the limitations of floating point shortly.
modules#
The core python language is extended by a standard library that provides additional functionality. These added pieces are in the form of modules that we can import into our python session (or program). This is similar to #including
-ing a header in C++.
The math
module provides functions that do the basic mathematical operations as well as provide constants (note there is a separate cmath
module for complex numbers).
In python, you import
a module. The functions are then defined in a separate namespace—this is a separate region that defines names and variables, etc. A variable in one namespace can have the same name as a variable in a different namespace, and they don’t clash. You use the “.
” operator to access a member of a namespace.
Note
By default, when you type stuff into the python interpreter or here in the Jupyter notebook, or in a script, it is in its own default namespace, and you don’t need to prefix any of the variables with a namespace indicator.
import math
math
provides the value of pi
print(math.pi)
3.141592653589793
This is distinct from any variable pi
we might define here
pi = 3
print(pi, math.pi)
3 3.141592653589793
Note here that pi
and math.pi
are distinct from one another—they are in different namespaces.
floating point operations#
The same operators, +
, -
, *
, /
work as usual for floating point numbers. To raise an number to a power, we use the **
operator (this is the same as Fortran)
R = 2.0
print(math.pi*R**2)
12.566370614359172
operator precedence follows that of most languages. See
https://docs.python.org/3/reference/expressions.html#operator-precedence
in order of precedence:
quantities in
()
slicing, calls, subscripts
exponentiation (
**
)+x
,-x
,~x
*
,@
,/
,//
,%
+
,-
(after this are bitwise operations and comparisons)
Parentheses can be used to override the precedence.
The math module provides a lot of the standard math functions we might want to use.
For the trig functions, the expectation is that the argument to the function is in radians—you can use math.radians()
to convert from degrees to radians, ex:
print(math.cos(math.radians(45)))
0.7071067811865476
Notice that in that statement we are feeding the output of one function (math.radians()
) into a second function, math.cos()
When in doubt, as for help to discover all of the things a module provides:
help(math.sin)
Help on built-in function sin in module math:
sin(x, /)
Return the sine of x (measured in radians).
strings#
python doesn’t care if you use single or double quotes for strings:
a = "this is my string"
b = 'another string'
print(a)
print(b)
this is my string
another string
Many of the usual mathematical operators are defined for strings as well. For example to concatenate or duplicate:
print(a+b)
this is my stringanother string
print(a + ". " + b)
this is my string. another string
print(a*2)
this is my stringthis is my string
There are several escape codes that are interpreted in strings. These start with a backwards-slash, \
. E.g., you can use \n
for new line
a = a + "\n"
print(a)
this is my string
“”” can enclose multiline strings. This is useful for docstrings at the start of functions (more on that later…)
c = """
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum."""
print(c)
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.
a raw string does not replace escape sequences (like \n). Just put a r
before the first quote:
d = r"this is a raw string\n"
print(d)
this is a raw string\n
We can format strings when we are printing to insert quantities in particular places in the string. A {}
serves as a placeholder for a quantity and is replaced using the .format()
method:
a = 1
b = 2.0
c = "test"
print("a = {}; b = {}; c = {}".format(a, b, c))
a = 1; b = 2.0; c = test
But the more modern way to do this is to use f-strings
print(f"a = {a}; b = {b}; c = {c}")
a = 1; b = 2.0; c = test
Note the f
preceding the starting "