Data Types In Python
Last updated
Last updated
What is Data Type?
In computer science and computer programming, a data type or simply type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data. or in more simpler terms , Data Type tells what type of Data it is.
Variables and Data Types:
Variables can hold values, and every value has a data-type. Python is a dynamically typed language; hence we do not need to define the type of the variable while declaring it.
A variable can hold different types of values. For example, a person's name must be stored as a string whereas its id must be stored as an integer.
Python provides various standard data types that define the storage method on each of them. The data types defined in Python are given below.
Numbers : Integer(int) , Float(float)
Strings (str)
Boolean (bool)
Tuples (tuple)
Lists (list)
Sets (set)
Dictionary (dict)
In this lecture we will be covering Data Types related to Numbers and Strings and Boolean.
Type function tells what type of data a value holds.
In [1]:
Out[1]:
In [2]:
Out[2]:
We have two kinds of data type under Numbers : Integer (int) and Float (float).
Integer data types are the type of data which holds positive and negative non decimal values. For Example - 1,2,3,-1,-123,-4789765,0
In [3]:
Out[3]:
Float data types are the type of data which holds positive and negative decimal values. For Example - 1.0,2.1,3.5,-1.6,-123.56,-4789765.0,0.001
In [16]:
Out[16]:
The string can be defined as the sequence of characters represented in the quotation marks. In Python, we can use single, double, or triple quotes to define a string.
or in more simpler terms, anything written under single double or triple quotes are strings. For Example -'Console Flare',"Training",'''Python''','1.2',"2.33",'''4''','!@#$^&'
In [19]:
Boolean type provides two values, True and False. These values are used to determine the given statement true or false.True can be represented by any non-zero value or 'T' whereas false can be represented by the 0 or 'F'. Consider the following example.
or in more simpler terms, boolean has only two values either True or False.
Note: True and False should always be written with Capital T and F.
In [20]:
Out[20]:
Store an integer value in a variable name age and find its type using type function.
Store a number in string form so that the data type of the number is string
Does the data type of a variable change when the value inside the variable changes?