Akratv

Lesson 7 โ€“ Functions and Scope in Python

๐Ÿ“˜ Introduction

The current image has no alternative text. The file name is: aKIRA.jpg

In Python, lists, tuples, and sets are three important ways to store collections of items. Each has unique features and is used in different scenarios.
Understanding these structures is crucial for managing data effectively.


๐Ÿ”น 1. Lists in Python

  • A list is an ordered collection that can hold multiple items.
  • Lists are mutable (you can change them after creation).

Example:

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

# Adding an item
fruits.append("orange")
print(fruits)         # ['apple', 'banana', 'cherry', 'orange']

# Changing an item
fruits[1] = "mango"
print(fruits)         # ['apple', 'mango', 'cherry', 'orange']

๐Ÿ”น 2. Tuples in Python

  • A tuple is an ordered collection like a list, but it is immutable (cannot be changed after creation).
  • Use tuples when you want data to stay constant.

Example:

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

# Trying to change a value will cause an error
# colors[0] = "yellow" โŒ Not allowed

๐Ÿ”น 3. Sets in Python

  • A set is an unordered collection of unique items.
  • Sets automatically remove duplicates.

Example:

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

# Adding items
numbers.add(6)
print(numbers)        # {1, 2, 3, 4, 5, 6}

# Removing items
numbers.remove(3)
print(numbers)        # {1, 2, 4, 5, 6}

๐Ÿ”น 4. Key Differences

FeatureListTupleSet
Orderedโœ… Yesโœ… YesโŒ No
Mutableโœ… YesโŒ Noโœ… Yes
Duplicatesโœ… Allowedโœ… AllowedโŒ Not Allowed
Use CaseGeneral collectionsFixed dataUnique items collection

โœ… Summary

  • Lists are flexible, ordered, and changeable.
  • Tuples are ordered but fixed (immutable).
  • Sets are unordered and unique.

โฌ‡๏ธ Download Akratvุชุญู…ูŠู„ โฌ‡๏ธ Download Akratv ุชุญู…ูŠู„ Server 2 โฌ‡๏ธ Download Akratv ุชุญู…ูŠู„ Server 3

Related Posts

Eagle

Lesson 9: Dictionaries in Depth 1. Introduction to Dictionaries Dictionaries are one of the most powerful and flexible data structures in Python.They store data as key-value pairs and are extremely…

Read more

NovaPlayer

Lesson 8: Lists, Tuples, and Sets in Python 1. What is a Dictionary in Python? A dictionary in Python is a collection of key-value pairs. ๐Ÿ“Œ Example: 2. Creating Dictionaries…

Read more

FivG_Pro

๐Ÿ”ฐ What Is a Function? A function is a reusable block of code that performs a specific task.Instead of repeating code, we can write it once and call it whenever…

Read more

Show7_Pro

๐ŸŸข Lesson 6 โ€“ Intermediate Level ๐Ÿ”ฐ What Is a Module? A module is simply a Python file (.py) containing code (functions, variables, classes) you can reuse in other programs….

Read more

GoldiptvPro

๐ŸŸข Lesson 5 โ€“ Intermediate Level ๐Ÿ”ฐ What Are Exceptions? An exception is an error that occurs during the execution of a program.If not handled, it will stop the program…

Read more

Domingo

๐ŸŸข Lesson 4 ๐Ÿ”ฐ What Is OOP? Object-Oriented Programming (OOP) is a programming style where you organize code into objects that contain: Python supports OOP using: ๐Ÿงฑ Example: Why Use…

Read more