Skip to main content

Posts

Showing posts from March, 2026
  User Defined Exception in Python  Theory In Python, an exception is an error that occurs during program execution and interrupts the normal flow of the program. Sometimes the built-in exceptions are not enough for a program. In such cases, programmers can create their own exceptions called User Defined Exceptions . A user defined exception is created by defining a new class that inherits from the Exception class . This helps programmers handle custom errors based on specific conditions in the program . Syntax class ExceptionName ( Exception ): pass Basic Example Program # User Defined Exception Example class AgeError ( Exception ): pass age = int ( input ( "Enter your age: " )) try : if age < 18 : raise AgeError else : print ( "You are eligible to vote" ) except AgeError : print ( "Error: Age must be 18 or above" ) Sample Output Case 1 Enter your age: 20 You are eligible to vote Case 2 En...
  Database Operations in Python Creating Tables, Insert, Update, Delete, Read and Transaction Control Python can interact with databases using modules like sqlite3 . It allows programmers to perform database operations such as creating tables, inserting records, updating data, deleting data, and reading records . These operations are commonly called CRUD operations : Create – Create tables and insert data Read – Retrieve data from tables Update – Modify existing records Delete – Remove records from tables Python also supports Transaction Control , which ensures database operations are completed safely. 1. Creating a Table A table is created using the CREATE TABLE SQL command. import sqlite3 conn = sqlite3 . connect( "college.db" ) cursor = conn . cursor() cursor . execute( """ CREATE TABLE IF NOT EXISTS student( id INTEGER PRIMARY KEY, name TEXT, department TEXT ) """ ) conn . commit() 2. Insert Data ...
  Date and Time Module in Python  Python provides built-in modules to work with date and time . The most commonly used modules are: datetime module – used to work with dates and times. time module – used for time-related functions such as delays and timestamps. 1. datetime Module The datetime module helps to get the current date, time, and perform date calculations . Importing the module import datetime Example: Display Current Date and Time import datetime now = datetime . datetime . now() print ( "Current Date and Time:" , now ) Output (example): Current Date and Time: 2026-03-09 10:30:15.234567 Example: Display Only Current Date from datetime import date today = date . today() print ( "Today's Date:" , today ) Output: Today's Date: 2026-03-09 Example: Formatting Date and Time from datetime import datetime now = datetime . now() formatted = now . strftime( "%d-%m-%Y %H:%M:%S" ) print ( "Formatt...
  Built-in Modules in Python  Built-in modules are modules that come pre-installed with Python . You do not need to install them separately . They provide ready-made functions and classes to perform common tasks such as mathematics, date handling, file operations, and system interaction. You can use them in a program using the import statement. import module_name Example: import math print ( math . sqrt( 25 )) Output: 5.0 Common Built-in Modules in Python 1. math Module  Provides mathematical functions. Example: import math print ( math . sqrt( 16 )) print ( math . factorial( 5 )) print ( math . pi) Functions: sqrt() – Square root factorial() – Factorial value pi – Value of π pow() – Power calculation 2. random Module  Used to generate random numbers. Example: import random print ( random . randint( 1 , 10 )) print ( random . choice([ 10 , 20 , 30 , 40 ])) Functions: randint() – Random integer choice() – Random se...

prasath M

  Decision Making in Python (with Diagram)  In Python , decision making allows a program to choose different actions based on conditions . The program evaluates a condition and decides which block of code should execute. Decision making is done using conditional statements like if , if-else , and if-elif-else . if Statement The if statement executes a block of code only when the condition is True . Syntax if condition : statement Example x = 10 if x > 5 : print ( "x is greater than 5" ) Diagram (Flowchart) Start | Condition | +----+----+ | | True False | | Execute Skip Statement Statement | End if – else Statement The if-else statement is used when there are two possible outcomes . Example num = 7 if num % 2 == 0 : print ( "Even Number" ) else : print ( "Odd Number" ) Diagram Start | Check ...

prasath M

  Types of Loops in Python  In Python , loops are fundamental control structures that allow a program to execute a block of code repeatedly . They are commonly used for tasks like processing lists, iterating through data, or performing repeated calculations. Python mainly provides two primary types of loops . 1. For Loop The for loop is used to iterate over a sequence such as a list, tuple, string, or a range of numbers. It is typically used when the number of iterations is known . Syntax for variable in sequence : # code block Example for i in range ( 5 ): print ( i ) Output 0 1 2 3 4 Explanation range(5) generates numbers from 0 to 4 . The loop runs once for each value in the sequence. 2. While Loop The while loop repeatedly executes a block of code as long as a condition remains true . It is commonly used when the number of iterations is not predetermined . Syntax while condition : # code block Example i = 1 while i <=...

prasath M

  Types of Operators in Python  In Python , operators are special symbols used to perform operations on variables and values . They are an essential part of programming because they allow calculations, comparisons, and logical decisions. Python provides several types of operators.  Arithmetic Operators These operators perform mathematical calculations . Operator Meaning Example + Addition 5 + 3 = 8 - Subtraction 5 - 3 = 2 * Multiplication 5 * 3 = 15 / Division 6 / 3 = 2.0 % Modulus (remainder) 5 % 2 = 1 ** Exponent (power) 2 ** 3 = 8 // Floor division 7 // 2 = 3 Example a = 10 b = 3 print ( a + b ) print ( a % b )  Comparison (Relational) Operators These operators compare two values and return True or False . Operator Meaning Example == Equal to 5 == 5 != Not equal 5 != 3 > Greater than 7 > 5 < Less than 3 < 5 >= Greater than or equal 5 >= 5 <= Less than or equal 3 <= 4 Example x = 5 y = 10 print ( x < y ) ...