Posts

Showing posts from March, 2025

set

 A set in Python is an unordered, mutable collection of unique elements. It does not allow duplicate values and is commonly used for mathematical set operations like union, intersection, and difference. Key Characteristics of Sets: ✅ Unordered – Elements do not maintain a specific order. ✅ Mutable – You can add or remove elements. ✅ Unique Elements – No duplicate values allowed. ✅ No Indexing – Cannot access elements using an index. # Creating sets number = {1, 2, 3, 4, 5} print(number) print(type(number)) # Sets ignore duplicate values newnumber = {1, 2, 3, 4, 5, 4, 3, 2, 2, 1, "tt"} print(newnumber) # Adding an element newnumber.add("Praveen") print(newnumber) # Merging two sets thisset = {"apple", "banana", "cherry"} tropical = {"pineapple", "mango", "papaya"} thisset.update(tropical) print(thisset) # Merging a list into a set thisset = {"apple", "banana", "cherry"} mylist =...

Understanding Classes and Objects in Python

  Understanding Classes and Objects in Python Python is an object-oriented programming (OOP) language, which means it allows developers to create and work with objects. Understanding   classes   and   objects   is fundamental to mastering Python. In this blog, we will explore these concepts with practical examples. What Are Classes and Objects? A   class   is a blueprint for creating objects. It defines attributes (variables) and behaviors (methods) that its objects will have. An   object   is an instance of a class. It contains real values assigned to the attributes defined in the class blueprint. 1️⃣ Creating a Simple Class and Object Let's start with a simple example of a class   Addition , which adds two numbers: class Addition: first = 10 second = 20 def addNumber(self): c = self.first + self.second # Using self to access class variables print(c) # Creating an object obj = Addition() # Calling the met...

Learning Django: A Step-by-Step Guide to Models and Databases

Learning Django: A Step-by-Step Guide to Models and Databases Introduction Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. One of its core features is the Object-Relational Mapping (ORM) system, which allows developers to interact with databases using Python code. In this guide, we will explore Django models, migrations, and databases from basic to advanced. 1. Setting Up Your Django Project Install Django pip install django Create a Django Project django-admin startproject myproject cd myproject Start the Development Server python manage.py runserver Open http://127.0.0.1:8000/ in your browser to see the Django welcome page. 2. Creating a Simple Django App Create an App python manage.py startapp myapp Register the App in settings.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions...

Template Inheritance and Handling Static Files in Django

Django's Template Inheritance and Static Files Management features allow developers to create maintainable layouts and integrate CSS, JS, and Images seamlessly into web applications. This guide will explore both concepts with practical examples. ✅ 1. Template Inheritance in Django 🧠 What is Template Inheritance? Template Inheritance allows you to create a base template with a common layout (e.g., header, footer, navigation) and then create specific child templates that extend this base template. This helps avoid code duplication and keeps the templates clean and manageable . 📂 Example Project Structure: myproject/ ├── myapp/ │ ├── templates/ │ │ └── myapp/ │ │ ├── base.html │ │ ├── home.html │ │ └── about.html └── manage.py ✅ 1. Creating a Base Template ( `` ) <!DOCTYPE html> <html> <head> <title>{% block title %}My Website{% endblock %}</title> <link rel="stylesheet" href="{...

Mastering Django Template Language (DTL): A Step-by-Step Guide

  Mastering Django Template Language (DTL): A Step-by-Step Guide Django's Template Language (DTL) is a powerful tool for rendering dynamic data on web pages using HTML templates . This guide will cover how to pass data from views to templates , display variables, use conditional statements, apply loops, and implement filters with practical examples. ✅ 1. Passing Data from Views to Templates 🧠 How to Pass Data: Data is passed using the context dictionary in the render() function. # views.py from django.shortcuts import render # Simple Template Rendering def home(request): return render(request, 'myapp/home.html') # Dynamic Data Rendering def user_info(request): context = { 'name': 'John Doe', 'age': 25, 'fruits': ['Apple', 'Banana', 'Cherry'], 'is_logged_in': True, 'score': 85, 'hobbies': ['Reading', 'Gaming', ...

How to Add Templates in Django: Step-by-Step Guide

  How to Add Templates in Django: Step-by-Step Guide Django's Template System allows developers to create web pages by using HTML templates. Adding templates to a Django project involves setting up the template directory , configuring settings.py , and rendering templates through views. This guide will focus solely on how to add templates without involving dynamic data. ✅ 1. Setting Up Your Django Project # Step 1: Create a new Django project and app django-admin startproject myproject cd myproject python manage.py startapp myapp 📂 Project Directory Structure: myproject/ ├── myapp/ │ ├── templates/ │ │ └── myapp/ │ │ ├── home.html │ └── views.py ├── manage.py └── settings.py myapp/templates/myapp/ : This is where all app-specific templates will reside. Keeping templates within the app directory is a best practice to avoid conflicts when multiple apps are used. ✅ 2. Configuring Templates in settings.py TEMPLATES = [ { 'BACKEND...

Handling 404 Errors in Django: Custom and Default Approaches

  Handling 404 Errors in Django: Custom and Default Approaches In web development, handling errors gracefully is crucial to providing a smooth user experience. One of the most common errors is the 404 Not Found error, which occurs when a requested resource is not available. In Django, there are multiple ways to handle 404 errors, either by displaying a custom message or by using Django’s default 404 page . This article will explore both approaches with practical examples. 1. What is a 404 Error in Django? A 404 error is an HTTP status code indicating that the server could not find the requested page. In Django, a 404 error is raised when a URL is not mapped to a valid view or when data requested is not found. ✅ Common Scenarios for 404 Errors: A user tries to access a non-existent page. Invalid data or ID in a URL parameter. Missing resources like images or files. 2. Methods for Handling 404 Errors in Django Django provides two primary ways to handle 404 errors: ✅ ...