Posts

PostgreSQL With Python

Image
  Setup For Work step 1: Install pgadmin4 in your system (Note* please remember root password)   step 2: create a database in the name 'test-all' step 3: create a virtual environment (if you don't know then  visit here ) step 4: pip install psycopg2 step 5: Now create a python script and try with the below code Code Connecting Database: import psycopg2 def connect_my_db (db, user, password, host):     try:         connection = psycopg2.connect(         database=db, user = user,          password = password, host = host,          port = "5432")         print("Database Connected")                    return  connection     except Exception as e:         print("Unable to connect DB") connection   =  connect_my_db ('test-all', 'postgres', '12345', '127.0.0...

SQLite In Python

Image
Setup For Work SQLite3 comes with python by default. So, you don't need to install it explicitly. Now create a python script and try with the below code. Connecting Database [ db_connect.py ]: import sqlite3 def create_db():     try:          connection  = sqlite3.connect('test.db')    # if test.db is there then it will connect or it will create a new           return  connection     except(sqlite3.DatabaseError, sqlite3.ProgrammingError):         print("DB Connection Error") connection   =  connect_my_db () Create Table : import sqlite3 import db_connect connection   =  connect_my_db () cursor_object = connection.cursor() query = ''' CREATE TABLE  DemoTable  (username text, mail text, phone text, id integer); ''' try:     cursor_object.execute(query)     print("Table created successfully") except Exception as e:   ...

MySQL With Python

Image
  Setup For Work step 1: Install MySQL in your system (Note* please remember root password)   step 2: create a database in the name 'db10' step 3: create a virtual environment (if you don't know then  visit here ) step 4: pip install mysql-connector-python step 5: Now create a python script and try with the below code Code Connecting Database: import psycopg2 import mysql.connector def connect_my_db (db, user, password, host):     try:         connection = mysql.connector.connect(         database=db, user = user,          password = password, host = host,          port = "3306")         print("Database Connected")         return connection     except Exception as e:         print("Unable to connect DB") connection = connect_my_db('db10', 'root', '12345', 'localhost') Create Table : import mysql.conn...

Email Sending Using Python

Image
Code Step1:  Please visit the Less Secure App    and make it 'on' Step2: We will use 'smtplib' Then, Example Code: import smtplib  def send_my_message(sender_email_id, sender_email_id_password, receiver_email_id):     # creates SMTP session      s = smtplib.SMTP('smtp.gmail.com', 587)      # start TLS for security      s.starttls()      # Authentication      s.login(sender_email_id, sender_email_id_password)     # message to be sent      message = "Message_you_need_to_send"     # sending the mail      s.sendmail(sender_email_id, receiver_email_id, message)      # terminating the session      s.quit() sender_email_id = "demo@gmail.com" sender_email_id_password = "***********" receiver_email_id = "demoDEMO@gmail.com" send_my_message(sender_email_id, sender_email_id_password, receiver_email_id) Get Help

Polymorphism in Python

Image
   Concept Polymorphism  is the ability of an object to take on many forms. The most common use of  polymorphism  in OOP occurs when a parent class reference is used to refer to a child class object. Polymorphism in python  defines methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class inherits the methods from the parent class. Also, it is possible to modify a method in a child class that it has inherited from the parent class Code Example The word polymorphism means having many forms. In programming, polymorphism means the same function name (but different signatures) being uses for different types First Let me show you Builtin Example: len('Hello world') len([1,2,3,4,5]) len((1,2,3,4,5)) From the above example, you can see len() function is taking different from of input.  def add(x=1, y=2, z=3):        res = x + y + z        return res print(...

Inheritance in Python

Image
  Concept Inheritance is the ability of any class to extract and use features of other classes. It is the process by which new classes called the derived classes are created from existing classes called Base classes. Code Example Example 1: class Human:     def __init__(self, z):         self.address = z     def print_address(self):         print(self.address) class Student(Human):          def __init__(self, x, y, z):         Human.__init__(self, z)         self.name = x         self.roll = y     def print_name(self):         print(self.name)     def print_roll(self):         print(self.roll) # Object Creation obj = Student("Kuntal", 10, "Kolkata") print(type(obj)) obj.print_name() obj.print_roll() obj.print...

Class in Python

Image
   Concept Python is an object oriented programming language. Almost everything in Python is an object, with its properties and methods. A Class is like an object constructor, or a "blueprint" for creating objects. A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state. Code Example Example 1: class Human:            pass >>> o = Human() >>> type(o) <class '__main__.Human'> Example 2: class Student:     """         Attributes & Methods     """     def __init__(self, x=0, y="name"):      ...