If you’ve been figuring out how to learn python coding, then you’ve come to the right place. Python is a great language for beginners since it has a very smooth learning curve. It is easy to read and write, compared to more complex languages like Java or C++. Despite its simplicity, it can be integrated with more complex languages like C++, which is why the potential of what it can do in the right hands is immense and top companies are desperately looking for talented and skilled python developers.
You may have often heard a programmer say, “Once you understand the logic behind coding, every language is the same, only it’s syntax will change”, this holds for python as well. So, I encourage you to spend as much time as possible, while learning the basics. When I use the word basics, I am referring to fundamental concepts such as variables, data types, data structure, functions, loops etc. Concepts such as these are universal to any language that has ever been created. Once you understand these, learning the syntax of a new language shouldn’t take you more than a week.
In this article of ‘how to learn python programming’, we will try to understand what variables are in python. If you don’t have a firm understanding of variables, going ahead with your journey will be difficult and frankly quite pointless, so please take your time with this.
What is a variable in programming?
A variable is a container which can store one or multiple values. Any time we need to access the values stored inside a variable; we make a call to the variable by using its name. A variable can hold almost any data type from numbers to strings to dataframes. Every creation of a variable takes of memory space of your computer. The more values stored in it, the larger memory space it acquires. We know from our experiences of running out of data that nobody likes large software since it takes up a lot of space on our device. So, a good coder always uses his variables sparingly only when necessary. You should try to avoid creating arbitrary variables.
Some call it creating a variable, some call it declaring a variable, however, the syntax to do it in python is extremely simple. All we need is a unique identifier and an assignment operator(=). The syntax is shown below:
Syntax:
Identifier = value
The part on the left side of the assignment operator is the name of the variable and the part on its right side is the value being stored. Here is an example of creating and displaying variable below:
Code:
A = 2
Pizza = ’Round’
Color1 = ‘Blue’
print(A)
print(Pizza)
print(Color1)
Output:
2
Round
Blue
In the above code, we’ve created three variables A, Pizza and Color1. We then use the print statement to show the values stored within them.
Here are some rules you need to keep in mind while declaring a variable:
- The names of a variable must begin with a letter or an underscore
Eg.
Blue = ‘Blue’
_blue_ = ‘Blue_underscore’
In this example, Blue and _blue_ are the variables which are created
2. The name of the variable cant be a reserved keyword in python
Eg.
For = 2
This would throw an error because ‘For’ is a reserved keyword in python.
3. You can use numbers in the name of a variable as long as it’s not at the start.
Eg
Blue1 = ‘One’
Blue2 = ’two’
Here two variables Blue1 and Blue2 are created
4. Variable names are case-sensitive.
Eg.
Blue = 5
blue = 6
In this case, Blue and blue are two separate variables holding different values.
Unlike many of the old languages where we had to explicitly declare the data type of a variable, we don’t need to do that in python, since it can do the job automatically as it is an interpreter. Based on the value you pass to the variable, it determines the data type of the variable. If you pass it a number then it will choose ‘int’ or ‘float’ as its data type if you pass an alphabet or word, then ‘String’ will be chosen by the interpreter.
There are mainly 2 major types of variables in python which has been divided, which is based on the scope of their accessibility
A. Global Variables
These are variables that are created outside of a function in python. The point of this is that you can call the variable from anywhere in your code. Every function has unlimited access to the variable. You can also use the global keyword to create a global variable inside a function
Code:
A= 2
Def demo(y):
global x = 5
Print(y)
demo(A)
print(x)
Output:
2
5
In the above code, we see that a variable ‘A’ has been created and since it is outside the function demo, we can say it’s a global variable. We pass its value to the demo function and we get an output that displays the value stored in it, which is 2, whereas, the print statement displays the global variable that is declared inside the demo function using the ‘global’ keyword.
B. Local Variables
These are variables that are created inside a function. One can only access these variables within the scope of the function inside which they are created. Calling as local variable from outside its function will cause the program to crash.
Code:
A= 2
Def demo(y):
B = 3
Print(B)
demo(A)
Output:
3
In the above code, B is a local variable since it has been created inside the function demo. Since we’re Printing the local variable ‘B’ the output will be 3, even though we’ve passed A as a parameter to the function. If we try to access B from outside the function, python will throw an error.Now that you have some understanding when it comes to variables, which is a building block of programming, you can start experimenting with these concepts that you have learnt since the best way to learn python is by practising a little bit every day.