๐ Ultimate Guide to Python Programming & Data Structures


๐ Introduction
Python is one of the most popular programming languages due to its simplicity, readability, and vast ecosystem. Whether you're a beginner or an experienced developer, mastering Python and Data Structures & Algorithms (DSA) will greatly enhance your problem-solving skills.
๐น What is Python?
Python is a high-level, interpreted, and dynamically typed programming language known for its easy-to-read syntax and vast libraries.
โก Features of Python:
Interpreted: Executes code line by line, making debugging easier.
Dynamically Typed: No need to declare variable types; Python determines them at runtime.
Platform Independent: Python code can run on different operating systems without modification.
Extensive Libraries: Python has a vast collection of libraries for AI, ML, web development, and automation.
Object-Oriented: Supports OOP concepts like encapsulation, inheritance, and polymorphism.
Highly Readable: Uses indentation instead of braces
{}
for structuring code.
๐ป Installing Python on Windows, Linux, and Ubuntu
๐ Windows:
Download Python from Python Official Website.
Install the downloaded
.exe
file and check Add Python to PATH.Open Command Prompt and verify installation:
python --version
๐ง Linux (Ubuntu/Debian):
Open the terminal and update package lists:
sudo apt update
Install Python:
sudo apt install python3
Verify installation:
python3 --version
๐ฅ Python Basics
1๏ธโฃ Python Data Types
Python has multiple built-in data types. Below are the most commonly used:
Data Type | Description | Example |
Integer | Whole numbers | x = 10 |
Float | Decimal numbers | y = 3.14 |
String | Text data | s = "Hello" |
List | Ordered, mutable collection | arr = [1, 2, 3] |
Tuple | Ordered, immutable collection | t = (1, 2, 3) |
Dictionary | Key-value pairs | d = {"key": "value"} |
Boolean | True or False values | flag = True |
Set | Unordered collection with unique elements | s = {1, 2, 3} |
2๏ธโฃ Python Variables and Operators
Variables store data values, and operators perform operations on them.
โ Example:
x = 10
y = 5
sum = x + y # Addition
print(sum) # Output: 15
โ Types of Operators:
Arithmetic Operators (
+
,-
,*
,/
,//
,%
,**
)Comparison Operators (
==
,!=
,>
,<
,>=
,<=
)Logical Operators (
and
,or
,not
)Bitwise Operators (
&
,|
,^
,~
,<<
,>>
)
3๏ธโฃ Control Flow Statements
Control flow statements help in decision-making and loops.
โ If-Else Statements:
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
โ Loops:
๐ For Loop:
for i in range(5):
print(i)
๐ While Loop:
x = 0
while x < 5:
print(x)
x += 1
๐๏ธ Object-Oriented Programming (OOP) in Python
๐ Key Concepts:
Class & Object - Blueprint and instance of a class.
Encapsulation - Hiding internal details from outside.
Inheritance - Reusing properties from a parent class.
Polymorphism - Different forms of the same method.
โจ Example:
class Car:
def __init__(self, brand):
self.brand = brand
def show(self):
print("Brand:", self.brand)
c = Car("Tesla")
c.show()
๐ Data Structures & Algorithms in Python
๐ข Arrays
Arrays store multiple values in a single variable.
arr = [1, 2, 3, 4, 5]
print(arr[2]) # Output: 3
๐ Stack (LIFO)
stack = []
stack.append(1)
stack.append(2)
stack.pop() # Removes 2
๐ Searching Algorithms
Binary Search (โก Fast Search)
def binary_search(arr, x):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == x:
return mid
elif arr[mid] < x:
left = mid + 1
else:
right = mid - 1
return -1
๐ Sorting Algorithms
Bubble Sort ๐ซง
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
Selection Sort ๐
def selection_sort(arr):
for i in range(len(arr)):
min_index = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
๐ Multithreading in Python
import threading
def print_numbers():
for i in range(5):
print(i)
thread = threading.Thread(target=print_numbers)
thread.start()
๐ Frequently Asked Questions
โ What is the difference between list and tuple?
Feature | List | Tuple |
Mutability | Mutable | Immutable |
Syntax | [] | () |
โ What is the time complexity of binary search?
- O(log n) because the search space is divided by 2 at each step.
Subscribe to my newsletter
Read articles from manas Chakraborty directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
