Sitemap

Data Structures in Python

Python Made Easy - (Article 9) Telugu Explanation

Press enter or click to view image in full size

What are Data Structures?

Data Structures ante - data ni store, manage, access cheyyadaniki use chese containers.

Example:
Imagine nenu oka cupboard lo items pettaali anukunta -

  • 1st shelf lo books,
  • 2nd shelf lo clothes,
  • 3rd shelf lo tools.

Ippudu cupboard lo anni neatly arrange ayyi untayi - whenever needed, manam easy ga item find cheyyachu.

Get D Naresh’s stories in your inbox

Join Medium for free to get updates from this writer.

Alage Python lo kooda, data ni store cheyyadaniki different containers unnayi:
➡ List - items order lo untayi, change cheyyachu.
➡ Tuple - order untundi, kaani change cheyyalem.
➡ Set - duplicates undavu, order guarantee ledu.
➡ Dictionary - key:value pairs lo data untundi (like name: “Naresh”, age: 25).

Types of Data Structures in Python

Press enter or click to view image in full size

1. List

Press enter or click to view image in full size

List ante - multiple values ni [square brackets] lo store chestam.
Example:

fruits = ["apple", "banana", "cherry"]
print(fruits)

Output:

['apple', 'banana', 'cherry']

Accessing List Items (Indexing)

print(fruits[0])  # apple
print(fruits[1]) # banana

Negative Indexing

print(fruits[-1])  # cherry
print(fruits[-2]) # banana

Slicing (Range of Items)

print(fruits[0:2])

Output:

['apple', 'banana']
Press enter or click to view image in full size

Changing Values

fruits[1] = "mango"
print(fruits)

Output:

['apple', 'mango', 'cherry']

Adding Items

fruits.append("orange")
print(fruits)

Output:

['apple', 'mango', 'cherry', 'orange']

Removing Items

fruits.remove("mango")
print(fruits)

Loop Through List

for item in fruits:
print(item)

Example Mini Program

Print all even numbers from a list

numbers = [1, 2, 3, 4, 5, 6]
for n in numbers:
if n % 2 == 0:
print(n)

Output:

2
4
6

2. Tuple

Press enter or click to view image in full size

Tuple ante - unchangeable list.
Use (round brackets).

colors = ("red", "green", "blue")
print(colors)

Output:

('red', 'green', 'blue')

Access Tuple Items

print(colors[0])  # red

You can’t change or add items in tuple.

Example

student = ("Naresh", 25, "Python")
for item in student:
print(item)

Output:

Naresh
25
Python

3. Set

Press enter or click to view image in full size

Set ante - unique values collection.
Use {curly braces}.
Duplicates automatically remove avutayi.

nums = {1, 2, 3, 3, 4, 5}
print(nums)

Output:

{1, 2, 3, 4, 5}

Add / Remove Items

nums.add(6)
nums.remove(2)
print(nums)

Loop Through Set

for n in nums:
print(n)

Set Operations (Union, Intersection)

a = {1, 2, 3}
b = {3, 4, 5}
print(a | b)  # Union
print(a & b) # Intersection

Output:

{1, 2, 3, 4, 5}
{3}

4. Dictionary

Press enter or click to view image in full size

Dictionary ante - key: value pairs collection.
Use {curly braces} with : between key and value.

person = {"name": "Naresh", "age": 25, "city": "Hyderabad"}
print(person)

Output:

{'name': 'Naresh', 'age': 25, 'city': 'Hyderabad'}

Access Values

print(person["name"])

Output:

Naresh

Add / Update Values

person["email"] = "naresh@example.com"
person["age"] = 26
print(person)

Remove Items

person.pop("city")
print(person)

Loop Through Dictionary

for key, value in person.items():
print(key, ":", value)

Output:

name : Naresh
age : 26
email : naresh@example.com

String Operations (Bonus)

Strings kuda chinna data structure laaga untundi.
Text ni " " lo store chestam.

Press enter or click to view image in full size
name = "Naresh"
print(name[0]) # N
print(name[1:4]) # are
print(name.lower()) # naresh
print(name.upper()) # NARESH
print(len(name)) # 6

Loop Through String

for ch in name:
print(ch)

Check Substring

print("resh" in name)  # True
print("xyz" not in name) # True

Example Mini Program

text = "python is easy"
count = 0
for ch in text:
if ch == 'a':
count += 1
print("Number of 'a's:", count)

Output:

Number of 'a's: 1

Key Takeaways

Press enter or click to view image in full size

Practice Questions

  1. Create a list of your 5 favorite movies and print only the first 3.
  2. Create a tuple of student details (name, age, grade).
  3. Create a set of fruits and perform union and intersection.
  4. Create a dictionary of 3 friends and their favorite colors.
  5. Write a program to count vowels in a string.

I have added more methods and detailed explanations.

Click here 👉 To explore the complete structured guide on Python Data Structures.

Previous & Next

Next Article: 👉10. Strings and String Operations in Python
Previous Article: 👉8. Star Patterns and Logical Programs