
Play Store Application link – Java to Python in 17 Steps – App on Google Play
Github project link – https://github.com/kuldeep101990/Python_step5
Data structures are essential for managing and organizing data efficiently in programming. Python offers built-in data structures like lists, tuples, sets, dictionaries, and strings. If you’re coming from Java, understanding these structures with their comparisons will make your transition smoother.
Lists: Flexible Arrays
A list in Python is a versatile data structure, equivalent to Java’s ArrayList
, but it supports operations like slicing, dynamic resizing, and mixed data types.
Java Example (ArrayList):
import java.util.*;
public class ListExample {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
System.out.println(list);
}
}
Python Equivalent:
my_list = [10, 20]
print(my_list)
Common List Operations:
my_list.append(30) # Add element
my_list.remove(20) # Remove element
print(my_list[0]) # Access by index
print(my_list[1:3]) # Slicing (sublist)
Real-world analogy: A list is like a to-do list where you can add, remove, or reorder tasks as needed.
Tuples: Immutable Sequences
Tuples are similar to lists but cannot be modified (immutable). In Java, you can think of tuples as final
arrays or using third-party libraries like Apache Commons.
Python Example:
coordinates = (10, 20)
print(coordinates[0])
Real-world analogy: Tuples are like a pair of shoes; you can’t split them but can use them as a unit.
Sets: Unordered Unique Collections
Python sets are similar to Java’s HashSet
. They ensure uniqueness and support operations like union and intersection.
Java Example (HashSet):
import java.util.*;
public class SetExample {
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
set.add(10);
set.add(20);
System.out.println(set);
}
}
Python Equivalent:
my_set = {10, 20}
print(my_set)
Common Set Operations:
my_set.add(30) # Add element
my_set.remove(20) # Remove element
print(my_set.union({40, 50})) # Union
print(my_set.intersection({10, 30})) # Intersection
Real-world analogy: Sets are like a guest list where duplicate names are not allowed.
Dictionaries: Key-Value Pairs
Dictionaries in Python are similar to Java’s Map
interface.
Java Example (HashMap):
import java.util.*;
public class MapExample {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("Alice", 25);
map.put("Bob", 30);
System.out.println(map);
}
}
Python Equivalent:
my_dict = {"Alice": 25, "Bob": 30}
print(my_dict)
Common Dictionary Operations:
my_dict["Charlie"] = 35 # Add or update key-value pair
print(my_dict["Alice"]) # Access value by key
my_dict.pop("Bob") # Remove key-value pair
print(my_dict.keys()) # Get all keys
print(my_dict.values()) # Get all values
Real-world analogy: A dictionary is like a phone book where you look up names (keys) to find phone numbers (values).
Strings: Text Management
Strings in Python, like Java, are immutable. However, Python’s string handling is simpler with built-in methods and slicing.
Java Example:
public class StringExample {
public static void main(String[] args) {
String text = "Hello";
System.out.println(text.length());
System.out.println(text.substring(0, 2));
}
}
Python Equivalent:
text = "Hello"
print(len(text)) # Length of string
print(text[:2]) # Slicing
Common String Operations:
text = "Python"
print(text.lower()) # Convert to lowercase
print(text.upper()) # Convert to uppercase
print(text.replace("P", "J")) # Replace characters
Real-world analogy: Strings in Python are like pre-printed labels—you can create new ones but not modify the originals.
Complete Python Program
Here’s a program demonstrating these data structures:
# Python Data Structures Example
def main():
# List Example
my_list = [10, 20, 30]
my_list.append(40)
print("List:", my_list)
# Tuple Example
coordinates = (10, 20)
print("Tuple:", coordinates)
# Set Example
my_set = {10, 20, 30}
my_set.add(40)
print("Set:", my_set)
# Dictionary Example
my_dict = {"Alice": 25, "Bob": 30}
my_dict["Charlie"] = 35
print("Dictionary:", my_dict)
# String Example
text = "Hello, Python!"
print("String:", text.upper())
if __name__ == "__main__":
main()
How to Run:
- Save the code in a file named
data_structures_demo.py
. - Run it using the terminal:
python data_structures_demo.py
Sample Output:
List: [10, 20, 30, 40]
Tuple: (10, 20)
Set: {40, 10, 20, 30}
Dictionary: {'Alice': 25, 'Bob': 30, 'Charlie': 35}
String: HELLO, PYTHON!
This blog post gives you a practical understanding of Python’s data structures with real-world analogies and code examples. Experiment with these concepts to get hands-on experience!