You are on page 1of 89

< A Practical Self-Study Guide for Absolute Beginners > PYTHON

PRIMER

A SELF-STUDY APPROACH

Bibek Bhandari
Zenitha Chalise

Python Primer: A Project-Based Learning Path

About Authors
Meet the authors of " Python Primer: A Self Study Approach" - Bibek
Bhandari and Zenitha Chalise.

Both are high school graduates with a shared passion for computer science
and programming. As self-taught programmers, they have dedicated
themselves to mastering the intricacies of various programming languages.

Inspired by the challenges faced by Nepalese high school students,


particularly during seminars and workshops organized via Cosog Nepal
organization, Bibek, and Zenitha have co-authored a book that puts self-
study at the forefront. " Python Primer" is designed to be your self-study
companion, offering a friendly and accessible approach to learning Python
programming.

Key Highlights:

Self-Study Focus: Tailored for independent learning, each chapter


empowers readers to progress at their own pace.

Project-Based Learning: Engage with Python through hands-on projects,


making learning practical and relevant.

Accessible Language: Authored by Bibek Bhandari and Zenitha Chalise,


the book clarifies complex concepts for readers of all experience levels.

' Python Primer' is more than a book; it's a self-study companion designed
for exploration, creativity, and project-based learning. Perfect for high
school students in Nepal and beyond, eager to delve into the exciting areas
of Python programming.

Content

Introduction --------------------------------- 1

Welcome to Python Primer ------------------------------- 1

Why Learn Python? -------------------------------------- 2

How to Use This Book ------------------------------------ 4


Setting Up Your Development Environment --------------

Chapter 1: Hello, Python! ------------------ 9

Your First Python Program (Hello World) ------------------- 9

Understanding Variables and Data Types ------------------ 13

Project: Building a Simple Calculator ---------------------- 17

Chapter 2: Making Decisions ------------- 20

Using Conditional Statements (if, else) -------------------- 20

Project: Creating a Number Guessing Game ---------------- 23

Chapter 3: Loops and Iteration ---------- 27

Working with Loops (for, while) -------------------------- 27

Project: Generating a Fibonacci Sequence ---------------- 30

Chapter 4: Lists and Dictionaries -------- 33

Managing Data with Lists -------------------------------- 33

Organizing Data with Dictionaries ------------------------- 36

Project: Creating a To-Do List Application ------------------ 39

Chapter 5: Functions and Modules ----- 43

Defining and Calling Functions --------------------------- 43

Organizing Code with Modules---------------------------- 46

Project: Building a Simple Contact Book ------------------- 49


Chapter 6: Handling Files ---------------- 52

Reading and Writing Text Files ---------------------------- 52

Project: Creating a Text File Word Counter----------------- 55

Chapter 7: Introduction to Classes ------ 59

Defining Classes and Objects------------------------------ 59

Project: Building a Simple Bank Account System ----------- 62

Chapter 8: Working with APIs ------------ 66

Making API Requests with Python------------------------- 66

Project: Fetching Data from a Public API ------------------ 69

Chapter 9: Building a Web Scraper ------ 73

Introduction to Web Scraping ---------------------------- 73

Project: Extracting Information from a Website ------------ 78

Chapter 10: Basic Data Visualization ---- 82

Creating Simple Charts with Matplotlib--------------------- 82

Project: Plotting Data from a CSV File ---------------------- 86

Conclusion ----------------------------------- 90

Recap of Your Python Journey ----------------------------- 90

Next Steps and Further Learning Resources----------------- 93

Introduction

Welcome to "Python Primer: A Self Study


Approach" ! This book is designed to take you on a hands-on journey
through the world of Python programming. Whether you're a complete
beginner or someone looking to refresh your skills, this book will guide you
step-by-step in building practical projects.

Why Learn Python? Python is a versatile and powerful programming


language used in web development, data analysis, artificial intelligence,
and more. Its clean syntax and readability make it an excellent choice for
beginners.

Why learn Python?

Welcome to the world of Python, a programming language that combines


versatility, readability, and power. Here's why investing your time in
learning Python is a smart decision:

Versatility: Python's applications span web development,

data

analysis,

artificial

intelligence, and more, making it a go-to language for diverse projects.

Clean Syntax: Python's clear and readable syntax is beginner-friendly,


minimizing the learning curve and emphasizing code readability for both
novices and seasoned developers.

Extensive Libraries: With a rich standard library and numerous


frameworks, Python simplifies complex tasks, letting you focus on
problem-solving rather than coding from scratch.

2
Community Support: Join a vibrant and supportive community of
developers ready to share knowledge, answer questions, and collaborate,
fostering an ideal environment for learning and growth.

Industry Relevance: Python's widespread adoption by major companies


underscores its efficiency and productivity, making it a valuable skill across
various professional landscapes.

Gateway to Technologies: Python serves as a gateway to cutting-edge


technologies like machine learning and artificial intelligence, opening doors
to exciting career opportunities and advancements.

Job Opportunities: The growing demand for Python developers means


learning Python enhances your employability, offering a wide array of job
opportunities in different industries.

How to Use This Book This book is structured around projects, which
means you'll be learning by doing.

Each chapter focuses on a specific topic and includes a project to reinforce


what you've learned. Follow along with the examples, type out the code,
and experiment with your own ideas.

Setting Up Your Development Environment

Before we dive into coding, let's set up your Python environment. In this
book, we'll be using Python 3.11. You can download it from the official

Python

website

(python.org).
Additionally, you'll need a text editor or integrated development
environment (IDE) to write and run your Python code. There are many
options available, but some popular choices include Visual Studio Code,
PyCharm, and Jupyter Notebook.

Step 1: Download and Install Python Go to the official Python website:


Python.org.

Click on the " Downloads" tab.

Select the appropriate installer for your operating system (Windows,


macOS, or Linux). If you're unsure, the website usually recommends the
latest version for your platform.

For Windows, make sure to check the box that says " Add Python X.Y to
PATH" during installation. This will make it easier to run Python from the
command line.

Step 2: Verify the Installation

After the installation is complete, you can verify if Python was installed
correctly: Open a command prompt (Windows) or terminal (macOS,
Linux).

Type python --version or python3 --version (for macOS/Linux) and press


Enter. This should display the installed Python version.

Step 3: Set Up a Virtual Environment (Optional but Recommended)

Setting up a virtual environment is a best practice as it allows you to isolate


your Python environment for different projects. Here's how you can do it:

Open a command prompt (Windows) or terminal (macOS, Linux).

Navigate to the directory where you want to create your project.


Run the following command to create a virtual environment:

Now, let's jump in and start coding!!

Chapter 1: Hello, Python!

Your First Python Program (Hello World) Introduction

In this chapter, you will take your first steps into the world of Python
programming by writing a simple program that prints the phrase "Hello,
World!" to the console. This is a common starting point for beginners in
programming and serves as a basic introduction to the syntax of the Python
language.

Writing Your First Python Program 1. Opening a Text Editor or


Integrated Development Environment (IDE): To write Python code, you
need a text editor or an Integrated Development Environment (IDE).
Popular choices include Visual Studio Code, PyCharm, and IDLE

(Python's built-in IDE).

2. Creating a New File: Open your chosen text editor or IDE and create a
new file. This will be where you write your Python code.

3. Writing the code:


4. Understanding the code:

The first line, # Chapter 1: Hello, Python!, is a comment. Comments in


Python start with the

# symbol and are ignored by the Python interpreter. They are used to add
explanations or notes to the code for human readers.

The line print("Hello, World!") is the core of our program. It uses the print()
function to display the text "Hello, World!" on the console.

5. Running the Program:

Saving the File: Save the file with a .py extension. For example, you can
name it hello_world.py.

Opening a Terminal or Command Prompt: Open a terminal or command


prompt window on your computer.

10

Navigating to the File Location: Use the cd command to navigate to the


directory where you saved your Python file. For example, if you saved it on
your desktop, you can use the command cd Desktop.

Executing the Program: Type python hello_world.py and press Enter. This
command tells the Python interpreter to run the hello_world.py file.

6. Interpreting the output:

After executing the program, you should see the output:

Congratulations! You've just written and executed your first Python


program.

Key Takeaways
Python programs are written in plain text files with a .py extension.

The print() function is used to display output on the console.

11

Comments are used to add explanations or notes to the code.

Running a Python program involves using the Python command followed


by the name of the Python file.

12

Understanding variables and Data Types Introduction

In this section, we will explore the concept of variables and data types in
Python. Variables are used to store and manipulate data, while data types
define the type of data that can be stored in a variable.

Variables

Definition: A variable is a container that holds a value or a reference to a


value.

Naming Convention: Variable names can consist of letters, numbers, and


underscores.

They cannot start with a number and are case-sensitive.

Examples:
13

Data Types

Python has several built-in data types, including: 1. Numeric Types: int:
Integer numbers (e.g., 5, -3, 0).

float: Floating-point numbers (e.g., 3.14, -2.5).

2. String:

Represents text and is enclosed in single (' ') or double (" ") quotes (e.g.,
"Hello", 'Python').

3. Boolean:

Represents True or False.

4. List:

A collection of items ordered and changeable (e.g., [1, 2, 3]).

5. Tuple:

Similar to a list but immutable (e.g., (1, 2, 3)).

14
6. Dictionary:

A collection of key-value pairs (e.g., {'key1':

'value1', 'key2': 'value2'}).

7. Set:

An unordered collection of unique items (e.g.,

{1, 2, 3}).

Assigning Values to Variables

Checking Data Types

You can use the type() function to check the data type of a variable:

15

Key Takeaways
Variables are used to store data.

Python has various data types, including numeric types, strings, booleans,
lists, tuples, dictionaries, and sets.

You can use the type() function to determine the data type of a variable.

16

Project: Building a Simple Calculator Introduction

In this project, you will apply what you've learned about variables and basic
operations to create a simple calculator program. This program will be able
to perform addition, subtraction,

multiplication,

and

division

operations based on user input.

Steps to Create the Simple Calculator User Input:

Use the input() function to prompt the user for two numbers and the
operation they want to perform.

Converting Input to Numbers:

Since input() returns a string, you'll need to convert the user input to
numeric values using int() or float().

Performing Operations:

Based on the user's chosen operation, use conditional statements (if, elif,
else) to perform the appropriate calculation.

17
Displaying the Result:

Use the print() function to display the result of the calculation.

Example code:

18
Running the calculator

1.Save the above code in a .py file (e.g., simple_calculator.py).

2. Open a terminal or command prompt and navigate to the directory


containing the file.

3.Run

the

program

using

python

simple_calculator.py.

Using the calculator

When you run the program, it will prompt you to enter two numbers and the
operation you want to perform.

For example:

The program will calculate the result (10 * 5 = 50) and display it.

Key takeaways

This project reinforces the concepts of variables, user input, conditional


statements, and basic arithmetic operations.

It demonstrates how to create a simple interactive program in Python.

19
Chapter 2: Making Decisions
Using Conditional Statements (if, else) Introduction

In this chapter, you'll learn how to use conditional statements in Python to


make decisions based on certain conditions.

Conditional statements allow your program to choose different paths of


execution depending on the values of variables.

The if Statement

The if statement is used to execute a block of code only if a specified


condition is true.

The else Statement

The else statement is used in conjunction with if to execute a block of code


if the condition in the if statement is false.

20
The elif Statement

The elif statement allows you to specify additional conditions to check if


the previous condition was false.

Nested if Statements

You can also nest if statements inside each other to handle more complex
scenarios.

21

Combining Conditions (Logical Operators) You can use logical operators


(and, or, not) to combine multiple conditions.
Key Takeaways

Conditional statements (if, else, elif) allow your program to make decisions
based on conditions.

You can nest if statements to handle more complex scenarios.

Logical operators (and, or, not) allow you to combine conditions.

22

Project: Creating a Number Guessing Game


Introduction
In this project, you will apply your knowledge of conditional statements to
create a simple number-guessing game. The program will generate a
random number, and the player must guess the correct number within a
certain number of tries.

Steps to Create the Number Guessing Game Generate a Random


Number:

Use the random module to generate a random number within a specified


range.

You can import the random module at the beginning of your code.

23

Get User Input:

Use the input() function to get the player's guess.

Compare the Guess:

Use conditional statements (if, elif, else) to compare the player's guess with
the secret number and provide feedback.
Repeat the Process:

You can use a loop (e.g., while) to allow the player to keep guessing until
they guess the correct number or run out of tries.

24

Keep Track of Tries:

Use a variable to keep track of the number of tries the player has made and
display it in the game.

End the Game:

End the game when the player guesses the correct number or exceeds the
maximum number of tries.

Example Code

Here's an example code snippet to get you started:


25

Running the Number Guessing Game Save the code in a .py file (e.g.,
number_guessing_game.py).

Open a terminal or command prompt and navigate to the directory


containing the file.

Run

the

program

using

python

number_guessing_game.py.

The game will prompt you to guess the number, provide feedback, and keep
track of your attempts until you guess the correct number.

Key Takeaways

This project reinforces your understanding of conditional statements and


introduces the concept of game loops.

It demonstrates how to create an interactive number-guessing game using


Python.

26
Chapter 3: Loops and
Iteration

Working with Loops (for, while)


Introduction
In this chapter, you will learn about loops and iteration in Python. Loops
allow you to repeat a block of code multiple times, making your programs
more efficient and versatile.

The for Loop

The for loop is used to iterate over a sequence (such as a list or a range of
numbers) and execute a block of code for each item in the sequence.

27

The while Loop


The while loop is used to repeatedly execute a block of code as long as a
specified condition is true.

Loop Control Statements

break: Used to exit the loop prematurely.

continue: Used to skip the current iteration and move to the next one.

28

Looping Through a Range of Numbers The range() function can be used


to generate a sequence of numbers within a specified range.

Key Takeaways

Loops allow you to repeat a block of code multiple times.

The for loop is used for iterating over sequences.

The while loop is used for repeating code as long as a condition is true.

Loop control statements (break and continue) provide additional control


over loop execution.

29
Project: Generating a Fibonacci

Sequence
Introduction
In this project, you will apply your knowledge of loops to generate a
Fibonacci sequence. The Fibonacci sequence is a series of numbers where
each number is the sum of the two preceding ones.

Steps to Generate a Fibonacci Sequence Get User Input:

Prompt the user to enter the number of terms they want in the Fibonacci
sequence.

Initialize Variables:

Set initial values for the first two terms of the sequence (first_term and
second_term).

30
Generate the Sequence:

Use a for loop to generate the desired number of terms in the sequence

Displaying the Sequence:

Print each term of the Fibonacci sequence.

Example code

Here's an example code snippet to generate a Fibonacci sequence

31

Running the Fibonacci Sequence Generator: Save the code in a .py file
(e.g., fibonacci_sequence.py).

Open a terminal or command prompt and navigate to the directory


containing the file.

Run
the

program

using

python

fibonacci_sequence.py.

The program will prompt you to enter the number of terms you want in the
Fibonacci sequence and then generate and display the sequence.

Key Takeaways

This project demonstrates how to

generate a Fibonacci sequence using a for loop.

It reinforces your understanding of loops and mathematical operations in


Python.

32
Chapter 4: Lists and
Dictionaries

Managing Data with Lists


Introduction
In this chapter, you will learn about lists, which are one of the most
versatile data structures in Python.

A list is a collection of items that can be of different types, such as numbers,


strings, or even other lists.

Creating Lists

You can create a list by enclosing a comma-separated sequence of items


within square brackets

[].

Accessing Elements

You can access individual elements of a list using their index. The index
starts at 0 for the first element.

21 33
Modifying Lists

You can change the value of an element in a list by assigning a new value to
it.

Adding and Removing Elements

Adding Elements:

append(): Adds an element to the end of the list.

Removing Elements

remove(): Removes the first occurrence of a specified value.

34

List Operations

Concatenation: You can concatenate two or more lists using the + operator.

Slicing: You can extract a portion of a list using slicing.

Key Takeaways

Lists are ordered collections of items.

Elements in a list can be of different types.


You can access, modify, add, and remove elements from a list.

35

Organizing Data with Dictionaries Introduction

In this section, you will learn about dictionaries, which are another essential
data structure in Python. Dictionaries allow you to store data in key-value
pairs, making it easy to retrieve and manipulate information.

Creating Dictionaries

You can create a dictionary by enclosing a comma-separated list of key-


value pairs within curly braces

{}.

Accessing Values

You can access the value associated with a specific key in a dictionary by
using the key itself.

36
Modifying Dictionaries

You can change the value associated with a key in a dictionary by assigning
a new value to it.

Adding and Removing Entries

Adding Entries:

You can add a new key-value pair to a dictionary by assigning a value to a


new key.

Removing Entries:

You can use the del keyword to remove a key-value pair.

37

Dictionary Methods

keys(): Returns a list of all the keys in the dictionary.


values(): Returns a list of all the values in the dictionary.

items(): Returns a list of key-value tuples.

Key Takeaways

Dictionaries store data in key-value pairs.

Keys are unique within a dictionary and can be of different types.

You can access, modify, add, and remove entries in a dictionary.

38

Project: Creating a To-Do List Application Introduction

In this project, you will combine your knowledge of lists and dictionaries to
create a simple to-do list application. The program will allow users to add,
view, and remove tasks from their to-do list.

Steps to Create the To-Do List Application Initialize the To-Do List:

Create an empty list to store the tasks.

Display Menu Options:


Create a loop to display menu options (e.g., Add Task, View Tasks, Remove
Task, Quit).

39

Add Task:

If the user chooses to add a task, prompt them for the task name and priority
level, and add it to the list of tasks as a dictionary.

View Tasks:

If the user chooses to view tasks, iterate through the list of tasks and display
them.

Remove Task:
If the user chooses to remove a task, prompt them for the task number and
remove it from the list.

40

Quit the Application:

If the user chooses to quit, exit the loop and end the program.

Example Code

Here's an example code snippet to get you started: 41


Running the To-Do List Application 1.Save the code in a .py file (e.g.,
todo_list.py).

2.Open a terminal or command prompt and navigate to the directory


containing the file.

3.Run the program using python todo_list.py.

The program will present a menu with options to add tasks, view tasks,
remove tasks, or quit.

Key Takeaways

This project combines lists and dictionaries to create a simple interactive to-
do list application.

It reinforces your understanding of data structures and loops in Python.

42
Chapter 5: Functions and
Modules

Defining and Calling Functions


Introduction
In this chapter, you will learn about functions, which are blocks of reusable
code. Functions allow you to break down your program into smaller,
manageable pieces, making your code more organized and easier to
maintain.

Creating Functions

You can define a function using the def keyword, followed by the function
name and a set of parentheses. Optionally, you can include parameters
within the parentheses.

43

Function Parameters

Parameters allow you to pass data into a function.

They act as placeholders for values that will be provided when the function
is called.
Default Parameters

You can provide default values for parameters in a function. If a value is not
provided when calling the function, the default value will be used.

44

Key Takeaways

Functions allow you to encapsulate and reuse blocks of code.

Functions can take parameters as input and return values as output.

The return statement is used to send a value back to the caller.

Default parameters provide fallback values if no value is provided.

45

Organizing Code with Modules


Introduction
In this section, you will learn about modules, which are files containing
Python code that define functions, variables, and classes. Modules help
organize your code into reusable units, making it easier to manage large
projects.

Creating Modules

A module is simply a Python file (with a .py extension) that contains code
you want to reuse in other Python programs.

Using Modules

You can use functions and variables defined in a module by importing it


into your Python program.

46
Module Aliases

You can give a module an alias to make it easier to reference in your code.

Importing Specific Functions

You can import specific functions or variables from a module rather than
the entire module.

47

Using Standard Library Modules

Python comes with a standard library of modules that provide a wide range
of functionality. You can use these modules without installing anything
extra.

48
Project: Building a Simple Contact Book Introduction

In this project, you'll apply the concepts of functions and modules to create
a simple contact book. This contact book will allow users to add new
contacts, view existing contacts, and search for specific contacts by name.

Steps to Build the Simple Contact Book Create a Contact Module:

Begin by creating a Python module named contact_book.py. This module


will contain functions for managing contacts.

49
Use the Contact Module in a Program: Develop

new

Python

script

(e.g.,

contact_program.py)

to

interact
with

the

contact_book module. Import the module and use its functions to add, view,
and search for contacts.

Run the Program:

Execute the contact_program.py script in the terminal or command prompt.

50

Experiment with Contacts:

Modify the script to add more contacts, view the updated contact list, and
search for different contacts to ensure the contact book functions as
intended.

Example Output

Key Takeaways

This project reinforces the concepts of functions and modules.

It demonstrates the use of a module to maintain and manage data (contacts,


in this case).

The contact book functions provide practical examples of how functions


can encapsulate and organize code for better readability.

51
Chapter 6: Handling Files
Reading and Writing Text Files
Introduction
In this chapter, you will learn how to work with files in Python. Files are a
common way to store and retrieve data, and Python provides built-in
functions for reading from and writing to files.

Opening and Closing Files

You can open a file using the open() function. It takes two arguments: the
file path and the mode (e.g., 'r' for read, 'w' for write).

To close a file after you're done with it, you can use the close() method.

52
Reading from a File

You can use the read() method to read the entire contents of a file.

You can also read a file line by line using a loop.

Writing to a File

You can open a file for writing using the 'w' mode. Be careful, as this will
overwrite the existing contents of the file.

53

Using Context Managers (with Statement) Using a context manager (the


with statement) ensures that a file is properly closed after you're done with
it.

Key Takeaways

Files are a common way to store and retrieve data.

You can use the open() function to open a file and specify the mode ('r' for
read, 'w' for write).

Use the read() method to read the entire contents of a file, or iterate over the
file line by line.

Use the write() method to write to a file.

Context managers (the with statement) provide a convenient way to handle


file operations.

54
Project: Creating a Text File Word Counter Introduction

In this project, you will create a Python program that reads a text file,
counts the occurrences of each word, and displays the word frequencies.

This project combines your knowledge of file handling, loops, and


dictionaries.

Steps to Create the Text File Word Counter Reading the File:

Use the open() function to open a text file in read mode. You can specify the
file path as an argument.

Tokenizing the Text:

Tokenization is the process of breaking the text into words or tokens. You
can use the split() method to split the content into words.

55
Counting Word Frequencies:

Create a dictionary to store word frequencies.

Iterate through the words, and for each word, update the dictionary.

Displaying Word Frequencies:

Display the word frequencies to the user.

56
Example Code

Here's an example code snippet to get you started: Running the Text File
Word Counter

1.Save the code in a .py file (e.g., word_counter.py).

2.Place a text file named sample.txt in the same directory as the text you
want to analyze.

3.Open a terminal or command prompt and navigate to the directory


containing the file.

4.Run

the

program

using

Python

word_counter.py.

57

The program will read the text from sample.txt, count the word frequencies,
and display the results.

Key Takeaways

This project combines file handling, text processing, and dictionary usage.

It demonstrates how to count word frequencies in a text file using Python.

58
Chapter 7: Introduction to
Classes

Defining Classes and Objects


Introduction
In this chapter, you will learn about classes and objects, which are
fundamental concepts in object-oriented programming (OOP). Classes
allow you to define blueprints for creating objects with specific attributes
and behaviors.

Creating a Class

You can define a class using the class keyword, followed by the class name.
Inside the class, you can define attributes (variables) and methods
(functions).

59

Creating Objects (Instances)

Once you have a class, you can create objects (instances) of that class. This
allows you to work with specific data associated with each instance.

Constructor Method (__init__)


The __init__ method is a special method called when a new instance of a
class is created. It is used to initialize the attributes of the object.

Class Attributes vs. Instance Attributes Class attributes are shared among
all instances of a class.

Instance attributes are specific to each individual instance.

60

Key Takeaways

Classes are blueprints for creating objects with specific attributes and
behaviors.

Objects (instances) are created from classes and allow you to work with
specific data.

The __init__ method is a special method used to initialize attributes when


an instance is created.

Class attributes are shared among all instances, while instance attributes are
specific to each instance.
61

Project: Building a Simple Bank Account System


Introduction
In this project, you will create a simple bank account system using classes.
The program will allow you to create accounts, deposit and withdraw
money, and check the account balance. This project will help you practice
working with classes and objects.

Steps to Create the Bank Account System Define the Account Class:

Create a class named Account with attributes like account_number,

owner_name,

and

balance.

Implement an __init__ method to initialize these attributes.

Implement Deposit and Withdraw Methods: Add methods to the Account


class for depositing and withdrawing money. These methods should update
the balance accordingly.

62
Display Account Information:

Add a method to the Account class to display the account information,


including the account number, owner name, and balance.

Create and Use Accounts:

Create instances of the Account class and use the methods to perform
operations like depositing, withdrawing, and displaying account
information.

63
Example Code

Here's an example code snippet to get you started: Running the Bank
Account System

1.Save the code in a .py file (e.g., bank_account.py).

2.Open a terminal or command prompt and navigate to the directory


containing the file.

3.Run the program using python bank_account.py.


64

The program will create an account, perform deposit and withdrawal


operations, and display the account information.

Key Takeaways

This project involves creating a bank account system using classes and
methods.

It provides hands-on experience with defining classes,

initializing

attributes,

and

implementing methods.

65

Chapter 8: Working with APIs Making API Requests with Python


Introduction
In this chapter, you will learn about Application Programming Interfaces
(APIs) and how to make API requests using Python. APIs allow different
software applications to communicate with each other.

What is an API?

An API (Application Programming Interface) is a set of rules and protocols


that allows different software applications to communicate with each other.

APIs enable the exchange of data and functionality between different


systems, making it possible to integrate and extend the capabilities of
software.

Making API Requests

To make API requests in Python, you will use the requests library. This
library provides easy-to-use functions for sending HTTP requests and
handling responses.

66
Installing the requests Library

You can install the requests library using pip: Making a GET Request

A GET request is used to retrieve data from a server.

Handling the Response

You can access the response content using the text attribute.

You can also check the status code to see if the request was successful.

67

Making Other Types of Requests

POST Request: Used to send data to a server.

PUT Request: Used to update data on a server.

DELETE Request: Used to delete data from a server.

Key Takeaways
APIs allow different software applications to communicate with each other.

The requests library in Python is used to make API requests.

Different types of requests (GET, POST, PUT, DELETE) can be used to


perform various operations.

68

Project: Fetching Data from a Public API Introduction

In this project, you will apply your knowledge of making API requests to
fetch data from a public API. You will use Python to retrieve information
from an external source and process the data as needed.

Steps to Fetch Data from a Public API Choosing a Public API:

Find a public API that provides data you are interested in. There are various
public APIs available, such as weather data, news articles, or financial
information.

Making an API Request:

Use the requests library to make an API request to the chosen API. Specify
the API endpoint and any required parameters.

69
Handling the Response:

Process the API response. Depending on the API, the data may be in JSON,
XML, or another format. Use appropriate methods to extract and display the
information you need.

Displaying the Data:

Display the fetched data to the user or perform further operations on it as


needed.

70

Example Code

Here's an example code snippet to demonstrate how to fetch weather data


from a public API: Running the Project
1.Choose a public API that provides data you are interested in.

2.Save the code in a .py file (e.g., api_fetcher.py).

3.Open a terminal or command prompt and navigate to the directory


containing the file.

4.Run the program using python api_fetcher.py.

The program will fetch data from the chosen API and display the
information to the user.

71

Key Takeaways

This project involves making API requests to fetch data from a public API.

It provides hands-on experience in working with external data sources and


processing API responses.

72

Chapter 9: Building a Web Scraper

Introduction to Web Scraping


Introduction
In this chapter, you will learn about web scraping, which is the process of
extracting information from websites. Web scraping allows you to gather
data from web pages for various purposes, such as analysis, research, or
automation.

What is Web Scraping?

Web scraping is the automated process of extracting data from websites. It


involves fetching the HTML content of a web page and parsing it to extract
the desired information.

Web scraping can be used for various purposes, including data collection,
market research, competitive analysis, and more.

73

Ethics and Legal Considerations When web scraping, it's important to


respect the terms of service of the website you're scraping. Some websites
may prohibit or restrict scraping activities.

Always check for a website's robots.txt file, which provides guidelines for
web crawlers.

Adhering to these guidelines is crucial for ethical scraping.

Tools for Web Scraping

Python provides several libraries for web scraping, including:

Beautiful Soup: A library for parsing HTML and XML documents, making
it easy to extract data.

Requests: Used for making HTTP requests to fetch the HTML content of
web pages.
Selenium: A browser automation tool that can be used for dynamic web
scraping.

74

Building a Web Scraper

To build a web scraper, you'll typically follow these steps:

Fetch the HTML Content:

Use the requests library to send a GET request to the URL of the web page
you want to scrape.

Parse the HTML:

Use a parsing library like Beautiful Soup to parse the HTML content and
navigate the DOM tree.

Extract Data:

Use Beautiful Soup to find and extract the specific elements or data you're
interested in.

75
Store or Process Data:

Once you've extracted the data, you can choose to store it in a file, database,
or process it further as needed.

Example Code

Here's an example code snippet to demonstrate how to scrape the title of a


web page: 76

Key Takeaways

Web scraping is the process of extracting data from websites.

Python provides libraries like Beautiful Soup and Requests for web
scraping.
Web scraping should be done ethically and in compliance with website
terms of service.

77

Project: Extracting Information from a Website


Introduction
In this project, you will build a web scraper to extract specific information
from a website. You will use Python, along with libraries like Requests and
Beautiful Soup, to automate the process of gathering data from web pages.

Steps to Extract Information from a Website Choose a Target Website:

Select a website from which you want to extract information. Identify the
specific data you're interested in.

Inspect the Website:

Use your web browser's developer tools to inspect the HTML structure of
the page. Identify the HTML

elements that contain the data you want to extract.

78
Write the Web Scraper:

Use Python along with the Requests and Beautiful Soup libraries to build
the web scraper. Fetch the HTML content, parse it, and extract the desired
information.

Process the Data:

Once you've extracted the data, you can process it further as needed. This
could involve cleaning, organizing, or storing the information.

79

Example Code

Here's an example code snippet to demonstrate how to extract information


from a website: Running the Project
1.Choose a website from which you want to extract information.

2.Inspect the HTML structure of the page to identify the elements


containing the desired data.

3.Save the code in a .py file (e.g., web_scraper.py).

4.Open a terminal or command prompt and navigate to the directory


containing the file.

5.Run

the

program

using

python

web_scraper.py.

80

The program will fetch the HTML content, extract the specified information,
and save it to a file.

Key Takeaways

This project involves building a web scraper to extract information from a


website.

It provides hands-on experience in automating data extraction tasks using


Python and web scraping libraries.

81

Chapter 10: Basic Data Visualization


Creating Simple Charts with Matplotlib Introduction

In this chapter, you will learn how to create basic data visualizations using
the Matplotlib library in Python. Data visualization is a powerful tool for
understanding and presenting data in a visual format, such as charts and
graphs.

What is Matplotlib?

Matplotlib is a widely used data visualization library in Python. It provides


a variety of functions and tools for creating different types of plots and
charts.

Matplotlib can be used for simple line plots, bar charts, scatter plots, and
more.

82

Installing Matplotlib
You can install Matplotlib using pip: Creating a Simple Line Plot

A line plot is used to display data points over a continuous interval. To


create a simple line plot, you can use the plot() function from Matplotlib.

Creating a Bar Chart

A bar chart is used to represent data with rectangular bars. You can create a
bar chart using the bar() function.

83
Creating a Scatter Plot

A scatter plot is used to visualize the relationships between two variables.


You can create a scatter plot using the scatter() function.

Customizing Plots

Matplotlib allows you to customize various aspects of your plots, including


titles, labels, colors, and styles.

84

Key Takeaways

Matplotlib is a popular data visualization library in Python.

You can create various types of plots, such as line plots, bar charts, and
scatter plots, to visualize your data.

Customization options in Matplotlib allow you to enhance the appearance


and readability of your plots.

85
Project: Plotting Data from a CSV File Introduction

In this project, you will use Python and Matplotlib to read data from a CSV
file and create visualizations.

CSV (Comma Separated Values) files are a common format for storing
tabular data. This project will help you practice reading data from files and
creating meaningful visualizations.

Steps to Plot Data from a CSV File Import Required Libraries:

Start by importing the necessary libraries, including matplotlib and csv.

Read Data from the CSV File:

Use the csv module to open and read the data from the CSV file.

86
Extract Data for Plotting:

Separate the data into appropriate lists or arrays for plotting. Depending on
the content of the CSV

file, you may have multiple columns of data.

Create the Plot:

Use Matplotlib to create the desired plot, such as a line plot, bar chart, or
scatter plot.

Customize the Plot (Optional):

Add titles, labels, legends, and customize the appearance of the plot as
needed.

87
Display or Save the Plot:

Choose to either display the plot or save it to a file.

Example Code

Here's an example code snippet to demonstrate how to plot data from a


CSV file:

88

Running the Project 1.Prepare a CSV file (data.csv) with suitable data for
plotting.

2.Save the code in a .py file (e.g., csv_plotter.py) in the same directory as
the CSV file.

3.Open a terminal or command prompt and navigate to the directory


containing the file.
4.Run the program using python csv_plotter.py.

The program will read the data from the CSV file and create a plot based on
the specified visualization.

Key Takeaways

This project involves reading data from a CSV file and creating
visualizations using Matplotlib.

It provides hands-on experience in processing external data and generating


meaningful plots.

89

Conclusion

Recap of Your Python Journey

Congratulations on completing the Python Primer!

Here's a quick recap of what you've learned: In Chapter -1

You started by writing your first Python program to display "Hello,


World!".

You learned about variables and different data types in Python.

In Chapt er -2

You explored conditional statements like if, else, and elif.

You applied these statements to create a number-guessing game.

In Chapt er -3

You learned about loops, both for and while, to perform repetitive tasks.

You implemented a program to generate a Fibonacci sequence.


90

In Chapt er -4

You studied data structures like lists and dictionaries to organize and
manage data.

You used them to create a To-Do List application.

In Chapt er -5

You delved into defining and calling functions to modularize your code.

You organized code into modules and built a Simple Contact Book.

In Chapt er -6

You learned how to read and write text files, and the importance of context
managers (with statements).

You created a Text File Word Counter to apply this knowledge.

In Chapt er -7

You understood the concept of classes and objects, and how they
encapsulate data and behavior.

You built a Simple Bank Account System using classes.

91

In Chapt er -8

You discovered how to make API requests in Python using the requests
library.

You applied this knowledge to fetch data from a public API.

In Chapt er -9
You learned about web scraping and how to extract information from
websites.

You practiced web scraping by extracting data from a website.

In Chapt er -10

You were introduced to data visualization using Matplotlib, creating line


plots, bar charts, and scatter plots.

You applied this knowledge to plot data from a CSV file.

92

Next Steps and Further Learning Resources

Now that you have a solid foundation in Python, here are some next steps
and resources for further learning:

Explore Advanced Topics: Dive deeper into Python by exploring advanced


topics like object-oriented

programming,

algorithms,

data

structures, and more.

Build Projects: Apply your skills by working on personal or open-source


projects. This hands-on experience is invaluable for solidifying your
understanding.

Learn Libraries and Frameworks: Depending on your interests, explore


popular Python libraries and frameworks like Django for web development,
TensorFlow for machine learning, and Pandas for data analysis.
Online Courses and Tutorials: Consider enrolling in online courses or
tutorials on platforms like Coursera, Udemy, or free resources like
Python.org and Real Python.

93

Books: There are many excellent Python books available that cover a wide
range of topics and expertise levels.

Community and Forums: Join Python forums and communities like Stack
Overflow, Reddit's r/learnpython, and Python's official forum for asking
questions and getting help.

Contribute to Open Source: Contribute to open-source projects to gain


practical experience and collaborate with other developers.

94

Advanced Topics: Object-Oriented Programming (OOP): Deepen your


understanding

of

classes,

inheritance,

polymorphism, and other OOP concepts.

Data Structures and Algorithms: Learn about more complex data


structures (trees, graphs) and algorithms for solving various problems
efficiently.

Advanced Libraries and Frameworks: Explore libraries like NumPy,


Pandas, SciPy for scientific computing, and frameworks like Django for
web development.

Database Interaction: Understand how to interact with databases using


Python, including SQL
databases like SQLite and PostgreSQL.

Concurrency and Parallelism: Learn about handling multiple tasks


concurrently, using libraries like threading and asyncio.

Web Development: Dive into web development using frameworks like


Flask or Django. Explore front-end technologies like HTML, CSS, and
JavaScript.

Projects:

Create a Web Application: Build a dynamic web application using a web


framework like Flask or Django.

Data Analysis and Visualization: Use Pandas for data manipulation and
Matplotlib or Seaborn for creating insightful visualizations.

Machine Learning Models: Implement and train machine learning models


using libraries like scikit-learn or TensorFlow.

Game Development: Explore game development using libraries like


Pygame.

Automation and Scripting: Automate repetitive tasks using Python scripts,


such as web scraping, data extraction, or file handling.

Contributions to Open Source: Contribute to existing open-source


projects or start your own and share it with the community.

Further Learning Resources: Online Courses and Tutorials: Platforms


like Coursera, Udemy, edX, and free resources like Python.org and Real
Python offer a wide range of courses.

Books: There are many excellent Python books available, covering a wide
range of topics and expertise levels.

Community and Forums: Join Python forums, communities, and attend


meetups to connect with other developers.
Hackathons and Coding Challenges: Participate in coding competitions
and challenges on platforms like LeetCode, HackerRank, or Kaggle

Thank you for taking this Python

Primer. If you have any further

questions or need assistance, feel free

to reach out at

primerwithpython@gmail.com.

One Last Thing...

If you enjoyed this book or found

it useful, we’d be very grateful if

you’d post a short review on

Amazon. Your support really

does make a difference and we

read all the reviews personally

so we can get your feedback and

make this book even better.

Keep coding, exploring, and

building!

Happy Coding!

You might also like