Python Data Types

By | April 8, 2019

In this tutorial, we will discuss all Data Types used in Python.

Table of Content

  • Python Data Types
  • Numbers
  • String
  • List
  • Tuples
  • Dictionary
  • Sets
  • Datatype Conversion explicitly

Python Data Types

Python is a pure object-oriented programming language, which means that it is made up of Class and Objects, that you will learn later on this tutorial. For now, just to understand, it is defined as that Class is a structure of data which store information. To use that information we use objects and objects are the instance of a class. As in Python, everything is made up of class and we use their objects for the specific operations.

The data type is one of the class presents in Python to use them we take variables (objects) as their instance.

In General, if we define a data type, it is a kind of data item as defined by the values it can take.

To find the data type of an object or variable in the Python use the type() function.

E.g.

k=10
print(type(k))

#Output

<class ‘int’>

There are 6 types of Data types in Python.

  • Numbers
  • String
  • List
  • Tuples
  • Dictionary
  • Sets

Python Numbers

All the digits whether it is real or imaginary, all comes in the Python number data-types.

There are 3 types of Numbers in Python Data types: int, float, and complex numbers.

int

Every integer whether it is negative or positive comes in int category but it does not contain any decimal value.

e.g.

var = 10
print(type(var))

#Output

<class ‘int’>

Float

Every number which contains a decimal value even .0 is termed as the float.

e.g.

var=10.0
print(type(var))
<class 'float'>

Complex numbers

A number which has an additional imaginary (a+ib) number comes in the category of complex numbers.

e.g.

var=10+10j
print(type(var))
<class 'int'>

Python String

Everything which is written in between double style inverted comma (“ ”) or single inverted comma (‘) is considered as the string. It does not matter whether it is a word or digit or a special character except backslash (\) ( it is used to escape the character present in the string).

The variable stores the given string in a sequential manner and use indexing to call the specific value present in that variable.

Indexing is a method which is used to call the sequentially stored data in an efficient manner. When Python store data in sequential order, it gives each value an index number starts with 0 so we can easily grab the specific value.

There is a lot more about string which you will learn in the upcoming tutorials.

Let’s understand the string with an example:

srting_1="44"
string_2= "This is a String"
print(srting_1[0]) # Here we are using indexing
print(srting_1)
print(string_2)
print(string_2[0:6]) # Here we are using String slicing calling the string_2 values from 0 to 6

#Output

4
44

This is a String

This i

Python List

The list is an ordered collection of different data types which stored in sequential order. The string is mutable which means you can change the values present in a list. To assign a list we use square brackets and to separate the values we use a comma (,).

List store the values in sequential order so it uses indexing to call the specific value present inside the list. The indexing in the list start with 0 and end one less than the total number of values present in the list.

Let’s understand it with an example:

#Output [1, 2, 3, 4, 5, 6, ‘Hello’, ‘world’, True] 1 2 [1, 20, 3, 4, 5, 6, ‘Hello’, ‘world’, True]

Python Tuple

Like the Python list, Python tuple is also a collection of different data types which stores the value in the sequential order and use the index to grab the specific values present in the tuple. The only difference between the tuple and list is tuple are immutable and use parenthesis instead of square brackets. The immutability of tuple signifies that once the values assigned inside the tuple parenthesis cannot be altered using the assignment operator. e.g.

tuple_1= (1,2,3,"tuple_1","is", "here")
print(tuple_1)
print(tuple_1[3]) # tuple indexing
print(type(tuple_1))

#Output

(1, 2, 3, ‘tuple_1’, ‘is’, ‘here’)
tuple_1
<class ‘tuple’>

Python Dictionary

A Python dictionary is an unordered collection of different data types with keys and values pairs. The Python dictionary does not store the values in sequential order so we use keys to grab the corresponding values.

To store a dictionary in a variable we use {} curly braces and like Python list dictionaries are mutable.

e.g.

dictionary = {"key1": "value1", "key2":"value2", "key3":"value3", "key4":"value4"}
print(dictionary["key1"])
print(dictionary["key2"])
dictionary["key4"] = 4 # dictionaries are mutable
print(dictionary)

#Output

value1
value2
{‘key1’: ‘value1’, ‘key2’: ‘value2’, ‘key3’: ‘value3’, ‘key4’: 4}

Python Sets

Python sets are an unordered collection of different or unique data-types and in the set, you can not assign two repetitive value, if you do so the set itself delete the other one. Sets do not use indexing.

e.g.

set_1={1,2,3,4,5,6,6,6,6,6,6,6,7,}
print(set_1)

#Output

{1, 2, 3, 4, 5, 6, 7}

In the above example, the set deletes all the repetitive 6 and show only one.

Python Data Conversion

In Python, you get the special function to change the data type of the variable explicitly. When you assign or declare a variable it automatically assigns a data type to the variable according to the assigned value, but with the help of some function, you can change the data type of the declared variable.

Here are the following conversions that can be performed in Python.

From integer to float

You can change an integer to a float using the float() function.

print(float(10))

#Output

10.0

From float to integer using int() function

print(int(10.2653))

#Output

10

From a integer or float to string using str() function

print(str(400.002))

#Output

‘400.002’

From string to a float and integer using float() and int() functions

k=”400″

print(int(k))

#Output

400
print(float(k))

#Output

400.0

From a string to a list using list() function

say="Hello world"
list(say)

#Output

[‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘w’, ‘o’, ‘r’, ‘l’, ‘d’]

Python Tutorials


Introduction

Flow Control

Functions

Datatypes

File Handling

Object & Class

Additional Tutorials

 

Leave a Reply

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