๐ง Why Python Programming for Data Science?
Python is one of the most loved languages for data science because:
- Itโs simple and readable, even for beginners
- It has a massive library ecosystem (NumPy, pandas, matplotlib)
- It allows interactive coding through tools like Jupyter Notebooks
- It has a huge global community that supports and contributes to its development
Want to learn about Data Science in detail? Follow this tutorial.
๐ค Python Basics Refresher (Before Jumping to Data Science)
- Dynamic Typing โ You donโt need to declare variable types.
x = 10 # Python knows x is an integer
- Indentation โ Python uses indentation instead of curly braces to structure code.
if x > 5: print("Greater than 5")
- Simple Syntax โ Clean and easy to write/read.
name = "Kunal" print("Hello", name)
๐ง Must-Know Libraries for Data Science
Library | Purpose |
---|---|
NumPy | Numerical computing, arrays |
pandas | Data manipulation and analysis |
matplotlib | Basic data visualization |
seaborn | Statistical visualizations (built on matplotlib) |
scikit-learn | Machine learning |
TensorFlow , PyTorch | Deep learning frameworks |
Statsmodels | Statistical testing and modeling |
XGBoost | High-performance gradient boosting |
๐งช Why JupyterLab and Anaconda?
In case you don’t have Anaconda installed so watch this tutorial below that will guide you on how to install Anaconda on Windows and Mac both:
Tool | Why Use It? |
---|---|
Jupyter Notebook | Lets you write and run Python code in chunks. Ideal for learning and testing. |
JupyterLab | More advanced version with multi-panel layout. Ideal for full projects. |
Anaconda | One-click installer that gives Python + all major data science libraries. |
โ You can visualize graphs, see outputs below code cells, and even export your work to PDF or HTML.
๐ผ Real-World Companies Using Python
Company | Use Case |
---|---|
Netflix | Content recommendations and streaming optimization |
Spotify | Music recommendations using machine learning |
Scalable backend development | |
Dropbox | Cloud storage with reliable Python infrastructure |
Uber | Real-time pricing and ride-matching using data science |
๐งโ๐ซ Final Summary for You:
“Python is the backbone of modern data science. Its simplicity, flexibility, and huge support ecosystem make it the go-to tool for analyzing data, building models, and creating powerful applications.”
๐ Python Basics: Variables, Data Types & Typecasting (For Data Science Beginners)
When learning Python for Data Science, mastering variables, data types, and typecasting is essential. Hereโs a beginner-friendly guide to help you understand it clearly.
โ What Are Variables?
Think of variables like labeled containers that store data. You donโt need to declare the type in advanceโPython figures it out on its own.
name = "Alice"
age = 25
is_student = True
name
holds a string (text)age
holds a numberis_student
holds a boolean (True/False)
๐ Common Python Data Types
Type | Example | Meaning |
---|---|---|
int | 10, -5 | Integer (whole) numbers |
float | 3.14, -0.5 | Decimal numbers |
str | “hello” | Text (string) |
bool | True, False | Boolean values (yes/no, on/off) |
list | [1, 2, 3] | Ordered, editable collection |
tuple | (1, 2, 3) | Ordered, unchangeable collection |
dict | {“a”: 1} | Key-value pairs |
๐ What is Typecasting?
Typecasting means converting one data type into another using Pythonโs built-in functions.
Example Conversions:
x = int("10") # "10" โ 10 (string to int)
y = str(25) # 25 โ "25" (int to string)
z = int(3.9) # 3.9 โ 3 (float to int, truncates not rounds)
lst = list("abc") # "abc" โ ['a', 'b', 'c'] (string to list)
โ ๏ธ Be careful: not all conversions work.
("hello") # โ Will raise a ValueError
๐ก Quick Tips:
- Use
type(variable)
to check the data type. - Always ensure compatibility before typecasting to avoid errors.
๐งต String & String Methods in Python โ Beginnerโs Guide
In Python, strings are one of the most commonly used data types, especially in data science, web scraping, automation, and more. This lesson dives into what strings are and the most useful string methods for beginners.
๐ค What is a String?
A string is a sequence of characters enclosed in:
- Single quotes โ
'Hello'
- Double quotes โ
"Hello"
Example:
name = "Alice"
greeting = 'Hello'
๐ Multiline Strings
To write strings that span multiple lines, use triple quotes:
message = """This is
a multiline
string."""
๐งฎ String Indexing and Slicing
- Python indexes start from 0
- You can access individual characters or a slice (subset) of a string
Example:
text = "Python"
text[0] # 'P'
text[-1] # 'n'
text[0:2] # 'Py'
text[:3] # 'Pyt'
text[3:] # 'hon'
๐ซ String Immutability
Strings in Python are immutable. This means once created, they canโt be changed.
Wrong:
text[0] = 'J' # โ This will throw an error
If you want to “change” a string, create a new one.
๐งฐ Common String Methods
Method | Description |
---|---|
str.lower() | Converts to lowercase |
str.upper() | Converts to uppercase |
str.strip() | Removes whitespace |
str.replace(old, new) | Replaces text |
str.split(sep) | Splits string into list |
str.join(list) | Joins list into string |
str.find(sub) | Finds first index of substring |
str.count(sub) | Counts occurrences of substring |
str.startswith() | Checks if string starts with value |
str.endswith() | Checks if string ends with value |
str.isdigit() | Checks if all chars are digits |
str.isalpha() | Checks if all chars are alphabets |
str.isalnum() | Checks if all chars are alphanumeric |
๐งช Examples
"hello".upper() # 'HELLO'
" Hello ".strip() # 'Hello'
"hello world".split() # ['hello', 'world']
"-".join(["2025", "04", "14"]) # '2025-04-14'
"python".find("th") # 2
๐ง String Formatting (f-Strings)
Use f-strings to create dynamic messages easily:
name = "Alice"
age = 30
f"Hello, {name}. You are {age} years old."
# 'Hello, Alice. You are 30 years old.'
๐ก Quick Recap:
- Strings are powerful text containers.
- They support indexing, slicing, and immutability.
- Use string methods to clean, search, or format data.
- f-Strings make it super easy to embed variables inside text.
๐ What Are Operators in python programming for data science?
Operators are special symbols used to perform operations on variables and values in Python. They help you manipulate data and make decisions in your code.
1๏ธโฃ Arithmetic Operators
Used for basic mathematical calculations.
Operator | Description | Example (a=10, b=5) | Output |
---|---|---|---|
+ | Addition | a + b | 15 |
- | Subtraction | a - b | 5 |
* | Multiplication | a * b | 50 |
/ | Division | a / b | 2.0 |
// | Floor Division | a // b | 2 |
% | Modulus (remainder) | a % b | 0 |
** | Exponentiation | a ** b | 100000 |
2๏ธโฃ Comparison Operators
Compare values and return True
or False
.
Operator | Description | Example (a=10, b=5) | Output |
---|---|---|---|
== | Equal to | a == b | False |
!= | Not equal to | a != b | True |
> | Greater than | a > b | True |
< | Less than | a < b | False |
>= | Greater than or equal to | a >= b | True |
<= | Less than or equal to | a <= b | False |
3๏ธโฃ Logical Operators
Combine multiple conditions.
Operator | Description | Example (x=True, y=False) | Output |
---|---|---|---|
and | Both True | x and y | False |
or | Either True | x or y | True |
not | Negation | not x | False |
4๏ธโฃ Bitwise Operators
Perform operations at the binary level.
Operator | Description | Example (a=5, b=3) | Output |
---|---|---|---|
& | AND | a & b | 1 |
` | ` | OR | `a |
5๏ธโฃ Assignment Operators
Assign values to variables with operations.
Operator | Example (a=10) | Equivalent |
---|---|---|
= | a = 5 | a = 5 |
+= | a += 5 | a = a + 5 |
-= | a -= 5 | a = a - 5 |
*= | a *= 5 | a = a * 5 |
/= | a /= 5 | a = a / 5 |
//= | a //= 5 | a = a // 5 |
%= | a %= 5 | a = a % 5 |
**= | a **= 5 | a = a ** 5 |
6๏ธโฃ Membership & Identity Operators
Check for existence and object identity.
Operator | Description | Example (lst=[1,2,3], x=2 ) | Output |
---|---|---|---|
in | Present in sequence | x in lst | True |
not in | Not present | x not in lst | False |
is | Same object | a is b | False |
is not | Different object | a is not b | True |
Hope you have understood about python programming for data science
๐ง Taking Input from the User in Python โ Easy Explanation
In Python, the input()
function is used to collect information from the user. It allows you to make your programs interactive. For example, you can ask the user their name, age, or any dataโand then use that info in your code.
โ Basic Syntax:
name = input("Enter your name: ")
print("Hello", name)
input()
displays the prompt message inside the quotation marks.- Whatever the user types is stored as a string.
๐ Type Conversion (Typecasting)
Since input()
always returns data as string, we need to convert it to other types like integers or floats when needed.
โ Example:
age = int(input("Enter your age: ")) # Converts input to integer
price = float(input("Enter the price: ")) # Converts input to float
int()
converts input into whole numbers.float()
converts input into decimal numbers.
If the user types something that cannot be converted (e.g. letters instead of numbers), Python will throw an error.
๐ Key Notes for Students:
input()
is always a string by default.- Use
int()
orfloat()
for numeric inputs. - This helps make programs dynamic and user-friendly.
- Always consider using type conversion when working with numbers.
๐จโ๐ซ Real-World Example:
name = input("Whatโs your name? ")
age = int(input("How old are you? "))
print(f"{name} will be {age + 1} years old next year!")
๐ฏ Operator Precedence in Python: What Runs First?
When you’re solving a math problem in Python, how does Python know which part to calculate first?
Just like in school math, Python follows a specific order of operations known as PEMDAS:
โ What is PEMDAS?
PEMDAS stands for:
- Parentheses
( )
โ Always done first. - Exponents
**
โ Powers come next. - Multiplication
*
, Division/
, Floor Division//
, and Modulus%
โ Left to right. - Addition
+
, Subtraction-
โ Done last, from left to right.
๐ง Why Operator Precedence Matters?
If you write something like:
result = 10 + 2 * 3
Python first multiplies 2 * 3 = 6
, then adds 10 + 6 = 16
.
But if you add parentheses:
result = (10 + 2) * 3
Python calculates 10 + 2 = 12
first, then 12 * 3 = 36
.
๐งช Examples You Can Show Students:
# Without parentheses
result = 10 + 2 * 3 # Output: 16
# With parentheses
result = (10 + 2) * 3 # Output: 36
# Right-to-left for exponentiation
result = 2 ** 3 ** 2 # 3**2 = 9, then 2**9 = 512
๐ก Quick Summary for Students:
- Always use parentheses to control what runs first.
- Exponentiation
**
is right-associative, meaning it runs from right to left. - Multiplication/Division/Modulus run before Addition/Subtraction.
๐ง Understanding if
, else
, and elif
in Python
Conditional statements are used to make decisions in your Python code. Just like how we decide things in real life (โif it rains, Iโll take an umbrellaโ), Python also allows you to execute different code based on whether a condition is true or false.
โ
Basic if
Statement
The if
statement runs a block of code only if the condition is true.
x = 10
if x > 5:
print("x is greater than 5")
- If
x > 5
is true, the print statement runs. - If itโs false, nothing happens.
๐ if-else
Statement
When you want to handle both true and false cases, use if-else
.
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
- If
x > 5
, the first message is printed. - Otherwise, the
else
block executes.
๐ if-elif-else
Statement
Use this when you have multiple conditions to check.
x = 5
if x > 10:
print("x is greater than 10")
elif x > 5:
print("x is greater than 5 but not more than 10")
elif x == 5:
print("x is exactly 5")
else:
print("x is less than 5")
- Conditions are checked from top to bottom.
- The first true condition executes, and the rest are skipped.
๐ Real-Life Use Case in Data Science
Example 1: Categorizing Data
age = 25
if age < 18:
category = "Minor"
elif age < 65:
category = "Adult"
else:
category = "Senior Citizen"
print("Category:", category)
You can use such logic to group users based on age.
Example 2: Conditional Logic on DataFrame
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Score': [85, 40, 75]}
df = pd.DataFrame(data)
df['Result'] = df['Score'].apply(lambda x: 'Pass' if x >= 50 else 'Fail')
print(df)
This is extremely useful for filtering and labeling data automatically.
๐ Summary for Students
if
: Runs when the condition isTrue
.if-else
: Adds an alternative when condition isFalse
.if-elif-else
: Lets you check multiple conditions in sequence.- Essential in data cleaning, filtering, and decision-making.
๐ง Understanding if
, else
, and elif
in Python
Conditional statements are used to make decisions in your Python code. Just like how we decide things in real life (โif it rains, Iโll take an umbrellaโ), Python also allows you to execute different code based on whether a condition is true or false.
โ
Basic if
Statement
The if
statement runs a block of code only if the condition is true.
x = 10
if x > 5:
print("x is greater than 5")
- If
x > 5
is true, the print statement runs. - If itโs false, nothing happens.
๐ if-else
Statement
When you want to handle both true and false cases, use if-else
.
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
- If
x > 5
, the first message is printed. - Otherwise, the
else
block executes.
๐ if-elif-else
Statement
Use this when you have multiple conditions to check.
x = 5
if x > 10:
print("x is greater than 10")
elif x > 5:
print("x is greater than 5 but not more than 10")
elif x == 5:
print("x is exactly 5")
else:
print("x is less than 5")
- Conditions are checked from top to bottom.
- The first true condition executes, and the rest are skipped.
๐ Real-Life Use Case in Data Science
Example 1: Categorizing Data
age = 25
if age < 18:
category = "Minor"
elif age < 65:
category = "Adult"
else:
category = "Senior Citizen"
print("Category:", category)
You can use such logic to group users based on age.
Example 2: Conditional Logic on DataFrame
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Score': [85, 40, 75]}
df = pd.DataFrame(data)
df['Result'] = df['Score'].apply(lambda x: 'Pass' if x >= 50 else 'Fail')
print(df)
This is extremely useful for filtering and labeling data automatically.
๐ Summary for Students
if
: Runs when the condition isTrue
.if-else
: Adds an alternative when condition isFalse
.if-elif-else
: Lets you check multiple conditions in sequence.- Essential in data cleaning, filtering, and decision-making.
๐ง What is match-case
in Python?
Now what is match-case
Python Programming For Data Science
Introduced in Python 3.10, match-case
is Python’s version of switch-case from other languages. It allows pattern matching to simplify conditional branching โ making code easier to read and manage.
โ๏ธ Syntax Example:
def http_status(code):
match code:
case 200:
return "OK"
case 400:
return "Bad Request"
case 404:
return "Not Found"
case 500:
return "Internal Server Error"
case _:
return "Unknown Status"
โ
case _:
acts like a default fallback if no other case matches.
๐ก Features of match-case:
- Clean alternative to long
if-elif-else
chains. case _
works as a default case.- Supports patterns like literals, variables, and structural patterns (e.g., tuples).
๐ฆ Real Use Case: Matching Data Structures
point = (3, 4)
match point:
case (0, 0):
print("Origin")
case (x, 0):
print(f"X-Axis at {x}")
case (0, y):
print(f"Y-Axis at {y}")
case (x, y):
print(f"Point at ({x}, {y})")
This pattern checks for a pointโs location and provides a response based on its position.
๐ Blog Notes for Students:
match-case
is only available in Python 3.10 and above.- Helps avoid messy nested conditions.
- It’s excellent for checking HTTP status codes, menu selections, input categories, or anything with fixed outcomes.
- Great for clean readable code, especially in data processing or automation tasks.
- Always include a
case _:
block to handle unknown or unexpected values.
๐งต String Formatting in Python: A Beginner’s Guide to format()
and f-Strings
In Python, strings are everywhereโfrom printing messages to displaying results. When it comes to inserting values into strings, Python gives you powerful formatting tools. In this guide, you’ll learn two popular ways:
- Using
.format()
method - Using f-Strings (Recommended)
๐ 1. Using .format()
Method
You can insert variables inside a string using curly {}
brackets and the .format()
method.
โ๏ธ Example:
name = "Alice"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
โก๏ธ Output: My name is Alice and I am 25 years old.
๐ Positional vs Keyword Arguments:
print("{} is learning {}".format("Alice", "Python")) # Positional
print("{name} is learning {language}".format(name="Alice", language="Python")) # Keyword
โ 2. Using f-Strings (Python 3.6+)
f-Strings are cleaner, faster, and more readable. Just add an f
before the string and insert variables inside {}
.
โ๏ธ Example:
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
โก๏ธ Output: My name is Alice and I am 25 years old.
๐งฎ Expressions Inside f-Strings
You can also run expressions directly inside {}
:
a = 5
b = 10
print(f"Sum of {a} and {b} is {a + b}")
โก๏ธ Output: Sum of 5 and 10 is 15
๐ข Formatting Numbers
Control how numbers appear using format specifiers:
pi = 3.14159
print(f"Pi rounded to 2 decimal places: {pi:.2f}")
โก๏ธ Output: Pi rounded to 2 decimal places: 3.14
๐ Padding and Alignment
Align text using :<
, :>
, and :^
inside f-Strings.
print(f"{'Python':<10}") # Left-align
print(f"{'Python':>10}") # Right-align
print(f"{'Python':^10}") # Center-align
โก๏ธ Output:
Python
Python
Python
๐ก Here 10
is the total space, and the symbols decide alignment.
๐ง Why Use f-Strings?
- Cleaner syntax
- Easier to read
- Supports inline expressions
- Great for formatting numbers and text
๐ Summary for Students
- Use
.format()
when working with older Python versions. - Prefer
f-strings
for simplicity and performance. - Practice inserting variables and expressions into strings.
- Use formatting for clean output in reports, dashboards, etc.
๐ Loops in Python: Explained for Beginners
Loops are a powerful feature in Python that let you execute a block of code repeatedly. Whether you’re processing a list of data, printing numbers, or automating a task, loops are essential in every Python program.
Python offers two main types of loops:
for
loopwhile
loop
Letโs explore both with examples and tips.
๐ 1. For Loop
A for
loop is used when you want to iterate over a sequence (like a list, tuple, or string).
Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
๐ Output:
apple
banana
cherry
You can also loop over a sequence of numbers using the range()
function:
for i in range(3):
print(i)
๐ Output:
0
1
2
๐ 2. While Loop
A while
loop keeps running as long as the condition is True
.
Example:
count = 0
while count < 3:
print(count)
count += 1
๐ Output:
0
1
2
โ 3. Loop Control Statements
Sometimes, you need to control how and when a loop should stop or skip. Hereโs how:
Statement | Description |
---|---|
break | Exits the loop completely |
continue | Skips the current iteration and moves to the next |
pass | Does nothing; used as a placeholder |
Example using break
:
for i in range(5):
if i == 3:
break
print(i)
๐ Output:
0
1
2
โจ Bonus Tip: Use Cases of Loops
- โ Repeating actions (e.g., sending emails in bulk)
- โ Reading files line-by-line
- โ Processing each item in a dataset
- โ Performing automation tasks
โ Summary
- Use
for
loops for known number of iterations or over collections. - Use
while
loops for uncertain or conditional repetition. break
,continue
, andpass
offer fine control over your loops.
List and List Methods in Python Programming for Data Science
In Python, a list is an ordered, mutable collection used to store multiple items. Lists are incredibly flexible and powerful โ you can store different data types (integers, strings, booleans, even other lists) in a single list.
โ Creating a List
# Empty list
my_list = []
# List with integers
numbers = [1, 2, 3, 4, 5]
# List with mixed data types
mixed_list = [1, "Hello", 3.14, True]
๐ ๏ธ Common List Methods
Method | Description | Example |
---|---|---|
append(x) | Adds x to the end of the list | my_list.append(10) |
extend(iterable) | Adds all elements from the iterable | my_list.extend([6, 7, 8]) |
insert(index, x) | Inserts x at the given index | my_list.insert(2, "Python") |
remove(x) | Removes the first occurrence of x | my_list.remove(3) |
pop([index]) | Removes and returns item at index (last if not given) | my_list.pop(2) |
index(x) | Returns the index of the first occurrence of x | my_list.index(4) |
count(x) | Counts how many times x appears | my_list.count(2) |
sort() | Sorts the list in ascending order | my_list.sort() |
reverse() | Reverses the order of the list | my_list.reverse() |
copy() | Returns a shallow copy of the list | new_list = my_list.copy() |
clear() | Empties the list | my_list.clear() |
๐งช Example Usage
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)
# Output: ['apple', 'banana', 'cherry', 'orange']
fruits.sort()
print(fruits)
# Output: ['apple', 'banana', 'cherry', 'orange']
๐ก Quick Tips
- Lists are mutable โ you can change, delete, and add elements anytime.
- Always use
append()
when adding one item andextend()
for multiple items. - Use
copy()
if you want to duplicate the list without changing the original. pop()
is great for removing items and using their values right away.
๐ง Tuples and Tuple Methods in Python โ Beginner Notes
Tuples are one of Pythonโs basic data structures. They are just like lists but immutable โ which means you canโt change their values once created. This makes them super useful for fixed data, like days of the week, coordinates, or records.
โ What is a Tuple?
A tuple is an ordered, immutable collection of items. Think of it like a list, but locked after creation.
# Creating a tuple
my_tuple = (1, 2, 3, 4)
โ Tuples are written using parentheses ()
โ They can contain mixed data types (numbers, strings, booleans)
๐ Creating Tuples
empty_tuple = () # Empty tuple
numbers = (1, 2, 3, 4, 5) # Tuple with integers
mixed_tuple = (1, "Hello", 3.14, True) # Mixed data types
single_element = (42,) # Single element tuple - must have a comma!
Note: If you forget the comma in a one-item tuple, Python won’t treat it as a tuple.
๐ Tuple Methods
Tuples have fewer methods than lists because they’re immutable.
Method | Description | Example |
---|---|---|
count(x) | Returns how many times x appears in the tuple | my_tuple.count(2) |
index(x) | Returns the index of the first occurrence of x | my_tuple.index(3) |
โก Tuple Characteristics
- ๐ Immutable: You can’t add, remove, or change elements once defined.
- ๐ Faster than lists: Tuple access is faster because of immutability.
- ๐ง Can be used as dictionary keys: Lists can’t be used as keys, but tuples can.
๐ Accessing Tuple Elements
You can access tuple elements just like lists โ using indexing and slicing.
my_tuple = (10, 20, 30, 40)
print(my_tuple[1]) # Output: 20
print(my_tuple[1:3]) # Output: (20, 30)
๐ฆ Tuple Packing and Unpacking
Packing = putting multiple values into one tuple.
Unpacking = extracting them back into variables.
# Packing
person = ("Alice", 25, "Engineer")
# Unpacking
name, age, profession = person
print(name) # Alice
print(age) # 25
๐ When to Use Tuples?
Use a tuple when:
- โ You need an unchangeable set of items (e.g. days of the week)
- โ You want a faster alternative to lists
- โ Youโre storing heterogeneous data (like records)
๐น Sets & Set Methods in Python โ Beginner’s Notes
๐ What is a Set in Python?
A set is:
- Unordered: No indexing or slicing.
- Mutable: You can add/remove items.
- Unique: No duplicate values allowed.
Itโs perfect when you want to store a group of distinct itemsโlike tags, IDs, or filtered results.
๐ Creating a Set
empty_set = set()
numbers = {1, 2, 3, 4, 5}
mixed_set = {1, "Hello", 3.14, True}
unique_numbers = set([1, 2, 2, 3, 4, 4, 5])
Output:
{1, 2, 3, 4, 5}
โ duplicates removed automatically!
โ Common Set Methods
Method | Description | Example |
---|---|---|
add(x) | Adds item x | my_set.add(10) |
update(iter) | Adds multiple items from iterable | my_set.update([6, 7, 8]) |
remove(x) | Removes x (raises error if not found) | my_set.remove(3) |
discard(x) | Removes x (no error if not found) | my_set.discard(3) |
pop() | Removes and returns a random element | my_set.pop() |
clear() | Empties the entire set | my_set.clear() |
copy() | Creates a shallow copy of the set | new = my_set.copy() |
๐งฎ Set Operations
Operation | Description | Example |
---|---|---|
union(set2) | Combines all unique elements | set1.union(set2) |
intersection(set2) | Common elements in both sets | set1.intersection(set2) |
difference(set2) | Elements in set1 not in set2 | set1.difference(set2) |
symmetric_difference(set2) | Elements in either set, but not both | set1.symmetric_difference(set2) |
issubset(set2) | Returns True if set1 is subset of set2 | set1.issubset(set2) |
issuperset(set2) | Returns True if set1 is superset of set2 | set1.issuperset(set2) |
๐ Example Usage
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
print(set1 | set2) # Union โ {1, 2, 3, 4, 5, 6}
print(set1 & set2) # Intersection โ {3, 4}
print(set1 - set2) # Difference โ {1, 2}
print(set1 ^ set2) # Symmetric Difference โ {1, 2, 5, 6}
๐ Key Properties of Sets
- โ No indexing or slicing
- โ Duplicates removed automatically
- ๐ Mutable โ you can update it anytime
๐ก When to Use Sets?
- You want a collection of unique items
- You need fast membership checks
- You want to eliminate duplicates from data