Python Basics 2: Datatypes
Tags: python, programming, beginners, tutorial
Published: 2024-06-08 05:11:30

**Datatype:**
Every value in Python has a datatype. The datatype is mainly the category of the data. There are basically 5 categories of datatypes; however, these categories have further classifications as well. 

**_1.Numeric Type:_**
**a) Integer (int):** positive or negative whole numbers (without a fractional part). Example: 10,-3,10000

**b) Floating point (float):** Any real numbers with 'decimal' points or floating point representation.
Example: -3.14, 10.23

**c) Complex Number:** Combionation of real and imaginary number. Example: 2+3i [we don't use this that much]

**_2.Boolean Type(bool):_** Data with one of the two built-in values True or False. Often used in the comparison operations of logical statements.
Example: 

```
x=10
y=15
print(y>x)
#output: True
```

**_3.Sequence Type:_** A sequence is an **ordered** collection of similar or different data types.

**a)String(str):** It is a sequence of ordered characters (alphabets-lower case, upper case, numeric values, special symbols). String is represented with quotation marks (single('), double(''), triple(' ' ', '' '''')).
              	
For example: 
using single quotes ('python'), 
double quotes ("python") or 
triple quotes ('''python''' or """python""")

**b)List:**  It is an ordered collection of elements where the elements are separated with a comma (,)and enclosed within square brackets []. 

The list can have elements with more than one datatypes.

For example: 
i) list with only integers; x= [1, 2, 3] and

ii) a list with mixed data types; y= [110, "CSE110",12.4550, [], None]. Here, 110 is an integer, "CSE110" is a string, 12.4550 is a float, [] is a list, None is NoneType.

**c)Tuple:** It is an ordered collection of elements where the elements are separated with a comma (,) and enclosed within parenthesis().

Example: x= ("apple", "banana", Cherry")

List and Tuple are used to contain multiple values in a single variable.

**Basic difference between List and Tuple:** You can change the values of a list(mutable) but you can't do the same with tuple(immutable) [more about this in later series]

**_4) Mapping Type:_**

**Dictionary:** It's an **unordered** collection of data. It's written as key:value pair form.
Example:

```
 cardict= {'brand' : 'Lamborghini' , 'model': 'Aventader', 'year'=2018}
            #key        #value
 
 print (cardict['model'])
 #Output:Avantader

```
**_5)Nonetype(None):_**It refers to a null value or no value at all. It's a special data type with a single value, None.  


![Datatypes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtbwjveini7h9d6ncvwu.PNG)

**Type Function:**
Type() function returns the type of argument(object) passed as a parameter. It's mainly used for debugging(Code correcting) purpose. 
To know the type of a variable just write type(variable name)

```
text="Python is awesome"
print(type)
#output:<class 'str'>
```
Example:
type("Hello python")   #output: str
type(2024)             #output: int
type(3.14)             #output: float
type(True)             #output: bool
type(None)             #output: NoneType

**isinstance function:**
The isinstance() function checks if an object belongs to a specified class or data type.
In the code:

`isinstance(myfloat, float) `
	 
This checks if the variable myfloat is an instance of the float class. If it is, it returns True, otherwise False.

Similarly, for integers:
`isinstance(myint, int)` 
This checks if the variable myint is an instance of the int class.