Sitemap
PratikMurlidharYadav

PratikMurlidharYadav

Python-Programming-Day3

Datatypes in Python

5 min readJun 15, 2023

Datatypes In Python:

Python sets the variable type based on the value that is assigned to it. In simple word datatype is type of value is hold by variable.
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. The interpreter implicitly binds the value with its type.

Consider the following example to define the values of different data types and checking its type.

a=10 
b="Hi Python"
c = 10.5
print(type(a))
print(ptype(b))
print(type(c))
Output:
<type 'int'>
<type 'str'>
<type 'float'>

TYPES OF DATATYPES:

1. FUNDAMENTAL DATATYPES

  1. Int, float, complex, Boolean, str all are fundamental data type.
  2. All fundamental data type are immutable(unchangeable).
  3. Once we create an object we can’t do any changes.
  4. If we want to do any changes then PVM first to check that is there any objects available with same required data if it is available, then ok otherwise new object will have created.
  5. Due to this memory management happen and this functionality really help to improve performance of program.

2. COLLECTION DATATYPE
List, tuples, set, frozen set, Dictionary all are collection datatype.

DATATYPES USE IN PYTHON:

1. Numeric Datatype:

In Python, numeric data type represents the data which has numeric value.

Integers — This value is represented by int class. It contains positive or negative whole numbers (without fraction or decimal). In Python there is no limit to how long an integer value can be. [Size of int is 4 byte]

Float — This value is represented by float class. It is a real number with floating point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation.

Complex Numbers — Complex number is represented by complex class. It is specified as (real part) + (imaginary part) j. For example — 2+3j.

# Python program to demonstrate numeric value

a = 5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
output:
Type of a: <class 'int'>
Type of b: <class 'float'>
Type of c: <class 'complex'>

2. Sequence Type:

In Python, sequence is the ordered collection of similar or different data types. Sequences allows to store multiple values in an organized and efficient fashion.

There are several sequence types in Python — String, List, Tuple

#Python Program for Creation of String

# Creating a String   
# with single Quotes
String1 = 'Welcome to the My World'
print("String with the use of Single Quotes: ")
print(String1)

for More Python check My #Github : https://github.com/yadavpratik/python-programs.git

2. List :
A list can be defined as a collection of values or items of different types. The items in the list are separated with the comma (,) and enclosed with the square brackets [].

Get PratikMurlidharYadav’s stories in your inbox

Join Medium for free to get updates from this writer.

feature of list :

  1. The list are ordered
  2. The objects of the list can access by index.
  3. Indexing always start from 0 up to last element present in list.
  4. The list are mutable type. mutable means we can add, remove, or change the elements present in list.
  5. The list are growable we can increase or decrease the list by adding or removing elements.
  6. The list can store all type element like numbers, strings, float value etc.
    List allows duplicate value.
#note:
Where to use list:
the list can be used in the scenario in which
we need to store the simple collection with no constant value.
where the elements can be changed.

Example: a = [1,2,”Peter”,4.50,”Ricky”,5,6]

List = ["pratik", 23, "yadav"]  
print("\nList containing multiple values: ")
print(List)
print(List[0])
print(List[2])
Output :
List containing multiple values:
['pratik', 23, 'yadav']
pratik
yadav

4. Tuple:
Python Tuple is used to store the sequence of immutable Python objects. The tuple is similar to lists since the value of the items stored in the list can be changed, whereas the tuple is immutable, and the value of the items stored in the tuple cannot be changed.

A tuple can be written as the collection of comma-separated (,) values enclosed with the small () brackets. The parentheses are optional but it is good practice to use.

feature of Tuple:

  1. Tuple are ordered.
  2. The element of the tuple can access by index.
  3. Indexing always start from 0 up to last element present in list.
  4. The tuple are immutable type. immutable means we can’t add, remove, or change the elements present in tuple.
  5. The tuple can store all type of element like numbers, strings, float value etc.
#note:   Where to use: 
the tuple can be used in the scenario in which
we need to store the simple collection with constant value.
where item can’t be changed.

Example: T1 = (101, “Peter”, 22)
In given example tuple is created with name T1. And elements in
small braces () separated by comma (,).

T1 = (101, "Peter", 22)    
T2 = ("Apple", "Banana", "Orange")
T3 = 10,20,30,40,50
print(type(T1))
print(type(T2))
print(type(T3))
Outout:
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>

5. Sets :
A Python set is the collection of the unordered items. Each element in the set must be unique, immutable, and the sets remove the duplicate elements. Sets are mutable which means we can modify it after its creation.

The set can be created by enclosing the comma-separated immutable items with the curly braces {}. Python also provides the set() method, which can be used to create the set by the passed sequence.

# Creating a set
my_set = {1, 2, 3, 4, 5}
print(my_set) # Output: {1, 2, 3, 4, 5}

# Creating a set using the set() constructor
my_set = set([1, 2, 3, 4, 5])
print(my_set) # Output: {1, 2, 3, 4, 5}

Features of sets :

  1. Sets are unordered.
  2. The element of the sets cannot access by index.
  3. The set are mutable type. immutable means we can’t add, remove, or change the elements present in tuple.
  4. The set can store all type of element like numbers, strings, float value etc.
  5. Set doesn’t allows duplicate object.

6. Dictionary:

In Python, a dictionary allows you to store and retrieve key-value pairs. It is also known as an associative array or a hash table in other programming languages.

example :

# Creating a dictionary
person = {
'name': 'John',
'age': 30,
'city': 'New York'
}
# Accessing values in a dictionary
print(person['name']) # Output: John
print(person['age']) # Output: 30

features of Dictionary:

  1. Dictionaries are mutable, we can modify item by adding, removing, or updating key-value pairs after they are created.
  2. Dictionaries are unordered collections of key-value pairs. Unlike sequences such as lists or tuples, dictionaries do not maintain any specific order for their elements.
  3. Each key in a dictionary must be unique. If you attempt to assign multiple values to the same key, the last assigned value will overwrite the previous ones.

check My Github profile for python Programs : https://github.com/yadavpratik/python-programs.git

Thanks For Reading!

--

--