Skip to main content

Learn Python #4 Variables & Data Types

  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.
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 = True
  True 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

Popular Posts

7554 bất ngờ mở miễn phí tặng game thủ nhân dịp kỷ niệm 65 năm chiến thắng Điện Biên Phủ

  "Tiến tới kỷ niệm 65 năm chiến thắng Điện Biên Phủ(7/5/1954 - 7/5/2019) . Và cũng nhân dịp kỷ niệm 10 năm làm game của Hiker Games. Như đã hứa, xin được gửi tặng cộng đồng game thủ Việt Nam phiên bản 7554 hoàn toàn miễn phí. Các bạn có thể truy cập vào website http://www.7554.vn để download. Để đảm bảo không có hiểu nhầm nào, xin lưu ý, đây không phải là phiên bản làm lại của 7554.  Đây chỉ là phiên bản đã loại bỏ phần kiểm tra bản quyền để bất cứ ai cũng có thể tải về và trải nghiệm game. Chúc các bạn chơi game vui vẻ!"  Via 7554 Dưới đây là video hướng dẫn tải game:  Chúc các bạn chơi game vui vẻ !

Nha's Blog #20260407 A Long Tuesday of Paperwork, Rain

A Long Tuesday of Paperwork, Rain Một ngày thứ Ba dài với giấy tờ, mưa Today was Tuesday, normally a working day, but I took a day off to handle paperwork for my permanent residency. The morning was gloomy, and the streets were still wet from days of spring rain. Thinking I had the whole day, I went to pachinko early since machines after reset are often better. Things went smoothly, and I managed to win a little. Hôm nay là thứ Ba, lẽ ra là ngày làm việc nhưng tôi xin nghỉ để đi làm giấy tờ xin vĩnh trú. Buổi sáng khá âm u, đường vẫn còn ướt do mưa xuân mấy ngày nay. Nghĩ rằng mình có cả ngày nên tôi tranh thủ đi pachinko từ sớm vì máy sau khi reset thường dễ chơi hơn. Mọi chuyện khá suôn sẻ và tôi thắng được một chút. Around 10 AM, I headed to the station to start my paperwork journey, only to realize I had forgotten my bank book and insurance documents. I had to turn back. On the way home, it started raining lightly, but that turned out to be a good thing since I could grab an umbrel...

Nha's Blog #20260411 A Conan Movie Night and a Surprisingly Endless Hotpot

A Conan Movie Night and a Surprisingly Endless Hotpot Một buổi xem Conan và nồi lẩu “ăn mãi không hết” Today is Saturday, and as a fan of Detective Conan, I couldn’t miss the latest movie release. I had booked my ticket a few days earlier, and as soon as it officially premiered, I headed straight to the cinema. I arrived about 15 minutes late due to buying popcorn and drinks, but that was intentional since movies usually start later after all the ads and warnings. Hôm nay là thứ Bảy, và là một fan của Thám tử lừng danh Conan nên tôi không thể bỏ qua phần phim mới nhất chiếu rạp. Tôi đã đặt vé từ vài ngày trước, và ngay khi phim chính thức công chiếu, tôi lập tức ra rạp. Tôi vào muộn khoảng 15 phút do mua bỏng và nước, nhưng đó là trong tính toán vì phim thường chiếu muộn sau hàng loạt quảng cáo. About five minutes after I sat down, the movie started. I won’t spoil anything, but I’ll share my thoughts. The title hints at a case happening on the road, and interestingly, the setting s...