Python Data Types and Collections

Python

In Python, data types are the building blocks of any program. They determine the kind of data that can be stored and the operations that can be performed on that data. Let's break down the common data types in Python in simple terms.

Numbers

Numbers in Python are used to store numeric values. There are three main types.

  • Integers: Whole numbers, e.g., a = 10
  • Floating Points: Numbers with decimals, e.g., a = 10.0
  • Complex Numbers: Numbers with a real and imaginary part, e.g., a = complex(10, 5), which represents 10+5j.

Strings

Strings are sequences of characters, like words or sentences. In Python, you can create a string by enclosing the text in either single quotes (') or double quotes (").

str = 'computer science'
print(str)  # Outputs: computer science

You can also access specific characters or parts of the string by using indexing.

Boolean

Booleans in Python represent values that are either True or False. They are used to check conditions and control the flow of a program. For example, you can compare two values, and the result will be either True or False.

str = "comp sc"
b = str.isupper()  # Checks if all characters in the string are uppercase
print(b)  
# Outputs: False

Lists

Lists in Python are collections of items that can hold different data types, such as numbers, strings, and more. They are ordered, meaning each item has a specific position (or index), and are created using square brackets []. Lists are mutable, so you can easily add, remove, or change items after the list is created. Items in a list are stored in the order they were added.

list = [6, 9]
list[0] = 55  # Changes the first item in the list to 55
print(list)  
# Outputs: [55, 9]

Lists can contain integers, strings, or even other lists.

Tuples

Tuples in Python are similar to lists, but they have one key difference: they are immutable, meaning once a tuple is created, its elements cannot be changed. Tuples are useful when you need to store a collection of items that should not be modified. They are created using parentheses (), and like lists, they can hold different types of data. Each item in a tuple has an index, and the order of elements is preserved.

tup = (66, 99)
print(tup)  
# Outputs: (66, 99)

If you try to modify a tuple, Python will throw an error.

Sets

A set is an unordered collection of unique items. In other words, a set cannot have duplicate elements.

Sets in Python are unordered collections of unique items, meaning they don't allow duplicate elements. Created with curly braces {} or the set() function, sets have no indexes and their order isn't fixed. You can add or remove items, but the order of elements may change.

set1 = {11, 22, 33, 22}
print(set1)  
# Outputs: {33, 11, 22}

Even though '22' is added twice, it only appears once in the set.

Dictionaries

Dictionaries in Python store data as key-value pairs. Each key is unique and maps to a value. Created with curly braces {}, they allow you to add, remove, or change pairs, and you access values using their keys.

dict = {'subject': 'comp sc', 'class': '11'}
print(dict)  
# Outputs: {'subject': 'comp sc', 'class': '11'}

Dictionaries are very versatile and allow for efficient data lookups using keys.

Python Lists: A Deeper Look

Lists in Python are very powerful because they can store multiple types of data and can be easily modified. Some key operations are,

Creating a List

To create a list in Python, use square brackets [] and separate the items with commas.

List = ['Baibhav']
print(List)  
# Outputs: ['Baibhav']

You can also create lists with multiple values, mixed data types, or even nested lists.

Adding Elements

To add elements to a list in Python, you can use the append() method to add a single item to the end of the list or the extend() method to add multiple items.

List = [1, 2, 3]
List.append(4)
print(List)  
# Outputs: [1, 2, 3, 4]

Removing Elements

To remove elements from a list in Python, you can use remove(), pop(), or slicing.

List.remove(2)
print(List)  
# Outputs: [1, 3, 4]

Python Dictionaries: An Overview

Python dictionaries are data structures that store collections of key-value pairs. Each key in a dictionary is unique and maps to a specific value, allowing for efficient retrieval of data. Keys must be immutable types (such as strings, numbers, or tuples), while values can be of any data type. They provide fast lookups, additions, and modifications of items but do not maintain any particular order of elements.

Creating and Adding Items

Dict = {1: 'Python', 2: 'Programming'}
Dict[3] = 'Language'
print(Dict)  
# Outputs: {1: 'Python', 2: 'Programming', 3: 'Language'}

Accessing Items

You can access values by their keys.

print(Dict[1])  
# Outputs: Python

Removing Items

You can delete items from a dictionary using del, pop(), or clear() methods.

Conclusion

Python offers a rich set of data types and collection types like lists, tuples, sets, and dictionaries. Each of these types has unique features that make them suitable for different use cases in programming. Understanding these types is crucial for writing efficient and effective Python code.


Similar Articles