02- Data Types, Variables, If-Else
Data Types
Data Types are used to categorize different kinds of information.
This allows us to have clear definitions of what one can do with specific kinds of data.
str- (string) a sequence of characters
int- (integer) positive or negative whole number
float- a real number, has a decimal point
bool- (boolean) a logical value, either True or False
Boolean values are really important! They are used to make decisions.
type()
Variables
You can think of a variable as a name associated with some value…
A more precise definition-
Variable- A location in memory that is paired with a name and stores some value
… I will try my best to explain this…
say… x = 5
Variables are stored in memory, at some location. This location is unknown to us, the programmers.
So when we declare the variable x, as x = 5, Python will find a location in memory to store our variable. That location (address) is paired with x, and at that location/address the number 5 is stored.
Variables are useful because they allow you to save certain values and those values can even change!
Variables can have pretty much any name, as long as it isn’t a keyword in Python.
Some examples of keywords are… True, False, def, and, ……. etc.
Can not have two variables with the same name…
It’s generally good practice to have variable names that are relevant to or descriptive of the information that’s being stored.
Comparison Operators
Comparison operators are used to compare two things. There are several comparison operators, including < (less than), > (greater than), and now == (equal to / double equal sign).
Using the double equal sign will return a bool (True or False) indicating whether or not the two things compared are equal.
!=
Similarly, != (not equal) can be used to check if two things are not equal.
!= will return a boolean value. It will return True if the two things compared are not equal, and False if the two things compared are equal.
If-Else
Sometimes in programming you want different things to happen depending on some condition.
An if-else statement is one way to do this. It specifies what to do.
Say we have two numbers that we want to compare. We want to print a message indicating whether or not they are equal.
If I wanted to compare different numbers, I would have to go back and change the values of a and b.
To make this program more useful, I will turn this code into a function, and have a and b as parameters instead, so that I can call this function with the numbers as arguments.
The if-else statement allows us to define what will happen in two cases. In the previous example, the cases were if the numbers were equal or if the numbers were not equal.
What if there were more than two cases?
If-Elif-Else
An if-elif-else statement is like an if-else statement, but it allows you to have more cases!
elif is short for else-if.
After checking the first condition in the if-block, you can check another condition in an elif-block. And actually you can have as many elif-blocks as you want! Or as many as would make sense for your program.
You should end in an else-block because it acts almost as a default case. The else-block will get executed if all other cases are False.
The order of the arguments/parameters MATTERS
first = 12, second =2
Python will start with the if- part,
first == second will return False, so it’ll move onto the next elif- part.
first < second will return True, so it’ll print “first number is greater than second number”
Since one of the conditions was already met, it will not move on to the else- part, and this block will be done.
It will then print “program complete” and our function call is done.
Comments
Comments are super useful in coding! Adding comments is helpful to you and anyone trying to read or understand your code.
To comment on one line
use # and everything on the same line, after # will be commented out.
To comment on multiple lines
surround your comments in ‘‘‘ triple single quotes ’’’ or “““ triple double quotes ”””
When our program is running, Python will skip over comments.