Programmer Coding

Data type in python

What is Mutable?

Variable means something that can be changed . In Python, “mutability” is the ability to change the value of an object. These are usually objects that store data.

Object which are mutable are:

  • Lists
  • Dictionaries
  • Sets

 

  • What is Lists?

A list contains collection of any records kind: strings, ints, different lists. The matters inside a listing are generically called “elements”. not like strings, lists are “mutable” – they may be modified

Example
# Original Friends list
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha", "Birendra Yadev", "Yathartha Gaurav"]

print("Best Friend : ", friends)
Output

Best Friend :  [‘Ramkrishna Kumar’, ‘Shubham Kashyap’, ‘Vishnu Singh’, ‘Ayushi’, ‘Satyam Keshri’, ‘Mausam Mishra’, ‘Jitendra Shrama’, ‘Ankit Jha’, ‘Birendra Yadev’, ‘Yathartha Gaurav’]

Lists Methods:

  • append()
  • extend()
  • insert()
  • remove()
  • Pop()
  • index()
  • count()
  • sort()
  • reverse()

 

append()

The append() method is used to add an element to the end of a list.

Example
# Original Friends list

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Mausam Mishra"]

# Using append() to add a new friend

friends.append("Vikas Anand")

print(friends)
Output

[‘Ramkrishna Kumar’, ‘Shubham Kashyap’, ‘Vishnu Singh’, ‘Mausam Mishra’, ‘Vikas Anand’]

extend()

The extend() method is one of the three ways to add elements to a list; the other two being append() and insert()

Example
# Original Friends list

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh"]

# List of new friends to be added

new_friends = ["Akash Raz", "Ayushi"]

# Using extend() to add new friends

friends.extend(new_friends)

print(friends)
Output

[‘Ramkrishna Kumar’, ‘Shubham Kashyap’, ‘Vishnu Singh’, ‘Ayushi’, ‘Akash Raz’]

insert()

Just like append(), the insert() method is used to add stuff to a list. However, it isn’t limited only to the end of the list — it can add an element at any given position.

Example
# Original Friends list

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Mausam Mishra"]

# Using insert() to add a new friend at index 2

friends.insert(2, "Jitendra Sharma")

friends.insert(3, "Akash Raz")

print(friends)
Output

[‘Ramkrishna Kumar’, ‘Shubham Kashyap’, ‘Jitendra Sharma’, ‘Akash Raz’, ‘Vishnu Singh’, ‘Mausam Mishra’]

Pop()

The pop() method is used to remove an element at a given position, from a list, and return it.

# Original Friends list

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Satyam Keshri", "Jitendra Sharma", "Mausam Mishra"]

# Using pop() to remove and return the friend at index 1

removed_friend = friends.pop(2)

print(removed_friend)  # Output: Shubham Kashyap

print(friends)
Output

Vishnu Singh # removed

[‘Ramkrishna Kumar’, ‘Shubham Kashyap’, ‘Satyam Keshri’, ‘Jitendra Sharma’, ‘Mausam Mishra’]

index()

Returns the first appearance of a particular value.

Example
# Original Friends list

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Mausam Mishra", "Jitendra Sharma", "Akash Raz", "Vikas Anand", "Ayushi"]

# Using index() to find the index of a friend

index = friends.index("Mausam Mishra")

print("Your index Value is : ",index)
Output

Your index Value is :  3

count()

Returns the number of elements with the required value.

# Original Friends list

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Vishnu Singh", "Mausam Mishra"]

# Using count() to count the occurrences of a friend

count = friends.count("Vishnu Singh")

print(count)
Output

2

sort()

Sorts the list in ascending order. If reverse = True is specified, sorts in descending orde

Example
# Original Friends list

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi","Satyam Keshri","Mausam Mishra", "Jitendra Shrama", "Ankit Jha","Birendra Yadev", "Yathartha Gaurav"]

# Using sort() to sort the list in alphabetical order

friends.sort()

print(friends)
Output

[‘Ankit Jha’, ‘Ayushi’, ‘Birendra Yadev’, ‘Jitendra Shrama’, ‘Mausam Mishra’, ‘Ramkrishna Kumar’, ‘Satyam Keshri’, ‘Shubham Kashyap’, ‘Vishnu Singh’, ‘Yathartha Gaurav’]

reverse()

Reverses the order of the list

Example
# Original Friends list

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi","Satyam Keshri","Mausam Mishra", "Jitendra Shrama", "Ankit Jha","Birendra Yadev", "Yathartha Gaurav"]

# Using reverse() to reverse the list

friends.reverse()

print(friends)
Output

[‘Yathartha Gaurav’, ‘Birendra Yadev’, ‘Ankit Jha’, ‘Jitendra Shrama’, ‘Mausam Mishra’, ‘Satyam Keshri’, ‘Ayushi’, ‘Vishnu Singh’, ‘Shubham Kashyap’, ‘Ramkrishna Kumar’]

  • What is Dictionary?

Dictionaries are used to store data values in key:value pairs.

A dictionary is a collection which is ordered*, changeable and do not allow duplicates

Dictionary Methods:

  • keys()
  • values()
  • items()
  • get()
  • update()
  • copy()

keys():

Returns a view of all keys in the dictionary.

Example
#Original Friends list

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha", "Birendra Yadev", "Yathartha Gaurav"]

friends_dict = dict.fromkeys(friends,0)

print(friends_dict)
Output

{‘Ramkrishna Kumar’: 0, ‘Shubham Kashyap’: 0, ‘Vishnu Singh’: 0, ‘Ayushi’: 0, ‘Satyam Keshri’: 0, ‘Mausam Mishra’: 0, ‘Jitendra Shrama’: 0, ‘Ankit Jha’: 0, ‘Birendra Yadev’: 0, ‘Yathartha Gaurav’: 0}

values():

Returns a view of all values in the dictionary.

Example
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha", "Birendra Yadev", "Yathartha Gaurav"]

# Create a dictionary with default values set to 0

friends_dict = dict.fromkeys(friends, 0)

# Get a view of all values in the dictionary

values_view = friends_dict.values()

# Print the values view

print(values_view)
Output

dict_values([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

items():

Returns a view of all key-value pairs in the dictionary as tuples.

Example
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha", "Birendra Yadev", "Yathartha Gaurav"]

# Create a dictionary with default values set to 0

friends_dict = dict.fromkeys(friends, 0)

# Get a view of all key-value pairs

items_view = friends_dict.items()

# Print the items view

print(items_view)
Output

dict_items([(‘Ramkrishna Kumar’, 0), (‘Shubham Kashyap’, 0), (‘Vishnu Singh’, 0), (‘Ayushi’, 0), (‘Satyam Keshri’, 0), (‘Mausam Mishra’, 0), (‘Jitendra Shrama’, 0), (‘Ankit Jha’, 0), (‘Birendra Yadev’, 0), (‘Yathartha Gaurav’, 0)])

get():

Returns the value associated with a given key. If the key does not exist, returns a default value.

Example
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha", "Birendra Yadev", "Yathartha Gaurav"]

# Create a dictionary with default values set to 0

friends_dict = dict.fromkeys(friends, 0)

# Get the value associated with the key 'Ramkrishna Kumar'

value = friends_dict.get('Ramkrishna Kumar')

print(value)

# Trying to get the value for a non-existent key

value = friends_dict.get('Akash Raz', 'Not Found')

print(value)
Output

0

Not Found

update():

Updates the dictionary with key-value pairs from another dictionary or iterable.

Example
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha", "Birendra Yadev", "Yathartha Gaurav"]

# Create a dictionary with default values set to 0

friends_dict = dict.fromkeys(friends, 0)

# Create another dictionary with some updated values

updated_values = {'Ramkrishna Kumar': 5, 'Ayushi': 3, 'Mausam Mishra' : 8, 'Shubham Kashyap' : 7, 'Jitendra Sharma' : 2}

# Update friends_dict with key-value pairs from updated_values

friends_dict.update(updated_values)

# Print the updated friends_dict

print(friends_dict)
Output

{‘Ramkrishna Kumar’: 5, ‘Shubham Kashyap’: 7, ‘Vishnu Singh’: 0, ‘Ayushi’: 3, ‘Satyam Keshri’: 0, ‘Mausam Mishra’: 8, ‘Jitendra Shrama’: 0, ‘Ankit Jha’: 0, ‘Birendra Yadev’: 0, ‘Yathartha Gaurav’: 0, ‘Jitendra Sharma’: 2}

copy():

Returns a shallow copy of the dictionary.

Example
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha", "Birendra Yadev", "Yathartha Gaurav"]

# Create a dictionary with default values set to 0

friends_dict = dict.fromkeys(friends, 0)

# Create a shallow copy of the dictionary

friends_dict_copy = friends_dict.copy()

# Print the copied dictionary

print("Copied friends dictionary:",friends_dict_copy)
Output

Copied friends dictionary: {‘Ramkrishna Kumar’: 0, ‘Shubham Kashyap’: 0, ‘Vishnu Singh’: 0, ‘Ayushi’: 0, ‘Satyam Keshri’: 0, ‘Mausam Mishra’: 0, ‘Jitendra Shrama’: 0, ‘Ankit Jha’: 0, ‘Birendra Yadev’: 0, ‘Yathartha Gaurav’: 0}

  • What is Sets?

It is a collection of unique items.
It is used to eliminate duplicate items from a list.
Also, it supports set operations like union intersection and difference.

Example
# Original set of friends

friends = {"Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha"}

# Original set of friends before any modifications

print("Original set:", friends)
Output

Original set: {‘Satyam Keshri’, ‘Ramkrishna Kumar’, ‘Shubham Kashyap’, ‘Yathartha Gaurav’, ‘Vishnu Singh’, ‘Ayushi’, ‘Jitendra Shrama’, ‘Birendra Yadev’, ‘Ankit Jha’, ‘Mausam Mishra’}

 

Following are the methods that you can use on sets:

  1. add()
  2. update()
  3. len()
  4. remove()
  5. pop()

Add()

Use add() method to add one item to a set

Example
# Original set of friends

friends = {"Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha"}

friends.add("Birendra Yadev ")

print("After adding :", friends)
Output

After adding : {‘Ayushi’, ‘Satyam Keshri’, ‘Ankit Jha’, ‘Mausam Mishra’, ‘Vishnu Singh’, ‘Birendra Yadev ‘, ‘Jitendra Shrama’, ‘Shubham Kashyap’, ‘Ramkrishna Kumar’}

Update()

Use update() method to add more than one item

Example
# Original set of friends

friends = {"Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha"}

new_friends = {"Yathartha Gaurav", "Vaishnvee "}

friends.update(new_friends)

print("After updating with :", friends)
Output

After updating with : {‘Mausam Mishra’, ‘Satyam Keshri’, ‘Ankit Jha’, ‘Vishnu Singh’, ‘Vaishnvee ‘, ‘Ayushi’, ‘Yathartha Gaurav’, ‘Shubham Kashyap’, ‘Jitendra Shrama’, ‘Ramkrishna Kumar’}

Len()

Returns the number of elements in the set

Example
# Original set of friends

friends = {"Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha"}

number_of_friends = len(friends)

print("Number of friends:", number_of_friends)
Output

Number of friends: 8

remove()

Use remove() or discard() method to remove an item in a set

Example
# Original set of friends

friends = {"Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha"}

friends.remove("Ramkrishna Kumar")

print("After removing 'Ramkrishna Kumar':", friends)
Output

After removing ‘Ramkrishna Kumar’: {‘Ayushi’, ‘Ankit Jha’, ‘Shubham Kashyap’, ‘Mausam Mishra’, ‘Jitendra Shrama’, ‘Satyam Keshri’, ‘Vishnu Singh’}

Pop()

It removes a random element from the set and returns the removed element

# Original set of friends

friends = {"Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha"}

removed_friend = friends.pop()

print("Removed friend:", removed_friend)

print("After popping:", friends)
Output

Removed friend: Jitendra Shrama

After popping: {‘Satyam Keshri’, ‘Shubham Kashyap’, ‘Vishnu Singh’, ‘Ramkrishna Kumar’, ‘Ayushi’, ‘Mausam Mishra’, ‘Ankit Jha’}

 

What is Immutable?

Immutable is the whilst no change is feasible over the years. In Python, if the value of an item can not be modified over time, then it’s miles known as immutable. once created, the cost of those objects is everlasting.

Object which are Immutable are:
  • Numbers (Integer, Float, Booleans & Complex)
  • Strings
  • Tuples
  • Frozen Sets

Numbers:

Python has built-in support for storing and manipulating digital data (Python Numbers). In general, you will use code in almost every Python application. Obviously, all computer applications involve numbers. This tutorial discusses different types of Python numbers and their properties.

  • Integer

Represents whole numbers without any fractional part

Example

# Integer num_int = 10 print(num_int)

Output:

10

  • Float

Represents numbers with a decimal point or in exponential form.

Example
# Float

num_float = 3.14

print(num_float)
Output:

3.14

  • Booleans

Represents a binary value, either True or False, used for logical operations.

Example
# Boolean

is_active = True

print(is_active)
Output:

True

  • Complex

Represents complex numbers in the form of “real + imaginary” where the real and imaginary parts are floating-point numbers.

Example
# Complex

num_complex = 3 + 4j

print(num_complex)
Output:

(3+4j)

String:

Sequence of characters enclosed within quotes, immutable in Python.

# Example of a string
my_string = "programmercoding.com!"

print(my_string)
Output

programmercoding.com!

Tuple:

Ordered collection of elements, immutable and enclosed within parentheses.

# Example of a tuple
my_tuple = (1, 2, 3, 4, 5,5)

print(my_tuple)
Output

(1, 2, 3, 4, 5, 5)

frozen set:

Immutable version of sets, created using frozenset(), containing hashable

# Example of a frozen set
my_frozen_set = frozenset({1, 2, 3, 4, 5,5})

print(my_frozen_set)
Output

frozenset({1, 2, 3, 4, 5})

Mutable vs Immutable Data Types in Python

Mutable or immutable is the fancy phrase for explaining the belongings of data types of being able to get up to date after being initialized. The simple explanation is for that reason: A mutable item is one whose internal nation is changeable. on the opposite, once an immutable object in Python has been created, we cannot alternate it in any way.

2 thoughts on “Data type in python”

Leave a Comment

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

Scroll to Top