This is the second of many articles on Python Fundamentals for Everyone, a python tutorial series which focuses on python fundamentals.
You can refer to previous article written in this series below.
Python Fundamentals for Everyone — Introduction to Python
This is the first of many articles on Python Fundamentals for Everyone, a python tutorial series which focuses on…
At the end of this article you will have knowledge on what are variables in python, how are they assigned and the basic operations the can be performed by various operators in python.
Without much ado, let’s just dive into it!
So, what is a variable?
Just like how we have a store room in our house where we store things, in any programming language (Python, C, C++), variable is a storage location where some value is stored.
In other words, variable refers to a storage location which holds or stores some value.
The value stored inside a variable can be accessed or updated later.
A value may contain similar or different type of values ie., it may contain numeric, alphabetic, alphanumeric or even special symbols.
>>> str = “Hello World”
So here, ‘str’ is a variable which contains alphabetic values. In programming terms, ‘str’ is called as the variable name which holds string value. Therefore, we say that, ‘str’ is of datatype string.
>>> num = 2
Similarly, ‘num’ is a variable name which holds numeric value, hence, ‘num’ is of datatype integer.
>>> pi = 3.14
And ‘pi’ is a variable which contains floating point (decimal values) so, ‘pi’ is of datatype float.
Python supports built-in function ‘type()’ which takes values as arguments and returns it’s data type.
Built-in functions are basically already existing chunk of code that are present inside the python interpreter and is always available.
For instance, when you want to print something, you just write >>>print(‘hello world’) and hello world is printed.
But what actually happens is that, a chunk of code present in the interpreter, is executed when ever print() is called.
Can I give any name to the variable as I wish? Well, python variables have some name rules.
- Python is a case sensitive language which means that a variable Student and student are treated as different
- A python variable must begin with either letters (a-z or A-Z) or underscore(_)
- Reserved names cannot be used as a variable name, for example ‘if’ cannot be used as a variable name because ‘if’ is a reserved word which is used to check conditions.
The following code snippet shows some examples of valid and invalid variable names in python.
By now, from the above examples you must be familiar with how the variables are assigned values, ie., <variable name> =<expression>
For example, str = ‘hello’
Python also supports multiple assignments these are also called as chain assignments. In case multiple variables hold a single value then it can be assigned like this: x=y=z=1
Here, x, y and z are all assigned a single value 1.
Well, well done on sticking till the end of this article! ;)
Now you know what are variables and how to declare them and have an understanding on built-in functions.
In the next article we will look at Operators and Operands.