Presents
Python
-Krishna katigar
What is Python
Python is a High-level, Object Oriented Programming Language with Object,
Modules, Thread, Exception..Etc
It is simple but yet Powerful Programming language
Python is a Programming Language that coombines features of C and Java
Application For Python
Web Application – Django, Pyramid, Flask, Bottle
Desktop[ GUI Application – Tkinter]
Console Based Application
Games and 3D Application
Mobile Application
Scientfic and Numeric
Data Science
Machine Learning – scikit-learn and TensorFlow
Data Analysis – Mataplotib, Scaborn
Business Application
Compilor
Python Compilor – Python Compilor Converts the Program source code into
Byte code.
Type of Python Compilors:
• Cpython
• Jpython/Jython
• PyPy
• RubyPython
• IronPython
• StacKless Python
• AnacondaPython
Compile and Run
Write a Source code/Program
Compile the program using ptython cmpilor
Compile converts the Python Program into Byte Code
Copmputer/Machine can not understand Byte Code sowe Convert into
Machine Code using PVM
PVM uses an interpreter which understands the Byte Code and
converts it into machine code
Machine Coe instructions are then Executed by the processor and
results are displayed.
Run
Program
Using
Source code/ PVM Binary Code/
Byte Code Computer
program Compile Machine Code
using
Python Output
File name.py Compilor File name.pyc
PVM(Python Virtual Machine)
Python Virtual machine(PVM) is Program which provides
programming environment
The role of PVM is t convert the Byte code Instructions into
machine Code so the Computer can execute those machine
code instrucions and display the output.
Interpreter converts the byte code into machin code and
sends that machine code to the computer
Interpreter
Source code/ Binary Code/
Compile Byte Code Computer
program Machine Code
using
Python
Output
Compilor
PVM
Identfier
An idenifier is a name having a few letters, numbers and special
characters_(Underscore).
It Should always start with a non-numeric character.
Its is used to identify a variable, function, symbolic constant, class etc.
Ex :
• X2
• PI
• Sigma
Keywords or Reserved Words
Keyword means which contains some special meaning.
Python language uses the dollowing keywords which are not avaiable to
users to ue them as identifires.
Data Types
Python – Data Types
Sequence
Numeric Dictionary Boolean Set
Type
Integer Float String Tuple
Complex
List
Number
Dictionary
What is a Python Dictionary?
A dictionary is a built-in Python data type that stores key-value pairs.
Keys must be unique and immutable (e.g., strings, numbers, tuples).
Values can be of any data type.
Common Dictionary Methods
keys(): Returns a view of all keys.
values(): Returns a view of all values.
items(): Returns a view of key-value pairs.
get(key, default): Retrieves value for a key with an optional default.
pop(key, default): Removes and returns value for a key.
Set
What is a Python Set?
A set is an unordered collection of unique elements.
Sets are mutable and do not allow duplicate values.
Key Operations on Sets
Adding Elements: Use add() method.
Removing Elements: Use remove() or discard() methods.
Checking Membership: Use in keyword.
Common Set Methods
union(other_set): Returns a set with elements from both
sets.
intersection(other_set): Returns a set with common
elements.
difference(other_set): Returns a set with elements in the
current set but not in the other.
difference_update(other_set): Updates the set to remove
elements found in the other set.
symmetric_difference(other_set): Returns elements in
List
Lists are mutable sequences that can hold multiple items.
You can manipulate lists using various built-in methods.
Common List Operations:
Adding items: append(), extend()
Removing items: remove(), pop()
Accessing items: list[index]
Tuple
What is a Python Tuple?
A tuple is an immutable sequence of Python objects.
Tuples are used to store multiple items in a single variable.
Unlike lists, tuples cannot be modified after their creation.
Key Operations on Tuples
Accessing Elements: Use indexing, similar to lists.
Slicing: Extract a portion of the tuple.
Concatenation: Combine tuples with +.
Repetition: Repeat tuple elements with *
Common Tuple Methods
count(value): Returns the number of occurrences of a value.
Index(value): Returns the index of the first occurrence of a value
Tuple Unpacking
Tuple unpacking allows you to assign elements of a tuple to multiple variables in a single
statement.
String
Strings are sequences of characters.
Python provides several methods for manipulating strings.
Common String Operations:
Accessing characters: string[index]
Slicing: string[start:end]
String methods: upper(), lower(), replace()
Conditional Statements
Conditional statements allow the program to make decisions based on
conditions.
They control the flow of execution.
Syntax:
if condition:
# code to execute if condition is
True
elif another_condition:
# code to execute if
another_condition is True
else:
Loops in Python
Loops are used to execute a block of Example: For Loop
code repeatedly.
They help automate repetitive for i in range(5):
tasks. print(i)
Types of Loops: Example: While Loop
For Loop: Iterates over a count = 0
sequence (like a list or a range). while count < 5:
While Loop: Continues to print(count)
execute as long as a specified
count += 1
condition is True.
What is a Function?
A function is a reusable block of code that performs a specific task.
Functions helps in organizing code, making it modular and easier to
maintain.
Syntax:
def function_name(parameters):
# code to execute
return value
Example:Simple Function
Copy code
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
What is Scope?
Example: Local vs Global Scope
Scope refers to the visibility or x = 10 # Global variable
accessibility of variables in different def my_function()
parts of your code. y = 5 # Local variable
There are two main types of scope: print("Inside function:", x, y)
●
Local Scope: Variables defined within my_function()
a function. print("Outside function:", x)
●
Global Scope: Variables defined # print(y) # This would raise an error
outside of any function. ●
The variable x is accessible both inside and
outside the function, while y is only accessible
inside my_function.
●
Trying to print y outside the function would raise
an error.
Parameters and Return Values
Parameters: Variables that are Example: Function with Parameters
passed into a function when it is and Return
called.
def add(a, b):
Return Values: The output that a
function produces when it is called. return a + b
Benefits: result = add(3, 5)
print(result) # Output: 8
Functions can perform
operations with different inputs add(2,5)
and return results. The add function takes two
parameters, a and b, and returns
their sum.
The result of the function call is
stored in the variable result and
printed.
Introduction to Modules
What are Modules?
Explanation:
A module is a file containing Python code (functions, classes,
variables).
Modules allow for code organization and reuse.
Python has many built-in modules, and you can create your own.
Example: Creating a Custom Module
Code (in a separate file named mymodule.py):
def multiply(a, b):
return a * b
How to Import Modules
Use the import statement to include a module in your code.
You can import specific functions or classes or import the entire
module.
Syntax:
Import module_name
from module_name import
function_name
Importing a Custom Module
Import mymodule
result = mymodule.multiply(4, 5)
print(result) # Output: 20
Here, we define a function multiply in a file called mymodule.py.
This module can be imported into other Python files.
Introduction to Object-Oriented Programming (OOP)
•OOP is a programming paradigm based on the concept of "objects"
•Objects are instances of classes which can contain:
•Data (attributes)
•Functions (methods)
•A Class is a blueprint for creating objects
•It defines attributes and methods common to all objects of that type
•
•An Object is an instance of a class
•Created using the class name
•The __init__ method a special method called when an object is created
•Used to initialize object attributes
Constructors & Inheritance in Python
•Special method used to initialize objects
•Defined using __init__()
•Automatically called when an object is created
Types of Constructors
Default Constructor: No arguments
Parameterized Constructor: Accepts arguments to set values
•
Constructors & Inheritance in Python
•Allows a class (child) to inherit properties and methods from another class (parent)
•Promotes code reuse
Types of Inheritance
• Single
• Multiple
• Multilevel
• Hierarchical
•
What is Polymorphism?
Polymorphism means many forms
The same method name can have different behaviors depending on
the object
Makes code more flexible and extensible
Exception Handling in Python
•An exception is an error that occurs during program execution
•Without handling, it crashes the program
•Python provides a way to catch and handle exceptions using:
•try
•except
•Finally
The try-except Block
• Code that might raise an error is written inside try
• Error handling code goes inside except
•
•finally block always runs, no matter what
•Used for cleanup actions (like closing files)
What is a Regular Expression?
A regular expression (regex) is a special pattern used to search,
match, or extract data from text.
Used in:
Form validation (emails, phone numbers)
Data extraction (scraping, logs)
Text search and replace
Python uses the re module to work with regex.
Basic Regex Syntax
●
Pattern ●
Meaning ●
Example Match
●
\d ●
Digit (0-9) ●
2, 98
●
Word character (a-z, A-Z,
●
\w ●
hello_123
0-9, _)
●
Any character except
●
. ●
a.c matches abc, axc
newline
●
+ ●
One or more ●
a+ matches a, aa, aaa
●
* ●
Zero or more ●
lo* matches l, lo, loo
●
[] ●
Character set ●
[aeiou] matches vowels
●
^ ●
Start of string ●
^Hi matches "Hi there"
●
$ ●
End of string ●
end$ matches "The end"
Introduction to File Handling
File handling allows Python to create, read, write, and delete file
It is useful for:
• Storing data permanently
• Logging
• Reading configuration or data files
• Built-in functions:
• open(), read(), write(), close()
File Modes:
●
Mode ●
Description
●
'r' ●
Read (default)
●
'w' ●
Write (overwrite)
●
'a' ●
Append
●
'r+' ●
Read and write
Reading and Writing a File
# Writing to a file
with open("demo.txt", "w") as f:
f.write("Hello, Python!\nFile handling is easy.")
# Reading the file
with open("demo.txt", "r") as f:
content = f.read()
print(content)
Appending and Reading Lines
# Appending new data
with open("demo.txt", "a") as f:
f.write("\nThis is a new line.")
# Reading line by line
with open("demo.txt", "r") as f:
for line in f:
print(line.strip())
Presents
Web Scrapping
-Krishna katigar
What is Web Scraping?
Web scraping is the process of automatically
extracting data from websites.
It helps collect information from websites in a
structured format.
Often used in data analysis, market research, price
monitoring, and more.
Why Use Web Scraping?
•Automates data collection.
•Saves time and reduces manual work.
•Useful when APIs are unavailable.
•Enables large-scale data extraction.
Common Use Cases
•Price comparison sites
•News aggregation
•Social media sentiment analysis
•Job listing aggregators
•Real estate market analysis
Web Scraping Tools
BeautifulSoup (Python)
Scrapy (Python)
Selenium (Browser automation)
Puppeteer (Node.js)
Requests (Python library for making HTTP
requests)
Introducing BeautifulSoup
• A Python library used to extract data from HTML and XML files
• Parses the page and creates a parse tree for easy data extract
• Often used with requests library for fetching web pages
Installing BeautifulSoup
pip install beautifulsoup4
pip install requests
Simple Web Scraping Example
import requests
from bs4 import BeautifulSoup
url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.title.text
print("Page Title:", title)
Features of BeautifulSoup
•Simple and Pythonic syntax.
•Powerful tree traversal and searching.
•Supports multiple parsers (like html.parser, lxml, etc.).
•Easy to integrate with other Python libraries.
Email
[email protected] Conta ct: +91-9449034387 / +91-9353412662
Website www.doketrun.com
Address DocketRun Tech Pvt. Ltd.
Hubballi, Karnataka - 580030, India
Confidential and Proprietary. Copyright (c) by DocketRun Tech Pvt Ltd. All Rights
www.docketrun.com Reserved All images are actual or prototype DocketRun products