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

Python_merged

Uploaded by

affansayeed234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python_merged

Uploaded by

affansayeed234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

A

Project Synopsis
on

“Movie Recommendation System”

Submitted in partial fulfilment for the award of degree of


BACHELOR OF TECHNOLOGY
in
COMPUTER SCIENCE AND ENGINEERING

Submitted By:
Tushar Sharma [21EGJCS158]
Vinay Kumar Jha[21EGJAD027]
Justin tirkey [21EGJCS060]

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


GLOBAL INSTITUTE OF TECHNOLOGY
JAIPUR (RAJASTHAN)-302022
Session 2024-25
Comprehensive Report on Python

Your Name

October 19, 2024


Contents

1 Introduction 3
1.1 History of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Key Features of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Use Cases of Python 4


2.1 Web Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1.1 Example: Simple Flask Application . . . . . . . . . . . . . . . . . . . . . 4
2.2 Data Science and Machine Learning . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.1 Example: Data Analysis with Pandas . . . . . . . . . . . . . . . . . . . . 4
2.3 Automation and Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3.1 Example: Automating File Renaming . . . . . . . . . . . . . . . . . . . . 5

3 Python Syntax and Basics 6


3.1 Variables and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.1.1 Example: Variable Declaration . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2.1 Example: If Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2.2 Example: For Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3.1 Example: Defining a Function . . . . . . . . . . . . . . . . . . . . . . . . . 7

4 Object-Oriented Programming in Python 8


4.1 Basic Concepts of OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.1.1 1. Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.1.2 Example: Encapsulation in Python . . . . . . . . . . . . . . . . . . . . . . 8
4.1.3 2. Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.1.4 Example: Inheritance in Python . . . . . . . . . . . . . . . . . . . . . . . 9
4.1.5 3. Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.1.6 Example: Polymorphism in Python . . . . . . . . . . . . . . . . . . . . . . 9
4.1.7 4. Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.1.8 Example: Abstraction in Python . . . . . . . . . . . . . . . . . . . . . . . 9

5 Popular Libraries in Python 11


5.1 NumPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.1.1 Example: Using NumPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.2 Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.2.1 Example: Data Manipulation with Pandas . . . . . . . . . . . . . . . . . . 11
5.3 Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.3.1 Example: Simple Plotting with Matplotlib . . . . . . . . . . . . . . . . . . 12

6 Conclusion 13

1
7 References 14

2
Chapter 1

Introduction

Python is a high-level, interpreted programming language known for its clear syntax and read-
ability. Developed by Guido van Rossum and first released in 1991, Python has gained immense
popularity across various domains. Its design philosophy emphasizes code readability, making
it accessible for beginners and efficient for experienced programmers. Python is used in web
development, data analysis, artificial intelligence, scientific computing, automation, and more.

1.1 History of Python


The Python programming language has evolved significantly since its inception. Python 2.0
was released in 2000, introducing new features like list comprehensions and a garbage collection
system. The release of Python 3.0 in 2008 marked a major turning point, as it aimed to rectify
design flaws in earlier versions. This version is not backward compatible, which prompted some
controversy but ultimately led to a more streamlined language. The latest version of Python
continues to add features and enhance performance.

1.2 Key Features of Python


• Simple and Easy to Learn: Python’s syntax closely resembles English, making it easier
for newcomers to learn.

• Interpreted Language: Python is an interpreted language, meaning that code is exe-


cuted line by line, allowing for quick testing and debugging.

• Dynamically Typed: Python does not require variable declaration; types are deter-
mined at runtime, which offers flexibility but requires careful handling.

• Extensive Libraries and Frameworks: Python boasts a rich ecosystem of libraries


and frameworks, such as NumPy for numerical computing, Pandas for data analysis, and
Django for web development.

• Community Support: Python has a vibrant community that contributes to its libraries,
documentation, and forums, making it easy to find help and resources.

3
Chapter 2

Use Cases of Python

Python is employed in a myriad of applications due to its versatility. Here are some notable
use cases:

2.1 Web Development


Frameworks like Django and Flask simplify the development of robust web applications.

2.1.1 Example: Simple Flask Application


Flask is a micro web framework for Python that provides the essentials to build web applications.

Listing 2.1: Simple Flask Application


from flask import Flask

app = Flask ( __name__ )

@app . route ( ’/ ’)
def home () :
return " Hello , ␣ World ! "

if __name__ == ’ __main__ ’:
app . run ( debug = True )

This simple Flask application creates a web server that responds with ”Hello, World!” when
accessed at the root URL. The ‘debug=True‘ option allows for real-time error checking during
development.

2.2 Data Science and Machine Learning


Python is the preferred language for data scientists due to its extensive libraries.

2.2.1 Example: Data Analysis with Pandas


Pandas is a powerful library for data manipulation and analysis.

Listing 2.2: Data Analysis with Pandas


import pandas as pd

# Load a dataset

4
data = pd . read_csv ( ’ data . csv ’)

# Display basic statistics


print ( data . describe () )

# Filter data for analysis


filtered_data = data [ data [ ’ column_name ’] > threshold ]

In this example, we load a CSV file into a DataFrame, display its statistical summary, and
filter the data based on a specific condition.

2.3 Automation and Scripting


Python is widely used for automating repetitive tasks.

2.3.1 Example: Automating File Renaming


With the ‘os‘ library, you can automate file management tasks easily.

Listing 2.3: File Renaming Script


import os

# Rename files in a directory


for filename in os . listdir ( ’ path / to / directory ’) :
if filename . endswith ( ’. txt ’) :
os . rename ( os . path . join ( ’ path / to / directory ’ , filename ) ,
os . path . join ( ’ path / to / directory ’ , f ’ new_prefix_
{ filename } ’) )

This script iterates through a directory and renames all ‘.txt‘ files by adding a prefix.

5
Chapter 3

Python Syntax and Basics

Understanding Python’s syntax is crucial for writing effective code.

3.1 Variables and Data Types


Python supports various data types, including integers, floats, strings, and lists. Variables are
dynamically typed and do not require explicit declaration.

3.1.1 Example: Variable Declaration

Listing 3.1: Variable Declaration


x = 10 # Integer
y = 3.14 # Float
name = " Python " # String
is_valid = True # Boolean

3.2 Control Structures


Control structures, such as if statements, loops, and exception handling, are fundamental for
flow control in Python.

3.2.1 Example: If Statement

Listing 3.2: If Statement Example


if x > 5:
print ( " x ␣ is ␣ greater ␣ than ␣ 5 " )
else :
print ( " x ␣ is ␣ not ␣ greater ␣ than ␣ 5 " )

3.2.2 Example: For Loop

Listing 3.3: For Loop Example


for i in range (5) :
print ( i )

This loop will print numbers from 0 to 4. Python’s ‘range()‘ function generates a sequence
of numbers.

6
3.3 Functions
Functions are reusable blocks of code that perform specific tasks.

3.3.1 Example: Defining a Function

Listing 3.4: Defining a Function


def greet ( name ) :
return f " Hello , ␣ { name }! "

print ( greet ( " Python " ) )

The ‘greet‘ function takes a name as an argument and returns a greeting string.

7
Chapter 4

Object-Oriented Programming in
Python

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes
to structure software in a way that enhances code reusability and maintainability. Python fully
supports OOP and allows for the creation of classes that can encapsulate data and functions.

4.1 Basic Concepts of OOP


The four fundamental principles of OOP are encapsulation, inheritance, polymorphism, and
abstraction.

4.1.1 1. Encapsulation
Encapsulation is the bundling of data and methods that operate on that data within a single
unit, or class. This principle restricts direct access to some of an object’s components and can
prevent the accidental modification of data.

4.1.2 Example: Encapsulation in Python

Listing 4.1: Encapsulation Example


class BankAccount :
def __init__ ( self , owner , balance =0) :
self . owner = owner
self . __balance = balance # Private attribute

def deposit ( self , amount ) :


if amount > 0:
self . __balance += amount

def withdraw ( self , amount ) :


if 0 < amount <= self . __balance :
self . __balance -= amount

def get_balance ( self ) :


return self . __balance

In this example, the ‘BankAccount‘ class encapsulates the ‘balance‘ attribute, making it
private and accessible only through its methods.

8
4.1.3 2. Inheritance
Inheritance allows a class to inherit attributes and methods from another class. This promotes
code reuse and creates a hierarchical relationship between classes.

4.1.4 Example: Inheritance in Python

Listing 4.2: Inheritance Example


class Animal :
def speak ( self ) :
return " Animal ␣ speaks "

class Dog ( Animal ) :


def speak ( self ) :
return " Woof ! "

class Cat ( Animal ) :


def speak ( self ) :
return " Meow ! "

Here, the ‘Dog‘ and ‘Cat‘ classes inherit from the ‘Animal‘ class and override the ‘speak‘
method to provide specific behavior.

4.1.5 3. Polymorphism
Polymorphism allows methods to do different things based on the object it is acting upon, even
when they share the same name. This can be achieved through method overriding and method
overloading.

4.1.6 Example: Polymorphism in Python

Listing 4.3: Polymorphism Example


def animal_sound ( animal ) :
print ( animal . speak () )

dog = Dog ()
cat = Cat ()

animal_sound ( dog ) # Outputs : Woof !


animal_sound ( cat ) # Outputs : Meow !

In this example, the ‘animals ound‘f unctioncallsthe‘speak‘methodof dif f erentanimalobjects, demonstratin

4.1.7 4. Abstraction
Abstraction is the principle of hiding complex implementation details and showing only the
essential features of an object.

4.1.8 Example: Abstraction in Python

9
Listing 4.4: Abstraction Example
from abc import ABC , abstractmethod

class Shape ( ABC ) :


@abstractmethod
def area ( self ) :
pass

class Rectangle ( Shape ) :


def __init__ ( self , width , height ) :
self . width = width
self . height = height

def area ( self ) :


return self . width * self . height

The ‘Shape‘ class is an abstract base class that defines a common interface for all shapes,
while the ‘Rectangle‘ class implements the ‘area‘ method.

10
Chapter 5

Popular Libraries in Python

Python’s extensive libraries provide pre-built functionalities that streamline development across
various domains.

5.1 NumPy
NumPy is a powerful library for numerical computing, enabling array operations and mathe-
matical functions.

5.1.1 Example: Using NumPy

Listing 5.1: NumPy Example


import numpy as np

# Create a NumPy array


arr = np . array ([1 , 2 , 3 , 4 , 5])

# Calculate the mean


mean_value = np . mean ( arr )

This code snippet demonstrates how to create a NumPy array and calculate its mean.

5.2 Pandas
Pandas is a library for data manipulation and analysis, providing data structures like DataFrames.

5.2.1 Example: Data Manipulation with Pandas

Listing 5.2: Pandas Example


import pandas as pd

data = { ’ Name ’: [ ’ Alice ’ , ’ Bob ’ , ’ Charlie ’] ,


’ Age ’: [25 , 30 , 35]}

df = pd . DataFrame ( data )

# Display the DataFrame


print ( df )

11
Here, we create a DataFrame from a dictionary and print its contents.

5.3 Matplotlib
Matplotlib is a plotting library that provides a wide range of visualization options.

5.3.1 Example: Simple Plotting with Matplotlib

Listing 5.3: Matplotlib Example


import matplotlib . pyplot as plt

x = [1 , 2 , 3 , 4]
y = [10 , 20 , 25 , 30]

plt . plot (x , y )
plt . xlabel ( ’X - axis ’)
plt . ylabel ( ’Y - axis ’)
plt . title ( ’ Simple ␣ Plot ’)
plt . show ()

This code generates a simple line plot using Matplotlib.

12
Chapter 6

Conclusion

Python’s versatility and ease of use make it a leading choice for developers across various fields.
Its extensive libraries, strong community support, and clear syntax allow for efficient problem-
solving and application development. As Python continues to evolve, it remains an essential
tool for anyone looking to enter the world of programming, data science, web development, or
automation.

13
Chapter 7

References

• Van Rossum, G., & Drake, F. L. (2009). Python 3 Reference Manual.

• Lutz, M. (2013). Learning Python. O’Reilly Media.

• McKinney, W. (2018). Python for Data Analysis. O’Reilly Media.

• Hunter, J. D. (2007). Matplotlib: A 2D graphics environment. Computing in Science &


Engineering, 9(3), 90-95.

• NumPy. (n.d.). NumPy. https://numpy.org

• Pandas. (n.d.). Pandas. https://pandas.pydata.org

• Flask. (n.d.). Flask. https://flask.palletsprojects.com

14

You might also like