Datatype & Variables

Variables are named locations which are used to store references to the object stored in memory. The names we choose for variables and functions are commonly known as Identifiers. In python Identifiers must obey the following rules.

  1. All identifiers must start with letter or underscore ( _ ) , you can’t use digits. For e.g my_var  is valid identifier while 1digit  is not.
  2. Identifiers can contain letters, digits and underscores ( _  ).
  3. They can be of any length.
  4. Identifier can’t be a keyword (keywords are reserved words that Python uses for special purpose).Following are Keywords in python 3.

Assigning Values to Variables

Values are basic things that programs works with. For e.g 1 , 11 , 3.14 , "hello"  are all values. In programming terminology they are also commonly known as literals. Literals can be of different type for e.g 1 , 11  are of type int , 3.14  is float and "hello"  is string . Remember in python everything is object even basic data types like int, float, string, we will elaborate more on this in later chapters.

In python you don’t need to declare types of variable ahead of time. Interpreter automatically detects the type of the variable by the data it contains. To assign value to a variable equal sign ( = ) is used. =  is also known as assignment operator.

Following are some examples of variable declaration:

Note: In the above code x  stores reference to the 100  ( which is an int object ) , x  don’t store 100 itself.

In Python comments are preceded by a pound sign ( # ). Comments are not programming statements that python interpreter executes while running the program. Comments are used by programmers to remind themselves how the program works. They are also used to write program documentation.

Simultaneous Assignments

Python allow simultaneous assignment syntax like this:

this statements tells the python to evaluate all the expression on the right and assign them to the corresponding variables on the left. Simultaneous Assignments is helpful to swap values of two variables. For e.g

Expected Output:

Python Data Types

Python has 5 standard data types namely.

a) Numbers
b) String
c)  List
d) Tuple
e) Dictionary
f)  Boolean – In Python True and False  are boolean literals.  But the following values are also considered as false.

  • 0 – zero , 0.0 ,
  • [] – empty list , () – empty tuple , {} – empty dictionary ,  ”
  • None


Receiving input from Console

input()  function is used to receive input from the console.

Syntax:  input([prompt]) -> string

input()  function accepts an optional string argument called prompt  and returns a string.

Note that input()  returns string even if you enter a number, to convert it to an integer you can use int() or eval() .

Importing modules

Python organizes codes using module. Python comes with many in built modules ready to use for e.g there is a math  module for mathematical related functions, re  module for regular expression and so on. But before you can use them you need to import them using the following syntax:

You can also import multiple module using the following syntax:

here is an example

First line import all functions, classes, variables, constant in the math  module. To access anything inside math module we first need to write module name followed by ( . ) and then name of class, function, constant or variable. In the above example we are accessing a constant called pi  in math  module

In next chapter we will cover numbers in python.

Leave a Reply

6 Comments on "Datatype & Variables"

Notify of

Sort by:   newest | oldest | most voted
1 year 2 months ago

I’ve started with python 2.0 on code so this is a bit new to me some bits just don’t make sense. :(

1 year 4 months ago

In variables example, its mentioned:
a = b = c = 100 # this statement assign 200 to c, b and a.

I think it(200 instead of 100) is typo mistake..

1 year 4 months ago

In the last example, yo don’t actually change the value. You just assign x value to x and y to y.
So the output is: