๐Ÿ Ultimate Guide to Python Programming & Data Structures

ยท

4 min read

๐Ÿ 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:

  1. Download Python from Python Official Website.

  2. Install the downloaded .exe file and check Add Python to PATH.

  3. Open Command Prompt and verify installation:

     python --version
    

๐Ÿง Linux (Ubuntu/Debian):

  1. Open the terminal and update package lists:

     sudo apt update
    
  2. Install Python:

     sudo apt install python3
    
  3. Verify installation:

     python3 --version
    

๐Ÿ”ฅ Python Basics

1๏ธโƒฃ Python Data Types

Python has multiple built-in data types. Below are the most commonly used:

Data TypeDescriptionExample
IntegerWhole numbersx = 10
FloatDecimal numbersy = 3.14
StringText datas = "Hello"
ListOrdered, mutable collectionarr = [1, 2, 3]
TupleOrdered, immutable collectiont = (1, 2, 3)
DictionaryKey-value pairsd = {"key": "value"}
BooleanTrue or False valuesflag = True
SetUnordered collection with unique elementss = {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:

  1. Class & Object - Blueprint and instance of a class.

  2. Encapsulation - Hiding internal details from outside.

  3. Inheritance - Reusing properties from a parent class.

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

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?

FeatureListTuple
MutabilityMutableImmutable
Syntax[]()
  • O(log n) because the search space is divided by 2 at each step.

ย