This video will expand on the basic concepts that we talked about in the previous video. In the first video, we talked about a terminology called assignment.
x = 5 is an assignment.
You assign values to variables. A variable can be assigned many times, and it always keeps the value of its most recent assignment.
If I do, x = 7, it replaces x = 5.
You can do x = x + 1, which adds 1 to its most recent assignment.
If we print(x), we would get 8 because 7 was its most recent assignment, and we add 1 to 7.
In the previous video, we also briefly talked about functions. We used the print() function and the type function().
A reminder of what functions are. Functions are fancy terms for defining commands that we can use in Python, and Python comes with a lot of preconfigured functions like print() and type().
When we use a specific function, it is called a function call.
When I write type(4), I am calling the function type with a value 4.
When calling a function, the value inside the parentheses is called an argument. 4 is an argument of the function.
All you need to know is that values inside the parentheses of functions are called arguments. That’s the name that they’re given.
When I print(type(4)), type(4) gives us the value, <type ‘int’>, which we print and display.
Whenever a function call gives us a value, we call it the return value.
Using the type function, we expect the function to give us back the type of the value, and if the function gives us anything back, that value given back is just called the return value.
We’ve talked a bit about types so far. Sometimes, you don’t have the type that you want.
For example, you could have a string like fruits = ” apples”
Before the word, apples, you want to put a number like 5 to get 5 apples.
We talked about how you could use the + operator to combine strings together.
However, you cannot combine an integer with a string because they’re not the same type.
Python has built-in functions that convert values from one type to another type.
There’s a function called str() that converts a value to string type. I can use str on the number 5 and add the str(5) to fruits because a string and be combined with another string.
num_fruits = str(5) + fruits.
There is a function called int() that converts a type to an integer if possible.
int parentheses with a value inside them. For example,
4 has quotation marks, so it’s a string, but since the string is also a valid number, we can convert the string into an integer with the int function
However, if we were to put a word in the int() function like int(“microwavesam”), there’s an error because int() cannot convert strings that aren’t numbers into integers.
You can also use int() with a float. A float has a decimal point value like 1.5. When you use int() with a float, every number after the decimal point is deleted. So int(1.5) is 1.
The numbers in this case, 5, are removed.
int(1.58). 58 are removed. Integers are whole numbers, so when a float is converted to an integer. There’s no rounding. The integer just deletes every number after the decimal point.
There’s a function called float() and str() for converting types to floats and strings.
float(4) becomes 4.0 because floats always have numbers after the decimal point, so an integer and whole number like 4 gets a decimal point and an automatic 0.
str(4) becomes “4” with quotation marks since strings always have a pair of quotation marks.
We’ve talked quite a bit about numeric types so far. Integers and floats.
You might be thinking why not just have a type called number and make classifying numbers easier?
Why do we have integers for exact whole numbers and floats for values with decimal point numbers? One reason is because of different behavior.
Depending on your version of Python, the division operator works differently for float and integers.
If you’re using Python 3.6 like me, you can write the expression 1 / 2 and get 0.5. Python 3.6 automatically gives the result of type float when expected.
Let’s say that you were using Python 2.7, another popular version of Python.
If I divide, 1 / 2, the result is 0 because when you divide 2 integers, you get an integer in Python 2.7 and below.
When an integer will have numbers after the decimal point, it will automatically delete every number after the decimal point.
What was supposed to be 0.5 became 0 in Python 2. Python 3 automatically converts the result to a float keeping the 0.5.
To fix this problem for Python 2, if we use 1.0 / 2.0 (two floats because we put a 0 after the decimal point), then we’ll get a float.
If you wanted to get an integer in Python 3 after dividing two integers, you can use 2 backslashes, and you’ll get 0 when you divide 1 with 2 like 1 // 2.
As we saw with division, integers and floats can have different behaviors, but they also are represented differently too.
I’m not just talking about oh, whole numbers are integers. I’m talking about inside the computer and memory. We need to explain what exactly is a float?
Floats are approximations of any number. What does that mean? For a human being, it’s easy to know that 1 / 10 is 0.1, a tenth.
Python stores floats inside memory only through 1s and 0s. The binary numbers. With binary numbers you can’t represent 0.1 exactly!
0.1 is stored as this gigantic approximation: 0.1000000000000000055511151231257827021181583404541015625
because Python can’t compute 0.1 exactly.
Many decimal point numbers like 0.1 cannot be represented exactly in the language, so we have a specific type, float, that deals with these complications.
When we print(1 / 10), Python rounds that gigantic approximation for us, so we still get 0.1 like we would expect.
We have a type, float, because numbers with decimal points aren’t exact representations, so Python separates whole numbers and decimal numbers with different types to account for their different behaviors.
Every now and then, you get strange behavior when adding two floats because of how floats are represented and stored!
To us, 0.1 + 0.2 is easy. It’s 0.3.
In Python, print(0.1 + 0.2) is 0.30000000000000004.
Next thing that we’ll talk about are imports.
Python has this thing called modules. Modules are just a fancy term for a package of preconfigured functions.
Functions like print() and type() come usable with Python, but Python doesn’t expect everyone to want all the preconfigured functions, so Python expects you to import them and specify oh I want these functions inside this module.
You can import a module by typing:
One such module is math.
We typically put imports at the top of the program.
This module comes with a bunch of preconfigured functions like sqrt().
To use a module’s functions, you type the module name, then a ., and then the function. For example,
x = math.sqrt(4)
math.sqrt(4) computes the square root of 4.
You don’t have to remember about modules and their functions.
Most of the time, I would suggest using Google to find out what a module can do.
For example, we can Google the math module and find all of its preconfigured functions.
Use Google to find out if Python has a preconfigured function for what you want to do or a module with a function.
We’ve talked a lot about these preconfigured functions that come with Python and its modules, but what about adding our own functions?
Specific commands that will do what we want them to do.
We need to define a function. Or in other words, create a function.
The layout of a function in Python goes like this.
def is short for definition.
The function definition starts from the left.
The statements below that function definition must be tabbed once to the right.
The tab is how Python knows that oh, these statements are part of the function, so if the user wants to run the function, do all the things below that are tabbed once right.
You can name the function anything that you want except the same rules for naming variables apply to naming functions.
values inside the definition of a function are called parameters.
Parameters are just another one of those fancy terms used for defining that the function expects variables inside the parentheses.
Earlier in the video we talked about arguments. When we use print(4), 4 is an argument.
However, when we’re making a function definition, the variable would be called a parameter. For example:
Let’s say that we were creating the function called print_number.
The variable, number, is called a parameter. But when we use our function, print_number(4), the 4 inside the parentheses is called an argument because we’re using the function.
The word, parameter, is used for the value in creating the function definition.
The word, argument, is used for the value in using the function.
Create a function vs use a function. Parameter vs argument.
You’ll see all of these terms often in the future, so it’s useful to understand the difference even though they can be confusing.
Whenever you’re writing the first line of a function at the end of the line, you’ll always see a colon.
Let’s make a function that uses no parameters.
Let’s say that we wanted a function, a command, that printed the first 3 letters of the alphabet each on a new line. We’ll call it alphabet
Parameters are optional. Empty parentheses means that there are no parameters.
To use the function that we created, all we have to do is type alphabet(). The same way that we would use the print() or type() function.
We can create functions that call or use other functions. Let’s say that we wanted to print the first three letters of the alphabet three times.
We can write:
When we use print_three_alphabets(), the function uses our alphabet function 3 times, and each time alphabet() function runs, it’s one by one.
The most important reason why we create our own functions is to make a program smaller by reducing repetitions!
We can have a repetitive action in a single line of Python if we create a function!
For the functions that we’ve been using like print() or type(), they only require one value. If you want to display the number, 4, you write print(4).
You can create functions with multiple parameters.
Let’s say that we wanted a function to display the number of animals a person has.
def print_num_animals(number, animal):
We separate each parameter with a comma.
Since I expect the user to send a number for the first parameter of the function, I’ll change that number to a string.
num_string = str(number)
Earlier in the video, we said that we could convert types, adn the str() function converts the value to the string type.
Then, I’ll combine this string of the number with animal.
num_animals = num_string + " " + animal
In the first video, we said that we could use plus with strings, and what plus with strings does is simply combine the strings.
Now, we can print the new variable we created, which is the number combined with a space and the animal name.
Let’s use the function, print_num_animals. Let’s say that we had 2 dogs.
When we use a function with multiple parameters, we separate the arguments by a comma just like how we made the definition.
The function takes the 2 arguments and uses those variables to print the number of animals.
Let’s talk about the order of execution. What’s the order of things run inside a Python script?
When you see the function definition with def, and a bunch of statements below the definition, the function won’t execute unless you USE the function.
You need to define a function and write that function definition before using it.
If I put alphabet() parentheses, trying to use it, before the function definition, I get an error that says alphabet is not defined.
In Python, statements are executed in order. Starting from line 1 to the end of the program.
Python is computed line by line in order.
If you had a lot of errors in your program, Python would look at your program, compute the first line, keep on going line by line until it finds your first error, and then stop. You’ll only see the first error message.
Then, you fix your program and run it again. Python would start at line 1 and continue until it sees the next error!
The last thing that we’ll talk about in the video is the concept of local variables. What do local variables mean?
When we declare variables we’ve seen that we can use variables the same way that we use values because the variables are equivalent to the values that we’ve assigned.
Like x = 5.
x is equivalent to 5, and we can use x in a function like print(x), and 5 would be displayed.
When variables are assigned inside a function, the variables belong to that function. The variables are local to that function.
They can not be used outside of that function. That’s where we get the term, local variable.
Let’s take a look at our print_num_animals function. We declared and assigned a variable called num_animals.
Can we use that variable? No because since the variable was declared and assigned in that function, the variable belongs to that function.
If we try to, print(num_animals) outside of the function, we get this error.
num_animals is not defined.
num_animals is local to the function where it was declared and assigned, so you have to pay attention where you make your variables.