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

Full Download Ultimate Python Programming 1st Edition Deepali Srivastava PDF DOCX

The document promotes the ebook 'Ultimate Python Programming' by Deepali Srivastava, which offers a comprehensive introduction to Python programming, including over 650 programs and 900 practice questions. It emphasizes the book's hands-on approach, covering essential concepts and problem-solving skills, and provides links to download the ebook and additional resources. The author, with a background in mathematics and computer science, aims to make Python accessible for beginners while also catering to experienced programmers.

Uploaded by

fouhyryserc6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Full Download Ultimate Python Programming 1st Edition Deepali Srivastava PDF DOCX

The document promotes the ebook 'Ultimate Python Programming' by Deepali Srivastava, which offers a comprehensive introduction to Python programming, including over 650 programs and 900 practice questions. It emphasizes the book's hands-on approach, covering essential concepts and problem-solving skills, and provides links to download the ebook and additional resources. The author, with a background in mathematics and computer science, aims to make Python accessible for beginners while also catering to experienced programmers.

Uploaded by

fouhyryserc6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Get the full ebook with Bonus Features for a Better Reading Experience on ebookgate.

com

Ultimate Python Programming 1st Edition Deepali


Srivastava

https://ebookgate.com/product/ultimate-python-
programming-1st-edition-deepali-srivastava/

OR CLICK HERE

DOWLOAD NOW

Download more ebook instantly today at https://ebookgate.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Expert Python Programming 2nd Edition Jaworski

https://ebookgate.com/product/expert-python-programming-2nd-edition-
jaworski/

ebookgate.com

Python Programming 4th Edition Singh A.

https://ebookgate.com/product/python-programming-4th-edition-singh-a/

ebookgate.com

Black hat Python Python programming for hackers and


pentesters 1st Edition Seitz

https://ebookgate.com/product/black-hat-python-python-programming-for-
hackers-and-pentesters-1st-edition-seitz/

ebookgate.com

Practical Maya Programming with Python 1st Edition


Galanakis

https://ebookgate.com/product/practical-maya-programming-with-
python-1st-edition-galanakis/

ebookgate.com
Core Python Programming 2nd Edition Wesley Chun

https://ebookgate.com/product/core-python-programming-2nd-edition-
wesley-chun/

ebookgate.com

Programming Python 3rd ed Edition Mark Lutz

https://ebookgate.com/product/programming-python-3rd-ed-edition-mark-
lutz/

ebookgate.com

Learning BeagleBone Python Programming Unleash the


potential of BeagleBone using Python 1st Edition Hiam

https://ebookgate.com/product/learning-beaglebone-python-programming-
unleash-the-potential-of-beaglebone-using-python-1st-edition-hiam/

ebookgate.com

Advanced Computer Programming in Python 1st Edition Karim


Pichara

https://ebookgate.com/product/advanced-computer-programming-in-
python-1st-edition-karim-pichara/

ebookgate.com

Python Programming for Teens 1st Edition Kenneth A.


Lambert

https://ebookgate.com/product/python-programming-for-teens-1st-
edition-kenneth-a-lambert/

ebookgate.com
Cover
Ultimate
Python
ProgrammingLearn Python with 650+
programs, 900+ practice
questions, and 5 projects
Deepali Srivastava

logo

www.bpbonline.com
First Edition 2024

Copyright © BPB Publications, India

ISBN: 978-93-55516-558

All Rights Reserved. No part of this publication may be reproduced,


distributed or transmitted in any form or by any means or stored in a
database or retrieval system, without the prior written permission of the
publisher with the exception to the program listings which may be entered,
stored and executed in a computer system, but they can not be reproduced
by the means of publication, photocopy, recording, or by any electronic and
mechanical means.

LIMITS OF LIABILITY AND DISCLAIMER OF WARRANTY

The information contained in this book is true to correct and the best of
author’s and publisher’s knowledge. The author has made every effort to
ensure the accuracy of these publications, but publisher cannot be held
responsible for any loss or damage arising from any information in this
book.

All trademarks referred to in the book are acknowledged as properties of


their respective owners but BPB Publications cannot guarantee the
accuracy of this information.

www.bpbonline.com
Dedicated to

Sri Anjaneya Swamy


About the Author
Deepali Srivastava has a Masterʼs degree in Mathematics and is an author
and educator in the field of computer science and programming. Her books
“C in Depthˮ and “Data Structures Through C in Depthˮ are widely used as
reference materials by students, programmers and professionals looking to
enhance their understanding of programming languages and data structures.
These books are known for their clarity, depth of coverage, and practical
approach to learning. In addition to her writing, Deepali Srivastava has
been involved in creating online video courses on Data structures and
Algorithms, Linux and Python programming. Her books and courses have
helped 350,000+ students learn computer science concepts. Her work has
been appreciated by students and has been a valuable resource for those
looking to build their programming skills.
Acknowledgement
I would like to thank God for blessing me with the opportunity and
inspiration to write this book, and for giving me the strength to do it.

I am grateful to my husband Suresh Kumar Srivastava for always believing


in my capabilities and consistently inspiring me to give my best. He
introduced me to book writing and helped me unleash my potential. His
thoughtful suggestions and feedback helped me improve the content and
presentation of this book.

I would like to thank my parents, my brother and my sister-in-law for their


unwavering love and support. Blessings of my parents and late parents-in-
law are a major source of my inner strength.

I am indebted to my teachers in my journey of education, especially my


teachers and friends in MJP Rohilkhand University Bareilly, where I got
introduced to the world of programming.

I extend my appreciation to the readers of my books and students of my


online courses for their interest in my work, and for their appreciation and
suggestions. Any sort of feedback is valuable to me and helps me in
improving my work and creating better content.

I am grateful to the BPB publication team for their guidance and support
provided during every step of the publishing journey. Special appreciation
goes to the editing team, layout team, and all other contributors involved in
bringing this book to life.
Preface
Python is a widely used general-purpose programming language. Its
popularity can be attributed to its simplicity and a rich set of powerful
features. The clean and intuitive syntax makes it an excellent choice for
novices, allowing them to grasp the fundamentals of programming quickly,
and the advanced features make it appealing to experienced programmers
too. It can run on various platforms, including Windows, macOS, and
Linux. Since it is an open-source software, it is freely available to all.

The widespread usage of Python is evident in the technology world, with


major companies and organizations such as Google, Amazon, Instagram,
Facebook, and NASA using it in different ways. Whether you are involved
in machine learning, data science, artificial intelligence, scientific
computing, automation or you need to create robust web applications and
games, Python provides the necessary tools and resources. The extensive
collection of libraries available in Python can be effectively utilized across
diverse domains. Therefore, adding Python to your skill set can greatly
enhance your capabilities and open up numerous opportunities in various
fields.

This book provides a thorough and comprehensive introduction to Python,


focusing on the core programming concepts and problem-solving skills
required for building a solid foundation in programming. Throughout the
book, there are numerous programming examples and end-of-chapter
exercises to give you a hands-on experience. The exercises include
multiple-choice questions and programming problems; multiple-choice
questions will assess both your memory and comprehension of the topic,
while the programming exercises will provide you with a chance to apply
the acquired concepts. The book includes coding conventions and best
practices for writing efficient, readable, and maintainable code. The code in
the book is written and tested using Python version 3.11, which is the most
recent version at the time of publishing the book.

Python is easy to learn. You can start writing Python programs within a few
days. However, if you wish to leverage all the powerful features of Python,
a more in-depth exploration is required. The content in this book can assist
you in achieving that. This book includes 21 chapters that gradually
introduce new topics so learners can proceed at a sustainable pace. If you
are a beginner, start from the first chapter and go through all the chapters in
order, and work out the examples and exercises along the way. If you have
a working knowledge of Python, you can quickly browse through the initial
chapters and then randomly jump to topics that are new to you or that you
want to master. However, I would still recommend reading the chapters in
sequence to get the most out of the book. If you are transitioning from
some other language, you might be tempted to skip the initial information,
but I would suggest you go through all the basic details to avoid any
confusion later. Here is a brief summary of the chapters presented in the
book.

Chapter 1 is an introduction to Python and shows the installation process.


Chapter 2 covers the fundamental elements of Python, such as data types,
variables, input, output, and many other basic concepts you need to get
started in Python. Chapter 3 provides a detailed explanation of strings that
represent textual data in Python. Chapters 4 and 5 cover the container
types: lists, tuples, dictionaries, and sets. Chapter 6 provides an insight into
conditional execution. In chapter 7, we will see how to perform repetitive
tasks using loops, and chapter 8 discusses some common looping
techniques in Python. Chapter 9 introduces the concept of comprehensions
which help us write shorter and readable code.

Chapter 10 contains detailed coverage of functions. We will see how to


create our own functions and will discuss parameters, arguments,
arguments passing, function objects, and many other details about
functions. Chapter 11 shows how to create and use modules and packages.
Chapter 12 is about namespaces and scoping rules. Chapter 13 shows how
to write programs that can create files, write data into files, and read the
data stored in files. Chapters 14, 15, and 16 provide you with a strong
understanding of the object-oriented concepts. We will discuss classes,
objects, methods, inheritance, polymorphism, and magic methods. Chapters
17 and 18 are devoted to advanced topics like iterators, generators, and
decorators. Chapter 19 is about functional programming and lambda
functions. Chapter 20 shows how to handle run-time errors in Python, and
Chapter 21 discusses context managers that are used to automate common
resource management patterns.

At the end of each chapter, you will find exercises, and their solutions are
provided at the end of the book. I would suggest that you try to solve these
exercises by yourself before looking at the solution. Solving exercises and
writing code will help you to internalize the concepts presented in the book.

Some typographical conventions are followed throughout the book for a


good reading experience. The code snippets and programs in the book
appear in this font to differentiate them from the regular text. Program
elements, such as variable names, types, etc., within the regular text, are in
this font. Any output produced by the code on the screen as a result of
running a program or anything that the user has to input through the screen
appears in this font.

My aim was to write an absolute hands-on book that is simple enough to


follow and yet gives detailed knowledge. Reading this book will be a
breeze, yet it will give you a comprehensive knowledge of Python and
instill the confidence to excel in any written test, interview, or professional
work. Programming is fun only when you get your hands dirty with code.
Reading a book is not enough for learning programming. I highly
recommend that you try the coding examples and exercises presented in the
book. The efforts you put in to strengthen your fundamentals of core
programming concepts will take you a long way in your software
development journey.

By the end of this book, you will develop a strong foundation in core
Python skills and will get the ability to explore the vast range of
functionalities offered by the standard library and third-party libraries. As
you progress, you will continue to be amazed by the capabilities of Python
and the remarkable libraries available. With your newfound skills you can
venture into diverse fields like data science or machine learning. Moreover,
if this is the first programming language you are learning, equipped with
the foundation of programming concepts and problem-solving skills, you
can easily learn any other programming language.
After using this book as a tutorial to learn the language, you can always
refer to it as a handy resource whenever you need to recall or review any
concept and apply it to your work.

Writing this book was a very enjoyable, insightful, and amazingly


satisfying journey for me and I am sure my readers will have a similar
experience while reading the book. I hope you enjoy reading the book and
start loving Python.

Happy programming!
Code Bundle and Coloured Images
Please follow the link to download the

Code Bundle and the Coloured Images of the book:

https://rebrand.ly/z815rfg

The code bundle for the book is also hosted on GitHub at


https://github.com/bpbpublications/Ultimate-Python-Programming. In case
there’s an update to the code, it will be updated on the existing GitHub
repository.

We have code bundles from our rich catalogue of books and videos
available at https://github.com/bpbpublications. Check them out!

Errata

We take immense pride in our work at BPB Publications and follow best
practices to ensure the accuracy of our content to provide with an indulging
reading experience to our subscribers. Our readers are our mirrors, and we
use their inputs to reflect and improve upon human errors, if any, that may
have occurred during the publishing processes involved. To let us maintain
the quality and help us reach out to any readers who might be having
difficulties due to any unforeseen errors, please write to us at :

[email protected]

Your support, suggestions and feedbacks are highly appreciated by the BPB
Publications’ Family.

Did you know that BPB offers eBook versions of every book published,
with PDF and ePub files available? You can upgrade to the eBook version
at www.bpbonline.com and as a print book customer, you are entitled to a
discount on the eBook copy. Get in touch with us at :

[email protected] for more details.


At www.bpbonline.com, you can also read a collection of free technical
articles, sign up for a range of free newsletters, and receive exclusive
discounts and offers on BPB books and eBooks.

Piracy

If you come across any illegal copies of our works in any form on the
internet, we would be grateful if you would provide us with the location
address or website name. Please contact us at [email protected]
with a link to the material.

If you are interested in becoming an author

If there is a topic that you have expertise in, and you are interested in either
writing or contributing to a book, please visit www.bpbonline.com. We
have worked with thousands of developers and tech professionals, just like
you, to help them share their insights with the global tech community. You
can make a general application, apply for a specific hot topic that we are
recruiting an author for, or submit your own idea.

Reviews

Please leave a review. Once you have read and used this book, why not
leave a review on the site that you purchased it from? Potential readers can
then see and use your unbiased opinion to make purchase decisions. We at
BPB can understand what you think about our products, and our authors
can see your feedback on their book. Thank you!

For more information about BPB, please visit www.bpbonline.com.

Join our book’s Discord space

Join the book’s Discord Workspace for Latest updates, Offers, Tech
happenings around the world, New Release and Sessions with the Authors:

https://discord.bpbonline.com
Table of Contents
1. 1. Introduction to Python

1. 1.1 What makes Python so popular

2. 1.2 Python implementation

3. 1.3 Installing Python

4. 1.4 Python Interactive Mode

5. 1.5 Executing a Python Script

6. 1.6 IDLE

7. 1.7 Getting Help

2. 2. Getting Started

1. 2.1 Identifiers

2. 2.2 Python Types

3. 2.3 Objects

4. 2.4 Variables and assignment statement

5. 2.5 Multiple and Pairwise Assignments

6. 2.6 Deleting a name

7. 2.7 Naming convention for constants

8. 2.8 Operators

1. 2.8.1 Arithmetic operators


2. 2.8.2 Relational operators

3. 2.8.3 Logical operators

4. 2.8.4 Identity operators

5. 2.8.5 Membership operators

6. 2.8.6 Bitwise operators

9. 2.9 Augmented assignment statements

10. 2.10 Expressions

11. 2.11 Order of operations: Operator Precedence and Associativity

12. 2.12 Type Conversion

13. 2.13 Statements

14. 2.14 Printing Output

15. 2.15 Getting user input

16. 2.16 Complete programs

17. 2.17 Comments

18. 2.18 Indentation in Python

19. 2.19 Container types

20. 2.20 Mutable and Immutable Types

21. 2.21 Functions and methods

22. 2.22 Importing

23. 2.23 Revisiting interactive mode


24. 2.24 Errors

25. 2.25 PEP8

26. Exercise

3. 3. Strings

1. 3.1 Indexing

2. 3.2 Strings are immutable

3. 3.3 String Slicing

4. 3.4 String Concatenation and Repetition

5. 3.5 Checking membership

6. 3.6 Adding whitespace to strings

7. 3.7 Creating multiline strings

8. 3.8 String methods

9. 3.9 Case-changing methods

10. 3.10 Character classification methods

11. 3.11 Aligning text within strings

12. 3.12 Removing unwanted leading and trailing characters

13. 3.13 Searching and replacing substrings

14. 3.14 Chaining method calls

15. 3.15 String comparison

16. 3.16 String conversions


17. 3.17 Escape Sequences

18. 3.18 Raw string literals

19. 3.19 String formatting

20. 3.20 String formatting using the format() method of string class

21. 3.21 Representation of text - character encodings

22. Exercise

4. 4. Lists and Tuples

1. 4.1 Accessing individual elements of a list by indexing

2. 4.2 Getting parts of a list by slicing

3. 4.3 Changing an item in a list by index assignment

4. 4.4 Changing a Portion of the list by slice assignment

5. 4.5 Adding an item at the end of the list by using append()

6. 4.6 Adding an item anywhere in the list by using insert()

7. 4.7 Adding multiple items at the end by using extend() or +=

8. 4.8 Removing a single element or a slice by using the del


statement

9. 4.9 Removing an element by index and getting it by using pop()

10. 4.10 Removing an element by value using remove()

11. 4.11 Removing all the elements by using clear()

12. 4.12 Sorting a List

13. 4.13 Reversing a List


14. 4.14 Finding an item in the list

15. 4.15 Comparing Lists

16. 4.16 Built-in functions used on lists

17. 4.17 Concatenation and Replication

18. 4.18 Using a list with functions from the random module

19. 4.19 Creating a list

20. 4.20 Using range to create a list of integers

21. 4.21 Using the repetition operator to create a list of repeated


values

22. 4.22 Creating a list by splitting a string

23. 4.23 Converting a list of strings to a single string using join()

24. 4.24 List of Lists (Nested lists)

25. 4.25 Copying a list

26. 4.26 Shallow copy and deep copy

27. 4.27 Repetition operator with nested lists

28. 4.28 Tuples

29. 4.29 Tuple packing and unpacking

30. Exercise

5. 5. Dictionaries and Sets

1. 5.1 Dictionaries

2. 5.2 Adding new key-value pairs


3. 5.3 Modifying Values

4. 5.4 Getting a value from a key by using the get() method

5. 5.5 Getting a value from a key by using the setdefault() method

6. 5.6 Getting all keys, all values, and all key-value pairs

7. 5.7 Checking for the existence of a key or a value in a dictionary

8. 5.8 Comparing dictionaries

9. 5.9 Deleting key-value pairs from a dictionary

10. 5.10 Creating a Dictionary at run time

11. 5.11 Creating a dictionary from existing data by using dict()

12. 5.12 Creating a dictionary by using the fromkeys() method

13. 5.13 Combining dictionaries

14. 5.14 Nesting of dictionaries

15. 5.15 Aliasing and Shallow vs. Deep Copy

16. 5.16 Introduction to sets

17. 5.17 Creating a set

18. 5.18 Adding and Removing elements

19. 5.19 Comparing sets

20. 5.20 Union, intersection, and difference of sets

21. 5.21 Frozenset

22. Exercise
6. 6. Conditional Execution

1. 6.1 if statement

2. 6.2 else clause in if statement

3. 6.3 Nested if statements

4. 6.4 Multiway selection by using elif clause

5. 6.5 Truthiness

6. 6.6 Short circuit behavior of operators and and or

7. 6.7 Values returned by and and or operators

8. 6.8 if else operator

9. Exercise

7. 7. Loops

1. 7.1 while loop

1. 7.1.1 Indentation matters

2. 7.1.2 Removing all occurrences of a value from the list


using the while loop

3. 7.1.3 while loop for input error checking

4. 7.1.4 Storing user input in a list or dictionary

2. 7.2 for loop

1. 7.2.1 Iterating over a string with for loop

2. 7.2.2 Unpacking in for loop header

3. 7.2.3 Iterating over dictionaries and sets


4. 7.2.4 Iterating through a series of integers

3. 7.3 Nesting of Loops

1. 7.3.1 Using nested loops to generate combinations

2. 7.3.2 Iterating over nested data structures

4. 7.4 Premature termination of loops using the break statement

5. 7.5 continue statement

6. 7.6 else block in Loops

7. 7.7 pass statement

8. 7.8 for loop vs. while loop

9. Exercise

8. 8. Looping Techniques

1. 8.1 Iterating in sorted and reversed order

2. 8.2 Iterating over unique values

3. 8.3 Index-Based for loops

4. 8.4 Making in-place changes in a list while iterating

5. 8.5 Skipping some items while iterating

6. 8.6 Using range and len combination to shuffle a sequence

7. 8.7 enumerate function

8. 8.8 Iterating over multiple sequences using zip

9. 8.9 Modifying a collection while iterating in a for loop


10. 8.10 Infinite loop with break

11. 8.11 Avoiding complex logical conditions using break

12. Exercise

9. 9. Comprehensions

1. 9.1 List Comprehensions

2. 9.2 if clause in list comprehension

3. 9.3 Ternary operator in list comprehension

4. 9.4 Modifying a list while iterating

5. 9.5 Getting keys from values in a dictionary using list


comprehension

6. 9.6 Using list comprehensions to avoid aliasing while creating


lists of lists

7. 9.7 Multiple for clauses and Nested list Comprehensions

8. 9.8 Extracting a column in a matrix

9. 9.9 Dictionary Comprehensions

10. 9.10 Inverting the dictionary

11. 9.11 Set Comprehensions

12. 9.12 When not to use comprehensions

13. Exercise

10. 10. Functions

1. 10.1 Function Definition


2. 10.2 Function call

3. 10.3 Flow of control

4. 10.4 Parameters and Arguments

5. 10.5 No type checking of arguments

6. 10.6 Local Variables

7. 10.7 return statement

8. 10.8 Returning Multiple Values

9. 10.9 Semantics of argument passing

1. 10.9.1 Why study argument passing

2. 10.9.2 Pass by assignment

3. 10.9.3 Assignment inside function rebounds the parameter


name

4. 10.9.4 Immutables vs Mutables as arguments

5. 10.9.5 How to get the changed value of an immutable type

6. 10.9.6 How to prevent change in mutable types

7. 10.9.7 Digression for programmers from other languages

8. 10.9.8 Advantages of Python’s information passing

10. 10.10 Default Arguments

11. 10.11 Default arguments that could change over time

12. 10.12 Positional and Keyword Arguments

13. 10.13 Unpacking Arguments


14. 10.14 Variable number of positional arguments

15. 10.15 Variable number of keyword arguments

16. 10.16 Keyword-only arguments

17. 10.17 Positional-Only Arguments

18. 10.18 Multiple Unpackings in a Python Function Call

19. 10.19 Arguments and Parameters summary

20. 10.20 Function Objects

21. 10.21 Attributes of a function

22. 10.22 Doctrsings

23. 10.23 Function Annotations

24. 10.24 Recursive Functions

25. Exercise

11. 11. Modules and Packages

1. 11.1 Modules

2. 11.2 Types of modules

3. 11.3 Exploring modules

4. 11.4 Creating and naming a new module

5. 11.5 Importing a module

6. 11.6 Importing all names from a module

7. 11.7 Restricting names that can be imported


8. 11.8 Importing individual names from a module

9. 11.9 Using an alias while importing

10. 11.10 Documenting a module

11. 11.11 Module search Path

12. 11.12 Module object

13. 11.13 Byte compiled version of a module

14. 11.14 Reloading a module

15. 11.15 Scripts and modules

16. 11.16 Packages

17. 11.17 Importing a package and its contents

18. 11.18 Subpackages

19. 11.19 Relative imports

20. Exercise

12. 12. Namespaces and Scope

1. 12.1 Namespaces

2. 12.2 Inspecting namespaces

3. 12.3 Scope

4. 12.4 Name Resolution

5. 12.5 global statement

6. 12.6 nonlocal statement


7. Exercise

13. 13. Files

1. 13.1 Opening a File

2. 13.2 File opening modes

3. 13.3 Buffering

4. 13.4 Binary and Text Files

5. 13.5 Closing a file

6. 13.6 with statement

7. 13.7 Random Access

8. 13.8 Using seek in text mode

9. 13.9 Calling seek in append mode

10. 13.10 Reading and writing to the same file

11. 13.11 Reading a File using read()

12. 13.12 Line oriented reading

13. 13.13 Writing to a file

14. 13.14 Redirecting output of print to a file

15. 13.15 Example Programs

16. 13.16 File Related Modules

17. 13.17 Command Line Arguments

18. 13.18 Storing and Retrieving Python objects using pickle


19. Exercise

20. Project : Hangman Game

14. 14. Object Oriented Programming

1. 14.1 Programming Paradigms

2. 14.2 Introduction to object-oriented programming

3. 14.3 Defining Classes and Creating Instance Objects

4. 14.4 Adding methods to the class

5. 14.5 Adding instance variables

6. 14.6 Calling a method inside another method

7. 14.7 Common pitfalls

8. 14.8 Initializer

9. 14.9 Data Hiding

10. 14.10 Class Variables

11. 14.11 Class and object namespaces

12. 14.12 Changing a class variable through an instance

13. 14.13 Class Methods

14. 14.14 Creating alternative initializers using class Methods

15. 14.15 Static Methods

16. 14.16 Creating Managed Attributes using properties

1. 14.16.1 Creating read only attributes using properties


2. 14.16.2 Creating Computed attributes using properties

3. 14.16.3 Deleter method of property

17. 14.17 Designing a class

18. Exercise

19. Project : Quiz creation

20. Project : Snakes and Ladders Game

21. Project : Log in system

15. 15. Magic Methods

1. 15.1 Overloading Binary Arithmetic operators

2. 15.2 Reverse methods

3. 15.3 In-place methods

4. 15.4 Magic Methods for comparison

5. 15.5 Comparing objects of different classes

6. 15.6 String representation of an instance object

7. 15.7 Construction and destruction of objects

8. 15.8 Making instance objects callable

9. 15.9 Overloading type conversion functions

10. 15.10 List of magic methods

11. Exercise

12. Project : Date Class


16. 16. Inheritance and Polymorphism

1. 16.1 Inheriting a class

2. 16.2 Adding new methods and data members to the derived class

3. 16.3 Overriding a base Method

4. 16.4 Invoking the base class methods

5. 16.5 Multilevel Inheritance

6. 16.6 object class

7. 16.7 Multiple Inheritance

8. 16.8 Method Resolution Order (MRO)

9. 16.9 super and MRO

10. 16.10 Polymorphism

11. 16.11 Abstract Base classes

12. 16.12 Composition

13. Exercise

17. 17. Iterators and Generators

1. 17.1 Iterables

2. 17.2 Iterators

3. 17.3 for loop Iteration Context – How for loop works

4. 17.4 Iteration Tools

5. 17.5 Iterator vs Iterable


6. 17.6 Creating your own Iterator

7. 17.7 Making your class Iterable

8. 17.8 Some More Iterators

9. 17.9 Lazy evaluation

10. 17.10 itertools Module

11. 17.11 Generators

12. 17.12 Generator function vs Normal function

13. 17.13 Generator expressions

14. Exercise

18. 18. Decorators

1. 18.1 Prerequisites for understanding decorators

2. 18.2 Introduction to decorators

3. 18.3 Writing your first decorator

4. 18.4 Applying your decorator to multiple functions

5. 18.5 Automatic decoration syntax

6. 18.6 Decorator Example: Timer

7. 18.7 Decorator Example: Logger

8. 18.8 Decorator Example: Counting function calls

9. 18.9 Applications of decorators

10. 18.10 Decorating functions that take arguments


11. 18.11 Returning values from decorated functions

12. 18.12 Decorator Example: Checking return values

13. 18.13 Decorator Example: Checking argument values

14. 18.14 Applying Multiple Decorators

15. 18.15 Preserving metadata of a function after decoration

16. 18.16 General template for writing a decorator

17. 18.17 Decorators with parameters

18. 18.18 General template for writing a decorator factory

19. 18.19 Decorator factory example

20. 18.20 Applying decorators to imported functions

21. 18.21 Decorating classes

22. 18.22 Class Decorators

23. 18.23 Class Decorators with parameters

24. Exercise

19. 19. Lambda Expressions and Functional Programming

1. 19.1 Lambda expression

2. 19.2 Comparing def statement and lambda expression

3. 19.3 Examples of lambda expressions

4. 19.4 Using Lambda expressions

5. 19.5 Using lambda expressions for returning function objects


6. 19.6 Lambda expressions as closures

7. 19.7 Creating jump tables using lambda functions

8. 19.8 Using lambda expressions in sorted built-in function

9. 19.9 Functional programming

10. 19.10 map

11. 19.11 map with multiple iterables

12. 19.12 filter

13. 19.13 Reducing an iterable

14. 19.14 Built-in reducing functions

15. 19.15 operator module

16. Exercise

20. 20. Exception Handling

1. 20.1 Types of Errors

2. 20.2 Strategies to handle exceptions in your code

3. 20.3 Error Handling by Python (Default exception handling)

4. 20.4 Built-in Exceptions: Python Exceptions Class Hierarchy

5. 20.5 Customized Exception Handling by using try…except

6. 20.6 Catching multiple exceptions using multiple except


handlers and single except handler

7. 20.7 How to handle an exception

8. 20.8 Guaranteed execution of finally block


9. 20.9 else Block

10. 20.10 Why do we need an else block

11. 20.11 How to get exception details

12. 20.12 Nested try statements

13. 20.13 Raising Exception

14. 20.14 Re-raising Exception

15. 20.15 Chaining Exceptions

16. 20.16 Creating your own exceptions in Python (Custom


exceptions)

17. 20.17 Assertions

18. Exercise

21. 21. Context Managers

1. 21.1 with statement

2. 21.2 Implementing our own context manager

3. 21.3 Exception raised inside with block

4. 21.4 Why we need with statement and context managers

5. 21.5 Runtime context

6. 21.6 Example: Sending output of a portion of code to a file

7. 21.7 Example : Finding time taken by a piece of code

8. 21.8 Using context managers in the standard library

9. 21.9 Nested with statements and multiple context Managers


10. 21.10 Implementing a context manager by using a decorator on a
generator

11. Exercise

22. Solutions

23. Index
Introduction to Python 1

Python is a widely used high-level and general-purpose programming


language originally developed by Guido Van Rossum in the early 1990s in
the Netherlands. It is maintained by a community of core developers who are
actively engaged in its growth and advancement. Although the official logo
of Python shows two intertwined snakes, it is not named after any snake.
Van Rossum named this language after a 1970s comedy show 'Monty
Python's Flying Circus'.
Python has three major versions; the initial version, Python 1.0, was released
in January 1994. The second major version, Python 2.0, was released in
2000, and the third major version, Python 3.0, was released in 2008. Python
3 is not backward compatible with Python 2; this means that the code written
in Python 2 may not work as expected in Python 3 without making some
modifications. In this book, we will use Python 3. The latest release of
Python is available on its official website www.python.org. Python is an
open-source software, which means that it is free to use and distribute.

1.1 What makes Python so popular


Python is a general-purpose language used in a wide variety of domains. It is
used extensively in different fields such as web development, data mining,
artificial intelligence, image processing, robotics, network programming,
developing user interfaces, database programming, scientific and
mathematical computing, game programming, and even education. Most of
the top companies and organizations, such as Google, Facebook, Amazon,
and NASA, use Python in different ways. Let us see some of the key factors
that contribute to Python's popularity.
Python is very easy to learn. It doesn't take much time to become productive
with Python. This is why it is often the introductory programming language
taught in many universities. Compared to languages such as C++ or Java,
Python code tends to be more concise, requiring fewer lines of code to
achieve the same functionality. Due to the simple syntax of Python,
programmers can focus more on finding the solution to a problem instead of
getting caught up in complex language features. Python uses indentation for
grouping together statements, resulting in a visually clean layout that
enhances code readability.
Python offers a convenient command line interface known as the 'Python
interactive shell' or 'Python REPL' (Read-Eval-Print Loop). With the Python
interpreter, you have the option to work interactively, allowing you to test
and debug small sections of code in real-time. The interactive mode serves
as a useful tool for experimenting and exploring Python's features.
One of the main advantages of Python is that it takes care of memory
management automatically. Python's built-in memory management system
allocates memory when needed and frees it up when it is no longer in use.
Programmers do not have to worry about managing memory manually, as
they would have to do in other languages like C or C++.
Python includes a vast standard library of modules; this is why the phrase
'Batteries included' is often used for Python. These modules contain code
that you can use in your own programs. In addition to the extensive standard
library, many third-party libraries are also available for use. Thus, you have
access to lots of prewritten reusable code in the form of standard library
modules and third-party modules, which can do most of the work for you
and save you from reinventing the wheel. This code can be incorporated into
your code to develop complex solutions with minimal effort. Whether you
are working on web programming, creating graphics, analyzing data,
performing mathematical calculations, engaging in scientific computing, or
developing games, you will find reusable code modules that can help you
achieve your goals.
Python supports multiple programming paradigms, including procedural,
functional, and object-oriented programming. Thus, programmers have the
flexibility to choose the coding structure that best suits their needs. The
object-oriented features of Python are much easier to implement and are
more intuitive when compared to similar features found in other
programming languages.
Python is a cross-platform and portable programming language, which
means that programs written in Python can be developed and executed on
various hardware platforms and operating systems. The same code can be
executed on multiple platforms without making any significant changes. The
cross-platform development minimizes the efforts required to adapt the
programs to different systems and thus facilitates code reuse and sharing on
different platforms.
Python has the capability to interact with software components written in
other languages. Python code can call libraries written in C and C++, and it
can also integrate with components developed in Java and .NET. This allows
Python programmers to tap into the strengths and functionalities of other
languages and libraries written in them. Python is also embeddable which
means that Python code can be placed within the code of another language
like C or C++.
Another reason for Python's popularity is its large base of active and
supportive developer community. Community members are actively engaged
in improving and enhancing the capabilities of Python as well as in
developing various libraries and tools. There are numerous resources and
extensive support available due to the vibrant community members.
Python has emerged as the preferred programming language for developers
because of its ease of use and powerful features. It is suitable both for
beginners and experts alike, and due to its versatility, it can be used in a
variety of applications.
In the next section we will learn about Python implementations and will see
what happens internally when a Python program is executed. While it is not
necessary to have this knowledge in order to write and run programs, having
a fundamental understanding of what occurs behind the scenes during
program execution is beneficial for a comprehensive understanding of the
language.

1.2 Python implementation


The terms C, C++, Basic, Java, or Python refer to programming languages,
which are essentially sets of rules and specifications. In order to use these
languages, they need to be implemented by creating software that allows us
to write programs in that language and run them on a computer. The
implementation of a language is the program that actually runs the code that
you write in that language. An implementation translates the source code to
native machine code instructions (binary 0s and 1s) so that the computer's
processor can execute it.
There are primarily two approaches to implementing a programming
language: compilation and interpretation. In compilation, a compiler
translates the complete program code in one go to another language such as
machine code or bytecode. If the translated code is machine code that is
understood by the processor, then it is directly executed, and if it is
bytecode, then it has to be again input to another interpreter or compiler. In
interpretation, an interpreter translates the code to machine code one line at a
time; a line of code is read, translated, and executed, then the next line is
read, translated, and executed, and so on. The code is translated line by line
at run time, so the interpreted implementations tend to be slower than the
compiled ones, which translate the whole code at once.
An implementation of a language can be a compiler, interpreter, or a
combination of both. A programming language can have multiple
implementations, and these implementations can be written in different
languages and can use different approaches to compile or interpret code. The
notion of interpretation and compilation is associated with language
implementation rather than the language itself; describing a language as
compiled or interpreted is not technically correct. The language
implementations that are written for a language are described as compiled or
interpreted and not the language. Compilation or interpretation is not a part
of the language specification; it is an implementation decision. The
implementations of C and C++ mostly use the compilation approach, while
Java, Python, and C# implementations generally use a combination of
compilation and interpretation techniques. C and C++ compilers translate
source code to machine code, which is executed directly by the processor.
Python has multiple implementations. The original and standard
implementation of Python is CPython written in C language. It is the most
widely used and up-to-date implementation of Python. When you download
Python software from the official site python.org, this is the implementation
that you get. The other implementations are Jython written in Java, and
IronPython written for the .NET platform. PyPy is the implementation that is
written in RPython, which is a subset of Python.
The software that is used for running Python programs is referred to as
Python interpreter. Let us understand how CPython interpreter combines the
compilation and interpretation techniques to execute a Python program.
We write our Python code in a source file (.py file), but the computer cannot
understand and execute this code; it can execute only machine code, which
consists of instructions written in binary form (0s and 1s). The source code
has to be converted to machine code so that the processor can execute it. The
source code is not directly converted to machine code. It is first compiled
into an intermediate form known as the bytecode. This bytecode is a low-
level code that is Python-specific and platform-independent, but it is not
understandable to the processor.
There is another software called Python Virtual Machine (PVM), that is
responsible for executing this bytecode on a specific platform. The bytecode
passes through the Python Virtual machine; it interprets this bytecode, which
means that it converts the bytecode instructions to machine code instructions
one by one and sends these machine code instructions to the processor for
execution, and we get the output. So, the job of PVM is to convert the
bytecode instructions to machine code instructions that the processor can
understand and execute.

Figure 1.1: The execution of a Python program

This is what happens when we execute a Python program. The intermediate


compilation step is hidden from the programmer; we can just type and run
our program immediately. The programmer does not have to explicitly
compile the code, so there is no separate compile time in Python; there is
only runtime. The compilation to bytecode is done to improve the efficiency
as the bytecode can be interpreted faster than the original source code.
In this whole process, the bytecode complier is a software that converts
source code to bytecode, and PVM is a software that converts bytecode to
machine code for the target platform. Python Virtual machine contains some
platform-specific components that may be implemented differently for each
platform. This allows the virtual machine to covert the bytecode into native
machine code according to the platform. It abstracts away the underlying
hardware and operating system details and thus provides a consistent
runtime environment for Python programs across different platforms. Both
the bytecode compiler and the virtual machine are part of the Python
interpreter software and are included in your Python installation.
The intermediate bytecode is generally cached for faster execution. It is
stored in .pyc or .pyo files inside a folder named __pycache__ and the
programmer can just ignore these files. When the program is run multiple
times without modifying the source code, the compiled bytecode from the
cached file is loaded and executed instead of re-compiling from source code
to bytecode every time. This bytecode is stored only for imported files, not
for the top-level scripts; we will see the difference between the two later in
the book.
The Jython implementation translates Python code into Java bytecode,
enabling its execution on a Java virtual machine. An advantage of Jython is
its ability to directly access Java libraries. Similarly, IronPython is designed
for the .NET framework and facilitates integration with .NET components.
Some implementations of virtual machines (bytecode interpreters) use just-
in-time (JIT) compilation approach to speed up the interpretation process.
The PyPy implementation of Python has better speed as it includes a just-in-
time compiler for faster execution of the bytecode. Just-in-time compiler
will compile the frequently executed blocks of bytecode to machine code
and cache the result. Next time, when the virtual machine has to execute the
same block of bytecode, the precompiled(cached) machine code is utilized
and executed, resulting in faster execution. So, the JIT compiler uses the
compilation approach to improve the efficiency of bytecode execution.

1.3 Installing Python


To download Python, visit the official website of Python. On the homepage,
select the Downloads option to go to the download page, or you can directly
go to www.python.org/downloads/. The website will automatically detect
your operating system and provide a suitable installer that corresponds to
your system's requirements, whether it be 32-bit or 64-bit. Click on the
Download button to download the installer (.exe) file for the latest version of
Python. At the time of writing this book, the latest version is 3.11.3. If you
wish to download any previous version of Python, you can scroll down the
page and click on the download button located next to the version number
you desire.

Figure 1.2: Official website of Python

Once the download is complete, double-click on the installer to execute it


and begin the installation process. On the first screen of the installer, you
will be presented with two choices: "Install Now" and "Customize
Installation." Clicking on "Install Now" will install Python with the default
features, while clicking on "Customize Installation" will allow you to
change the installation location or install other optional and advanced
features. The defaults should work well for now, so we will go with Install
Now. Before clicking on Install Now, make sure to select the Add
python.exe to PATH checkbox, as this will add Python to your system's
PATH environment variable and will enable you to run Python from the
command prompt.

Figure 1.3: Installing Python

Click Yes if it asks for permission to make changes to your device. The
installation begins, and all the required Python files, along with the standard
library, will be installed on your system.
Figure 1.4: Installation in progress

After the installation is complete, the following pop-up box will appear. This
shows that Python is installed on your system. Click on Close to complete
the installation and exit the installer. The appearance of the images shown in
the screenshots may vary depending on the version of Python that you
choose to install.
Figure 1.5: Installation successful

To verify the installation, write cmd in the Start search menu to open the
command prompt window and type the command python --version.
If Python has been successfully installed on your system, it will show the
version of the Python installed. Now write python (all in lowercase) in the
command window. You will see a line with some text describing the Python
version, and after that, you will see a prompt with three greater-than signs
(>>>). This is the Python shell prompt. Write 8 + 2 and press Enter; you
will get the output as 10 on the next line. The prompt appears again; this
time, write print('Hello world'), and the text Hello world will
appear on the next line. This verifies that Python is up and working on your
system. On this interactive Python shell, you can execute single statements
of Python. To quit this Python shell and come back to your command
prompt, type quit() or exit() or press Ctrl-Z.
Figure 1.6: Verifying installation on the command line

You can also verify your installation by opening the Integrated Development
and Learning Environment application(IDLE), which is installed by default
with Python. To open IDLE, type idle or python in the Start search menu
and click on the IDLE app. If the installation is successful, IDLE will show
an interactive Python shell window in which you can type Python commands
at the shell prompt (>>>) and execute them.

Figure 1.7: Verifying installation on IDLE

Installation on Mac is done in a similar way. Most macOS systems come


with Python, but usually, it is the 2.x version. To check if Python is installed,
type python --version on your terminal. To check if Python 3 is
installed, type python3 --version on your terminal. If Python 3 is not
installed, you can install it from the official website, and if it is installed, you
can update it to get the latest version.
Visit the official Python website and download the installer package (.pkg
file) that if offers for your system. After downloading, double-click on the
installer to run the installation process. Proceed with the installation by
following the on-screen instructions and accepting the defaults. You may
need to enter your administrator password to authorize the installation.
After the installation process is complete, Python's installation folder will
automatically open up. Inside this folder, you will find IDLE application,
which, as we have seen, is the development environment that comes with
Python. Double-click on this application to open it. If the installation is
successful, IDLE will display the interactive Python shell. You can type
print('Hello world') at the shell prompt to verify that it is
functioning correctly. To confirm the installation on the terminal, open the
Terminal application and type python3 –version, and press Enter.
This should show the version of Python that you have installed. Type
python3 to open the Python shell, which shows the >>> prompt where you
can start typing Python statements. You can close this Python shell by
entering Ctrl D or typing exit().
Installation on Linux can be done through the Package Manager specific to
your distribution. Linux systems come with Python installed on them. To
check whether Python is installed correctly or to check before installation
whether Python is already there on the system, execute the following
command irrespective of your operating system:
$ python3 --version
or
$ python --version
On Mac and Linux, the python --version command will mostly show
the Python 2 version and the python3 --version command will show
the Python 3 version.
After you have installed Python on your machine, you can either use an
Integrated Development Environment (IDE) to write and execute your
Python scripts, or you can write your script in a text editor and execute them
on the command line. An IDE combines a text editor and software tools to
provide a program development environment. You can create, edit, run, and
debug your programs using a single interface, and this makes program
development easier.
We will be working on IDLE, which is the built-in IDE for Python. IDLE is
included with the Python standard distribution for Windows and macOS, so
there is no need to install it separately. To get started with Python, IDLE is a
good IDE. It has an interactive interpreter and features like smart
indentation, auto-completion, and syntax highlighting, and also includes a
basic debugger. There are many other popular text-based editors and IDEs
available that can work with Python. If you want, you can choose any of
them to write your programs.
Eclipse is the IDE for development in Java. If you are familiar with Eclispe,
you can install the PyDev plugin to develop Python programs. If you are
comfortable working with Vim, you can use it for Python development by
adding some plugins. PyCharm is the Python IDE for professional
developers by JetBrains and comes in both free and paid editions. Sublime
Text is a code editor that supports Python and many other languages. If you
don't want to install Python on your computer, there are online platforms
available that provide a web-based Python interpreter.

1.4 Python Interactive Mode


Whether you work on the command line or use IDLE, there are two ways in
which you can use the Python interpreter - script mode and interactive mode.
In the script mode, we write our program statements in a file and then
execute the contents of that file to get the output of the whole program. In
the interactive mode, we type single Python statements on the prompt, and
we get to see the output immediately. This interactive experimentation is
particularly useful for beginners who have just started to learn the language.
Even when we have learned the language well, we can use this mode to
write short snippets of code and see how they work before putting them into
a big program. As we have seen in the previous section, we can enter the
interactive mode (Python shell) either through the command line or through
IDLE.
The shell prompt or interactive prompt (>>>) denotes that you are in the
Python interactive mode, so you can type just any valid Python statement or
expression, hit Enter, and the result will be displayed immediately.
>>> 4 + 6
10
>>> print('Hello')
Hello
The interactive mode has active memory that remembers the previously
executed statements on the prompt. However, this memory is active only for
the current session. If you exit the interpreter and open it again, the code you
typed in will not be available or remembered. So, if you want to retain and
reuse your code, you should place it in a file and save it.
Although the interactive mode is not used for developing programs, it can
serve as an excellent learning tool and can also be used to test code snippets.
This book will often use this mode to explain different language features.
You can also use this mode to play around with different Python constructs
and functionalities and explore more about them.
Here is something different and interesting that you can try on the interactive
prompt. Type import this, and you will get a short poem written by
Tim Peters. This poem summarizes the style and philosophy of Python in the
form of some guiding principles.
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the
rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to
guess.
There should be one-- and preferably only one --
obvious way to do it.
Although that way may not be obvious at first
unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a
bad idea.
If the implementation is easy to explain, it may be
a good idea.
Namespaces are one honking great idea -- let's do
more of those!

1.5 Executing a Python Script


Interactive mode is good for experimenting and exploring, but when we have
to write complete working programs that can be reused, we need to save our
work in a file. In script mode, we create a file of Python statements and
instruct the interpreter to execute the whole file, which is often called a
script. The interpreter will execute all statements in the file sequentially,
maintaining the order in which they appear.
There are two different ways of creating and executing scripts - we can write
the script in a text editor and execute it on the command line, or we can
create and execute the script in a development environment like IDLE. First,
let us see how to execute a script from the command line.
Create a new text file using a text editor like Notepad++. Write the following
two lines of code in the file and save it with the name hello.py; all Python
files are conventionally saved with the .py extension.
print('Hello!')
print(5 + 3)
You have written your first Python program. Now, let us see how to run it
from the command line. On the command prompt, type python followed by
the filename with full path and press Enter. Our program will be executed,
and we can see the output.

Figure 1.8: Executing a Python program on the command line


If you do not want to type the whole path, you can first change your current
directory to the directory in which you have your file by using the cd
command. On recent versions of Windows and Python 3.3 onwards, you can
write py instead of python or even write the name of the file to execute it.
Next, let us see how to run a Python script using IDLE, the built-in IDE of
Python. When you open IDLE on your system, the Shell window appears. In
the File Menu, click on New File, and a new window will open with Untitled
written on its title bar. Save the file with the name hi.py. By default, your file
will be saved in the Python installation folder where the Python code is
stored. It is better to make a working folder for your programs in some other
location and save your files in that folder. After saving the file, write the
following code in the file:
print('Hi!')
print(5 - 2)
To run this program, either press F5 or click on Run Module in the Run
menu. The output of the program appears in the Python Shell window.
Similarly, we can execute any existing Python program in IDLE; for
example, we can open and execute our file hello.py that we had created
using a text editor.
So now you know how to create a Python program and execute it. You can
either use IDLE to write and run your programs, or you can write your
program in a text editor and then run it on the command line. For beginners,
using IDLE is recommended. If you are using a text editor, Notepad++
would be a better choice than Notepad. You should not use a word processor
like MS Word, which uses formatted text. The text editor should store text in
its pure form.

1.6 IDLE
To write programs effectively, you need to have a good understanding of the
programming environment. Therefore, it is worth spending some time
looking at the features of IDLE, the IDE that you will be using to write your
programs. If you choose to use a different programming environment, make
sure that you familiarize yourself with it before starting to write programs.
Another Random Scribd Document
with Unrelated Content
Gutenberg” appears, or with which the phrase “Project
Gutenberg” is associated) is accessed, displayed, performed,
viewed, copied or distributed:

This eBook is for the use of anyone anywhere in the United


States and most other parts of the world at no cost and
with almost no restrictions whatsoever. You may copy it,
give it away or re-use it under the terms of the Project
Gutenberg License included with this eBook or online at
www.gutenberg.org. If you are not located in the United
States, you will have to check the laws of the country
where you are located before using this eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is


derived from texts not protected by U.S. copyright law (does not
contain a notice indicating that it is posted with permission of
the copyright holder), the work can be copied and distributed to
anyone in the United States without paying any fees or charges.
If you are redistributing or providing access to a work with the
phrase “Project Gutenberg” associated with or appearing on the
work, you must comply either with the requirements of
paragraphs 1.E.1 through 1.E.7 or obtain permission for the use
of the work and the Project Gutenberg™ trademark as set forth
in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is


posted with the permission of the copyright holder, your use and
distribution must comply with both paragraphs 1.E.1 through
1.E.7 and any additional terms imposed by the copyright holder.
Additional terms will be linked to the Project Gutenberg™
License for all works posted with the permission of the copyright
holder found at the beginning of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files
containing a part of this work or any other work associated with
Project Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute


this electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the
Project Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™
works unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or


providing access to or distributing Project Gutenberg™
electronic works provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project


Gutenberg™ electronic work or group of works on different
terms than are set forth in this agreement, you must obtain
permission in writing from the Project Gutenberg Literary
Archive Foundation, the manager of the Project Gutenberg™
trademark. Contact the Foundation as set forth in Section 3
below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on,
transcribe and proofread works not protected by U.S. copyright
law in creating the Project Gutenberg™ collection. Despite these
efforts, Project Gutenberg™ electronic works, and the medium
on which they may be stored, may contain “Defects,” such as,
but not limited to, incomplete, inaccurate or corrupt data,
transcription errors, a copyright or other intellectual property
infringement, a defective or damaged disk or other medium, a
computer virus, or computer codes that damage or cannot be
read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except


for the “Right of Replacement or Refund” described in
paragraph 1.F.3, the Project Gutenberg Literary Archive
Foundation, the owner of the Project Gutenberg™ trademark,
and any other party distributing a Project Gutenberg™ electronic
work under this agreement, disclaim all liability to you for
damages, costs and expenses, including legal fees. YOU AGREE
THAT YOU HAVE NO REMEDIES FOR NEGLIGENCE, STRICT
LIABILITY, BREACH OF WARRANTY OR BREACH OF CONTRACT
EXCEPT THOSE PROVIDED IN PARAGRAPH 1.F.3. YOU AGREE
THAT THE FOUNDATION, THE TRADEMARK OWNER, AND ANY
DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE
TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL,
PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE
NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of
receiving it, you can receive a refund of the money (if any) you
paid for it by sending a written explanation to the person you
received the work from. If you received the work on a physical
medium, you must return the medium with your written
explanation. The person or entity that provided you with the
defective work may elect to provide a replacement copy in lieu
of a refund. If you received the work electronically, the person
or entity providing it to you may choose to give you a second
opportunity to receive the work electronically in lieu of a refund.
If the second copy is also defective, you may demand a refund
in writing without further opportunities to fix the problem.

1.F.4. Except for the limited right of replacement or refund set


forth in paragraph 1.F.3, this work is provided to you ‘AS-IS’,
WITH NO OTHER WARRANTIES OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of
damages. If any disclaimer or limitation set forth in this
agreement violates the law of the state applicable to this
agreement, the agreement shall be interpreted to make the
maximum disclaimer or limitation permitted by the applicable
state law. The invalidity or unenforceability of any provision of
this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the


Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and
distribution of Project Gutenberg™ electronic works, harmless
from all liability, costs and expenses, including legal fees, that
arise directly or indirectly from any of the following which you
do or cause to occur: (a) distribution of this or any Project
Gutenberg™ work, (b) alteration, modification, or additions or
deletions to any Project Gutenberg™ work, and (c) any Defect
you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new
computers. It exists because of the efforts of hundreds of
volunteers and donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project
Gutenberg™’s goals and ensuring that the Project Gutenberg™
collection will remain freely available for generations to come. In
2001, the Project Gutenberg Literary Archive Foundation was
created to provide a secure and permanent future for Project
Gutenberg™ and future generations. To learn more about the
Project Gutenberg Literary Archive Foundation and how your
efforts and donations can help, see Sections 3 and 4 and the
Foundation information page at www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-
profit 501(c)(3) educational corporation organized under the
laws of the state of Mississippi and granted tax exempt status
by the Internal Revenue Service. The Foundation’s EIN or
federal tax identification number is 64-6221541. Contributions
to the Project Gutenberg Literary Archive Foundation are tax
deductible to the full extent permitted by U.S. federal laws and
your state’s laws.

The Foundation’s business office is located at 809 North 1500


West, Salt Lake City, UT 84116, (801) 596-1887. Email contact
links and up to date contact information can be found at the
Foundation’s website and official page at
www.gutenberg.org/contact
Section 4. Information about Donations to
the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission
of increasing the number of public domain and licensed works
that can be freely distributed in machine-readable form
accessible by the widest array of equipment including outdated
equipment. Many small donations ($1 to $5,000) are particularly
important to maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws


regulating charities and charitable donations in all 50 states of
the United States. Compliance requirements are not uniform
and it takes a considerable effort, much paperwork and many
fees to meet and keep up with these requirements. We do not
solicit donations in locations where we have not received written
confirmation of compliance. To SEND DONATIONS or determine
the status of compliance for any particular state visit
www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states


where we have not met the solicitation requirements, we know
of no prohibition against accepting unsolicited donations from
donors in such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot


make any statements concerning tax treatment of donations
received from outside the United States. U.S. laws alone swamp
our small staff.

Please check the Project Gutenberg web pages for current


donation methods and addresses. Donations are accepted in a
number of other ways including checks, online payments and
credit card donations. To donate, please visit:
www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could
be freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose
network of volunteer support.

Project Gutenberg™ eBooks are often created from several


printed editions, all of which are confirmed as not protected by
copyright in the U.S. unless a copyright notice is included. Thus,
we do not necessarily keep eBooks in compliance with any
particular paper edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg
Literary Archive Foundation, how to help produce our new
eBooks, and how to subscribe to our email newsletter to hear
about new eBooks.
Welcome to Our Bookstore - The Ultimate Destination for Book Lovers
Are you passionate about books and eager to explore new worlds of
knowledge? At our website, we offer a vast collection of books that
cater to every interest and age group. From classic literature to
specialized publications, self-help books, and children’s stories, we
have it all! Each book is a gateway to new adventures, helping you
expand your knowledge and nourish your soul
Experience Convenient and Enjoyable Book Shopping Our website is more
than just an online bookstore—it’s a bridge connecting readers to the
timeless values of culture and wisdom. With a sleek and user-friendly
interface and a smart search system, you can find your favorite books
quickly and easily. Enjoy special promotions, fast home delivery, and
a seamless shopping experience that saves you time and enhances your
love for reading.
Let us accompany you on the journey of exploring knowledge and
personal growth!

ebookgate.com

You might also like