Python Data Types – Free Python Tutorials

Today we will be learning more about python data types. In the earlier classes, we have covered the topics like the features of Python, it’s advantages and limitations, areas of application and other basic concepts like identifiers and reserved words in Python programming language. Python data types are more or less similar to data types in other languages with slight changes. It is also a dynamically typed programming language.


Dynamically Typed Programming Language- 

Compared to other programming languages in Python one need not declare the data type of the variables beforehand and it will be assigned automatically. Due to this feature Python is called a Dynamically Typed Programming Language.

For example-
>>>num= 10

In other languages like C, C++ or Java when we declare the variable of a certain data-type it cannot be changed to another type effortlessly.

For example in Java-
int num=10;
num=true;

This program on being executed will give out an error as incompatible types- boolean cannot be converted to int. 

Whereas in Python-

>>>num= 10 #declaring variable num
>>>type(num) #checking its data type
<class ‘int’>
>>>num

Output : 10


Example Program –

>>>num= True #declaring variable with boolean type
>>>type(num)

Output : <class ‘bool’>

In the above program first the num variable is of type int and then is then changed to boolean by assigning it a boolean value. This is possible in Python and yields no errors.


The various data types in Python are:-

  1. int
  2. float
  3. complex
  4. bool
  5. Str
  6. bytes
  7. bytearray
  8. range
  9. list
  10. tuple
  11. set
  12. frozenset
  13. dict
  14. None

These are the predefined data types in Python.


1. int-

The int or integer data type is used to hold integral values i.e. whole numbers without a decimal point. There are 4 number systems in which we can declare the int values. They are:

* Decimal form– We simply write the number itself.
Example: num= 10

* Binary form – We write the 0b/0B followed by the number.
Example: num= 0b10

* Octal form – We write the 0o/0O followed by the number.
Example: num= 0o10

* Hexadecimal form– We write the 0x/0X followed by the number.
Example: num= 0x10

Note :- In python there is no maximum range of digits that a data type can have unlike other languages like Java, C, C++ etc. As every thing is an object in python and thus the terminologies of range and size do not apply in python. 


Base Conversions:

Using these three functions we can convert the numbers from one base to another:-

  • bin()
  • oct()
  • hex()

We can just put the number from any base in the function to convert it into the required base.

For example:- >>>bin(2) #here we enter the whole number 2 to convert into binary

Output:- 0b10 #output in binary form

For converting into decimal explicit conversion is not required as the number is stored in decimal format.

Note :- In Python2 we have the long data type to hold integer values whereas in Python3 we don’t.


2. float –

It is used to represent numbers with floating points.

Example-
>>>flo=321.765 #declaring a float variable
>>>type(flo) 

Output: <class 'float'>

Note : For float data type we cannot declare the variables as binary, octal or hexadecimal as it will be an invalid syntax.

Example:-
>>>num= 0B32.657 #we assign a 

Output:SyntaxError: invalid syntax

  • The float data type can also exponential form.

Example:

#declaring a number in exponential type
>>> num= 4.2e2
#printing num
>>> num

Output:420


3. complex- 

The complex type is used to represent the numbers with an imaginary part. It is written as a+bj.

Where a= real part, b= imaginary part and j= square root of -1

Example:-

>>> num= 2+3j #declaring variable num of complex type
>>>type(num)

Output:<class ‘complex’>

Note: Imaginary part only accepts decimal values whereas real part can have binary, octal or even hexadecimal form.

Example:-

>>> num= 0b10+3j
#here the number is declared with real part as binary
  • We can carry out arithmetic operations on complex numbers like addition, subtraction. etc.
  • To print just the real part of the complex number we can use the inbuilt attribute a.real
  • To print just the imaginary part of the complex number we can use the inbuilt attribute a.imag

4. bool  (boolean)- 

True and False are the values which are of a boolean type.

Example:-

>>> flag= True 
>>> type(flag)
#printing the type of variable flag

Output: <class ‘bool’>

Note:- T in True and F in False needs to be in capitals.

Since True is considered to be 1 and False is treated as 0 we can have outputs like-

Example:>>>True+True
Output: 2

Example:>>>True+False
Output:1

Example:>>> print(True/False)
Output:1.0


5. str (String)-

A string is a sequence of characters which can be declared inside ‘ ‘or “ “.

Multi-line string literals are declared within triple quotes.

Example:-

>>>name= ‘Tannavi’ #declaring a variable 'name'
>>>type(name)

Output:<class ‘str’>

Note:- For multiple line string we have declared it within triple single quotes or triple double quotes.

Example:

>>>name= ‘’’Tannavi
            Gaurav’’’

This is all about the data types in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *