(upbeat music)
- [Lavanya] Oftentimes when you're programming,
you'll be tasked with answering a specific question.
So to answer a question through coding,
you transform the information that you have access to.
Now keep in mind, information is also known as data
and you can transform this data
in a way that addresses the question at hand.
Now a lot of data might be available
and it can be a little overwhelming to look at.
So how do you navigate all this data
and determine which pieces of data are useful for your task?
Well, you do this by categorizing the data
into different types which helps you stay organized
so you can complete your task.
I'm going to help you learn the main types of data
and how they're written in Python.
Let's begin with Booleans.
Booleans are used to represent truth values.
A Boolean can have a value of either true or false
and in Python, the data type of a Boolean
is written as bool.
Next, let's talk about integers.
Integers in programming are like integers in math.
They're whole numbers including positive values,
negative values and zero.
Examples of integers include three, zero and -100.
And in Python, the data type of an integer
is written as int.
Next let's talk about floating point numbers.
Floating point numbers
are numbers that are expressed as decimals.
In other words, numbers with a decimal point
including negative values, positive values and zero.
Examples of floating point numbers
include 0.0, 4., 4.0, 5e10, 5e-10 as well as -3.3.
Now 5e10 represents five times 10 to the 10
and 5e-10 represents five times 10 to the negative 10.
And in Python, the data type of a floating point number
is written as float.
Now let's talk about strings.
Strings are sequences of characters enclosed by quotes
and these quotes can be either a pair of double quotes
or a pair of single quotes.
Examples of strings include I am a string,
elephant, A, a space and empty quotes.
And in Python, the data type of a string is written as str.
Now I want to zoom in on the difference
between strings and integers.
Any sequence of characters enclosed by quotes is a string
so seven in quotes is a string
while seven on its own is an integer.
Now you might come across an arithmetic expression
that has the value of an integer.
This expression has the data type int.
So two plus five is an integer
while two plus five in quotes is a string.
Now what's the implication of two plus five being an integer
and two plus five in quotes being a string?
Well, they have different results when printed out.
When you print out two plus five, you get seven.
Whereas when you print out two plus five in quotes,
you get two plus five itself.
This is because an arithmetic expression
enclosed in quotes is a string.
So there you have it.
Booleans, integers, floating point numbers and strings
are four important data types to keep in mind
when programming
and now you know how to categorize your data.
Did you ever play hangman as a kid?
If so, you may remember that one player,
also known as player one, comes up with a word or a phrase
and the other player, also known as player two,
has to guess it.
So when is the game over?
Well, the game is over when player two
either uses up all of their guesses
without correctly guessing the word
or they guess the word correctly
using the guesses they have.
Either way, the players need to keep track
of how many guesses are left throughout the game.
So as a fun exercise, let's say you were tasked
with coding hangman, meaning you're building the game
so it can be played online.
How exactly would you keep track
of how many guesses are remaining?
You do this by using a variable.
A variable in programming is like a storage box.
You can use a variable to store
any kind of information or data.
Variables are useful because you can access
and change the contents of a variable any time you want.
So I want to help you learn how to create your own variable.
Let's go back to the storage box analogy.
If you're organizing or packing some things,
you may divide your things into categories
based on what they are or what they're used for
and put them into boxes.
Then you might label the boxes with a short word or phrase
indicating what the boxes contain.
Similarly, to create a variable,
you must provide a name that indicates
what the variable contains
and provide the value of the variable
which is the data you want to store in the variable.
Let's go back to the example from earlier.
If you're coding hangman,
you can keep track of how many guesses are left in a game
using a variable.
You would create the variable at the beginning
since this information is needed from the start
till the end of the game.
You could name the variable number of guesses
or any other relevant name.
As for the value of this variable,
you could set it to a number of your choice,
representing how many guesses
player two has in total per game.
As the game progresses, each time player two makes a guess,
the variable will need to be updated accordingly.
The update consists
of decreasing the variable's value by one.
So in the end, it will look something like this.
In the first cell, I assume that this is the beginning
of the hangman game.
I create a variable named guesses
and I assign it a value of 10.
10 represents how many guesses
player two has in total per game.
I chose 10 arbitrarily.
If you're following along, you can choose any number
you want to be the value of this variable.
And then I print out guesses.
When I do this, 10 gets printed out.
In the next cell, I assume that player two
has made their first guess.
So I need to update the variable guesses.
And I do this by decreasing the value
of this variable by one.
So now when I print out guesses, I get nine.
In the third cell, I assume that player two
has made another guess.
Again, I need to update the variable guesses
and I do this by decreasing its value by one.
Now when I print out guesses, I get eight.
One thing you can see here
is that the variable takes on the role of its value
so when I print out the variable guesses,
the value that guesses has at that time
is what gets printed out.
So that's it.
Now you know how to use variables to store data in programs.
And if you're going to build a game,
you can keep track of information crucial to the game
using variables.
(classical music)