Python tutorial for Beginners - Learn Python on Your own - tutorial-2. Variable and Datatypes

In this blog we will go through the topics on variable and datatypes in python and how to use them..

Variable and datatypes.

In this blog, we will learn variable and datatypes in python and how to use them. So, lets get started.

As we know print is the python built-in function which just shows the output on the screen [ value given between the brackets ( ) ] on to the screen.

So we are able to do the same thing using a variable.

Using of variable:

pycharmprint min

In above code the we have used print() twice. Firstly, The value to be output is given directly in between the brackets Secondly, The value is assign to message and then given to print. One thing to observe is the both the methods give same output.

So the concept of variable in python is we create a small storage memory and store there the value and address of storage is name as variable i.e, 'message in above code is variable' .

To get clear idea, Lets compare variable with a book. The variable is like an index ( page no ) of the content and the value ( data ) of the variable is the actually content. Or you can say variable is empty box with its name where we can store value in it. Or Variable is alias name of the the data. Here we have given the data 'Hello world, My name is masi' a alias name as 'message'

message = 'Hello world'

And '=' is use to assign value to the variable.

So to create variable you need to give name of variable followed by '=' sign and the value to assign to variable.

Syntax: . variable_name = data .

Assign data from right side of '=' to left side.

Benefit of using variable is it can be used repeatedly.

message = 'Hello world'



C:\Users\Masi\AppData\Local\Programs\Python\Python38-32\python.exe "C:/Users/Masi/Desktop/python tutorials/"
hello world
hello world
hello world

Process finished with exit code 0

As you can observe, we have store the value in message and reused it.

variable data can be updated. Example:

message = "First value of variable"

message = "Second value of a variable"          #changing the value of variable.

second_message = message                        #assign value to variable using variable.


First value of variable
Second value of a variable
Second value of a variable

The value of variable can be changable and is updated in above example. Also we can assign data to variable using another variable.

Try to play around with code. It will increase your understanding about the concepts. Changes you do can be seen on the console output.


Comments are very important in your programs. They are used to tell you what something does in English, and they also are used to disable parts of your program if you need to remove them temporarily. Here’s how you use comments in Python:

1 # A comment, this is so you can read your program later.
2 # Anything after the # is ignored by python.
4 print "I could have code like this." # and the comment after is ignored
6 # You can also use a comment to "disable" or comment out a piece of code:
7 # print "This won't run."
9 print "This will run."


I could have code like this.
This will run.

As you can see any code after # in line is not excuted.see the print code is not excuted which is having #. We mainly use comments to give an explaination of code. Its is consider as a better coding standard.

Common Error:

message = "First value of variable"


C:\Users\Masi\AppData\Local\Programs\Python\Python38-32\python.exe "C:/Users/Masi/Desktop/python tutorials/"
Traceback (most recent call last):
  File "C:/Users/Masi/Desktop/python tutorials/", line 2, in <module>
NameError: name 'mesage' is not defined

Process finished with exit code 1
See in above code the variable name is message but in print the value given is mesage which is not defined or used. So its throwing error. To fix it just change the mesage in print to message.

Also if you were using the ide ( pycharm ) it would have notified you. And also when ever you get this kind of error. Just read the message carefully to understand the reason of this kind of behaviour.

In programming, value of the variable is known as 'data'.

Naming a variable rules:

  • Variable names can contain only letters, numbers, and underscores.They can start with a letter or an underscore, but not with a number.For instance, you can call a variable message_1 but not 1_message.
  • Spaces are not allowed in variable names, but underscores can be used to separate words in variable names. For example, first_message works,but first message will cause errors.
  • Avoid using Python keywords and function names as variable names;that is, do not use words that Python has reserved for a particular programmatic purpose, such as the word print. “Python Keywords and Built-in Functions should not be used”
  • Variable names should be short but descriptive. For example, name is better than n, student_name is better than s_n, and name_length is better than length_of_persons_name.
  • Be careful when using the lowercase and the uppercase letter because they are consider as different in python. MESSAGE is diffent than message.


Variable are storage for the data and data can be of different types. We mainly describe type of data store in the variable as datatype and they are mainly

  1. Numbers
  2. Strings
  3. List
  4. Dictionarys
  5. Sets
  6. Tuples
  7. Booleans
  8. Objects
  9. Files
  10. Funtions
  11. Modules
  12. Classes
  13. Types.etc..,

Dont get panic. We will see all of them step by step in an organised manner. Also the syntax for creating different datatypes is same.


Data like integers, floats ,complex numbers, decimal numbers , fractions etc., comes under the Number datatype.

See the examples to understand

For intergers:

# assigning integer value to varaible
value1 = 1006026

# the integer value can also be assign to varaible
# using int() built-in funtion.
value2 = int(86658)



For Float or decimal values:

# assign float value to variable
value3 = 10.2568

# the float value can also be assign to varaible
# using float() built-in funtion.
value4 = float(5.698)



For Complex numbers

# assign complex number values to variable
value6 = (5+7j)

# assign complex number value to variable using complex() built-in function.
value5 = complex(4,3)

In above examples int(), float(), complex() are built in python functions. You can understand the functionality of them by examples.

Basic operations between variables:

1. Addition i.e, x + y
x = 5
y = 6

# adding the variables 


2. subtracting i.e, x - y
x =12
y = 7

# subtracting the variables 


3. multiply i.e, x * y
x =12
y = 7

# multiply the variables 
print(x * y)


4. divide i.e, x / y
x =12
y = 6

# divide the variables 
print(x / y)


5.reminder x % y
x = 121
y = 10

# reminder of division the variables 
print(x % y)



given operations performed for int type can be performed for float and complex.

Operations between the different types of variables is allowed
x = 1554            # int
y = 25.689          # float

# Dividing the interger value with float is allowed 
print(x / y)



you can add, subtract, Multiply and divide between float, int and complex numbers.

z = (4+3j)
t = 25.689

# adding the complex value with float is 
print(z + t)




You can find the type of the number using the built in function type()

z = (4+3j)
t = 25.689
x = 5896

# getting the type of value using type()


<class 'complex'>
<class 'float'>
<class 'int'>

Also you can change the data from integer to float or vice-versa or from integer to complex,etc., Using the int(), float(), complex() built in functions.

x = 4586

y = float(x)

z = 783216.84562486

t = int(z)


<class 'int'>
<class 'float'>
<class 'float'>
<class 'int'>

So that it in this tutorial. In next tutorial we will learn about strings and how to use and manipulate them.

Thanks 😃

Previous Tutorial

Next Tutorial

Did you like it? Why don't you try also...

What is Http

In this blog we will learn about what is http and how its used on internet.

Python tutorial for Beginners - Learn Python on Your own - tutorial-3. Strings and Booleans

In this blog we will go through the topics on boalean and strings in python and how to use them..

Python tutorial for Beginners - Learn Python on Your own - tutorial-1. Introduction

Learn step by step the basics of the Python for newbies.