0% found this document useful (0 votes)
12 views

Naveen Python Lab

Python Lab Manual Amity
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Naveen Python Lab

Python Lab Manual Amity
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 66

AMITY INSTITUTE OF

INFORMATION TECHNOLOGY
(AIIT)

Naveen Singh
Enrolment No. A710145023049
Python (Django & Kivy) Lab MCA
Semester – 1
2

Index
Sl.No Experiments Page No
1 Python program for Accepting input from user and 7
print it

2 Printing addition, subtraction, multiplication and 7


division of two numbers using user input
3 7
Finding data type of value assigned to a variable

4 Write a Python program to find if the number entered 8


by user is even or odd

5 Write a python program to find the number entered by 8


user is positive or not

6 Write a python program to find the largest among three 9


numbers

7 Write a python program to find if the person is eligible 9


for voting or not.

8 Write a python program to find if the number is 10


divisible by 12 or not

9 Write a python program to calculate the electricity bill 10


according to

10 Write a python program to print the last two digit of a 11


number
11 Write a python program to display the grade based on 11
student's percentage

12 Multiplication table of a given number 12


13 12
Sum of all odd and even number in a given range

14 To accept a word from user and reverse it 13

15 To find if a number is armstrong number or not 13


16 To print all the number in a given range 14
17 Python program to unpack tuple in variables 14
18 Since a tuple is immutable that means we cannot add 15
an element in a tuple. Write a program to create a new
tuple by merging tuples with the + operator

19 15
Write a python program to convert tuple into string

20 Write a program to check whether element exist in 16


tuple or not

21 Write a program to reverse a tuple 16


22 Write a program to multiply all the elements of tuple 17

23 Python program to add ed at the end of a given verb 17


string

24 18
Python program to check if the string is a pangram.

3
25 Python program takes two lists as inputs and prints 18
common Output in the third list.

26 Python program to find if the number is a perfect 19


number or not.

27 Fibonacci series 19
28 Kivy Installation 20
29 Creating simple hello world program 21

30 Kivy Program to display Image widget 22


31 Kivy Program to create Button 23 - 24
32 Kivy Program to display Label and checkboxes 25 - 26
33 Installation of django 27 -28
34 Hello world program in django 29 - 30
35 Program to print date and time using django 31 - 32
36 Program to print time after 10 hrs through dynamic 33 -35
URLs using django

37 Program to demonstrate Template in django 36 - 38


38 Creating Django admin interface 39 - 41
39 Creating Django Models 42 - 44
40 CRUD operations on models 45 - 47
41 a) Python Programs on functions 48 - 49
b) Programs on recursion to find the factorial of a
number

4
42 a) Python Programs on lambda function 50 - 51
b) Programs on _init_function

43 a) Create a class which has init function and normal 52


def function for displaying the details b) Program to
print star pattern

44 Program on matplotlib 53 - 56
45 Program on pandas 57 - 59
46 Program on Web scraping using beautifulsoup 60 - 62
47 Programs on scipy 63 -64

5
Practical No
1
Python program for Accepting input from user and print it

Practical No 2
Printing addition, subtraction, multiplication and division of two
numbers using user input

6
Practical No
Write a python program to find
Practical No 3
Finding data type of value assigned to a variable
4
Python if the number entered by
user is even or odd

Practical No 5
Write a python program to find the number entered by user is positive
or not

7
Practical No
Write a program to find
6 the largest among
three
numbers

Practical No 7
Write a python program to find if the person is eligible for voting or
not.

8
Practical No
Write a python program to find

9
Practical No
Write a python program to find
8
if the number is divisible
by 12 or not

Practical No 9
Write a python program to calculate the electricity bill according to

10
Practical No
Write a python program to

10
print the last two digit of a
number

Practical No 11
Write a python program to display the grade based on student's
percentage

11
Practical No

12
Practical No
Write a python program to
12 Multiplication
table of a given number

13
Practical No
Practical No 13
Sum of all odd and even number in a given range

14
Practical No
14
To accept a word from user and reverse it

Practical No 15
To find if a number is armstrong number or not

15
Practical No

16
To print all the number in a given range

16
Practical No

Practical No 17
Python program to unpack tuple in variables
18
Since tuple is immutable that means we cannot add an element in a
tuple. Write a program to create a new tuple by merging tuples with
the + operator.

17
Practical No

Practical No 19
Write a python program to convert tuple into string

18
Practical No
program to
20
Write a check whether element exist in tuple or
not

Practical No 21
Write a program to reverse a tuple

19
Practical No
program to
22
Write a multiply all the elements of tuple

Practical No 23
Python program to add ed at the end of a given verb string

20
Practical No
program to
24
Python check if the string is a pangram.

Practical No 25
Python program to take two lists as inputs and prints common Output
in the third list.

21
Practical No
program to

26
Python find if the number is a perfect number or not.

22
Practical No
program to

Practical No 27
Fibonacci series

23
Practical No
28
Kivy Installation
Step 1: Update the pip and wheel before installing kivy by entering this
command in cmd-
python -m pip install --upgrade pip wheel setuptools

Step 2: Install the dependencies-


python -m pip install docutils pygments pypiwin32
kivy.deps.sdl2 kivy.deps.glew python -m pip install
kivy.deps.gstreamer python -m pip install kivy.deps.angle

Step 3: Install kivy.


python -m pip install kivy

24
Practical No
29 Creati
ng simple hello world program
import kivy
kivy.require('1.10.0')

from kivy.app import App from

kivy.uix.button import Label

class HelloKivy(App):

def build(self): return

Label(text ="Hello World")

helloKivy = HelloKivy()
helloKivy.run()

Output:

25
Practical No
30 Kivy
Program to display Image widget
import kivy from kivy.app import App
kivy.require('1.9.0') from
kivy.uix.image import Image,AsyncImage
class MyApp(App):

def build(self):

return AsyncImage(source
='https://cuet-2023-public.s3.ap-south-1.amazonaws.com/site-admin23/
ac61e5f2feb 4b0f534b1bc4794cdc772cf1679dca69cc262d5c710ef24b321a83/Amity
%20University%20Mum bai1677911309_upload_logo.jpg')
MyApp().run()

Output:

26
Practical No
31
Kivy Program to create Button
from kivy.app import App from
kivy.uix.button import Button from
kivy.uix.label import Label from
kivy.uix.boxlayout import BoxLayout

class ButtonApp(App):
def build(self):
layout = BoxLayout(orientation='vertical')
self.message_label = Label(text='')

button = Button(text='Click me', size_hint=(None, None),


size=(200, 50), pos_hint={'center_x': 0.5, 'center_y': 0.5},
background_color=(1, 1, 0, 1))
button.bind(on_press=self.on_button_press)

layout.add_widget(button)

layout.add_widget(self.message_label)

return layout

def on_button_press(self, instance):


self.message_label.text = 'Hello Everyone\nWelcome to Kivy'

if __name__ == '__main__':
ButtonApp().run()

27
Output:

Onclick

32 Kivy
Program to display Label and checkboxes
import kivy from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label from
kivy.uix.checkbox import CheckBox from

28
Practical No
kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import
BoxLayout class check_box(BoxLayout):

def __init__(self, **kwargs): super(check_box,


self).__init__(**kwargs) self.orientation = 'vertical'
self.add_widget(Label(text='Subjects Studied',
halign='center')) checkbox_layout = GridLayout(cols=2,
spacing=-20)
checkbox_layout.add_widget(Label(text='Male')) self.active
= CheckBox(active=False)
checkbox_layout.add_widget(self.active)
checkbox_layout.add_widget(Label(text='Female'))
self.active = CheckBox(active=False)
checkbox_layout.add_widget(self.active)
checkbox_layout.add_widget(Label(text='Other'))
self.active = CheckBox(active=False)
checkbox_layout.add_widget(self.active)
self.add_widget(checkbox_layout)

class CheckBoxApp(App):
def build(self):
return check_box()

if __name__ == '__main__':
CheckBoxApp().run()

Output:

29
33
Installation of django
Step 1: Create virtual environment in django: We should first go to the
directory where we want to create the virtual environment then we type the
following command to create virtual environment in django.
python -m venv env_site

Step 2: Activate the virtual environment: Run the activation script located in
the Scripts directory within the virtual environment folder.
env_site\Scripts\activate.bat

Step 3: Install Django: Install django by giving following command

30
Practical No
pip install django

Step 4: Start a project by following command-


django-admin startproject geeks_site

Step 5: Change directory to geeks_site


cd geeks_site

Step 6: Start the server- Start the server by typing following command in
cmd-
python manage.py runserver

Step 7: To check whether the server is running or not go to the web


browser and enter http://127.0.0.1:8000/ as URL.

31
32
Practical No
34
Hello world program in django
Step 1: Create a new Django project by running:
django-admin startproject hello_project

This will create a directory named hello_project with the basic structure of a
Django project.

Step 2: Navigate to the Project Directory


cd hello_project

Step 3: Create a Django app within your project. This is where the
application-specific code will reside.
python manage.py startapp hello_app

Step 4: Define a View


Open the views.py file in the hello_app directory. Define a simple view
function that will handle the HTTP request and return a basic response.
views.py
from django.http import HttpResponse

def hello_world(request):
return HttpResponse("Hello, World!")

Step 5: Configure URLs


Create a urls.py file in the hello_app directory to define the URL patterns
for your app.
urls.py
from django.urls import path
from .views import
hello_world

urlpatterns = [ path('hello/', hello_world,


name='hello_world'), ]

Step 6: Include App URLs in Project URLs

33
Open the urls.py file in the hello_project directory and include the URLs of
the hello_app. urls.py
from django.contrib import admin
from django.urls import path,
include

urlpatterns = [ path('admin/',
admin.site.urls),
path('app/',
include('hello_app.urls')), ]
Step 7: Start the Django development server:
python manage.py runserver

Output:

35
Program to print date and time using django.
Step 1: Create a new Django project by running:
django-admin startproject hello_project

This will create a directory named hello_project with the basic structure of a
Django project.

Step 2: Navigate to the Project Directory


cd hello_project

Step 3: Create a Django app within your project. This is where the
application-specific code will reside.
python manage.py startapp hello_app

34
Practical No
Step 4: Define a View
Open the views.py file in the hello_app directory, and import the necessary
modules: views.py
from django.http import HttpResponse
from datetime import datetime

def current_datetime(request):
now = datetime.now() formatted_datetime = now.strftime("%Y-
%m-%d %H:%M:%S") return HttpResponse(f"Current Date and Time:
{formatted_datetime}")

Step 5: Configure URLs


Create a urls.py file in the hello_app directory and define the URL pattern
for the current_datetime view urls.py
from django.urls import path
from .views import
current_datetime urlpatterns = [
path('current_datetime/',
current_datetime,
name='current_datetime'),
]

Step 6: Include App URLs in Project URLs


Open the urls.py file in the hello_project directory and include the URLs of
the hello_app. urls.py
from django.urls import path
from .views import
current_datetime

urlpatterns = [ path('current_datetime/', current_datetime,


name='current_datetime'),
] from django.contrib import
admin from django.urls import
path, include

urlpatterns = [ path('admin/',
admin.site.urls),
path('app/',
include('hello_app.urls')), ]
Step 7: Start the Django development server:

35
python manage.py runserver

Output:

36
Program to print time after 10 hrs through dynamic URLs using django
Step 1: Create a new Django project by running:
django-admin startproject hello_project

This will create a directory named hello_project with the basic structure of a
Django project.

Step 2: Navigate to the Project Directory


cd hello_project

Step 3: Create a Django app within your project. This is where the
application-specific code will reside.
python manage.py startapp hello_app

Step 4: Define a View


Create a view that calculates both the current time and the time after 10
hours views.py
from django.http import HttpResponse
from datetime import datetime,
timedelta

def time_info(request):

36
Practical No
now = datetime.now() formatted_current_time =
now.strftime("%Y-%m-%d %H:%M:%S")

time_after_10_hours = now + timedelta(hours=10)


formatted_time_after_10_hours = time_after_10_hours.strftime("%Y-%m-
%d
%H:%M:%S")

response_text = f"Current Time: {formatted_current_time}<br/>"


response_text += f"Time after 10 hours:
{formatted_time_after_10_hours}" return HttpResponse(response_text)
Step 5: Configure URLs
Create a urls.py file in the hello_app directory and and add a dynamic URL
pattern for the time_info view urls.py
from django.http import HttpResponse
from datetime import datetime,
timedelta

def time_info(request):
now = datetime.now() formatted_current_time =
now.strftime("%Y-%m-%d %H:%M:%S")

time_after_10_hours = now + timedelta(hours=10)


formatted_time_after_10_hours = time_after_10_hours.strftime("%Y-%m-
%d
%H:%M:%S")

response_text = f"Current Time: {formatted_current_time}<br/>"


response_text += f"Time after 10 hours:
{formatted_time_after_10_hours}"

return HttpResponse(response_text)
from django.urls import path
from .views import time_info

urlpatterns = [ path('time_info/', time_info,


name='time_info'), ]
Step 6: Include App URLs in Project URLs
Open the urls.py file in the hello_project directory and include the URLs of
the hello_app.
urls.py
from django.http import HttpResponse

37
from datetime import datetime,
timedelta

def time_info(request):
now = datetime.now() formatted_current_time =
now.strftime("%Y-%m-%d %H:%M:%S")

time_after_10_hours = now + timedelta(hours=10)


formatted_time_after_10_hours = time_after_10_hours.strftime("%Y-%m-%d
%H:%M:%S")

38
response_text = f"Current Time: {formatted_current_time}<br/>"
response_text += f"Time after 10 hours:
{formatted_time_after_10_hours}"

return HttpResponse(response_text)
from django.urls import path
from .views import time_info

urlpatterns = [ path('time_info/', time_info,


name='time_info'),
] from django.contrib import
admin from django.urls import
path, include

urlpatterns = [ path('admin/',
admin.site.urls),
path('app/',
include('hello_app.urls')), ]
Step 7: Start the Django development server:
python manage.py runserver

Output:

Practical No 37
Program to demonstrate Template in django
Step 1: Create a new Django project by running:

39
django-admin startproject my_project

This will create a directory named my_project with the basic structure of a
Django project.

Step 2: Navigate to the Project Directory


cd my_project

Step 3: Create a Django app within your project. This is where the
application-specific code will reside.
python manage.py startapp my_app

Step 4: Create a Templates Directory


Create a directory named templates inside your app directory
mkdir my_app/templates

Step 5: Create a Template File


Create a new file named greeting.html inside the my_app/templates
directory greeting.html
<!DOCTYPE html>
<html>
<head>
<title>Greeting Page</title>
</head>
<body>
<h1>Hello, {{ name }}!</h1>
</body>
</html>

Step 6: Define a View


Create a new file named views.py inside the my_app directory
views.py
from django.shortcuts import
render def greet_user(request):
context = {
'name': 'Rajdeep',

40
} return render(request, 'greeting.html',
context)
Step 7: Configure URLs
Create a new file named urls.py inside the my_app directory
urls.py

from django.urls import path


from .views import greet_user

urlpatterns = [ path('greet/', greet_user,


name='greet_user'), ]

Step 8: Include App URLs in Project URLs


Open the urls.py file in the my_project directory and include the URLs of
the my_app. urls.py
from django.contrib import admin
from django.urls import path,
include urlpatterns =
[ path('admin/', admin.site.urls),
path('app/',
include('my_app.urls')),
]

Step 9: Start the Django development server:


python manage.py runserver

Output:

41
Practical No 38
Creating Django admin interface
Step 1: Create a new Django project by running:
django-admin startproject new_project

This will create a directory named new_project with the basic structure of a
Django project.

Step 2: Navigate to the Project Directory


cd new_project

Step 3: Create a Django app within your project. This is where the
application-specific code will reside.
python manage.py startapp new_app

Step 4: Define a Model in new_app/models.py


Open the models.py file inside the new_app directory and define a simple
model. models.py
from django.db import models

class MyModel(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()

def __str__(self):
return self.name

Step 4: Run Migrations


Run the following commands to apply the initial migrations
python manage.py makemigrations
python manage.py migrate

42
Step 5: Create a Superuser: Before using the admin interface, you need to
create a superuser account. Run the following command to create a
superuser:
python manage.py createsuperuser

This command will ask you to enter a username, email address, and
password for the superuser

Step 6: Register the Model in new_app/admin.py


from django.contrib import
admin from .models import
MyModel
admin.site.register(MyModel)

Step 7: Start the Django development server:


python manage.py runserver

Output:

43
Practical No 39
Creating Django Models
Step 1: Create a new Django project by running:
django-admin startproject new_project

This will create a directory named new_project with the basic structure of a
Django project.

Step 2: Navigate to the Project Directory


cd new_project

Step 3: Create a Django app within your project. This is where the
application-specific code will reside.
python manage.py startapp new_app

44
Step 4: Open settings.py file in project directory
settings.py
# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'new_app',
]

Step 5: In the new_app/models.py file we define all objects called Models


models.py
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()

def __str__(self):
return self.name

Step 6: Run Migrations


Run the following commands to apply the initial migrations
python manage.py makemigrations
python manage.py migrate

Step 7: Register the Model in new_app/admin.py


from django.contrib import
admin from .models import
MyModel
admin.site.register(MyModel)

Step 8: Start the Django development server:


python manage.py runserver

45
Output:

Practical No 40 CRUD
operations on models
tep 1: Create a new Django project by running:
django-admin startproject new_project

This will create a directory named new_project with the basic structure of a
Django project.

46
Step 2: Navigate to the Project Directory
cd new_project

Step 3: Create a Django app within your project. This is where the
application-specific code will reside.
python manage.py startapp new_app

Step 4: In the new_app/models.py file we define all objects called Models


models.py
from django.db import models
class MyModel(models.Model):
name =
models.CharField(max_length=100)
description = models.TextField()
def __str__(self): return self.name

Step 5: Run Migrations


Run the following commands to apply the initial migrations
python manage.py makemigrations
python manage.py migrate

Step 6: Register the Model in new_app/admin.py


from django.contrib import
admin from .models import
MyModel
admin.site.register(MyModel)

Step 7: Start the Django development server:


python manage.py runserver

Output:
Create:

47
Update:

Update:

48
Delete:

Practical No 41
a) Python Programs on functions.

49
50
b) Programs on recursion to find the factorial of a number

Practical No 42
a) Python Programs on lambda function

51
b) Programs on _init_function

52
53
Practical No 43
a) Create a class which has init function and normal def function for
displaying the details

b) Program to print star pattern

54
Practical No 44
Program on matplotlib

55
56
57
58
Practical No 45
Program on pandas

59
60
61
Practical No 46 Program on
Web scraping using beautifulsoup

62
63
Practical No 47
Programs on scipy

64
65
66

You might also like