Python Programming For Data Science

python programming for data science
You are currently viewing Python Programming For Data Science
python programming for data science

Python Programming For Data Science

๐Ÿง  Why Python Programming for Data Science?

Table of Contents

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)

  1. Dynamic Typing โ€“ You donโ€™t need to declare variable types.
    • x = 10 # Python knows x is an integer
  2. Indentation โ€“ Python uses indentation instead of curly braces to structure code.
    • if x > 5: print("Greater than 5")
  3. Simple Syntax โ€“ Clean and easy to write/read.
    • name = "Kunal" print("Hello", name)

๐Ÿ”ง Must-Know Libraries for Data Science

LibraryPurpose
NumPyNumerical computing, arrays
pandasData manipulation and analysis
matplotlibBasic data visualization
seabornStatistical visualizations (built on matplotlib)
scikit-learnMachine learning
TensorFlow, PyTorchDeep learning frameworks
StatsmodelsStatistical testing and modeling
XGBoostHigh-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:

ToolWhy Use It?
Jupyter NotebookLets you write and run Python code in chunks. Ideal for learning and testing.
JupyterLabMore advanced version with multi-panel layout. Ideal for full projects.
AnacondaOne-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

CompanyUse Case
NetflixContent recommendations and streaming optimization
SpotifyMusic recommendations using machine learning
InstagramScalable backend development
DropboxCloud storage with reliable Python infrastructure
UberReal-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 number
  • is_student holds a boolean (True/False)

๐Ÿ“Š Common Python Data Types

TypeExampleMeaning
int10, -5Integer (whole) numbers
float3.14, -0.5Decimal numbers
str“hello”Text (string)
boolTrue, FalseBoolean 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

MethodDescription
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.

OperatorDescriptionExample (a=10, b=5)Output
+Additiona + b15
-Subtractiona - b5
*Multiplicationa * b50
/Divisiona / b2.0
//Floor Divisiona // b2
%Modulus (remainder)a % b0
**Exponentiationa ** b100000

2๏ธโƒฃ Comparison Operators

Compare values and return True or False.

OperatorDescriptionExample (a=10, b=5)Output
==Equal toa == bFalse
!=Not equal toa != bTrue
>Greater thana > bTrue
<Less thana < bFalse
>=Greater than or equal toa >= bTrue
<=Less than or equal toa <= bFalse

3๏ธโƒฃ Logical Operators

Combine multiple conditions.

OperatorDescriptionExample (x=True, y=False)Output
andBoth Truex and yFalse
orEither Truex or yTrue
notNegationnot xFalse

4๏ธโƒฃ Bitwise Operators

Perform operations at the binary level.

OperatorDescriptionExample (a=5, b=3)Output
&ANDa & b1
``OR`a

5๏ธโƒฃ Assignment Operators

Assign values to variables with operations.

OperatorExample (a=10)Equivalent
=a = 5a = 5
+=a += 5a = a + 5
-=a -= 5a = a - 5
*=a *= 5a = a * 5
/=a /= 5a = a / 5
//=a //= 5a = a // 5
%=a %= 5a = a % 5
**=a **= 5a = a ** 5

6๏ธโƒฃ Membership & Identity Operators

Check for existence and object identity.

OperatorDescriptionExample (lst=[1,2,3], x=2)Output
inPresent in sequencex in lstTrue
not inNot presentx not in lstFalse
isSame objecta is bFalse
is notDifferent objecta is not bTrue
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() or float() 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:

  1. Parentheses ( ) โ€“ Always done first.
  2. Exponents ** โ€“ Powers come next.
  3. Multiplication *, Division /, Floor Division //, and Modulus % โ€“ Left to right.
  4. 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 is True.
  • if-else: Adds an alternative when condition is False.
  • 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 is True.
  • if-else: Adds an alternative when condition is False.
  • 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:

  1. Using .format() method
  2. 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 loop
  • while 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:

StatementDescription
breakExits the loop completely
continueSkips the current iteration and moves to the next
passDoes 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, and pass 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

MethodDescriptionExample
append(x)Adds x to the end of the listmy_list.append(10)
extend(iterable)Adds all elements from the iterablemy_list.extend([6, 7, 8])
insert(index, x)Inserts x at the given indexmy_list.insert(2, "Python")
remove(x)Removes the first occurrence of xmy_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 xmy_list.index(4)
count(x)Counts how many times x appearsmy_list.count(2)
sort()Sorts the list in ascending ordermy_list.sort()
reverse()Reverses the order of the listmy_list.reverse()
copy()Returns a shallow copy of the listnew_list = my_list.copy()
clear()Empties the listmy_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 and extend() 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.

MethodDescriptionExample
count(x)Returns how many times x appears in the tuplemy_tuple.count(2)
index(x)Returns the index of the first occurrence of xmy_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

MethodDescriptionExample
add(x)Adds item xmy_set.add(10)
update(iter)Adds multiple items from iterablemy_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 elementmy_set.pop()
clear()Empties the entire setmy_set.clear()
copy()Creates a shallow copy of the setnew = my_set.copy()

๐Ÿงฎ Set Operations

OperationDescriptionExample
union(set2)Combines all unique elementsset1.union(set2)
intersection(set2)Common elements in both setsset1.intersection(set2)
difference(set2)Elements in set1 not in set2set1.difference(set2)
symmetric_difference(set2)Elements in either set, but not bothset1.symmetric_difference(set2)
issubset(set2)Returns True if set1 is subset of set2set1.issubset(set2)
issuperset(set2)Returns True if set1 is superset of set2set1.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

Kunal Lonhare

I am the founder of Kuku Courses