Introduction to Python and Advanced Concepts 🙌

Jul 18, 2024

Lecture: Introduction to Python and Advanced Concepts

Introduction

  • Sallu Bhai's son, Ballu, didn't get a Bollywood break.
  • Learned Python in 2 days hoping for a 90 LPA job but only got frustration.
  • Creation of a Python course focusing on job aspects: Python with AI, handwritten notes, latest features.
  • Importance of machine learning, AI, data science, web development, and general scripting.

Why Python?

  • Python is the most loved and easiest language (Stack Overflow).
  • Beginner-friendly: resembles simple English.
  • Comprehensive Python handbook and cheat sheet are provided.
  • Handwritten notes and source code are available.

Key Features of Python

  • Easy to understand and short development time.
  • Excellent for scripting, AI, ML, data science due to dedicated libraries.
  • Free, open-source, and high-level language.
  • Portable across Linux, Windows, and MacOS.

Installing Python and Visual Studio Code (VS Code)

  • Step 1: Download and install Python from python.org
  • Step 2: Add python.exe to PATH during installation.
  • Step 3: Install Visual Studio Code (VS Code).
  • Step 4: Install the Python extension in VS Code for efficient coding.

Python Fundamentals

What is Programming?

  • Programming languages (Python, C, C++, JavaScript) communicate with computers.
  • Programming languages are analogous to human languages (e.g., English, Chinese).
  • Programs instruct computers on what to do (e.g., summing numbers).

Basic Python Syntax

  • Printing & Comments:
    print('Hello, World!')  # This is a comment
    
  • Types:
    a = 1   # int
    b = 2.0 # float
    c = 'Python' # string
    d = True # boolean
    e = None # NoneType
    
  • Rules for Variables:
    • Start with letter or underscore.
    • Subsequent characters can include digits.
    • No spaces or special characters allowed.

Operators and Expressions

  • Arithmetic Operators: +, -, *, /
  • Comparison Operators: ==, !=, >, <, >=, <=
  • Logical Operators: and, or, not
  • Assignment Operators: =, +=, -=, *=, /=

Built-in Functions and Modules

  • Type Checking:
    print(type(variable))
    print(float('5.6'))  # Type casting
    
  • Modules: Install and import external modules.

Advanced Concepts

String Manipulation

  • Creating Strings:
    name = 'Python' # or "Python" or '''Python'''
    
  • String Slicing and Indexing:
    print(name[0:3])  # 'Pyt'
    print(name[::-1]) # 'nohtyP'
    
  • String Functions: len(), endswith(), startswith(), capitalize(), find(), replace()

Lists and Tuples

  • Lists:
    friends = ['apple', 'mango', 1, True]
    friends.append('grape')  # modifying list
    
  • Tuples:
    values = (1, 2, 'Python')
    
  • List methods: .append(), .sort(), reverse(), pop()
  • Tuple methods: .count(), .index()

Dictionaries and Sets

  • Dictionary:
    data = {'name': 'Harry', 'marks': 95}
    data['age'] = 21  # Adding new key-value pair
    
  • Set:
    unique_items = {1, 2, 2, 3}  # Duplicates not allowed
    unique_items.add(4)
    

Conditionals and Loops

  • If-Else:
    if age > 18:
        print("Eligible")
    else:
        print("Not eligible")
    
  • Loops:
    while condition:
        # code block
    for item in sequence:
        # code block
    

Functions

  • Defining Functions:
    def greet(name):
        print(f'Hello, {name}')
    
  • Recursion and Lambda Functions:
    def factorial(n):
        if n == 0:
            return 1
        return n * factorial(n - 1)
    
    square = lambda x: x * x
    

Error Handling and File I/O

  • Try-Except Blocks:
    try:
        # Try to execute this block
    except Exception as e:
        print(e)
    finally:
        # This code runs no matter what
    
  • File Handling:
    with open('example.txt', 'w') as file:
        file.write('Hello, World!')
    with open('example.txt', 'r') as file:
        content = file.read()
    

Object-Oriented Programming (OOP)

  • Classes and Objects:
    class Dog:
        def __init__(self, name, age):
            self.name = name
            self.age = age
        def bark(self):
            print('Woof!')
    
    rex = Dog('Rex', 5)
    rex.bark()
    
  • Inheritance:
    class Animal:
        # Base class
    class Dog(Animal):
        # Derived class
    

Advanced Topics

  • Virtual Environments: Creating isolated Python environments.

touchvenvproject

  • pip install virtualenv

  • virtualenv myprojectenv

  • source myprojectenv/bin/activate

  • Lambda Functions: Inline anonymous functions

    add = lambda x, y: x + y
    print(add(2, 3))  # 5
    
  • Map, Filter, Reduce: Built-in functions for applying to lists.

    map(function, list)
    filter(function, list)
    reduce(function, list)
    
  • List Comprehensions:

    [x for x in range(5)]  # [0, 1, 2, 3, 4]
    
  • Decorators:

    def my_decorator(func):
        def wrapper():
            print("Something is happening before the function is called.")
            func()
            print("Something is happening after the function is called.")
        return wrapper
    

Useful Libraries

  • NumPy, Pandas: Data manipulation and analysis
  • Matplotlib, Seaborn: Data visualization
  • Flask, Django: Web development frameworks
  • Pillow: Image processing
  • OpenCV: Computer vision
  • Requests: Making HTTP requests

Conclusion

  • Emphasized the importance of practice and project-based learning.
  • Encouraged exploring advanced topics and building real-world projects.
  • Reiterated the extensive support and tutorials available for Python learners.