Hướng dẫn python variable exercises
❮ Previous Next ❯
Nội dung chính
Test Yourself With Exercises
Now you have learned a lot about variables, and how to use them in Python.
Are you ready for a test?
Try to insert the missing part to make the code work as expected:
Create a variable named
Go to the Exercise section and test all of our Python Variable Exercises:
Python Variable Exercises
❮ Previous Next ❯
Python Variable Exercises
Let’s check out some exercises that will help you understand Variables in Python better.
Below is a good example of mixing numbers and text inside the print() function
Assign: 3 to variable glass_of_water.
You can simply assign a number to the variable glass_of_water by typing it on the left side of the equal sign and the number on the right side of it.
Number on the right side doesn’t have to be in quotes.
Let's try to see what happens after assigning a new value to our variable. Note that program gets executed line by line.
Place the variable: glass_of_water inside the print function and observe what happens.
Alternatively, you can use “
You can read more about Python Operators here.
Python Variables (A fundamental concept of coding)
Variables contain data. So, you can call them later. This makes data reusable, recallable, reassignable etc.
Before we advance with additional functions and lessons, understanding data types will help you grasp the programming logic.
But even before that, it’s crucial to understand variables. Programmers coming from other languages would already know, but if you’re a newbie programmer you may not yet have had the chance to truly understand variables. In that case this lesson will help you.
Provided by HolyPython.com
Simplest definition of a variable is a “named container” of data you’d like to refer to in your program. There are 2 main reasons you may want to do this.
1) While assigning a value, variable is always located on the left of the equation and data on the right of it.
2) You can start a variable name with a letter or underscore (_)
3) But if you want to print a variable this will have to be without quotes: print(mytext), more on this later.
1) You can't start a variable name with a number.
2) You can't use symbols (except underscore) in a variable name.
3)You can't include spaces in your variable name.
Let’s say you have a big number you’re working with at hand: 149,597,870. This number denotes the average distance between sun and our planet earth in kilometers.
Let’s say you’re planning to refer to this number in quite a few calculations. Instead of typing it every time you can instead assign it to a variable and just refer to the variable itself. Take a look:
line 1 >>> sun_to_earth = 149
Our data could be way longer than this example. It could have 100 more lines. You can see that assigning it to a variable and giving it a name makes everything much more practical. This is a creation process. You can congratulate yourself.
* Let’s try to understand the code above step by step.
** Equal sign has a more appropriate name in this case which is assignment operator as that’s exactly what it does.
1- As in the example above, variable name is always located to the left of the equation during the assignment.
So, the variable goes on the left side and value being assigned goes on the right side.
2- Choosing a variable name is a somewhat strategic decision in Python (and programming in general) and it’s wise to give it some thought since you might refer to this name a ton of times in the future.
3- Practical naming conventions:
>>> dogsName = “d’Artagnan”
>>> print(“His name is ” , dogsName , “.” , ” He is a ” , dogsKind, “.”)
His name is d’Artagnan. He is a Saint Bernard.
Could you catch what’s happening in this example? We are printing some values directly inside the print function while we are also printing other values through variables which were assigned priorly.
Although we will see more about different data types in Python in next lesson you might want to make sure variable usage in this example is clear. You can take your time and also check out the Python variable exercises we have prepared for you.
Also variable names are case sensitive as you’ve probably noticed.
Advanced Concepts (Optional)
1- You can also assign multiple variables in one line in Python.
Here is a variable example with print function:
>>> i, j, k = “Hello”, 55, 21.0765
>>> print(i, j, k)
This is a perfect Python example as it bridges first three Python lessons. In first lesson we have seen the print function and here we are printing three Python variables at the same time.
Here data seems like text, number and number with decimals. In next lesson we will see how Python handles these different data types specifically. We will name them string, integer and float. But it’s more than just naming. They have their own Python methods and own way of existing.
“Have you installed Python yet?”
Having Python setup on your computer can expedite the learning experience and help you gain the real life skills in a non-simulation environment. Installing Python can have different nuances that can be confusing for everyone. We compiled a complete tutorial about the best and most convenient Python installing practices Check it out by clicking the button below.