MODULE 3: INTRODUCTION TO PYTHON PROGRAMMING
What is Python?
Python is a high-level, interpreted programming language known for its simplicity and
readability. It is widely used for various applications, including web development, data analysis,
artificial intelligence, machine learning, and more.
Features of Python
1. Easy to Learn and Use: Python has a simple syntax that resembles natural language,
making it beginner-friendly.
8
2. Interpreted: Python code is executed line by line, which helps in debugging.
3. Platform Independent: Python can run on different operating systems like Windows,
macOS, and Linux.
4. Dynamic Typing: You don’t need to declare variable types; Python assigns them
R
automatically.
5. Extensive Libraries: Python has a rich set of libraries, such as NumPy, Pandas, and
Matplotlib, for various purposes.
6. Object-Oriented: Python supports object-oriented programming, which helps in creating
reusable code.
7. Community Support: Python has a large community for support and resources.
C
Tokens in Python
Tokens are the smallest units in a Python program. They include:
1. Keywords: Reserved words like if, else, while, def, etc.
2. Identifiers: Names used for variables, functions, or objects.
3. Literals: Fixed values like numbers (10, 3.14) or strings ("Hello").
4. Operators: Symbols like +, -, *, /, etc.
5. Delimiters: Characters like (), {}, [], :, ,, etc.
Data Types in Python
1. Numeric Types:
○ int: Whole numbers (e.g., 10)
○ float: Decimal numbers (e.g., 3.14)
○ complex: Complex numbers (e.g., 3+4j)
2. Text Type:
○ str: Sequence of characters (e.g., "Hello")
3. Boolean Type:
○ bool: True or False
4. Sequence Types:
○ list: [1, 2, 3]
○ tuple: (1, 2, 3)
○ range: Sequence of numbers
5. Mapping Type:
○ set: {1, 2, 3}
8
○ dict: Key-value pairs (e.g., {"key": "value"})
6. Set Types:
○ frozenset: Immutable set
7. None Type: Represents no value or a null value (e.g., None).
R
Control Flow in Python
Control flow statements decide the direction of execution in a program:
[Link] Statement: Executes a block of code if a condition is true.
C
python
Copy code
if x > 10:
print("x is greater than 10")
[Link]-else Statement: Executes one block if true, another if false.
python
Copy code
if x > 10:
print("x is greater")
else:
print("x is smaller or equal")
[Link] Statement: Adds multiple conditions.
python
Copy code
if x > 10:
print("x is greater")
elif x == 10:
print("x is equal to 10")
else:
print("x is smaller")
Loops in Python
[Link] Loop: Iterates over a sequence (e.g., list, range).
python
8
Copy code
for i in range(5):
print(i)
[Link] Loop: Repeats as long as a condition is true.
python
Copy code
x=0
R
while x < 5:
print(x)
x += 1
[Link]: Exit the loop prematurely.
python
C
Copy code
for i in range(10):
if i == 5:
break
print(i)
[Link]: Skips the current iteration and moves to the next.
python
Copy code
for i in range(5):
if i == 3:
Continue
print(i)
Introduction to Python Libraries
Python libraries are collections of pre-defined code that provide specific functionalities to
simplify complex programming tasks. They allow developers to focus on the core logic rather
than writing common functionalities repeatedly.
Importance of Python Libraries
1. Ease of Use: Simplifies coding for various applications.
2. Time-Saving: Reduces development time by offering ready-made functions.
3. Versatility: Covers a broad range of tasks like data analysis, machine learning, web
development, and more.
4. Reusability: Functions can be reused across multiple projects.
Features of Python Libraries
●
●
8
Pre-written modules for specific tasks.
Easy to import and use in any Python program.
●
R
Extensive documentation and tutorials available.
● Active support from the Python community.
Types of Python Libraries
C
1. Data Manipulation Libraries
● NumPy: Handles numerical computations and large arrays.
● Pandas: Used for working with structured data like tables and data frames.
2. Data Visualization Libraries
● Matplotlib: Creates static, animated, and interactive plots.
● Seaborn: Enhances Matplotlib for better statistical graphics.
3. Machine Learning and AI Libraries
● Scikit-learn: Provides tools for machine learning and data mining.
● TensorFlow: A powerful library for deep learning and neural networks.
4. Web Development Libraries
● Django: A high-level framework for building complex web applications.
● Flask: A lightweight framework for small to medium web apps.
5. Scientific Computing Libraries
● SciPy: Supports advanced computations like calculus and linear algebra.
6. Game Development Libraries
● Pygame: Helps create video games with multimedia features.
7. Web Scraping Libraries
● BeautifulSoup: Extracts information from web pages.
● Scrapy: A framework for large-scale web scraping projects.
8. Utility Libraries
8
● OS: Provides functions to interact with the operating system.
● Requests: Simplifies sending HTTP requests to websites.
R
How to Use Python Libraries
[Link] the Library
Use the import keyword to include a library in your program.
import library_name
C
[Link] Missing Libraries
If the library is not installed, use pip to install it.
pip install library_name
[Link] Library Functions
Call the specific functions you need for your task.
import numpy as np
arr = [Link]([1, 2, 3])
print([Link]()) # Calculates the mean of the array
1.
Benefits of Python Libraries
● Reduces Complexity: Built-in functions reduce the need for writing complex code.
● Speeds Up Development: Pre-tested functions save debugging time.
● Encourages Standardization: Promotes the use of best practices in programming.
● Extensive Ecosystem: Thousands of libraries available for every domain.
8
R
C