UNIT 03
PYTHON
Introduction to Python
Python is a general-purpose, high-level, dynamic
programming language, used across many domains.
Created by Guido van Rossum in 1991 at CWI (Centrum
Wiskunde & Informatica), Netherlands.
Known for its clear syntax and readability, Python supports
multiple programming paradigms like procedural, object-
oriented, and functional programming.
Python consistently ranks among the top programming
languages globally
(Source: TIOBE Index, Stack Overflow Developer Survey).
Features of Python
Easy to Learn: Syntax is similar to English, making it intuitive
for beginners.
Dynamic Typing: No need to declare variable types; Python
infers them at runtime.
No Type Casting Required: Python handles data type
conversion automatically.
Platform Independent: Code runs seamlessly on Windows,
macOS, and Linux.
Interpreted Language: Python executes code line by line,
which helps in quick debugging.
Free and Open Source: Freely available for download and
modification under OSI-approved license.
Simplicity and Popularity
Beginner-Friendly: Minimal syntax and clean code structure
attract students and educators.
Wide Adoption: Used in academics, startups, and global tech
companies like Google, Instagram, and Netflix.
Community Support: Backed by a large and active global
community for resources and help.
Easy Installation: Python setup and installation process is
simple and beginner-friendly.
Extensive Library Support
Python has over 140,000+ libraries available, saving time
and effort in coding.
Library Support:
• A library is a collection of pre-written code that you can use
to perform common tasks.
• Python’s libraries cover a wide range of domains, making it
versatile and efficient for many types of projects.
Data Science:
•pandas: For data manipulation and analysis.
•numpy: For numerical operations.
•matplotlib: For data visualization.
Artificial Intelligence & Machine Learning:
•scikit-learn: Tools for machine learning and data mining.
•tensorflow & keras: Deep learning libraries.
Web Development:
•Flask: Lightweight web framework.
•Django: Full-featured web framework.
Game Development:
•pygame: Library for writing video games.
IoT (Internet of Things):
•[Link]: For controlling Raspberry Pi GPIO pins.
•microPython: Lightweight version of Python for microcontrollers.
Database Access:
•sqlite3: Built-in support for SQLite databases.
•MySQL-connector: For connecting to MySQL databases.
•SQLAlchemy: Powerful SQL toolkit and ORM.
Using these libraries, developers can:
• Reuse existing solutions.
• Avoid "reinventing the wheel."
• Speed up development.
• Improve code reliability and efficiency.
Applications of Python
Web Development – Frameworks like Flask and Django help in fast and scalable
web apps.
Data Science & Visualization – Widely used in statistical analysis, charts, and
dashboards.
Machine Learning & Artificial Intelligence – Preferred language for ML models
and AI systems.
Game Development – Used in building 2D/3D games using pygame.
Scientific Computing – Preferred in simulations, research, and numeric
processing.
IoT & Embedded Systems – Used in Raspberry Pi and microcontroller-based
applications.
Database Development – Supports major RDBMS and NoSQL databases.
Why Python is the
Preferred Choice
Versatility: One language can be used for backend,
automation, AI, and even game design.
Simplicity with Power: Offers both ease of learning and
capability to build complex systems.
Industry-Relevant: In-demand skill for data analysts, AI
developers, web developers, and software engineers.
Dynamic Nature: Allows variables to be declared and
initialized simultaneously; no fixed data types needed.
Future-Proof: Integral part of emerging technologies such as
Data Science, AI, IoT, and Big Data.
How to Download and Install
Python
1. Visit the Official Website
Go to [Link]
2. Download Python
1. Click on the "Downloads" tab.
2. The site automatically suggests the best version for your system (e.g.,
"Download Python 3.12.1").
3. Click the download button to get the .exe file (for Windows) or
appropriate installer for windows/ macOS/Linux.
3. Run the Installer
1. Double-click the downloaded installer.
2. Important: Check the box that says "Add Python to PATH" before
clicking Install Now.
3. Click Install Now to begin installation.
[Link] Installation
[Link] Command Prompt or Terminal.
[Link] python --version
[Link] installed correctly, it will display the version
number (e.g., Python 3.12.1).
Optional Tools:
• Install pip (usually comes with Python): Used
to install Python packages.
• Install IDEs like IDLE, VS Code, or PyCharm for
writing Python code efficiently.
pip (Python Package Installer)
• pip usually comes pre-installed with Python 3.4 and
above.
• To check if pip is installed:
• Open Command Prompt or Terminal and type: pip --version
If not installed:
• Download [Link]:
• Visit: [Link]
• Right-click > Save As (save it as [Link])
• Run in Command Prompt: python [Link]
• IDLE (Python’s Built-in IDE)Comes pre-
installed with Python.
• You can open it by typing IDLE in your Start
menu or application launcher.
Visual Studio Code (VS Code) – Free IDE
Steps to install:
[Link]: [Link]
[Link] Download for your OS (Windows/macOS/Linux)
[Link] the installer and follow on-screen instructions
[Link] installation:
•Open VS Code
•Install the Python extension:
•Go to Extensions (Ctrl+Shift+X)
•Search for Python
•Click Install
Introduction to Python Programs
Python programs are simple and beginner-friendly due
to:
Easy syntax (close to English)
• Python code looks more like everyday English, which
makes it easier to read and understand.
• Example: print("Hello, world!")
• This is straightforward compared to other languages
like Java or C++, which require more boilerplate
code.(specific format)
No need to declare data types
In Python, you don't have to specify the data type (like int,
float, or string)
when you create a variable. Python automatically figures
out the type based on the value you assign.
Example:
x = 10 # Python knows x is an integer
name = "Sam" # Python knows name is a string
In contrast, languages like Java require:
int x = 10;
String name = "Sam";
These features make Python:
• Easier to write and learn.
• Less prone to syntax errors.
• A good first language for students and beginners.
Hello Caias Program
Program:
print("Hello, Caias!")
Explanation:
print() is a built-in function to display output.
A great starting point for beginners
Input and Output
Program:
name = input("Enter your name: ")
print("Hello", name)
Explanation:
[Link]("Enter your name: ")
•This is a built-in Python function.
•It displays the message "Enter your name: " to the user.
•Then it waits for the user to type something and press Enter.
•Whatever the user types is stored as a string.
[Link] =
•This is a variable assignment.
•The user’s input is saved into the variable name.
[Link]("Hello", name)
•This displays output to the screen.
•It combines the string "Hello" with the value stored in name.
•For example, if the user entered Meghana, the output will be:
nginx
Hello Meghana
Basic Arithmetic Operations
val1 = 2
val2 = 3
# using the addition operator
result = val1 + val2
print(result)
val1 = 2
val2 = 3
# using the subtraction operator
result = val1 - val2
print(result)
val1 = 2
val2 = 3
# using the multiplication operator
result = val1 * val2
print(result)
val1 = 2
val2 = 3
# using the division operator
result = val1 / val2
print(result)
Using Built-in Python Libraries
Using statistics module
import statistics
data = [10, 20, 20, 30, 40, 40, 40, 50]
mean = [Link](data)
median = [Link](data)
mode = [Link](data)
print("Mean:", mean)
print("Median:", median)
print("Mode:", mode)
we calculate the standard deviation and variance
for a dataset to measure the spread of values.
import statistics
a = [1, 2, 3, 4, 5]
print([Link](a))
print([Link](a))
• from statistics import stdev
• # different datasets
• a = (1, 2, 5, 4, 8, 9, 12)
• b = (-2, -4, -3, -1, -5, -6)
• c = (-9, -1, 0, 2, 1, 3, 4, 19)
• d = (1.23, 1.45, 2.1, 2.2, 1.9)
• print(stdev(a))
• print(stdev(b))
• print(stdev(c))
• print(stdev(d))
• # Import the numpy library
• import numpy as np
• # Define the dataset
• x = [Link]([1,3,5,7,8,9, 10, 15])
• y = [Link]([10, 20, 30, 40, 50, 60, 70, 80])
• print([Link](x, y))
Notes:
•Use statistics for smaller, simple datasets.
•Use numpy and scipy for larger and more complex numeric
data,
especially in data science applications.
Basic Program Applications
Python basic programs help in:
Developing logic
Understanding control flow
Preparing for real-world applications like data analysis and automation