The concepts here build upon chapter 2 and the videos in Lesson 3. A complimentary page of examples is Prof Cravens page on using Python as a calculator.
A variable can be thought of as a "box" that holds an "object". In the image below, you can see that there is a box with a number "inside" it. This object is of type (or class) float, and it has been given the name pressure.
In code, we would say:
pressure = 3.14159
This one line of code would create a variable using the assignment operator where:
name = pressure
type = float (or <class 'float'>)
value = 3.14159
There are many types of object in Python. Those considered the most basic are:
string (str) or text
integer (int) meaning any full number
float (float) is a number with a decimal point
boolean (bool) can have only one of two values, either True or False
There are other types of object as well - that are a little more complex - we will look at also such as:
A list that holds a sequence of objects, e.g. [2, 3.6, True, "Hello"] which we can mutate
A tuple that holds a sequence of objects, e.g. (2, 3.6, True, "Hello") which we cannot mutate (immutable)
A dict which holds a key-value pair, e.g. my_dict = {1: "Peter", 2: "Mary"} where keys must be unique and values can be any class (type). More on this later.
There are also many others, but these are the ones we'll use. Later we can create our own types- this is called Object Oriented Programming- we will be able to create objects such as "Human", or "Player" or "Alien" or "Meteor".
summary of python data types
We will use almost (but not quite) all of the data types shown here, over the duration of this course.
Reference Lesson 3 (or Chapter 2) for the rules on variable names and mnemonics
A constant does not change, so "under the hood", things such as numbers and strings do not actually change. To clarify:
a constant never changes- like math.pi or the value for pi (3.14159...) does not change
In Python, any variable may be changed. In some other languages you can declare a variable as a constant and it can never then be changed, either by Thor's hammer or Odin's beard. If there is a variable that you wish to remain with the same value for the duration of your program, the convention is to capitalise that variable, e.g. SCREEN_WIDTH = 640 communicates to the reader that the variable SCREEN_WIDTH is not be be changed - should not be changed. It does not mean that it cannot be changed- this is important.
variables may point to different values, but once these values are created, they will not change.
user = "Mary"
user_age = 22
user = "Tom"
user_age = 31
while the values stored in or pointed to by user and user_age have certainly changed, all we have done is pointed those references to new boxes holding the values of Tom and 31. The actual boxes that held "Mary" and 22 are unchanged, and now just lost in memory. There is a bit of code called a garbage collector that looks for and picks up "lost objects" and removes them. This may make more sense later, but the point is that the values have not changed, we've essentially ignored them and created new ones.
All of the operators below are implemented "under the hood" (or at a level that we cannot readily see) in the code as functions that are referred to as methods. A method "works" on a class. 3.1 + 7 is permitted because both of these objects- while being technically different as an int and a float- belong to the same parent class of "num", which is an abstract class. This abstract class is not explicit- we cannot create a "num", but we can certainly create and use the children or sub-classes of num, which are float, int and bool.
The most frequently used and best known operators. Keep in mind that operators are implemented differently "under the hood" and may- or may not work across different types/classes. Under the hood, the familiar operators are known referred to as add, sub, mul, div and so on- not hard to guess which one is called what.
We will look in more depth at these later, but keep in mind what objects you can compare:
an int with an int
an int with a float
an int with a bool
a str with a str
a str with an int?
a str with a list?
"Under the hood" the relational operators are "known" by references such as gt, lt, eq, ne, gte, lte and so on- can you guess what these abbreviations may stand for?
These correspond with the logical gates, and are common also to database queries and expressions in spreadsheets. Though the nsyntax will vary by application and language, these are fundamental concepts of computer science.
Yes, there is more than the humble = available, mostly to be used as a succinct (but arguably less readable) form of assignment.
Note than one can also use
*= so if x were 7, running x *= 3 would result in x having a value of 21
/= so if x were 7, running x /= 3 would result in x having a value of 2.3333333333333335
//= so if x were 7, running x //= 3 would result in x having a value of 2
-= so if x were 7, running x -= 3 would result in x having a value of 4
This will apply the same logic: more difficult - initially at least - to follow, but often used as shorthand.
Bitwise operators operate on operands at a binary level. Meaning the bitwise operator looks directly at the binary digits or binary bits of an integer. Hence the name bitwise (bit by bit operation). The only one of these that we will look at it the inversion, or bitwise NOT method.
There are two types of special operators in the python programming language.
As the name suggests the identity operators compare the id (identity) of two or more python objects such as variables, values, and many more. In other words, others say that the identity operator can also be used to compare the memory locations of two objects.
Membership operators are used to verifying whether a particular element is a part of a sequence or not. Now a sequence can be a list, string, sets, dictionary, and tuples.