Hey guys, this is the third tutorial of the Python for Beginners Series. If you haven’t checked out our previous tutorials, first go and read them (especially Tokens in Python) because we have discussed keywords and identifiers in those tutorials that you should know before learning about variables.
What's in this tutorial?
What are variables?
Variable is a name that points to a location where your data is stored. In simple language, the variable holds your data. You can assign a value to a variable using = sign with the variable on left and value on the right.
Consider this example,
age = 10
In this example, age is a variable that points to the memory location where the data i.e., 10 is stored. Now whenever you want to use 10, you can also use variable age.
For example, if you do this
print(age)
The output will be 10
Also, it is important to know that the data inside the variable can be changed whenever you need. For example, you could store a value 20 in the same variable.
age = 20
Now the variable age holds value 20. The old value 10 is now replaced by the new value 20.
You can store all sorts of data in a variable like
name = "Coding Ground"
the variable name holds the string value Coding Ground.
It is important to remember that variables do not store values they are the pointers. They give reference to the memory location where the data is stored.
Before we dive any deeper, let us discuss the different type of data supported in python.
DataTypes
A datatype is simply the attribute of data that tells the interpreter what type of data it is going to process.
There are multiple data types supported in Python. Also, you can check the type of data using the type() function.
Numeric Data
The numeric data type includes integers, floating-point numbers, and complex numbers.
They are used as int, float, and complex. Consider the examples below,
Integers
These are the whole numbers which also include negative numbers. For example, 0,1,2,3,150, -200 etc are all integers. They don’t have a fraction part.
a = 10
print(type(a))
It will return
<class 'int'>
Floating Point Numbers
Floating Point numbers have a decimal part in them.
b = 10.2
print(type(b))
The output will be
<class 'float'>
Complex Numbers
These numbers are in the form of a + bj. They are as <real part> + <imaginary part>j
c = -1 + 5j
print(type(c))
The Output will be
<class 'complex'>
Now you see class because the data is an object or instance of a class.
For example, 10 is an instance of int class.
If you don’t understand it then don’t panic, We will be discussing classes and objects in future lectures.
Sequence
Sequences include lists, tuples and strings.
Basically every data type in which slicing can be done falls under this category.
Lists
Python lists contains elements within square brackets [ ].
a = ["Coding Ground",1,1.2,True]
print(type(a))
The Output would be
<class 'list'>
In the above example, the variable a holds a list of 4 elements, each of different data type.
Now each element of list has an index value and it starts from 0.
For example, the index value of the element “Coding Ground” in the above code will be 0.
We will discuss indexing and slicing in future tutorials.
Tuples
Tuples contain elements in round brackets or parenthesis ( ).
An example of a tuple would be
a = ("Coding Ground",1,1.2,True)
print(type(a))
The output would be
<class 'tuple'>
Now you might be wondering, what is the difference between list and tuple?
The answer is that Lists are mutable while tuples are not. That means you can change the elements of a list without replacing the whole variable value but you can not do that with tuple. You can not perform operations on them.
String
Anything within single or double quotes in Python is a string.
a = "Coding Ground"
print(type(a))
The output would be
<class 'str'>
Boolean
Boolean on values can only be either True or False.
a = True
print(type(a))
The Output will be
<class 'bool'>
Remember that True is a keyword and T is capital.
Mapping
There are 2 types of Data Types that fall under this category. These are Dictionary and Sets.
Dictionary
Dictionary is an unordered collection of key-value pairs. It uses curly braces.
If you’ve ever worked with JSON data then you must be familiar with Dictionary.
a = {'name' : "Coding Ground"}
print(type(a))
The Output will be
<class 'dict'>
Dictionaries provide a little more readability as compared to lists and tuples. This is because of the key-value pair. And this is the reason they are used when we have to work with a large amount of data.
Sets
Like dictionaries, Sets are also unordered collections with curly braces. But the difference is that they contain only unique values.
a = {1,2,3,4,5,1,2,3}
print(a)
print(type(a))
The output would be
{1, 2, 3, 4, 5}
<class 'set'>
Here, you can clearly see that we got only unique values as the output.
We cannot filter data from sets as sets are unordered pair. We can however use the classic set functions like union, intersection etc on two set values.
Now that we have discussed the data types, lets continue where we left the variables.
Type Casting or Type Conversion
Now that you have learnt about the different data types and how to assign a value to a variable. It is time to learn how you can convert the data type of some data stored in a variable.
Consider this example,
a = 10
print(a)
print(type(a))
a = float(a)
print(a)
print(type(a))
The output will be
10
<class 'int'>
10.0
<class 'float'>
As you can see we have just converted an integer value to a float one. But remember that you can not do this with all data. You can only do this with data having similar data type.
Now this is all common sense, I don’t need to explain this. If you are thinking how about converting complex to int or float, it won’t work because complex got j in it which isn’t used in any int value or float value.
But you can convert string into a list.
a = "Coding Ground"
a = list(a)
print(a)
print(type(a))
The output will be
['C', 'o', 'd', 'i', 'n', 'g', ' ', 'G', 'r', 'o', 'u', 'n', 'd']
<class 'list'>
So, guys, this is it for today. I hope you like it. If you have any problems or if anything is left unclear then tell us in the comment section below.
Happy Coding!
References
You can read more about additional data types at official python documentation