View Single Post
Old 2019-11-04, 00:37   #6
Dylan14's Avatar
Mar 2017

2·33·11 Posts
Default Basic Constructs: strings, integers, floats, complexes, bools

Okay, we've covered how to get python, we've covered how to be wary of our syntax, and we've learned how to comment and (hopefully) do it properly. We should be ready to go, right? Not so fast. In codes, unless we're doing something very simple like printing "Hello World!" to the screen (*), we will generally have functions that take some input, do stuff (**) with that input, and then return something back to the user, either directly or into another function.
Now in something like C or C++, functions themselves have type, like int, void, etc. etc. This is not the case with Python. Here, the variables themselves are typed, and the functions are not. This is important!
Okay, but what types are there to use? In standard Python 3, we have many different types that we can use. These include:
* bool (a boolean, which can take the value 0 or 1, or alternatively "True" or "False"
* int (an integer)
* float (a floating point number)
* complex (a complex number of the form a + bj) (***)
* containers and iterators (iterate over objects within the container)
* lists (mutable sequence, used to store collections of similar objects)
* tuples (immutable sequences containing heterogeneous data, or for use in sets/dictionaries)
* ranges (used for "for" loops)
* strings (for text data)
* and more (see for all built in types)

Some modules will introduce other types. A notable example is the array class in numpy (which can be thought of as a matrix). Here, we will focus on 5 classes which appear very often in python: strings, integers, floats, complexes and bools.

A bool is short for a boolean. This is a type which has two values: either 0 (denoting "off" or "false") or 1 (denoting "on" or "true"). The values themselves are not really helpful in themselves, but they are useful when setting conditions for while loops and for if/else statements, which we will cover later. The Boolean operators are "and", "or", and "not". To understand these, one should know the truth tables of these expressions (****). Let a and b be logical statements. Then the operators will yield

*and - evaluates false if one of a or b are false.
*or - evaluates false if both a and b are false.
*not - evaluates true if the statement is false, else it's false.
Booleans also come about when using comparisons, of which there are 8 of these defined in Python - < (less than), <= (less than or equal), > (greater than), >= (greater than), == (equal), != (not equal), is (object identity) and is not (negated object identity).
Some of these operators (such as <, <=, > and >=) are only defined in certain cases. For example, if one of the arguments is a complex number, then a TypeError will be raised.


Int is short for integer. Integers are defined as you would expect in mathematics. You can have integers as large as you would like in Python 3, as long as you have enough memory. For example, here is M1693:

The usual operators can be used for integers, such as +, -, *, /, % (modulus), abs(x) (absolute value), and ^ (exponentiation, which can be implemented by using ** or pow(x, y)). There is also //, which is the floored quotient function (or integer division), which gives the whole integer value of the division, rounded down. Also, there is the divmod function, which gives the pair x // y, x % y.

Floats are floating point numbers. This type is needed because often times, it is not necessary to have infinite precision (or even possible) to have infinite precision, due to memory constraints. Depending on what scale we are working with (nanoscale or planetary scale), we need to have accuracy at different scales. To deal with this, floating point numbers are needed. Floating point numbers are defined by the IEEE 754 standard (*****). These have the same operations available as integers.

Complexes cover complex numbers. A complex number can be written in Python via the following:

complex(real, (imag))
For example, the number 3 + 4i can be written by typing:

complex(3, 4)

You can't use the % operator or the divmod operator for complexes. However, you also get a new operator in the c.conjugate() function which returns the complex conjugate.


The last class we will cover are strings. Strings are used to convey text information. These can be parsed and stripped and otherwise manipulated (we will go over this later when we deal with list comprehension). A string is denoted by either single quotes ' ', double quotes " ", or triple quotes in either version (""" """ or ''' '''). The triple quotes can be used to do multiline strings.
Strings can contain any character you'd like, but some characters need to be escaped. To escape a character, you use a \ (backslash). This is used, for example, if you need to add a tab or a new line character. You can also use \ to prefix special characters like the quotes, or for characters defined by their ASCII or Unicode values. For example, typing

will yield

the symbol for lowercase gamma.

All of the escape characters can be found on this site:

(*) The "Hello World" program is usually the first program that people learning to program will write up. Granted, it's not really that useful on its own (you could just type echo "hello world" in a command prompt and do the same thing without invoking python). However, printing stuff to screen can be useful when you go debug a program. I will cover this in the next section.
(**) I'm pretty sure "stuff" is the technical term here.
(***) This is the notation that Python uses for complexes, which is similar to what an electrical engineer would use. Mathematicians should interpret this a + bi. Still, they mean the same thing:

i = j = \sqrt {-1}

(****) A truth table is a construct in logic used to determine the validity of a logical statement. More information can be found here:
(*****) See for an overview.

Last fiddled with by Dylan14 on 2020-08-11 at 14:04 Reason: actually spell out the symbol name, delete some extra return lines Firefox likes to add
Dylan14 is offline