0% found this document useful (0 votes)
5 views

Variables Assignment, Math Operations and Data Types

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Variables Assignment, Math Operations and Data Types

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Variables Assignment, Math Operations and Data

Types
[00:00:00.00] [MUSIC PLAYING]

[00:00:08.64] RYAN AHMED: Hello, everyone, and welcome to this lesson on variable
assignments, data types, and math operations. In Python, variables are used to hold values, which
could have several data types. Once these variables are defined, then you can perform math
operations on them, such as addition, subtraction, or multiplication.

[00:00:29.94] Please note that this is an introductory lesson to Python, and many of the
operations could be performed in Excel. So you might wonder, well, I can do this in Excel. Why
do I need Python for this? Well, Python offers greater scalability and efficiency compared to
Excel. It also offers faster execution and allows for automating complex tasks.

[00:00:51.45] Python also comes with several open-source libraries and module. So we do not
have to develop code from scratch. You would appreciate the power of Python once we move to
more complex tasks later on in the course. Here are the key learning objectives of this lesson.

[00:01:08.07] Learn how to define Python variables and assign values to them. Perform math
operations in Python. Understand the difference between various data types in Python, such as
integers, floating points, and Booleans.

[00:01:22.02] So let's hand over to our Jupyter notebook and get started.

[00:01:24.83] [MUSIC PLAYING]

[00:01:33.35] All right, so right now we are in the Jupyter notebook titled Variable Assignment,
Math Operations, and Data Types. Jupyter notebooks are open-source web applications that
allow you to build documents containing code, output generated from that code, comments, text
data, equations, and visualizations.

[00:01:54.60] Please note that Jupyter notebooks are super powerful because you can include
everything you need in one place. So you can share these notebooks with other people or with
your colleagues at your company easily and efficiently. So let me walk you through this Jupyter
notebook so you can understand the structure we will use throughout the entire course.

[00:02:16.67] First, we start by introducing a new concept, and we include them in a form of
slides that are embedded inside the Jupyter notebook. For example, today, we will cover variable
assignment, math operations, and data types. And you would notice that the slides here are
embedded in the Jupyter notebook.

[00:02:37.31] Next, we will cover a code demo and each of these cells in here contain detailed
description of what that cell does. And that is here included in a form of comments. And please
note that comments are highlighted here in green color, and they start with a hashtag here at the
beginning of the line.

[00:03:02.11] And then at the end of the notebook we will have a practice opportunity in the
form of a coding lab. So I will ask you to give it a try and attempt to solve the practice
opportunity on your own. And you can go ahead and write your solution in these empty code
cells in here. Afterwards, I will provide you with a detailed video explanation of the solution in
the next video.

[00:03:27.54] Finally, I've also included the practice opportunity solution at the end of the
notebook, but I strongly recommend that you do not check the solution and attempt to solve it on
your own. And then you can compare your answer to my answer that are included at the end. So
let's go ahead and get started with our first topic.

[00:03:46.20] All right, so in Python, variables are used to hold values, which could be integers,
strings, floating points, or Booleans. You can simply define a variable and assign a value to it
using the assignment operator. And you can do that in one single line of code.

[00:04:07.79] Simply, here you can just say variable x. So that will be the variable name. You
will use the assignment operator, which is equal sign. And then you put value of 10 in it, and that
will be the value to be assigned to the variable x. So let me show you a quick example.

[00:04:27.24] Let's assume that I have this information here about Apple company, and assume
that you have the following information. You have the company name. You also have the annual
revenue in billions of dollars. You have the number of employees as well. And you also have
whether Apple pays dividends or not.

[00:04:47.40] So let's define Python variables to hold these values. Please note that we will need
different data types to hold all these different variables. For example, to hold the company name,
you would notice that the name simply contains a sequence of characters. And that's why I'm
going to say company_name. That will be the variable name. And then I'm going to say equals
to. And then I'm going to add single quotation marks, and I will add Apple associated with it.

[00:05:20.57] This is what we call it a string data type or two stands for str for short. Strings are
a sequence of characters, and they are defined using single or double quotation marks. Well,
what if I wanted to store the annual revenue in billions of dollars as an example? Well, I can say
revenue equals to, and then you put 394.32. Please note that this data type is a floating point, and
floats are real numbers with decimal point dividing the integer value and the fractional parts.

[00:05:58.47] Next, if I would like to capture the number of employees, I'm going to say
num_employees equals to, and then you assign 154,000. And this is going to be an integer data
type, or int. And integers are whole numbers with no decimal points. And they can be positive or
negative.

[00:06:20.31] And then finally, we will define a variable. I'm going to call it pays_dividend. And
that could simply be yes or no, or either true or false. And that's why we call it a Boolean data
type, or bool for short. And Boolean is a data type that has one of two possible values, which is
either true or false, or 0 or 1. So let's go ahead and show you the code demo.

[00:06:48.02] All right, so let's go ahead and define a variable, called company name, and assign
a value to it. So here I'm going to say company_name equals to. And then I'm going to add single
quotation marks, and I'm going to add the name Apple to it.

[00:07:05.60] So to run or execute this cell, all you need to do is to press Shift and Enter at the
same time on your keyboard. Or alternatively, you can just click Run here to run or execute this
cell.

[00:07:20.10] So if you click Run, that is going to run this cell. Simply, although we have done
right now is we assigned the value of Apple to a variable called company name. If you wanted to
display the content of company name, you just press Shift and Enter for the next sum. And you
will see that simply Apple is contained in the variable company_name.

[00:07:44.44] Another point that I would like to show you as well is you might be wondering,
well, I can do this in Excel. Why am I using Python for this? Please note that this is just an
introductory lecture to Python, and we will build on what we learn to develop powerful
applications later on in this course.

[00:08:01.51] Python offers greater scalability and efficiency compared to Excel, and it offers
faster execution time and also allows for automating a lot of complex tasks. Also, Python comes
with several open source libraries, so you don't have to develop code from scratch.

[00:08:19.78] OK, so now we successfully defined our first variable, and that was the company
name. Next, let's define revenue, which is a floating point. But before we do that, let's go ahead
and confirm the data type for Apple or for the company name variable.

[00:08:39.28] To do that, I'm going to say type. I'm going to open parentheses and then specify
the name of the variable. And that will be company_name. So to run or execute the cell again,
you press Shift and Enter. And you will get the data type, and that is going to be str, which
stands for string.

[00:08:59.68] So let's define and display the second entry in our table, and I'm going to assign a
decimal number to it. So here I'm going to say revenue equals to 394.32. And if you wanted to
display as well the variable revenue, you just put revenue afterwards. So if you press Shift and
Enter, here you will see that simply you define the variable revenue. I assigned a decimal number
to it, and now I was able to display it as well in here, OK?

[00:09:29.99] If you wanted to check out the data type of revenue variable, you just say type.
You open parentheses, and then you specify the variable name. If you press Shift Enter, here we
go, you will get the data type of float. And as I mentioned before, floating points are real
numbers with a decimal point dividing the integer and the fractional parts.
[00:09:51.59] OK, next, let's define and display the third entry in the table, and that was number
of employees. So here I'm going to say num_employees equals to 154,000. And you display it,
Shift Enter, you get 154,000.

[00:10:10.28] If you want to check out the data type, you just say type of num_employees, and
you will get the data type of int, which stands for integer. And again, as I mentioned, integers are
whole numbers. So they don't have decimal points. And they could be positive or negative.

[00:10:27.50] And then finally, I'm going to define a Boolean data type. So I'm going to say
pays_dividend equals to true. And you press Shift and Enter, and you will get the output is going
to be true. If you want to check out the data type, you will get the data type of bool, which stands
for Boolean.

[00:10:48.34] Please note that true here is a reserved words, and it's highlighted here in green. So
simply, you can't use that as a variable name, essentially.

[00:10:58.21] OK, so after you define all these variables, what I could do right now is I can go
ahead and perform math operations on them. And math operations can be easily performed in
Python. For example, if you want to perform addition, you just say plus. If you want to do
subtraction, you just say minus. Multiplication is asterisk, or star here. And then division you just
add the division sign.

[00:11:23.07] So for example, let's assume that Apple employees increased by 100, for example.
So what I could do is if I can say num_employees and you add 100 to it, so you say plus 100.
And then what you do is that you assign the new value to the num_employees again.

[00:11:44.79] So if you press Shift and Enter, well, you will get 154,100, which is the one that
we just added right now.

[00:11:53.41] OK, finally, let's assume maybe that the number of employees at Apple maybe has
doubled, for example. And you wanted to perform multiplication. Simply, you can say
num_employees. You multiply that by 2, and you overwrite the number of employees. So if you
press Shift and Enter, here we go. You will get 154,100 times 2. You will end up with 308,200,
OK?

[00:12:19.30] All right, so that's it. That's simply all I have for this lesson. I hope you enjoyed it.
In the next lesson, we're going to have our practice opportunity number 1. So please, go ahead
give it a try. And I want you to write your solution in here. And then afterwards, I'm going to
provide you with a detailed video explanation so you can compare your solution to mine. Please,
stay tuned. Best of luck, and I'll see you in the next lesson.

[00:12:44.08] [MUSIC PLAYING]

You might also like