0% found this document useful (0 votes)
70 views60 pages

PYTHON Activities

The document outlines activities for learning Python using a lab manual. There are 33 activities aimed at teaching concepts like printing, variables, operators, loops, functions, classes, files, exceptions, Flask, Django, and databases. The activities increase in complexity and cover most fundamental and intermediate Python topics.

Uploaded by

gargvidushi06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views60 pages

PYTHON Activities

The document outlines activities for learning Python using a lab manual. There are 33 activities aimed at teaching concepts like printing, variables, operators, loops, functions, classes, files, exceptions, Flask, Django, and databases. The activities increase in complexity and cover most fundamental and intermediate Python topics.

Uploaded by

gargvidushi06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 60

PYTHON: -LAB-MANUAL ACTIVITIES

Learning outcome: Able to make websites, web servers, game


frameworks, desktop and CLI applications, and IDE using Python.

ACTIVITY-1
AIM: Print a string using a print statement

ACTIVITY-2
AIM: Print given string using indentation (space between characters.
ACTIVITY-3
AIM: Define Integer Variables, floating variables and string variables.

ACTIVITY-4
AIM: Write a program to add numbers and strings to the current list
using the append list method.
ACTIVITY-5
AIM: Write a python program to add, subtract, multiply and divide
given two numbers by using arithmetic operators.

ACTIVITY-6
AIM: Write a python program multiplying strings to form string with
repeating sequence.
ACTIVITY-7
AIM: Write a Python program to get the largest number from a list by
using max and min commands.

ACTIVITY-8
AIM: Write a Python program to find whether a given number
(accept from the user) is even or odd by using if else command.

ACTIVITY-9
AIM: Write a Python program to create a histogram from a given list
of integers by using for while loop.

ACTIVITY-10
AIM: Write a Python program to compute the greatest common
divisor (GCD) of two positive integers by using loop.

ACTIVITY-11
AIM: Write a Python program to get the least common multiple
(LCM) of two positive integers using if else and while commands.

ACTIVITY-12
AIM: Write a Python program to sort (ascending and descending) a
dictionary by value.

ACTIVITY-13
AIM: Write a Python program to create a tuple.

ACTIVITY-14
AIM: Write a Python program to create a tuple with different data
types.

ACTIVITY-15
AIM: Write a Python program to create a set.

ACTIVITY-16
AIM: Write a Python program to add member(s) in a set.

ACTIVITY-17
AIM: Write a Python program to find maximum and the minimum
value in a set.

ACTIVITY-18
AIM: Write a Python program to find the length of a set.

ACTIVITY-19
AIM: Write a Python program to convert temperatures to and from
Centigrade to Fahrenheit.

ACTIVITY-20
AIM: Write a Python program to find Fibonacci series.

ACTIVITY-21
AIM: Write a python program to find factorial using function in
Python IDLE.

ACTIVITY-22
AIM: Write a python program to find whether the given string is
palindrome or not by using function.

ACTIVITY-23
AIM: Write a python class to reverse a string word by word.

ACTIVITY-24
AIM: Write a python class named as circle by a radius and two
methods of computer area and perimeter of a circle.

ACTIVITY-25
AIM: Write a python program to sort a list of elements using bubble
sort algorithm.

ACTIVITY-26
AIM: Write a python program to copy content of a file to another
file.

OUTPUT-

Then you can see the content of sadhana.txt is copied from


destination.txt

ACTIVITY-27
AIM: Write a python program to find the frequency of words in a file.

ACTIVITY-28
AIM: Write a python program to illustrate exception handling.

ACTIVITY-29
AIM: Develop a python code to read a text file, copy the contents to
another file after removing the blank lines.

OUTPUT-

ACTIVITY-30
AIM: Create a database in MySQL and connect using python.
Step 1: Install necessary libraries

Make sure you have MySQL installed on your computer. Also, install the
required Python libraries: mysql-connector-python.

You can install it using pip:

pip install mysql-connector-python

Step 2: Set up the MySQL database.

Open your MySQL client (e.g., MySQL Command line) and create a
new database named "pythondb”.

Step 3: Connect Python to MySQL and interact with the database.

Output-
ACTIVITY-31
AIM: Perform CRUD operations on MySQL database using python.

Step 1 – Create database with named “sudhirdb” and then use


pythondb

Step2 – CREATE MySQL table using python Code

Output
Step 3 – Insert Data into trainee tables

Code

Output

Step 4 – READ Data from the trainee tables


Code

Output

Step 5 – UPDATE Data from the trainee tables

Code

Output
Step 6 – DELETE Data from the trainee tables

Code

Output
ACTIVITY-32
AIM: Perform CRUD operations on Mongo DB database using python.
To perform CRUD operations (Create, Read, Update, Delete) on a
MongoDB database using Python

Step 1: Install the ‘pymongo’ library, which is the official Python driver for
MongoDB.

pip install pymongo

Step 2: Open CMD and type mongosh then the mongoshell is


opened.

Step 3: Then create a new database and create a new collection.


Step 4: Connect Python to mongoDB and interact with the database.

1.Create

OUTPUT-
2.Read

3.Update

Output
4.Delete

Output

ACTIVITY-33
AIM: Create a class with related data elements and methods. Write
functionalities for basic operations.
ACTIVITY-34
AIM: Python program using a flag to validate if the input given by the
user, is an integer.

ACTIVITY-35
AIM: Create a simple python Flask app with at least 3 basic routes.
Step 1: Install Flask Make sure you have Flask installed. If not, you can install it
using the following command:

pip install Flask


Step 2: Create a Flask App File Create a new Python file for your Flask app.
Let's name it app.py.
Step 3: Import Flask and Create an App Instance In app.py, start by importing
the Flask class from the flask module and creating an instance of it:

from flask import Flask


app = Flask(__name__)
Step 4: Create Routes Define your routes using the @app.route decorator. For
this example, we'll create three routes: home, about, and contact.
from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
return '<h1>Welcome to My Web Page</h1><footer><a href =
"/contact">Contact us</a><br><a href = "/about">About</a></footer>'

@app.route('/about')
def about():
return '<header><a href ="/">Home Page</a></header><h1>About
Us</h1><p>Hello everyone my name is Sudhir<br> I am trainee at NSTI
Noida.</p><footer><a href = "/contact">Contact</a></footer>'

@app.route('/contact')
def contact():
return '<header><a href ="/">Home Page</a></header><h1>Contact Us</h1>You
can reach us at [email protected].<p><footer><a href =
"/about">Contact</a></footer>'

if __name__ == '__main__':
app.run(debug=True)
Step 5: Run the App At the end of your app.py file, add the following code to
run the app:

if __name__ == '__main__':

app.run(debug=True)
Step 6: Run the App Open a terminal, navigate to the directory where your
app.py file is located, and run the following command:

python app.py
Step 7: Access the Routes Open a web browser Click on urls and visit the
following URLs to access the different routes:

Home: http://127.0.0.1:5000/

About: http://127.0.0.1:5000/about

Contact: http://127.0.0.1:5000/contact
DJANGO
ACTIVITY-1
Aim: Install Django framework libraries in python and test for
installation.

Learning outcome: Able to install Django

Step: 1 Install Latest version of Python.

Step: 2 Install Pycharm Community Edition.

STEP:3 Open Pycharm and Create a new project where you want.

STEP:3 Open Terminal and install Django.

py –m pip install Django

Step: 2 To check python version.

py --version

Step: 3 To check Django version.

django-admin --version
Step: 4 To create a new project.

django-admin startproject Project_Name(project)

Step: 5 Then inside the Project.

cd Project_Name(project)

Step: 6 Then start the server.

py manage.py runserver

Then you find url http://127.0.0.1:8000/ (ctrl+click)

After clicking you will see the Django was successfully installed page.
ACTIVITY-2
Aim: Setup first Django application

Learning outcome: Able to display Hello World in the web browser


using Django.

Step: 1 Open Terminal and create a new project.

django-admin startproject Project_Name(project)


Step: 2 Then inside the Project.

cd Project_Name(project)

Step: 3 Then create an application.

py manage.py startapp Application_name(App)

Step: 4 Go to app settings(settings.py), then config to installed apps.

Step: 5 Go to views in application (views.py).


from django.http import HttpResponse
def index(request):
return HttpResponse('Hello world!!')
Step: 6 Now make a urls.py in Application.

Go to three lines on top > New > Python File > Drop a name >Enter
Type Following Code to access all urls in project urls.py file in app
urls.py file

Step: 7 Now go to project urls.py and write code then save the file.

from django.contrib import admin

from django.urls import path, include

urlpatterns = [

path (‘ ’,include(‘application_name.urls’),

path(‘admin/’,admin.site.urls),

]
Step: 8 Then go to project

cd Project_name(project)

Step: 9Then start the server

py manage.py runserver

OUTPUT-

ACTIVITY-3
Aim: Creating Django application with MVT architecture

Learning outcome: Able to understand creating Django using MVT.

STEP: 1 Create a Project through the terminal.

django-admin startproject Project_name

STEP: 2 Switch to project

STEP: 3 Create an app through the terminal.

py manage.py startapp App_Name

STEP: 4 Now open models.py in application and create a model.


STEP: 5 Now installed our app in settings.py.

STEP: 6 Now make the migration for application.

py manage.py makemigrations App_name


STEP: 7 Now migrate to manage.py.

py manage.py migrate

STEP: 8 Now we need to execute and check SQL statement.

py manage.py sqlmigrate App_Name 0001

Then you will see the table creation successfully!!

STEP: 9 Then open the python shell.

py manage.py shell
STEP: 10 Now Add the record into table.

STEP: 11 Go to views.py and define the function to show the records.

STEP: 12 Then go the models.py and add field ‘object’ to class


Myapp.

STEP: 13 Now we create a python file inside the application urls.py


and define the path for views.py
STEP: 14 Then go to project urls.py and add app urls.py file

STEP: 15 Now create a template directory in app folder and inside


templates directory create a index.html file for fetching the records
in table format.
STEP: 16 Run you server.

py manage.py runserver

Output

ACTIVITY-4
Aim: Create Django application that handles file uploading

Learning outcome: Able to understand file handling and how to


upload file.

Step: 1 Create new project folder for file uploading and install Django
in this folder
Step: 2 go to your terminal and Create a Project through the
terminal.

django-admin startproject Project_name

STEP: 2 Switch to project

STEP: 3 Create an app through the terminal.

py manage.py startapp App_Name

STEP: 4 Now installed our app in settings.py.

STEP: 5 Create forms.py file in app folder.


In file_upload_app/forms.py and include fields for name, age, and marksheet:
STEP: 6 Create Views:

In file_upload_app/views.py, create views to handle the form display and file


upload:

STEP: 7 Create Templates:

Inside the file_upload_app/templates directory, create the following HTML


templates.

upload_file.html:
upload_success.html

Step 8 Now go to your app and create new directory with media
name

STEP: 9 Configure Settings:


In your project's settings.py, add or modify the following settings and import
os in your setting.py

STEP: 10 Update URLs:

In file_upload_app/urls.py, create URL patterns for the views:


In file_upload_project/urls.py, include the app's URLs:

STEP: 11 Run Django server and see the output

Output
ACTIVITY-5
Aim: Create Django application that reads data from CSV and
display on page

Learning outcome: Able to understand basic computer network


technology.
ACTIVITY-6
Aim: Create Django application that reads data from JSON and
display on page.

Learning outcome: Able to understand how to reads data from


JSON and display on page.
ACTIVITY-7
Aim: Creating Django application which implement CRUD operations
over database

Learning outcome: Able to create model and perform CRUD


operations.

STEP: 1 Create a Project through the terminal.

django-admin startproject Project_name

STEP: 2 Switch to project


STEP: 3 Create an app through the terminal.

py manage.py startapp App_Name

STEP: 4 Now open models.py in application and create a model.

STEP: 5 Now installed our app in settings.py.


STEP: 6 Now make the migration for application.

py manage.py makemigrations App_name

STEP: 7 Now migrate to manage.py.

py manage.py migrate
STEP: 8 Now we need to execute and check SQL statement.

py manage.py sqlmigrate App_Name 0001

Then you will see the table creation successfully!!

STEP: 9 Then open the python shell.

py manage.py shell

STEP: 10 Now Add the record into table.

STEP: 11 Go to views.py and define the function to show the records.


STEP: 12 Then go the models.py and add field ‘object’ to class
Myapp.

STEP: 13 Now we create a python file inside the application urls.py


and define the path for views.py

STEP: 14 Then go to project urls.py and add app urls.py file


STEP: 15 Now create a template directory in app folder and inside
templates directory create a index.html file for fetching the records
in table format.

STEP: 16 Run you server.

py manage.py runserver
Output

STEP:17 Go to the index.html then add a link over here after the
table tag

STEP:18 Create a new html file add.html and create a form.


STEP:19 Go to views.py and define functions.

STEP:20 Now we need to provide path in urls.py.

STEP:21 Now run the server.


STEP 22: Now define a function add records go to again views.py

Then import HttpResponseRedirect and reverse module in views.py

STEP 23: Now define a path for addrecord function in urls.py

STEP 24: Now run server.


STEP:25 Go to the index.html then define a row for create a link for
delete the record.

Check Output

STEP:26 Then go to the views.py then define a function for delete.


STEP:27 Now define a path for delete function in urls.py

STEP:28 Now run the server and check output.

STEP:29 Go to the index.html then create a link for update the


record.

STEP:30 Now go to views.py and define functions.


STEP:31 Create a new html file update.html and create a form for
update records.

STEP:32 Now define a path for update and updaterecord function in


urls.py

STEP:33 Now run the server and check the output.


ACTIVITY-8
Aim: Create Django application validates user credentials on login
pages

Learning outcome: Able to understand basic user authentication


and authorization.
ACTIVITY-9
Aim: Create admin panel using Django using AJAX.

Learning outcome: Able to understand basic how to implement


AJAX.
ACTIVITY-10
Aim: Perform crud operations in Django app using AJAX at single
page.

Learning outcome: Able to understand basic computer network


technology.

You might also like