I like to be a creative variable rather than sticking to a dogma. -anonymous.
Hello Pythonistas🙋♀️, welcome back. In the previous two posts, we worked with numbers🔢 in python and you also by now have a rough idea of strings.
Before diving deeper into these and other data types any further let’s first talk about variables in python.
Trust me it’s much, I can say 10,000 times easier🥱 to work with variables in python than in any other language.
Keep reading📖 as at the end I will also tell you the biggest mistake❗ I have done with using variables that can save you years of hard work📝.
Contents
Previous post’s challenge’s solution
Before we get started here’s the solution for the previous post’s challenge:
-7
1+1*(4-12)
- First, 4-12 would be solved giving answer -8 and the new expression as:
1+1*(-8)
- Then, one would be multiplied to -8 giving answer -8 and the new expression as:
1-8
- Finally, 1 will be added to -8 giving our final answer as:
-7
Let’s get our hands dirty with variables. I am super excited🤩.
What does a variable look like in python?
Variables in python or in any programming language are containers🥛 that store some value which can be an integer, a floating-point number, a string, or any other data type.
However, when it comes to python, they are a bit🤏 different from the other languages as discussed below. For example:
website = "python.org"
Here, 'website'
is a variable that stores the value 'python.org'
.
Anywhere in the program when you will write✍ website
it would be taken as 'python.org'
.
You can use website
without quotation marks, if you put a quotation mark, it would be considered a string and not a variable so be careful ❗⚠.
'='
the assignment operator is used to give the value'python.org'
to the variable website
.
How are they different in python compared to other languages?
Variables in python are labels, not containers
Do you remember🤔 I just said that variables in python are quite different?
This is because in python they act as labels🏷. When you say a = 10
. a
is not a container storing value 10
. It is a label pointing out the integer 10
already present in the memory.
Python Auto-Detects🧐 data type of variables
In any other programming language, we have to define the data type of variables before creating them.
But python does this work for you👍.
It defines the type of the variable itself. For example, you can simply write:
a = 10
print(a)
print(type(a))
Output:-
10
<class 'int'>
You didn’t need to tell🗣 python that you are assigning an integer to a
it automatically detects this.
(type is a function used to know the data type of an object. If you don’t know what’s an object click here.)
Also, you can easily change the value of a variable like this:-
a = "Hello"
print(a)
print(type(a))
Output:-
Hello
<class 'str'>
Multiple Assignment
Python gives you the superpower💪 of multiple assignment.
This means you can assign/give more than one value to more than one variable at the same time.
Interesting, isn’t it?
This is how:-
website1, website2, website3 = "python.org", "python-hub.com", "python-hub.com/blogs"
print(website1)
print(website2)
print(website3)
Output:-
python.org
python-hub.com
python-hub.com/blogs
Another way for multiple assignment is:
a=b=c =10
print(a)
print(b)
print(c)
Output:-
10
10
10
Ok so let’s move on to see 👀 what are some characters that python doesn’t allow you to use while naming variables.
Invalid variables in python and variable naming rules
Python doesn’t allow you to take any name for your variable.
This is because there are a lot of words and characters reserved in python for specific uses.
For example:-
True
and False
for boolean.
is
, is not
, in
, and not in
as operators. And so on…
Because of these, there are a few options unavailable for you to be used.
Let’s see👀 what are the rules📑 you need to follow:-
- You cannot use numbers at the start meaning a variable name cannot be —>
'1python'
. - You are free to use all the alphabets from a-z and A-Z.
- Numbers can also be freely used. But, not in the beginning. Like a variable can be —->
'python1'
or'p1ython'
- Special characters like @#$%^&*()- or any other are also not allowed.
- You can use an underscore
'_'
anywhere in the variable. - There must be no use of white spaces.
- You cannot also use any sort of reserved words.
This is it, no more rules to bore🥱 you.
Now let’s see👀 how to declare a variable that has no value.
How to declare variables in Python without value?
You might wonder🤔 why do I need to create a variable without value?
Well, for now just understand how to, why to will be clear when you will make projects.
It is pretty simple to do just use the keyword None. Remember, no inverted comma and first ‘N’ capital. See this example.
var = None
print(type(var)) #<class 'NoneType'>
You can also do this by giving an empty string. For example,
str1 = ""
print(str1) #
That’s all for the topic now let’s move on to the major mistake that I made.
One major mistake while using variables
A very big mistake❗ that I made while using variables due to which all the projects I made in the first two years of my python journey are useless to me right now is, naming variables as ‘a’, ‘b’,…
I can’t stress how big this mistake is.
It might seem like a piece of simple or small🤏 advice. But, if you follow it then, it will make your programming journey much faster and smoother.
I hope this helps you and makes your coding journey super fun and fantastic.
Conclusion
After reading this post check if you are able to answer the following:-
- What are variables in python?
- What makes them different in python?
- How to assign multiple variables?
- What can you not use while naming them?
- How can you create one without value?
- And lastly, one practice that you should follow while using them.
Hope this helped you.
Challenge🧗♀️
You know what’s a variable now. You also have a brief idea of strings.
Your challenge is to figure out anyone way in which you can use the given variable in the given string.
string = "I am python and I am _ years old"
variable is age = 31
Go fast. I am waiting. Comment your answers below.
This was it for the post. Comment below suggestions if there and tell me whether you liked it or not. Do consider sharing this with your friends.
See you in the next post till then have a great time.😊
To read the next post click here.