This tutorial, I want to talk to you about using variables in Python. Now in Python you are going to be dealing with a lot of data. So a lot of times in our program we are going to be working with all types of information and data and values. Sometime that data can be difficult to manage. So in Python we have this thing called a variable. And it's basically just a container where we can store certain data values. When we use a variable when we put those data values inside containers and make it a lot easier for us to work with and manage all of different data inside of our programs. So variables are extremely helpful and it's sort of like a core topic in Python that you're really going to want to know and master. So in this tutorial I will show you everything you need to know to get started with variables. We're going to look at what they are? Why they are useful and we'll look at the different types of variables and the different of data that we can store inside of variables.
In python file I just have a very simple program and it's basically just printing some text out onto the screen. So this is a valid Python program I can go ahead and Run this program and you can see down in the console. Every thing prints out and it looks great. We have an awesome little python program here
print("I am NgDinhNha") print("I am 20 years old") print("I am NgDinhNha") print("I am 20 years old")
Well, let's say that inside of our story I wanted to change the character's name and character's age. I'm going to have to go through and I'm going to have to manually change the name and age at every place inside of this story where it's mentioned. So in order to the character's name and age I had to manually go in and change it in all of places. It's going to work it's going to be updated in our program. But I'll notice that in order to make that change like I had to look through the entire program find it where that value was and change it. And this was only 4 lines of story. Imagine if I had a story that was like thousands of lines long. And we mentioned that the character's name and age like hundreds of times. We don't have to look through each one of those lines and manually change the character's name. And that is not a very good way for us to do this. Is's really not a good way for us to manage the data in our program right the character's name and age. So we can actually use a variable in order to store the character's name and character's age. And when we use that variable it'll make it a lot easier for us to put the character's name and age inside of our little program.
Now it's going to go up above print statement and create a variable character_name and character_age then change it inside print statement.
So finally I want to talk to you guys about the different types of data that we can store inside of these variables. I'm actually storing what are called strings in character_name and character_age. So a string is basically just plain text straight. So the data types of Python called a string and it's just basically going to store plain text. So you know any text that you would have like in a story early know someone's name. You can store inside of a string. there's also another type of data that we can store inside of python which is numbers. So an additional storing text we can also store. When we're storing a number we don't need these quotation marks. You only need quotation marks when you're storing a string. And in addition to using whole numbers I can also use decimal numbers. Python are going to be able to store that number as well. You can store all different types of numbers and we can also store what's called a boolean value. And a boolean value is essentially a true or false value. There's a lot of instances in programming where we're going to want to represent true or false data. For example,
There are three basic types of data that we can work with it in python. There's a bunch of other types of data that we can use but I world say 99% of the time as a new user in a new programmer to python you're just going to be dealing with these three types of data. So those are the basic of the data also with variables
Now it's going to go up above print statement and create a variable character_name and character_age then change it inside print statement.
character_name = "NgDinhNha"character_age = "20" print("I am " + character_name + ".") print("I am " + character_age + " years old") print("I am " + character_name + ".") print("I am " + character_age + " years old")So now when I run this program it's going to print out the same story they were print out before. But now I don't actually have to type in the character's name and character's age. I can just refer to this variable. And the cool thing about variables as if I wanted to change the character's name. Now all I have to do is change it up in variables.
character_name = "Nha"character_age = "30"So that's where variables can come in handy and this con be a really awesome way to control and manage the data that's inside of our programs. With variables you also can modify their values. For example, let's say that halfway through the story I wanted to change the character's name. I can actually assign a new line in the middle of program like
character_name = "Nha"character_age = "30"print("I am " + character_name + ".") print("I am " + character_age + " years old") character_name = "NgDinhNha"character_age = "20"print("I am " + character_name + ".") print("I am " + character_age + " years old")and you'll see in the first part of the story and the second part of the story are changed. That is actually awesome way you know a lot of times in these programs you're going to want to be modifying the values of variables.
So finally I want to talk to you guys about the different types of data that we can store inside of these variables. I'm actually storing what are called strings in character_name and character_age. So a string is basically just plain text straight. So the data types of Python called a string and it's just basically going to store plain text. So you know any text that you would have like in a story early know someone's name. You can store inside of a string. there's also another type of data that we can store inside of python which is numbers. So an additional storing text we can also store. When we're storing a number we don't need these quotation marks. You only need quotation marks when you're storing a string. And in addition to using whole numbers I can also use decimal numbers. Python are going to be able to store that number as well. You can store all different types of numbers and we can also store what's called a boolean value. And a boolean value is essentially a true or false value. There's a lot of instances in programming where we're going to want to represent true or false data. For example,
character_name = "Nha"character_age = 30.5342is_male = TrueTrue of false value is maybe not something that you're used to dealing with like in the real world but in programming we're going to be using a true or false value all the time they're supper important.
There are three basic types of data that we can work with it in python. There's a bunch of other types of data that we can use but I world say 99% of the time as a new user in a new programmer to python you're just going to be dealing with these three types of data. So those are the basic of the data also with variables
Comments
Post a Comment