Data Types in Python

By
Data Types

Simple word, type of data is called data types in python.

example:

Like, human data we can how to identify gender, simple male and female.

same male and female are data types of human data.

  • Data types are actually classes and variables are object of these classes or instance of the classes.

Python has the following standard data type:

 

 

=> Numbers

Integers, real number and complex numbers come under the number category.

They are defined as int, float and complex in python.

  • Python can use the type() function to which type a variable or a value belongs to.
  • The instance() function to check whether the object belong to a particular class.
Example for int:


a = 50
print(a,"is a type ", type(a))

Output:

50 is a type <class ‘int’>

Example for float:


a = 50.0
print(a,"is a type ", type(a))

Output:

50.0 is a type <class ‘float’>

Example for complex :


a = 15 + 25j
print(a, "is complex number? ",isinstance(a, complex))

Output:

(15+25j) is complex number? True

=> String

String is a sequence of Unicode  characters.

The user can use single quotes or double quotes to represent the strings.

Multi-line strings like multi line comment can be denoted using triple quotes.

String are immutable object in python.

Example of String:


s = 'Hello Python world!'
print("My String is : ", s)
print("My String is : ", s[1])
print("My String is : ", s[4:])
print("My String is : ", s[:5])

Output:

My String is : Hello Python world!
My String is : e
My String is : o Python world!
My String is : Hello

Here, s[4:] means return 5 position to at the end of the string.
while, s[:5] means returns starting position to 4 position of the string.

=> List

List is stored as an ordered sequence of items. these items are mutable.

List is one of the most frequently used data types in Python and is very flexible.

All item of the list have same data type.

Declaring a list is very easy and straight forward.

Items separated by commas are enclosed within square brackets[].

The square bracket is representing the list.

The index values are starting at 0.

Example of List:


a = [5, 10, 15, 20, 25, 30, 35, 40]
print("Display Second number : ", a[1])
print("Display fourth position to end of position number : ", a[3:])
print("Display Starting number to fourth position number  : ", a[:4])

Output:

Display Second number : 10
Display fourth position to end of position number : [20,  25,  30,  35,  40]
Display Starting number to fifth position number  : [5, 10, 15, 20]

=> Tuple

A Tuple is an ordered sequence of items, same as the list.

The main difference is that tuples are immutable.

Tuples once created cannot be modified.

Tuples collected data has different type of data. like integer, String etc.

Tuples are protecting the data and are usually faster than list, as it can not change dynamically.

It is defined with parentheses() where items are separated by commas.

The index values are starting at 0.

Example of  Tuple:


t = (5, 'Hello', 25j)
print("Print second position : ",t[1])
print("Print start to end postion data : ",t[0:])

Output:

Print second position : Hello
Print start to end position data : (5, ‘Hello’, 25j)

=> Set

The set is an unordered collection of unique elements.

A set is defined by values separated by commas inside curly braces {}.

Example of  Set:


a = {10, 2, 50, 1, 30, 40}
print("Our set data is : ",a)
print("a data type is : ", type(a))

Output:

Our set data is : {10, 2, 50, 1, 30, 40}
a data type is : <class ‘set’>

=> Dictionary

The Dictionary is an unordered collection of key-value pairs.

Generally python can use a huge amount of data. Dictionaries are optimized for retrieving data.

The user must know the key to retrieve the value.

The declaration of dictionary without any items, is written with just two curly braces{}.

Properties of Dictionary

There are two important points to remember about keys:

  1. Should not allow duplicate keys.
  2. Keys are an immutable object.
Example of  Dictionary:


dict = {'Name':'India','State':'Gujarat'}
print(type(dict))
print("Data is :",dict)
print("Key is : ",dict['Name'])

Output:

<class ‘dict’>
Data is : {‘Name’: ‘India’, ‘State’: ‘Gujarat’}
Key is : India

Leave a Comment

Your email address will not be published.

Latest Blog