An open API service indexing awesome lists of open source software.

https://github.com/animator/learn-python

📖🐍 Free & Open Source book to master Python 3. Also available: PDF & Web Interface.
https://github.com/animator/learn-python

gssoc learn-python python python3

Last synced: about 1 year ago
JSON representation

📖🐍 Free & Open Source book to master Python 3. Also available: PDF & Web Interface.

Awesome Lists containing this project

README

          

[![Discord Server Invite](https://img.shields.io/badge/DISCORD-JOIN%20SERVER-5663F7?style=for-the-badge&logo=discord&logoColor=white)](https://bit.ly/heyfoss)

Contributors should go through the [Contributing Guide](https://github.com/animator/learn-python/blob/main/CONTRIBUTING.md) to learn how you can contribute to the project.

![Learn Python 3 Logo](images/learn-python.png)

Learn Python 3
=================
by **Ankit Mahato** [[About](https://animator.github.io)]
*Version 2022.10*

# How to read this book?

This book can be consumed in 3 ways:

- A nice web interface - [Link](https://animator.github.io/learn-python/)
![Learn Python 3 Website](images/web.png)
- A Downloadable PDF - [Link](https://github.com/animator/learn-python/raw/main/pdf/learn-python-v2022.10.pdf)
![Learn Python 3 PDF](images/pdf.png)
- Directly on GitHub - [Link](https://github.com/animator/learn-python)

Table of Contents
=================

* [Introduction](#introduction)
* [Introduction to Programming Languages](#introduction-to-programming-languages)
* [Introduction to Python](#introduction-to-python)
* [Advantages of Python](#advantages-of-python)
* [Installing Python in Windows](#installing-python-in-windows)
* [Installing Python in macOS (Apple)](#installing-python-in-macos-apple)
* [Execution Modes](#execution-modes)
* [Interactive Mode of Execution](#interactive-mode-of-execution)
* [Script Mode of Execution](#script-mode-of-execution)
* [Python Fundamentals](#python-fundamentals)
* [Tokens: Introduction](#tokens-introduction)
* [Tokens: Keywords](#tokens-keywords)
* [Tokens: Identifiers](#tokens-identifiers)
* [Tokens: Literals](#tokens-literals)
* [Tokens: Operators](#tokens-operators)
* [Tokens: Delimiters](#tokens-delimiters)
* [Character Set](#character-set)
* [Blocks and Indentation](#blocks-and-indentation)
* [Comments](#comments)
* [Variables, Objects & Data Types](#variables-objects--data-types)
* [What are Objects & Variables?](#what-are-objects--variables)
* [Variables & Assignment Statements](#variables--assignment-statements)
* [Built-in Data Types](#built-in-data-types)
* [Type Checking](#type-checking)
* [Type Casting](#type-casting)
* [Implicit Type Casting](#implicit-type-casting)
* [Explicit Type Casting](#explicit-type-casting)
* [Mutable & Immutable Data Types](#mutable--immutable-data-types)
* [Immutable Data Types](#immutable-data-types)
* [Mutable Data Types](#mutable-data-types)
* [Input / Output](#input--output)
* [How to Accept User Input](#how-to-accept-user-input)
* [Displaying Output](#displaying-output)
* [Operators & Expressions](#operators--expressions)
* [Introduction to Operators](#introduction-to-operators)
* [Arithmetic Operators](#arithmetic-operators)
* [Relational Operators](#relational-operators)
* [Assignment Operators](#assignment-operators)
* [Logical Operators](#logical-operators)
* [Identity Operators](#identity-operators)
* [Membership Operators](#membership-operators)
* [Expressions](#expressions)
* [Operator Precedence with Examples](#operator-precedence-with-examples)
* [Errors & Exception Handling](#errors--exception-handling)
* [Error Types](#error-types)
* [Syntax Error](#syntax-error)
* [Runtime Error](#runtime-error)
* [Logical Error](#logical-error)
* [Exceptions](#exceptions)
* [Exception Handling](#exception-handling)
* [Control Flow](#control-flow)
* [Introduction to Control Flow](#introduction-to-control-flow)
* [Sequential Flow](#sequential-flow)
* [Selection Statements: if .. else](#selection-statements-if--else)
* [Iteration: for](#iteration-for)
* [Iteration: while](#iteration-while)
* [Jump Statements](#jump-statements)
* [pass](#pass)
* [break](#break)
* [continue](#continue)
* [Nested Loops](#nested-loops)
* [Strings](#strings)
* [Strings: Introduction & Creation](#strings-introduction--creation)
* [Accessing Characters of a String](#accessing-characters-of-a-string)
* [String Operations](#string-operations)
* [Introduction to String Methods](#introduction-to-string-methods)
* [Convert Case of Strings](#convert-case-of-strings)
* [Check Characters of a String](#check-characters-of-a-string)
* [Split a String](#split-a-string)
* [Strip Characters from a String](#strip-characters-from-a-string)
* [Check Prefix or Suffix in a String](#check-prefix-or-suffix-in-a-string)
* [Find & Replace Characters in a String](#find--replace-characters-in-a-string)
* [Traversing a String](#traversing-a-string)
* [List](#list)
* [What is a Python List? How to Create a List?](#what-is-a-python-list-how-to-create-a-list)
* [Accessing Items of a List](#accessing-items-of-a-list)
* [Negative Indexing](#negative-indexing)
* [Modifying a List](#modifying-a-list)
* [Removing Item from a List](#removing-item-from-a-list)
* [List Operations](#list-operations)
* [Traversing a List](#traversing-a-list)
* [Built-in Functions that can be used for a List](#built-in-functions-that-can-be-used-for-a-list)
* [Introduction to List Methods](#introduction-to-list-methods)
* [Adding Items to a List](#adding-items-to-a-list)
* [Removing Items from a List](#removing-items-from-a-list)
* [Counting or Locating Items in a List](#counting-or-locating-items-in-a-list)
* [Reversing Items](#reversing-items)
* [Sorting a List](#sorting-a-list)
* [Copying a List](#copying-a-list)
* [Nested List](#nested-list)
* [List Comprehension](#list-comprehension)
* [Member-wise Operation](#member-wise-operation)
* [Filtering or Subsequence](#filtering-or-subsequence)
* [Sample Programs](#sample-programs)
* [Tuple](#tuple)
* [List vs Tuple](#list-vs-tuple)
* [How to Create Tuple?](#how-to-create-tuple)
* [What is a Singleton?](#what-is-a-singleton)
* [Accessing Items of a Tuple](#accessing-items-of-a-tuple)
* [Tuples are Immutable](#tuples-are-immutable)
* [Tuple Operations](#tuple-operations)
* [Traversing a Tuple](#traversing-a-tuple)
* [Built-in Functions that can be used for a Tuple](#built-in-functions-that-can-be-used-for-a-tuple)
* [Locating Items in a Tuple](#locating-items-in-a-tuple)
* [Counting the Occurrence of an Item](#counting-the-occurrence-of-an-item)
* [New Tuple with Reversed Items](#new-tuple-with-reversed-items)
* [New Tuple with Sorted Items](#new-tuple-with-sorted-items)
* [Nested Tuple](#nested-tuple)
* [Understanding the Immutable Nature of Tuples](#understanding-the-immutable-nature-of-tuples)
* [Dictionary](#dictionary)
* [What is a Dictionary?](#what-is-a-dictionary)
* [How to Create a Dictionary](#how-to-create-a-dictionary)
* [Key: Value Pairs](#key-value-pairs)
* [Sequence of (key, value) Tuples](#sequence-of-key-value-tuples)
* [Keyword/Named Arguments](#keywordnamed-arguments)
* [Key and Value Lists](#key-and-value-lists)
* [Keys with Default Value](#keys-with-default-value)
* [Accessing Items (Key:Value) of a Dictionary](#accessing-items-keyvalue-of-a-dictionary)
* [Updating a Dictionary](#updating-a-dictionary)
* [Removing an Item (Key:Value) from a Dictionary](#removing-an-item-keyvalue-from-a-dictionary)
* [Dictionary Operations](#dictionary-operations)
* [Traversing a Dictionary](#traversing-a-dictionary)
* [Built-in Functions for a Dictionary](#built-in-functions-for-a-dictionary)
* [Creating a Copy of a Dictionary](#creating-a-copy-of-a-dictionary)
* [Nested Dictionary](#nested-dictionary)
* [Sample Programs](#sample-programs-1)
* [Python Standard Library](#python-standard-library)
* [Built-in Functions](#built-in-functions)
* [Mathematical Functions](#mathematical-functions)
* [Type Functions](#type-functions)
* [Type Checking](#type-checking-1)
* [Built-in Type Functions](#built-in-type-functions)
* [I/O Functions](#io-functions)
* [Base/Unicode Conversion Functions](#baseunicode-conversion-functions)
* [What are Built-in Modules?](#what-are-built-in-modules)
* [math Module](#math-module)
* [Constants](#constants)
* [Functions](#functions)
* [random Module](#random-module)
* [statistics Module](#statistics-module)
* [File Handling](#file-handling)
* [File Handling in Python - Introduction & Overview](#file-handling-in-python---introduction--overview)
* [Text Files vs Binary Files - Use Case, File Formats, Examples](#text-files-vs-binary-files---use-case-file-formats-examples)
* [File Opening & Closing](#file-opening--closing)
* [File Reading](#file-reading)
* [Sequential Reading](#sequential-reading)
* [Ad-hoc Reading](#ad-hoc-reading)
* [Writing a File](#writing-a-file)
* [Reading & Writing Binary Files using pickle Module](#reading--writing-binary-files-using-pickle-module)
* [Dumping Data](#dumping-data)
* [Loading Data](#loading-data)
* [Example: Traversing a Binary File](#example-traversing-a-binary-file)
* [Reading & Writing a CSV File using csv Module](#reading--writing-a-csv-file-using-csv-module)
* [Reading CSV File](#reading-csv-file)
* [Writing CSV File](#writing-csv-file)
* [User Defined Functions, Modules & Packages](#user-defined-functions-modules--packages)
* [User Defined Functions](#user-defined-functions)
* [Function Structure](#function-structure)
* [Function Header](#function-header)
* [Function Body](#function-body)
* [Parameters and Arguments](#parameters-and-arguments)
* [Scope of Variables](#scope-of-variables)
* [Local Variables](#local-variables)
* [Global Variables](#global-variables)
* [Passing Objects of Mutable Type to a Function](#passing-objects-of-mutable-type-to-a-function)
* [Passing a List](#passing-a-list)
* [Passing a Dictionary](#passing-a-dictionary)
* [What is a Module? How to Create a Module?](#what-is-a-module-how-to-create-a-module)
* [Executable Scripts / Modules](#executable-scripts--modules)
* [What is a Package? Introduction to PyPi. How to Create a Python Package?](#what-is-a-package-introduction-to-pypi-how-to-create-a-python-package)

# Introduction

## Introduction to Programming Languages

In today's digital era, we depend on computers, smartphones and the internet to perform a plethora of tasks, like:

- A mathematical task, such as finding the square root of a number or solving a set of simultaneous equations.
- A text-based task such as reading a document and performing search/replace.
- Streaming and playing multimedia files containing audio and video.
- Using a search engine to find and visit a website.
- Playing an online multiplayer game with friends.
- and many more...

Softwares play an important role as they translate human activity into corresponding machine instructions which are executed to accomplish these tasks.

A **software** is a collection of programs where each program provides a sequence of instructions specifying how the computer should act.

These instructions have to be provided in **machine language** or **low level language** (0s and 1s) that is difficult to read or write for a human being.

This led to the invention of **high-level programming languages** in which programs can be easily written and managed. The human-readable programs written using high-level languages are converted into computer-readable machine code or byte-code using **compilers** or **interpreters**.

There are many high-level programming languages that are currently in wide use.

Some of the popular languages are Java, C, C++, C#, Go, Swift, JavaScript, PHP, Dart, Kotlin and Python.

## Introduction to Python

Guido van Rossum started the development of Python in December 1989. He released the first version (0.9.0) of Python for general public on February 20, 1991.

The language evolved over the next few decades and so did its definition, the current version of which is stated below:

> Python is a high-level, interpreted, object-oriented programming language with dynamic semantics.

Let us break down and analyze the above definition to gain a better understanding of Python:

**High-level**

Python is a **high-level programming language** which can be used to write a program in natural language (english) making it readable, writable, shareable and manageable.

While developing a Python program one is not required to handle the various components of computer architecture like registers, memory addresses and call stacks which have to be handled if an assembly language or a low-level language is used for development.

Python includes high-level language features like variables, data structures (lists, dictionaries, etc.), objects, expressions, modules, classes, functions, loops, threads, file handling, string handling, error handling and other computer science abstraction concepts.

**Interpreted**

In traditional programming languages like C or C++, codes are compiled into computer-readable machine code before it can be executed.

Python is an **interpreted language** where the Python interpreter reads and executes the program line by line.

The process is more time consuming compared to compiled code execution, but allows faster development as one does not have to go through the entire compilation step during testing and debugging. Also, the code can run on any platform as long as it has a valid Python installation (which includes interpreter) as there is no generation of platform dependent binaries.

**Object-oriented**

Python does not enforce **Object-oriented programming (OOP)**, but completely supports it.

A programmer can define Classes specifying the data in the form of attributes (or properties) and some programming logic in the form of member functions (or methods). Once a class is defined, the user can create an instance of that class which is known as an object.

In Python, everything (`int`, `list`, `dict`, etc.) is an object. We will cover more about objects in detail in the later sections.

**Dynamic Semantics**

As Python is an interpreted language in which the code is executed line-by-line, a python statement or expression is evaluated during run-time. This allows **dynamic typing** (type of a variable can change over its lifetime) and creation of dynamic objects during run-time, which provides more flexibility, usability and fewer lines of code as compared to statically-typed compiled languages like C/C++.

## Advantages of Python

The key advantages of Python are as follows:

**1. Easy to Learn**

The Python programming language is easy to learn with low technical and conceptual overhead. This makes it an ideal language for beginners to learn programming.

**2. Clear Syntax & Fewer Lines of Code**

Python's simple and easy to learn syntax increases code readability and leads to fewer lines of code.

A typical task which requires an average of twenty lines of code in C and seven in Java can often be done with just one line in Python.

Also, due to fewer lines of code the chances of making any error is significantly reduced.

**3. Open Source**

Python is an open source programming language, so anyone can view and contribute to its source code.

**4. Portable & Platform Independent**

The Python programming language is portable & platform independent as it can work on any Linux, MacOS or Windows device.

**5. Standard Library & Python Packages**

Python has a rich and extensive Standard Library, a collection of predefined functions for various tasks.

Python programmers also have at their disposal the vast ecosystem of more than 250,000 community contributed libraries in the Python Package Index (PyPI), where one can find a solution to every conceivable task.

**6. Web Application Development**

Some of the most popular web development frameworks (django, flask, etc.) are written in Python. This coupled with the availability of packages to connect to any database makes Python a great choice for web application development.

## Installing Python in Windows

Let's start with the Python 3 installation process on Windows operating system.

**Step 1: Download Installer**

Download the latest Windows installer from the [Python Software Foundation website](https://www.python.org/downloads/).

![Step 1: Download Installer](images/0201a.png)

**Step 2: Install Python 3**

Once the download is complete double-click and run it.

![Step 2: Install Python 3](images/0201b.png)

Select the checkbox ✅ `Add Python 3.9 to PATH`. This will enable you to install python packages and run python script via command-line.

Hit 🛡️ `Install Now` and complete the setup.

**Step 3: Verify Installation**

Once the setup is complete, click on the `Start` menu and open `Python 3.9 -> IDLE (Python 3.9 64 bit)` to launch the Python interpreter.

![Step 3: Launch Python Interpreter](images/0201c.png)

Python 3.9 is now successfully installed on your computer.

![Step 3: Verify Installation](images/0201d.png)

## Installing Python in macOS (Apple)

Let's start with the Python 3 installation process on macOS operating system.

**Step 1: Download Installer**

Download the latest macOS installer from the [Python Software Foundation website](https://www.python.org/downloads/).

![Step 1: Download Installer](images/0202a.png)

**Step 2: Install Python 3**

Once the download is complete double-click and run it.

![Step 2: Install Python 3](images/0202b.png)

Hit `Continue` and complete the setup.

**Step 3: Verify Installation**

Once the setup is complete, click on the `Launchpad -> IDLE` to launch the Python interpreter.

![Step 3: Verify Installation](images/0202c.png)

Python 3.9 is now successfully installed on your computer.

## Execution Modes

After installing the latest version of the Python interpreter, we can now write and execute some basic Python codes.

There are two ways to execute a Python program:

1. **Interactive Mode**: When the IDLE application is launched, the Python interpreter or the Python shell pops up on the screen. User can interact with the Python interpreter and execute statements (single line or multiline code snippets) directly in this Python shell.
2. **Script Mode**: This is the most commonly used method for executing a Python program. The entire Python program is written and saved in a file (`.py` extension) which can be executed using the IDLE application.

## Interactive Mode of Execution

Let us execute some basic Python statements and interact with the Python shell.

**Launching the Python Shell**

To launch the IDLE application click `[Windows Start Menu Button] -> [Python 3.9 Folder] -> [IDLE (Python 3.9 64 bit)]`.

![Launch IDLE](images/0201c.png)

The Python interpreter or the Python shell will pop-up on the screen.

![Python Shell](images/0201d.png)

The version (`3.9`) of the Python interpreter is displayed at the top of the window followed by the `>>>` symbol which indicates that the interpreter is ready to take instructions.

Python commands or statements can be input on this prompt. The input statements are executed instantaneously and any variable assignments are retained as long as the session is not terminated.

**Basic Arithmetic**

Let us perform some basic arithmetic operations in the interactive mode using an integer number (`2`) and a floating-point number (`3.5`):

``` python
>>> 2 + 2
4
>>> 2 * 3.5
7.0
```

It can be observed that the results of each of the above computations are displayed immediately in the shell.

**Storing Values/Results**

Instead of immediately displaying the results, they can also be stored in variables using the assignment symbol (`=`) as shown below:

``` python
>>> a = 2 + 2
>>> b = 2 * 3.5
```

The values of `a` and `b` can be accessed later for future calculations as shown below:

``` python
>>> a
4
>>> b
7.0
>>> a * 5
20
>>> b / 3
2.3333333333333335
```

**Basic String Operation**

Interactive mode is not just restricted to basic arithmetic or assignments. Let us join two strings - `"Hello, "` and `"world!"`.

``` python
>>> "Hello, " + "world!"
'Hello, world!'
```

The complete functionality of Python is easily accessible to a user via the **Interactive Mode**.

This makes it convenient for testing and instant execution of small code snippets (single line or few lines of code), a feature not available in compiled languages like C, C++ and Java.

But, the statements cannot be saved for future use and have to retyped for re-execution. This disadvantage is overcome by the use of Python in **Script Mode** as described in the next section.

## Script Mode of Execution

To write reusable codes, script mode is the most preferred mode of code execution.

**File Creation**

To create a new file using the IDLE application click `[File] -> [New File]`

![Create New File](images/0303a.png)

Write a simple Python program as shown below

``` python
a = 2 + 2
a
```

and save the script as `example.py` (`.py` file extension for all Python scripts) using `[File] -> [Save As...]`

![Save File](images/0303b.png)

**Script Execution**

Now run this script using `[Run] -> [Run Module]`.

![Execute File](images/0303c.png)

It can be observed that the code has been executed, but no output is displayed on the console (or the standard output) as all outputs have to be explicitly specified when running a code in the script mode.

This can be done by using the `print()` function which is used in Python scripts to display output on the output stream. Let us quickly add the `print()` function in the above code and execute it.

``` python
a = 2 + 2
print(a)
```

Now, when you run the script you will observe that the value of `a`, that is `4`, is now displayed on the console.

# Python Fundamentals

## Tokens: Introduction

When a Python code is executed, the Python interpreter reads each logical line and breaks it into a sequence of lexical units.

These lexical units are better known as **tokens** - the smallest individual units of a program. They are the building blocks of a Python code and can be classified into one of the following categories:

- **Keywords** : Reserved words that convey special meaning when processed by the Python interpreter.
- **Identifiers** : Names defined by the programmer to refer to objects that can represent variables, functions, classes, etc.
- **Literals** : Values specified in the program which belong to exactly one of the Python's built-in data types.
- **Delimiters** : Symbols that denote grouping, punctuation, and assignment/binding.
- **Operators** : Symbols that can operate on data and compute results.

## Tokens: Keywords

Keywords are reserved words that have special meaning when processed by the Python interpreter. They are case-sensitive and cannot be used for naming identifiers (class, function, variable or structure names).

The list of keywords in Python are provided below:

| | | |
|---|---|---|
| `True` | `False` | `import` |
| `from` | `as` | `None` |
| `and` | `or` | `not` |
| `in` | `is` | `try` |
| `except` | `finally` | `raise` |
| `del` | `global` | `nonlocal` |
| `lambda` | `def` | `class` |
| `with` | `if` | `elif` |
| `else` | `pass` | `for` |
| `while` | `continue` | `break` |
| `assert` | `return` | `yield` |
| `async` | `await` | |

## Tokens: Identifiers

Identifiers are used for defining the names of Python objects such as variables, functions, classes, modules, etc. The naming convention for identifiers is as follows:

- Must begin with a lowercase character (`a-z`) or an uppercase character (`A-Z`) or underscore sign (`_`).
- Followed by any number of letters (`a-z`, `A-Z`), digits (`0-9`), or underscores (`_`).
- Should not be a keyword.
- No special symbols are allowed like `!`, `@`, `#`, `$`, `%`, etc.

Some points to keep in mind while naming identifiers:

- Identifiers are case-sensitive in nature and any difference in case of any character refers to a different identifier. e.g., `length` and `Length` are different identifiers.
- Identifiers differing by only underscores are different. e.g., `unitlength` and `unit_length` are different identifiers.

It is also a good practice (although not compulsory) to follow the following procedure while naming identifiers:

- Identifiers should be named carefully with an emphasis on clarity and readability. For example, in a program that calculates the area of a rectangle, a good choice for identifier names are - `length`, `breadth` and `area`.
- Class names should start with uppercase character.
- Identifiers starting with an underscore have special meaning in a program.
- Variable, function and method names should be in lowercase characters, with underscores separating multiple words like `area_of_square`, `area_of_triangle`, etc.

## Tokens: Literals

Literals are tokens in the source code which represent fixed or constant values. They are often used in assignment statements for initializing variables or in comparison expressions.

The various types of literals available in Python are as follows:

### Numeric Literals

Numeric literals are used for representing numeric values in the source code. They can be of three types - integers, float point numbers and imaginary numbers.

#### Integer Literals

Integer literals are numbers without any fractional component.

In Python, integer literals can be written in four positional (base) numeral systems:

**i. Decimal or base-10 Integer**

A decimal integer literal consists of one or more digits (`0-9`) and cannot have any zeros preceding the first non-zero digit, except when the number is `0`.

Example base-10 integers:

``` python
34
3283298
864
0
```

`092` is not a valid decimal integer literal as a zero precedes the first non-zero digit `9`.

**ii. Binary or base-2 Integer**

A binary integer or base-2 integer begins with `0b` or `0B` followed by binary digits `0-1`.

For example, `27` can be written as a binary integer literal `0b11011`.

**iii. Octal or base-8 Integer**

An octal integer or base-8 integer begins with `0o` or `0O` followed by octal digits `0-7`.

For example, `27` can be written as an octal integer literal `0o33`.

**iv. Hexadecimal or base-16 Integer**

A hexadecimal integer or base-16 integer begins with `0x` or `0X` followed by digits `0-9` or letters `A-F` (case insensitive).

For example, `27` can be written as a hexadecimal integer literal `0x1B` or `0x1b`.

Thus, it can be observed that number `27` can be written in the program as `27` (decimal), `0b11011` (binary), `0o33` (octal) or `0x1B` (hexadecimal).

**Underscores in Integer Literals**

An optional character `_` (underscore) is also allowed in an integer literal to group digits for enhanced readability.

One underscore can occur between digits, and after base specifiers like `0o`.

They are ignored while determining the actual numerical value of the literal.

Some valid underscore usages are - `10_00_00_000`, `0b_1110_0101`, `0x23_123`.

#### Floating Point Literals

Floating point literals are real numbers present in the source code. They contain fractional component and/or exponential component.

The fractional component includes the digits after the decimal point (`.`).

Example floating point literals:

```
3.4
.4
8.
3.4E2
3.4e-2
```

In the above example, `.4` is equivalent to `0.4` and `8.` is equivalent to `8.0`.

The exponential component can be identified by the letter `e` or `E` followed by an optional sign (`+` or `-`) and digits (`0-9`). This exponent is equivalent to multiplying the real number with the power of `10`.

For example, `3.4E2` is equivalent to `3.4 x 10^2` or `340.0`, whereas `3.4e-2` is equivalent to `3.4 x 10^-2` or `.034`.

#### Imaginary Literals

To specify complex numbers and perform complex number mathematics, Python supports imaginary literals which are given by real or integer number followed by the letter `j` or `J` which represents the unit imaginary number.

Example imaginary literals:

```
3.5j
15.j
12j
.005j
3e100j
3.5e-10j
```

**Points to Note**

In Python,

- there is no specialized literal such as a complex literal. A complex number is actually represented in the program using an expression comprising a real number (integer/float numeric literal) and an imaginary number (imaginary literal). For example, `1 + 2j` consists of an integer literal (`1`) and a imaginary literal (`2j`).
- numeric literals do not include the minus sign (`-`). `-` is actually a unary operator it combines with a numeric literal to represent negative numbers. For example, in `-3.14` the numeric literal is `3.14` and `-` is an operator.

### Boolean Literals

The reserved words `True` and `False` are also boolean literals which can be used to specify the truth value in a program.

### String Literals

String literals are texts which can be specified in a variety of ways in Python:

- Single quotes: `'python'`
- Double quotes: `"python"`
- Triple quoted: `'''Triple python'''`, `"""Three python"""`.

Triple quoted strings can also span multiple lines.

Example:

``` python
s = "I am a String"

s1 = """A
multiline
String"""

s2 = '''Also a
multiline
String'''
```

The backslash (`\`) character can be used in a string literal to escape characters that otherwise have a special meaning, such as newline, linefeed, or the quote character.

| Escape Sequence | Meaning |
|--|--|
| `\\` | Backslash (`\`) |
| `\'` | Single quote (`'`) |
| `\"` | Double quote (`"`) |
| `\a` | ASCII Bell (BEL) |
| `\b` | ASCII Backspace (BS) |
| `\f` | ASCII Form-feed (FF) |
| `\n` | ASCII Linefeed (LF) |
| `\r` | ASCII Carriage Return (CR) |
| `\t` | ASCII Horizontal Tab (TAB) |
| `\v` | ASCII Vertical Tab (VT) |

Although `\'` and `\"` can be used to specify quote characters, Python allows embedding double quotes inside a single-quoted string (`'My name is "Python".'`) and single quotes inside a double-quoted string (`"Python's World"`).

String literals also support unicode characters which can be specified using `\u` escape sequence followed by the 4 letter unicode.

``` python
>>> print("E = mc\u00B2")
E = mc²
```

In the above example, `\u00B2` is the unicode character which represents the 'SUPERSCRIPT TWO'.

### Special Literal

`None` is a special literal which is used to denote the absence of value.

It should not be confused with `0` as `0` is an integer literal with a defined finite value, whereas `None` implies nothingness.

``` python
>>> a = None
>>> a
>>>
```

In the above example, the Python shell does not display any value of `a` as it is assigned as `None` which has no value.

### Collection of Literals

Python has the provision for specifying a collection of literals in the source code using a special syntax known as **"displays"**.

One can create specialized containers like list, set and dictionary using this syntax.

Some example collection of literals (displays) are provided below:

- **List**: `a = ['a', 'b', 'c']`
- **Set**: `a = {'a', 'b', 'c'}`
- **Dictionary**: `a = {'a':1, 'b':2, 'c':3}`

List, set and dictionary will be covered in detail in later chapters.

## Tokens: Operators

Operators are tokens which can be combined with values and variables to create expressions which evaluate to a single value. Python supports a rich set of operators:

``` python
+ - * **
/ // % @
<< >>
& | ^ ~
:= < >
<= >= == !=
```

Each of the above operators are covered in detail in the chapter - Operators.

## Tokens: Delimiters

Delimiters are tokens which are useful for organizing a program and are used in statements, expressions, functions, literal collections, and various other code structures.

They can be classified based on utility as follows:

### Grouping

`()`, `[]` and `{}` are delimiters used for:

- grouping expressions which can be spread across multiple physical lines.
- creating collection of literals like list display, dictionary display, set display.
- creating parenthesized sub-expression having the highest operator precedence (evaluated first) in a complex expression.

**Example**

``` python
days = ['Sunday', 'Monday',
'Tuesday', 'Wednesday',
'Thursday', 'Friday',
'Saturday']

sum_6 = (1 + 2 +
3 + 4 +
5 + 6)
```

is equivalent to

``` python
days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

sum_6 = (1 + 2 + 3 + 4 + 5 + 6)
```

### Punctuation, Decoration and Annotation

Tokens in Python which are used for punctuation, decoration and annotation are:

```
. , :
; @ ->
```

### Assignment/Binding

The assignment or binding delimiters are used for binding objects to names via assignment statements. The complete list of tokens are provided below:

```
= += -= *=
/= //= %= **=
@= &= |= ^=
<<= >>=
```

Except `=`, the rest of the tokens have an operator followed by `=` character.

These delimiters are also known as augmented assignment operators as they perform an operation in combination with assignment.

## Character Set

A set of valid characters that a programming language recognizes is known as its **character set**.

Python is a new age programming language which supports Unicode encoding standard. The default encoding for Python source code is UTF-8 (Unicode Transformation Format – 8-bit) which enables developers to use Unicode characters not only as literals, but also as identifiers.

This makes Python one of the very few programming languages that support multiple languages as shown in the example below:

**Code**

``` python
message = "हिन्दी में print करे"
print(message)

क = 1 # Devanagari Letter KA
ক = 2 # Bengali Letter KA
க = 3 # Tamil Letter KA
ક = 4 # Gujarati Letter KA
print(क + ক + க + ક)
```

**Output**

```
हिन्दी में print करे
10
```

## Blocks and Indentation

In traditional programming languages like C++ or Java, programs are organised in form of code blocks.

Each code block contains one or more statements which are enclosed between braces - `{` and `}` and are executed sequentially.

A sample C++/Java code is provided below which checks for an input `x`.

**C++**

``` c++
if (x < 10) {
cout << "x is less than 10" << endl;
if (x <= 5) {
cout << "x is less than or equal to 5" << endl;
}
else {
cout << "x is more than 5 but less than 10" << endl;
}
}
else {
cout << "x is not less than 10" << endl;
}
```

**Java**

``` java
if (x < 10) {
System.out.println("x is less than 10");
if (x <= 5) {
System.out.println("x is less than or equal to 5");
}
else {
System.out.println("x is more than 5 but less than 10");
}
}
else {
System.out.print("x is not less than 10");
}
```

It can be seen how indentations (`tab` at the beginning of line) are added (not required by programming language) to the code to increase readability, which helps in guiding readers through the code.

**Python**

Code blocks in Python are inspired by this idea as it makes it easier to understand a Python code.

A block of code is denoted by line indentation, typically **4 spaces** (preferred) or a **tab**. This indentation is used to determine the logical group of statements, with all statements within a group having the same level of indentation.

The corresponding Python code for the above C++/java examples is provided below.

Notice how the code blocks are indented according to the logic.

``` python
if x < 10:
print("x is less than 10")
if x <= 5:
print("x is less than or equal to 5")
else:
print("x is more than 5 but less than 10")
else:
print("x is not less than 10")
```

## Comments

Python supports single-line comments and multi-line comments to enhance code readability via adding documentation.

### Single Line Comments

A single line comment begins with `#`. Everything between `#` and the end of line is ignored by the Python interpreter.

``` python
# A single line comment.
a = 1 # assign a
```

### Multiline comments

A multiline comment begins with `'''` or `"""` and ends with the same.

``` python
"""
I am
a multiline
comment.
"""

'''
I am
also a multiline
comment.
'''
```

As compared to single line comments, multiline comments should begin at the same indentation level corresponding to their code block.

For example,

``` python
"""
Begin program
here
"""
if x < 10:
"""
Enter code block when x
is less than 10
"""
print("x is less than 10")
if x <= 5:
"""
Enter code block when x
is less than or equal to 5
"""
print("x is less than or equal to 5")
else:
"""
Enter code block when x
is more than 5 but less than 10
"""
print("x is more than 5 but less than 10")
else:
"""
Enter code block when x
is not less than 10
"""
print("x is not less than 10")
```

# Variables, Objects & Data Types

## What are Objects & Variables?

A program is a sequence of instructions which often acts on information (data) provided by the user.

The process of creating, storing and manipulating this data helps in the computation of new data or the end result.

**Variables are the fundamental building blocks of a program** which provide a way to store, access and modify values during the life-cycle of a program.

Each variable has:

- a name (handle),
- a type or data-type (kind of data), and
- a value (actual data).

In traditional programming languages like Java or C++, the type of the variable is pre-defined.

For example, if you want to use the value `1` inside the program, you can store it in a variable named `a` of type `int`.

```
int a = 1;
```

This `a` is synonymous to a box of fixed dimensions (fixed type) holding something (value `1`) inside it.

![Box 'a'](images/0501a.png)

In case we want to change the contents of the box, we can replace it with something similar (same type).

```
a = 2;
```

![Filled box 'a'](images/0501b.png)

The contents of this box can be replicated and placed in a similar (same type) box:

```
int b = a;
```

![Copy box 'a' contents](images/0501c.png)

Multiple boxes can exist, each containing an item having the same value.

```
int x = 3;
int y = 3;
int z = 3;
```

![Boxes 'x', 'y' & 'z'](images/0501d.png)

As shown above, the programming languages in which the variables (named boxes) are declared along with their types (size of the boxes) are known as **statically typed** languages.

The size of these boxes cannot change later in the program until the variable is re-initialized with the same name and different type.

**Python is a dynamically-typed language**, where every value or data item (of any type like numeric, string, etc.) is an object.

The variable names are just name-tags pointing to the actual object containing data of any type.

As there is no need of any variable declaration in Python before usage, there is no concept of default value (an empty box or `null`) which exists in other programming languages.

Whenever a new object is created in Python, it is assigned a unique identity (ID) which remains the same throughout the lifetime of that object. This ID is the address of the object in memory and the built-in function `id()` returns the value of this address.

``` python
>>> a = 1
>>> id(a)
140407745943856
>>> a = 2
>>> id(a)
140407745943888
```

In the above example, the ID of `a` changes as it points to a new object (`2`).

``` python
>>> b = a
>>> id(b)
140407745943888
```

Also, when `a` is assigned to `b`, instead of creating a new copy, `b` points to the same object as `a`.

## Variables & Assignment Statements

A variable is uniquely identified by a name (identifier) and follows the same naming convention:

- Must begin with a lowercase character (`a-z`) or an uppercase character (`A-Z`) or underscore sign (`_`).
- Followed by any number of letters (`a-z`, `A-Z`), digits (`0-9`), or underscores (`_`).
- Should not be a keyword.
- No special symbols are allowed like `!`, `@`, `#`, `$`, `%`, etc.

### Assignment

Variables can be bound to a reference of an object (of any type) using assignment statements.

You can create an object (data) and bind it's reference to a variable using equal sign (`=`):

``` python
count = 100 # integer
pi = 3.141 # real number
name = "Python" # string
```

Here, L-value refers to the assignable variables (`count`, `pi`, `name`) on the left side of the assignment and R-value refers to the expression on the right side of the assignment operator that has a value (`100`, `3.141`, `"Python"`).

As variables are just references, you can rebind them to another object of same or different type:

``` python
a = 100 # integer
a = 3.141 # real number
a = "Python" # string
```

### Deletion

The `del` statement can be used to unbind the reference to an object.

``` python
>>> a = 10
>>> del a
>>> a
Traceback (most recent call last):
File "", line 1, in
NameError: name 'a' is not defined
```

Accessing `a` results in a `NameError` as the reference (variable) to the object holding value `10` is deleted.

The object is also automatically cleaned up from the memory if there is no other variable referencing to it (garbage collection).

### Multiple Assignment

In Python, multiple assignment can be used to condense variables which are set to the same value:

``` python
>>> x = y = z = 'foo'
>>> x
'foo'
>>> y
'foo'
>>> z
'foo'
```

### Tuple Swapping

In Python, a temporary variable is not required to swap values of two variables. Values can be directly swapped (tuple swapping) as shown below:

``` python
>>> a = 'Hello'
>>> b = 'World'
>>> b, a = a, b
>>> a
'World'
>>> b
'Hello'
```

## Built-in Data Types

In Python, the `type` of a data (or value) is not linked to the variable, but to the actual object which contains it. This type is also known as the object's data type and is used for identifying the operations that can be performed on the data.

The following built-in data types are available in Python:

- **Numeric Types** - `int`, `float`, `complex`, `bool`
- **Sequence Types** - `list`, `tuple`, `str`
- **Set Type** - `set`
- **Mapping Type** - `dict`
- **Special Type** - `None`

Often sequence, set and mapping types are also collectively known as **iterables** as they are a collection of items on which a user can traverse (iterate).

### Numeric Types - `int`, `float`, `complex`, `bool`

Numeric data types are used for storing the following types of numbers:

**Integer Numbers**

Objects holding integer numbers like `-1, 0, 200` are of `int` data type.

**Real or Floating-point Numbers**

Objects holding real or floating point numbers like `-1.1, 3e2, 20.0` are of `float` data type.

**Complex Numbers**

Objects storing complex numbers like `2 + 1j, -3j, -1 + 2J` are of type `complex`.

Each complex number has two parts, the real part which is a numeric integer or floating point literal, and the imaginary part which is an imaginary literal.

**Boolean**

The boolean data type (`bool`) is a subtype of `int`. It stores the evaluated value of expressions represented as keywords - `True` (integer value `1`) and `False` (integer value `0`).

### Sequence Types - `str`, `list`, `tuple`

An ordered collection of items where each item can be accessed using an integer index is known as a sequence. The following three sequence data types are available in Python:

**String**

A string (`str` data type) is a sequence of zero or more unicode characters enclosed within a pair of single (`'`) or double (`"`) quotes.

Some example strings are - `"42", 'hello', "python"`.

**List**

A `list` is sequence of items of same or different data types which are enclosed within brackets - `[ ]`.

Some example lists are - `[1, 2, 3]`, `['abc', 23, 3.14]`, `['edpunk', 'python']`.

**Tuple**

A `tuple` is an immutable sequence of items of same or different data types which are enclosed within parentheses - `( )`.

Some example tuples are - `(1, 2, 3)`, `('abc', 23, 3.14)`, `('edpunk', 'python')`.

### Set Type - `set`

A `set` is an unordered collection of unique items of same of different data types which are enclosed in curly braces - `{ }`.

Some example sets are - `{1, 2, 3}`, `{'abc', 23, 3.14}`, `{'edpunk', 'python'}`.

### Mapping Type - `dict`

`dict` is a mapping data type which stores values in the form of key-value pairs.

It is used for representing data where you can quickly access the value (any data type) corresponding to a key (any data type except `list`, `set` or `dict`), just like a dictionary where you can lookup the meaning of a given word.

Keys and corresponding values are separated by colon (`:`).

The key-value pairs are separated by comma (`,`) and enclosed within curly braces - `{ }`.

Some example dictionaries are - `{1: "a", 2: "b", 3: "c"}`, `{"name": "edpunk", "language": "python"}`.

### Special Type - `None`

`None` is a special data type which is used to denote the absence of value in an object.

It is neither `0` nor `False` as these are defined finite values, whereas `None` implies nothingness.

## Type Checking

The built-in `type()` function can be used to fetch the data type of an object.

Examples:

``` python
>>> count = 100
>>> type(count)

>>> pi = 3.141
>>> type(pi)

>>> name = "Python"
>>> type(name)

```

This function can be used along with the `is` operator in an expression to test whether the object is of the given type.

``` python
>>> count = 100
>>> type(count) is int
True
```

The `in` operator can be used along with the `type()` function to test if the data type is one of the mentioned types.

``` python
# count is of type int or float
>>> type(count) in (int, float)
True
```

## Type Casting

The process of converting the data type of an object from one type to another is known as **Type Casting** or **Type Conversion**.

The two kinds of type casting supported in Python are:

### Implicit Type Casting

The Python interpreter automatically converts the data type without the need of user intervention when evaluating expressions to determine the final data type.

In the below example the final type of `c` is automatically determined as `float` by the Python interpreter.

``` python
>>> a = 1 # int
>>> b = 2.0 # float
>>> c = a + b
>>> c
3.0
>>> type(c)

```

### Explicit Type Casting

When the type conversion is explicitly specified by the user using the various built-in functions available in Python, it is known as explicit type casting.

The built-in functions which can be used for explicit type casting are as follows:

**1. `int()`**

Creates an `int` from a `bool`, `float` or `str` containing integer characters (with or without sign).

``` python
>>> int(True)
1
>>> int(2.3)
2
>>> int("2")
2
```

**2. `float()`**

Creates a `float` from a `bool`, `int` or `str` containing floating point literals (with or without sign).

``` python
>>> float(True)
1.0
>>> float(2)
2.0
>>> float("2.3")
2.3
```

`float()` also accepts the following string inputs -

- `"Infinity"`
- `"inf"`
- `"nan"` (not a number).

``` python
>>> float("Infinity") > 1
True
>>> float("nan")
nan
```

Floating point literals can also contain the following characters -

- `.`, which denotes the fractional part of a number.
- `e` or `E`, which denotes the exponent part of a number.

``` python
>>> float("3.14")
3.14
>>> float("10.")
10.0
>>> float("1e100")
1e+100
>>> float("3.14e-10")
3.14e-10
```

**3. `str()`**

Converts any object into a `str`.

``` python
>>> str(2)
'2'
>>> str([1, 2, 3, 4])
'[1, 2, 3, 4]'
```

**4. `tuple()`**

Creates a `tuple` from an iterable of type `str`, `list`, `set` or `range`.

``` python
>>> tuple('hello')
('h', 'e', 'l', 'l', 'o')
>>> tuple([1, 2, 3, 4])
(1, 2, 3, 4)
>>> tuple(range(6))
(0, 1, 2, 3, 4, 5)
```

**5. `list()`**

Creates a `list` from an iterable of type `str`, `tuple`, `set` or `range`.

``` python
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
>>> list({1, 2, 3, 4})
[1, 2, 3, 4]
>>> list(range(6))
[0, 1, 2, 3, 4, 5]
```

**6. `set()`**

Creates a `set` from an iterable of type `str`, `tuple`, `list` or `range`.

``` python
>>> set('hello')
{'o', 'e', 'l', 'h'}
>>> set([1, 2, 3, 4])
{1, 2, 3, 4}
>>> set(range(6))
{0, 1, 2, 3, 4, 5}
```

## Mutable & Immutable Data Types

### Immutable Data Types

A data type is said to be immutable when the value of an object of that type cannot be modified.

The following data types are immutable:

- `int`
- `float`
- `complex`
- `bool`
- `tuple`
- `str`
- `None`

You might be wondering if some of the above types are immutable then how are we able modify the values of variables?

In case of variable re-assignment, the original objects are not modified, but new objects (with new values) are created in a new memory location and are bound to the variables. The object containing the old value is destroyed if no other variable references it.

Let us take an example,

``` python
>>> a = 1
>>> id_a = id(a)
>>> a = 2
>>> id_a2 = id(a)
>>> id_a == id_a2
False
```

You can witness in the above example how the object containing the value `1` is different from the object containing the value `2`, and `a` points to the latest object.

Sequence data types like strings and tuples are also immutable, i.e., no modifications are permitted to any item once it is created and any attempt to do so raises an error.

``` python
>>> s = "Hello"
>>> s[1] = "P"
Traceback (most recent call last):
File "", line 1, in
TypeError: 'str' object does not support item assignment
>>> t = (1, 2, 3)
>>> t[1] = 0
Traceback (most recent call last):
File "", line 1, in
TypeError: 'tuple' object does not support item assignment
```

Although, similar to numeric types the variables can be re-assigned to new sequences.

``` python
>>> s = "Hello"
>>> id_s = id(s)
>>> s = "Help"
>>> id_s2 = id(s)
>>> id_s == id_s2
False

>>> t = (1, 2, 3)
>>> id_t = id(t)
>>> t = (0, 2, 3)
>>> id_t2 = id(t)
>>> id_t == id_t2
False
```

### Mutable Data Types

In Python, the following data types are mutable, i.e., any modification does not create a new object but modifies the existing object:

- `list`
- `set`
- `dict`

Let us take a list and modify its contents.

``` python
>>> l = [1, 2, 3]
>>> id_l = id(l)
>>> l[0] = 0
>>> l
[0, 2, 3]
>>> id_l2 = id(l)
>>> id_l == id_l2
True
```

Let us take an example of a dictionary and add a new `key:value` pair.

``` python
>>> d = {"a": "apple", "b": "boy"}
>>> id_d = id(d)
>>> d["c"] = "cat"
>>> d
{'a': 'apple', 'b': 'boy', 'c': 'cat'}
>>> id_d2 = id(d)
>>> id_d == id_d2
True
```

Let us take an example of a set and add new item.

``` python
>>> s = {"apple", "bat"}
>>> id_s = id(s)
>>> s.add("cat")
>>> s
{'cat', 'apple', 'bat'}
>>> id_s2 = id(s)
>>> id_s == id_s2
True
```

In the above examples, the `id` of the objects (`list`, `dict`, `set`) do not change, which implies that no new objects are created and the original objects are modified.

# Input / Output

## How to Accept User Input

`input()` function is used to accept new input data from the user.

When this function is encountered in the code, the python interpreter waits for the user to type a response which is read as a string and assigned to a variable.

``` python
>>> name = input()
edpunk
>>> name
'edpunk'
```

The function also has an optional string argument which is used as a prompt message for the user.

``` python
>>> name2 = input("Enter name: ")
Enter name: EdPunk
>>> name2
'EdPunk'
```

User input can be converted into integer or floating point numbers using the type conversion functions `int()` and `float()`.

``` python
>>> num = int(input("Enter n: "))
Enter n: 10
>>> type(num)

>>> num
10

>>> pi = float(input("Enter pi: "))
Enter pi: 3.14
>>> type(pi)

>>> pi
3.14
```

## Displaying Output

The built-in `print()` function is used to display an output (value of variables, expressions, etc.) on the standard output.

Let us go through a program which computes the area of a rectangle and displays it:

**Code**

``` python
length = 10
breadth = 5
area = length * breadth
print("Area:", area)
```

**Output**

```
Area: 50
```

`print()` function can also be used to output the value of multiple objects when they are provided as arguments to the function.

**Code**

``` python
a = 2 + 2
b = 2 * 3.5
print(a, b)
```

**Output**

```
4 7.0
```

In the above code, the values of `a` and `b` are separated by a blank space (the default value of `sep`).

This property can be modified by providing any user defined separator using the `sep` option.

Let us modify the code and provide `","` as the separator.

**Code**

``` python
a = 2 + 2
b = 2 * 3.5
print(a, b, sep=",")
```

**Output**

```
4,7.0
```

When expressions are provided as arguments to the `print()` function, output is the evaluated value of those expressions.

For example,

**Code**

``` python
print(2 + 2)
print(2 * 3.5)
print("Hello, " + "world!")
```

**Output**

```
4
7.0
Hello, world!
```

In the above code snippet, each `print()` function invocation creates a new line of output. This is because `end` parameter has the newline character (`'\n'`) as the default value in the `print()` function.

This can be modified by the user as shown below:

**Code**

``` python
print(2 + 2, end=",")
print(2 * 3.5, end=";")
print("Hello, " + "world!")
```

**Output**

```
4,7.0;Hello, world!
```

***Note***

All non-keyword arguments or expressions are converted to strings and written to the output stream by the `print()` function. They are separated by `sep` and followed by `end`. An empty `print()` invocation writes `end` parameter (an empty line as `end` defaults to the newline character `'\n'`).

# Operators & Expressions

## Introduction to Operators

Operators are symbols that perform a single simple task or operation on one or more values resulting in a single evaluated value.

The values on which these operators are applied are called **operands**.

### Unary Operators

Unary operators are applied on a single operand positioned on right side of the operator.

Following are some unary operators available in Python:

- `+` (plus): The unary `+` operator does not change the value of the operand.
- `-` (minus): The unary `-` operator changes the sign of the operand.
- `~` (bitwise `NOT`): The unary `~` results in the bit-wise inversion of the integer operand. The bit-wise inversion of `x` is defined as `-(x+1)`.

``` python
>>> x = 5
>>> -x
-5
>>> +x
5
>>> ~x
-6
```

### Binary Operators

Binary operators are applied on two operands.

For example, arithmetic operators (`+`, `–`, `*`, `/`) evaluate the result of mathematical computation of two values.

### Operators in Python

A rich set of operators are available in Python which can be categorized as follows:

- Arithmetic Operators - `+`, `–`, `*`, `/`, `%`, `**`, `//`
- Relational Operators - `==`, `!=`, `>`, `>=`, `<`, `<=`
- Assignment Operators - `+=`, `-=`, `*=`, `/=`, `%=`, `**=`, `//=`
- Logical Operators - `not`, `or`, `and`
- Identity Operators - `is`, `is not`
- Membership Operators - `in`, `not in`
- Bitwise and Shift Operators - `&`, `|`, `^`, `~`, `<<`, `>>`

## Arithmetic Operators

Arithmetic operations can be performed in Python using the following arithmetic operators:

**Addition**

The `+` operator adds the values of numeric operands.

``` python
>>> 2 + 3
5
>>> 2 + 3.0
5.0
```

In case the operands are of type `str`, `list` or `tuple`, the `+` operator concatenates these two sequences or strings.

``` python
>>> 'edpunk' + 'python'
'edpunkpython'
>>> ["ed", "punk"] + ["python", ]
['ed', 'punk', 'python']
```

**Subtraction**

The `-` operator subtracts the value of operand on right from the value of operand on left.

``` python
>>> 2 - 3
-1
```

**Multiplication**

The `*` operator multiplies the values of numeric operands.

``` python
>>> 2 * 3
6
```

In case the operands are of type `str`, `list` or `tuple`, the `*` operator returns a sequence or string self-concatenated the specified number of times.

``` python
>>> "python" * 3
'pythonpythonpython'
>>> ['ed', 'py'] * 3
['ed', 'py', 'ed', 'py', 'ed', 'py']
```

**Division**

The `/` operator divides the value of operand on left by the value of operand on right and returns the real number quotient.

``` python
>>> 6 / 2
3.0
>>> 5 / 2
2.5
```

**Floor Division**

The `//` operator divides the value of operand on left by the value of operand on right and returns the integer quotient.

``` python
>>> 5 // 2
2
```

**Modulus**

The `%` operator divides the value of operand on left by the value of operand on right and returns the remainder.

``` python
>>> 5 % 2
1
```

**Exponent**

The `**` operator raises the left operand to the power of the right operand.

``` python
>>> 5 ** 2
25
```

## Relational Operators

Relational operators are useful for comparing the values of the operands to determine their relationship. Following relational operators are available in Python:

**Equals to**

The `==` operator returns `True` if the value of operand on left is same as the value of operand on right.

``` python
>>> 2 == 2
True
>>> 2 == 3
False
```

In case of sequence operands like `str`, `list` or `tuple`, the result is `True` if both the sequences are exactly the same.

``` python
>>> "python" == "python"
True
>>> "pypi" == "python"
False
>>> [1, 2, 3] == [1, 2, 3]
True
```

As a sequence is an ordered collection of items, so the order in which the items are positioned is very important.

``` python
>>> [2, 1, 3] == [1, 2, 3]
False
```

**Not equal to**

The `!=` operator returns `True` if the value of operand on left is not equal to the value of operand on right.

``` python
>>> 2 != 2
False
>>> 2 != 3
True
>>> 'py' != 'oy'
True
>>> [2, 1, 3] != [1, 2, 3]
True
>>> [1, 2, 3] != [1, 2, 3]
False
```

**Greater than**

The `>` operator returns `True` if the value of operand on left is greater than the value of operand on right.

``` python
>>> 3 > 2
True
>>> 2 > 2
False
```

In case of strings operands, `>` operator perform comparison according to the Unicode code point (integer) of each character one-by-one.

The Unicode code point of a character can be obtained using the `ord()` function in Python.

The code point of first character of both operands are compared. In case they are equal, the code point of next character of both operands are compared and the process continues.

For example,

``` python
>>> "python" > "Python"
True
```

The code point of `"p"` (`112`) is greater than the code point of `"P"` (`80`). As `112` is greater than `80` the expression evaluates to `True`.

Let us take another example:

``` python
>>> "pYthon" > "python"
False
```

The code point of first character is same (`112`), so the next set of characters are compared. The code point of `"Y"` (`89`) is not greater than the code point of `"y"` (`121`) so the expression evaluates to `False`.

If two string operands `p` and `q` are of unequal lengths (`len(p) < len(q)`) and `p` is a substring of `q` such that `q = pt` where t is any string of length greater than `0`, then `q > p` returns `True`.

``` python
>>> "python" > "py"
True
```

In case of sequence operands like `list` or `tuple`, the items are compared one-by-one starting from index `0`.

``` python
>>> ["p","py","PY"] > ["p","Py","PY"]
True
>>> [1, 3] > [1, 2]
True
>>> [1, 3, 4] > [1, 2]
True
```

In the above examples, `"py"` is greater than `"Py"` and `3` is greater than `2` respectively.

If two sequences are of unequal lengths and the smaller sequence is the starting subsequence of the larger one, then the larger sequence is considered greater than the smaller one.

``` python
>>> [1, 2, 4] > [1, 2]
True
```

**Greater than or equal to**

The `>=` operator returns `True` if the value of operand on left is greater than or equal to the value of operand on right.

``` python
>>> 3 >= 3
True
>>> 2 >= 3
False
```

In case of sequence operands (`str`, `list`, `tuple`), the comparison operation performed is along the same lines as the `>` operator discussed above.

``` python
>>> "python" >= "Python"
True
>>> "python" >= "python"
True
>>> ["py", "py", "PY"] >= ["py", "Py", "PY"]
True
>>> [1, 2] >= [1, 2]
True
>>> [1, 2, 4] >= [1, 2]
True
```

**Less than**

The `<` operator returns `True` if the value of operand on left is less than the value of operand on right.

``` python
>>> 2 < 3
True
>>> 3 < 3
False
```

In case of sequence operands (`str`, `list`, `tuple`), the comparison operation performed is along the same lines as the `>` operator discussed above.

``` python
>>> "file" < "Pile"
False
# f(102) is > P(80)
>>> "py" < "python"
True
>>> ["Py", "PY"] < ["py", "PY"]
True
>>> ['a', 2] < ['a', 3]
True
>>> [1, 2] < [1, 2, 4]
True
```

**Less than or equal to**

The `<=` operator returns `True` if the value of operand on left is lesser than or equal to the value of operand on right.

``` python
>>> 2 <= 3
True
>>> 3 <= 3
True
```

In case of sequence operands (`str`, `list`, `tuple`), the comparison operation performed is along the same lines as the `>` operator discussed above.

``` python
>>> "file" <= "Pile"
False
# f(102) is > P(80)
>>> "py" <= "python"
True
>>> ["Py", "PY"] <= ["py", "PY"]
True
>>> ['a', 3] <= ['b', 2]
True
>>> [1, 2] <= [1, 2, 4]
True
```

## Assignment Operators

The assignment symbol (`=`) serves as a delimiter between the name and value in an assignment statement.

It binds (or rebinds) a value (data, variable, expression) on the right to the target variable on the left.

``` python
>>> x = 1
>>> x
1
>>> y = x
>>> y
1
>>> y = "python"
>>> y
'python'
```

Binary operators can be combined with the assignment symbol to create **Augmented Assignment Operators**.

These operators perform the binary operation on the two operands and assign the result to the original target (left operand).

If `` is a binary operator, then the expression `a = b` containing the augmented assignment operator is equivalent to `a = a b`.

**`+=`**

The `+=` operator adds a value (right operand) to the variable (left operand) and assigns the result to that variable.

``` python
>>> a = 2
>>> a += 3
>>> a
5
>>> x = "hello"
>>> y = "world"
>>> x += y
>>> x
'helloworld'
```

**`-=`**

The `-=` operator subtracts a value (right operand) from the variable (left operand) and assigns the result to that variable.

``` python
>>> a = 3
>>> a -= 2
>>> a
1
```

**`*=`**

The `*=` operator multiplies a value (right operand) to the variable (left operand) and assigns the result to that variable.

``` python
>>> a = 3
>>> a *= 2
>>> a
6
>>> x = "hi"
>>> x *= 3
>>> x
'hihihi'
```

**`/=`**

The `/=` operator divides the variable (left operand) by a value (right operand) and assigns the result to that variable.

``` python
>>> a = 4
>>> a /= 2
>>> a
2.0
```

**`//=`**

The `//=` operator floor divides the variable (left operand) by a value (right operand) and assigns the result to that variable.

``` python
>>> a = 5
>>> a //= 2
>>> a
2
```

**`**=`**

The `**=` operator raises the variable (left operand) to a power (right operand) and assigns the result to that variable.

``` python
>>> a = 4
>>> a **= 2
>>> a
16
```

**`%=`**

The `%=` operator computes modulus of the variable (left operand) and a value (right operand) and assigns the result to that variable.

``` python
>>> a = 4
>>> a %= 3
>>> a
1
```

## Logical Operators

Expressions using logical operators evaluate to a boolean value (`True` or `False`) based on the logical state of the operands.

### Logical State of Operands

In Python, all values except `0`, `None`, `False`, `""`, `''`, `()`, `[]`, `{}` have their logical state as `True`.

`bool()` built-in function can be used to determine the logical state of literals, variables or expressions.

The logical state of the following literals is `False`.

``` python
>>> bool(False)
False
>>> bool(0)
False
>>> bool([])
False
>>> bool(None)
False
>>> bool("")
False
>>> bool([])
False
>>> bool(())
False
>>> bool({})
False
```

Some example literals having boolean state as `True` are provided below.

``` python
>>> bool(True)
True
>>> bool(1)
True
>>> bool(2.0)
True
>>> bool(100)
True
>>> bool("python")
True
>>> bool(["py", "thon"])
True
```

**`not`**

The logical state of an operand can be reversed (`False` to `True`, and vice versa) using the logical `not` operator.

``` python
>>> n = 5
>>> bool(n)
True
>>> bool(not n)
False
```

**`or`**

The logical `or` operator returns `True` if the logical state of any of the two operands is `True`.

``` python
>>> True or False
True
>>> bool(1 or 0)
True
>>> False or False
False
```

**`and`**

The logical `and` operator returns `True` if the logical state of both the operands is `True`.

``` python
>>> True and True
True
>>> True and False
False
>>> bool(10 and 20)
True
>>> bool(1 and 0)
False
```

## Identity Operators

We have already witnessed how Python treats every value or data item as an object.

The relational operator `==` can be used to test whether the operands contain the same value.

``` python
>>> n = 1
>>> n2 = 1
>>> n == n2
True
```

This operator however does not check if both the operands are referring to the same object or different objects.

The identity operators `is` and `is not` are used to test whether two objects have the same or different identity (pointing to the same location in memory) respectively.

`a is b` is equivalent to `id(a) == id(b)`, where `id()` is the built-in function which returns the identity of an object.

``` python
>>> n = 1
>>> n2 = 1
>>> n is n2
True
```

In the above example, both variables `n` and `n2` point to that same memory location (same object).

``` python
>>> l = [1, 2, 3]
>>> l2 = [1, 2, 3]
>>> l == l2
True
>>> l is l2
False
```

In the above example, both lists `l` and `l2` although contain items with same values, are actually two different objects occupying different memory locations.

## Membership Operators

The operators `in` and `not in` test whether a value is present or not present in an iterable (string, list, tuple, set, dictionary).

``` python
>>> 1 in [1, 2, 3]
True
>>> "ed" in ["ed", "py", "hi"]
True
>>> "ed" in ("ed", "py", "hi")
True
>>> 'ed' in {'ed': 1, 'py': 2}
True

>>> "pen" not in ["pencil", "ink"]
True
>>> "pen" not in ["pen", "ink"]
False
```

## Expressions

Literals (constants), identifiers (variables) and operators can be combined to form an expression which always evaluates to a single value.

For example, `40 + marks` is an expression containing a literal (`40`), a variable (`marks`) and an operator (`+`).

Some valid expressions are provided below:

- `10`
- `a`
- `-a`
- `a - 10`
- `a + b`
- `4.0 * 3.5`
- `a == b`
- `c in d`
- `a is T`
- `"Hello" + "World"`
- `15 - a*4`
- `3*num + 9/4 - 10%count**2`

As shown above, standalone literals (like `10`) and variables (like `a`) are considered as expressions, but standalone operators are not expressions.

### Chained Expression

Comparison operators can be chained together in Python.

For example, `lower <= age <= upper` is a valid chained expression which is equivalent to the expression -

`lower <= age and age <= upper`.

If `a`, `b`, `c`, …, `y`, `z` are expressions and `op1`, `op2`, …, `opN` are comparison operators, then the chained expression `a op1 b op2 c ... y opN z` is equivalent to `a op1 b and b op2 c and ... y opN z`.

### Conditional Expression

Python does not have ternary operators (`?:`) like other programming languages. Hence, the keywords `if` and `else` are used to create conditional expressions which evaluates to a value based on the given condition.

For example,

``` python
var = t_val if cond else f_val
```

If the above condition `cond` evaluates to `True`, then the variable `var` is assigned `t_val`, else it is assigned `f_val`.

``` python
>>> value = 1 if 2 > 3 else -1
>>> value
-1
```

## Operator Precedence with Examples

While studying mathematics in middle school, we came across the **BODMAS** (Bracket, Of, Division, Multiplication, Addition, and Subtraction) rule which helps us in understanding how mathematical expressions are computed in the presence of multiple operators (`of`, `x`, `/`, `+`, `-`).

In Python, we have a large number of operators and a similar rule to determine the order of evaluation of an expression. This is known as **operator precedence** where the operator with higher precedence is evaluated before the operator with lower precedence in an expression.

The table below presents the precedence of operators in Python from highest to lowest. Operators in the same row have the same precedence, so in such cases the expression is evaluated from left to right.

| Operator | Description |
|--|--|
| `(expressions...)` | Parenthesized expression (Group) |
| `**` | Exponentiation |
| `+x`, `-x`, `~x` | Unary positive, Unary negative, Bitwise `NOT` |
| `*`, `@`, `/`, `//`, `%` | Multiplication, Matrix multiplication, Division, Floor division, Remainder |
| `+`, `-` | Addition, Subtraction |
| `<<`, `>>` | Shifts |
| `&` | Bitwise `AND` |
| `^` | Bitwise `XOR` |
| `\|` | Bitwise `OR` |
| `in`, `not in`, `is`, `is not`, `<`, `<=`, `>`, `>=`, `!=`, `==` | Membership, Identity & Comparisons |
| `not x` | Boolean `NOT` |
| `and` | Boolean `AND` |
| `or` | Boolean `OR` |
| `:=` | Assignment expression |

### Exercises

**Example 1**

Evaluate the expression

`15 - 2 * 4`

**Solution**

Step: `*` has higher precedence over `-`
`15 - 2 * 4`
= `15 - 8`
= `7`

**Example 2**

Evaluate the expression

`15 - 2 + 4`

**Solution**

Step: `-` and `+` have the same order of precedence so the expression is evaluated left to right
`15 - 2 + 4`
= `13 + 4`
= `17`

**Example 3**

Evaluate the expression

`15 - (2 + 4)`

**Solution**

Parenthesized expression `(...)` has the highest precedence so `+` is evaluated first
`15 - (2 + 4)`
= `15 - 6`
= `9`

**Example 4**

Evaluate the expression

`3 * 2 + 9 / 4 - 10 % 2 ** 2`

**Step 1**

`**` takes precedence
`3 * 2 + 9 / 4 - 10 % 2 ** 2`
= `3 * 2 + 9 / 4 - 10 % 4`

**Step 2**

`*`, `/` and `%` have the same precedence so they are evaluated left to right.
`3 * 2 + 9 / 4 - 10 % 4`
= `6 + 2.25 - 2`

**Step 3**

`+` and `-` evaluation
`6 + 2.25 - 2`
= `6.25`

**Example 5**

Evaluate the expression

`20 / 4 // 2 * 2 - 4 + 20`

**Step 1**

`*`, `/`, `//` and `%` have the same precedence so they are evaluated left to right.
`20 / 4 // 2 * 2 - 4 + 20`
= `5 // 2 * 2 - 4 + 20`
= `2 * 2 - 4 + 20`
= `4 - 4 + 20`

**Step 2**

`+` and `-` evaluation
`4 - 4 + 20`
= `20`

**Example 6**

Evaluate the expression

`not 6 <= 4 and 3 ** 3 > 12 / 3`

**Step 1**

`**` takes precedence
`not 6 <= 4 and 3 ** 3 > 12 / 3`
= `not 6 <= 4 and 27 > 12 / 3`

**Step 2**

`/` is next in line of precedence
`not 6 <= 4 and 27 > 12 / 3`
= `not 6 <= 4 and 27 > 4`

**Step 3**

Comparison operators are next in line of precedence
`not 6 <= 4 and 27 > 4`
= `not False and True`

**Step 4**

Boolean `NOT` is evaluated
`not False and True`
= `True and True`

**Step 5**

Boolean `AND` is evaluated
`True and True`
= `True`

# Errors & Exception Handling

## Error Types

A program contains **"bug(s)"** when it is unable to execute or produces an output which is different from what is expected. These bugs are generally introduced by a programmer unknowingly.

The process of identifying and eliminating these bugs or errors is known as **debugging**.

The three major types of errors are:

- Syntax Error
- Runtime Error
- Logical Error

## Syntax Error

Syntax error occurs when the program contains any statement that does not follow the prescribed Python rules or syntax which makes it difficult for the Python interpreter to parse (understand) and execute it.

Some common syntax errors are:

- Missing/Misspelled keyword
- Missing colon or brackets
- Empty block
- Incorrect position of keyword
- Incorrect block indentation

### Script Mode

When a code containing syntactically incorrect statement is executed using script mode via IDLE, an error dialog box is displayed.

![Syntax Error Dialog](images/0702a.png)

On closing the dialog box, the incorrect part of the code, the potential cause of error, is highlighted in red.

![Syntax Error Highlighted](images/0702b.png)

This error has to be rectified to execute the program correctly.

### Interactive Mode

When a syntactically incorrect statement is executed in the Python console (interactive mode), the Python interpreter displays it and also adds a little arrow (`^`) pointing at the entry point or token where the error was detected.

**Example**

``` python
>>> while True print('Hi!')
File "", line 1
while True print('Hi!')
^
SyntaxError: invalid syntax
```

In the above example there is a syntax error with `^` pointing to `print` function which the parser is unable to understand as there is a missing `:` (colon) after `True`.

## Runtime Error

A runtime error occurs when the program is terminated prematurely by the Python interpreter as it is unable to execute a statement although it is correct syntactically.

Some runtime error examples are:

- **ImportError**: Raised when the `import` statement has trouble loading a module or any definition from a module.
- **IOError**: Raised when the interpreter is not able to open the file specified in the program.
- **ZeroDivisionError**: Raised when a number is divided or mod by zero.
- **NameError**: Raised when an identifier is encountered which has not been defined.
- **ValueError**: Raised when an argument or operand is of required data type, but has undesired value.
- **IndexError**: Raised when the provided index in a sequence (string, list, tuple, etc.) is out of range.
- **KeyError**: Raised when a dictionary key is not found in the set of existing keys.
- **TypeError**: Raised while performing an operation on incompatible types.
- **IndentationError**: Raised when the indentation of a statement or code block is incorrect.

### Runtime Error Examples

**ZeroDivisionError**

``` python
n = 100
d = 0
print(n/d)
```

``` python
Traceback (most recent call last):
File "/Users/name/Desktop/test.py", line 3, in
print(n/d)
ZeroDivisionError: division by zero
```

**NameError**

``` python
n = 100
print(d)
```

``` python
Traceback (most recent call last):
File "/Users/name/Desktop/test.py", line 2, in
print(d)
NameError: name 'd' is not defined
```

**KeyError**

``` python
d = {1: "1st", 2: "2nd"}
print(d[3])
```

``` python
Traceback (most recent call last):
File "/Users/name/Desktop/test.py", line 2, in
print(d[3])
KeyError: 3
```

**TypeError**

``` python
n =1
s = "a"
tot = n + s
```

``` python
Traceback (most recent call last):
File "/Users/name/Desktop/test.py", line 3, in
tot = n + s
TypeError: unsupported operand type(s) for +: 'int' and 'str'
```

## Logical Error

Logical error or Semantic error is caused when the issue lies in the underlying meaning of the code which leads to an incorrect output.

As compared to syntax or runtime error there is no termination of the program.

Debugging a logical error requires inspection of the entire code as no guiding error message is displayed.

### Example

Let us write a program to calculate the average of two numbers

``` python
n = 10
m = 20
avg = n + m / 2
print("Average:", avg)
```

On executing the script the result is

```
Average: 20.0
```

This is incorrect as there is a logical error in the code.

Since `/` has a higher precedence over `+`, `m / 2` is being computed first.

We can modify the code to get rid of the logical error.

``` python
n = 10
m = 20
avg = (n + m) / 2
print("Average:", avg)
```

On executing the script, we now obtain the correct result

```
Average: 15.0
```

## Exceptions

We have witnessed that even if a program is syntactically correct, its execution may lead to a run-time error.

This error detected during execution is known as an **exception** which is an object created by the Python interpreter containing information regarding the error like type of error, file name and the location of the error (line number, token) in the program.

Some of the built-in exceptions that are raised by the Python interpreter are - `ImportError`, `ZeroDivisionError`, `NameError`, `ValueError`, `IndexError`, `KeyError`, `TypeError` and `IndentationError`.

Apart from the Python interpreter, a programmer can also trigger and raise an exception (along with a custom message) in the code using `raise` or `assert` statement.

### raise

The `raise` statement can be used to throw an exception in a program. The exception may or may not contain the custom error message (recommended).

Let us consider a program which accepts two numbers (`a` and `b`) from the user and prints the result `a/b`.

**Code**

``` python
a = int(input("Enter a: "))
b = int(input("Enter b: "))
print("a/b =", a/b)
```

**Output**

```
Enter a: 10
Enter b: 0
Traceback (most recent call last):
File "/Users/name/test.py",
line 3, in
print("a/b =", a/b)
ZeroDivisionError: division by zero
```

It can be observed that the Python interpreter raises a `ZeroDivisionError` when the value of `b` is entered as `0`.

Now we can modify the above code to raise an exception for such scenarios.

**Code**

``` python
a = int(input("Enter a: "))
b = int(input("Enter b: "))
if b==0:
raise Exception()
print("a/b =", a/b)
```

**Output**

```
Enter a: 10
Enter b: 0
Traceback (most recent call last):
File "/Users/name/test.py",
line 4, in
raise Exception()
Exception
```

An exception is raised, but it is not helpful.

Let us add some custom error message.

**Code**

``` python
a = int(input("Enter a: "))
b = int(input("Enter b: "))
if b==0:
raise Exception("b is zero")
print("a/b =", a/b)
```

**Output**

```
Enter a: 10
Enter b: 0
Traceback (most recent call last):
File "/Users/name/test.py",
line 4, in
raise Exception("b is zero")
Exception: b is zero
```

We can also raise any specific type of error as per the program logic as shown below:

**Code**

``` python
a = int(input("Enter a: "))
b = int(input("Enter b: "))
if b==0:
raise ValueError("The value of b cannot be zero")
print("a/b =", a/b)
```

**Output**

```
Enter a: 10
Enter b: 0
Traceback (most recent call last):
File "/Users/name/test.py",
line 4, in
raise ValueError("The value of b cannot be zero")
ValueError: The value of b cannot be zero
```

### assert

An `assert` statement is often used during code development to act like a safety valve which notifies the programmer in case the test expression is evaluated as `False`.

If the test expression’s value is `True`, the code execution continues normally.

An `AssertionError` is raised if the value is `False`.

**Code**

``` python
a = 3
b = 4
assert a == b
c = 5
```

**Output**

```
Traceback (most recent call last):
File "/Users/name/test.py",
line 3, in
assert a == b
AssertionError
```

The statement also allows for a message to be attached to the `AssertionError`.

**Code**

``` python
a = 3
b = 4
assert a == b, "a is not equal to b"
c = 5
```

Output:

```
Traceback (most recent call last):
File "/Users/name/test.py",
line 3, in
assert a == b, "a is not equal to b"
AssertionError: a is not equal to b
```

## Exception Handling

Exception handling is the process of properly handling an exception which can potentially crash a program during execution.

When an error occurs, the program throws an exception.

The runtime system attempts to find an **exception handler**, a block of code that can handle a particular type of error. Once located, the suitable exception handler **catches the exception** and executes the code block which can attempt to recover from the error. In case the error is unrecoverable, the handler provides a way to gently exit the program.

The `try` statement in Python specifies the exception handlers and/or cleanup code for a code block.

The various parts of a try statement are:

- `try` block: The block of statements within which an exception might be thrown.
- `except` clause(s): One or more exception handlers. Each `except` clause handles a particular type of exception. In case an exception of a particular type occurs in the `try` block, the corresponding `except` clause code block is executed.
- `else` clause: An optional `else` clause can also be included after the last `except` block. In case no exception is raised, none of the `except` blocks are executed. In this case, the `else` code block is executed.
- `finally` clause: An optional `finally` clause can be added at the end of the try statement which includes a block of statements that are executed regardless of whether or not any error occurred inside the try block. This block is usually setup for code cleanup and closing all open file objects.

Here's the general form of these statements:

``` python
try:
[code block]
except [exception1 [as identifier1]]:
[exception code block 1]
except [exception2 [as identifier2]]:
[exception code block 2]
...
...
else:
[code block executes if no error]
finally:
[code block always executed]
```

# Control Flow

## Introduction to Control Flow

A simple Python program can be treated as a block of code where each statement is executed by the Python interpreter in a sequential order from top to bottom.

But, in real world we would like to have some control over the execution of code such as:

- skip or execute a block (set of statements) based on certain conditions
- execute a block repeatedly
- redirect execution to another set of statements
- breaking up the execution

This control over the flow of execution is provided by **Control Flow Statements**.

They can be categorized as:

- Sequential
- Selection
- Iteration/Repetition
- Jump
- Procedural Abstraction - A sequence of statements are referenced as a single function or method call
- Recursion - Calling a method/function in the same method/function
- Exception Handling

## Sequential Flow

By default the code statements in Python are executed in Sequential order.

The below flow chart demonstrates how 3 statements are executed in a sequential order.

![Sequential Flow](images/0802a.png)

For example,

``` python
a = 2
b = 3
c = a*b
print(c)
```

The above code will be executed in the following sequential order:

![Sequential Flow of Code](images/0802b.png)

## Selection Statements: if .. else

Selection statements, also known as Decision making statements, control the flow of a program based on the outcome of one or many test expression(s). If the condition is satisfied (`True`) then the code block is executed. There is also a provision to execute another code block if the condition is not satisfied.

This process can be demonstrated using the below flowchart:

![Selection Flow](images/0803a.png)

Python supports `if` compound statement which provides this control. The `if` statement comprises:

- `if` keyword followed by the test expression, a colon `:` and an indented block of code which gets executed if the condition is satisfied
- (optional) one or many `elif` clause followed by their test conditions and their corresponding code blocks
- (optional) `else` clause and the corresponding code block which gets executed if none of the above conditions (`if`, `elif`) are satisfied

An example `if` statement is provided below:

``` python
'''
age - age of loan applicant
emp - is employed (bool)
cscore - credit scrore of applicant
'''
result = None
if age < 26 and not emp:
result = "Loan rejected"
elif age > 35 and cscore < 600:
result = "Loan rejected"
else:
result = "Loan approved"
print(result)
```

The control flow view of the above code is:

![Selection in Flow of Code](images/0803b.png)

### Examples

Let us go through some programming problems which utilize selection statements.

**1. Absolute Value**

Write a program to output the magnitude of difference between two numbers using conditional statement.

**Code**

``` python
n1 = int(input("Enter 1st number: "))
n2 = int(input("Enter 2nd number: "))

if n1 > n2:
diff = n1 - n2
else:
diff = n2 - n1

print("The difference of", n1, "and", n2, "is", diff)
```

**Output**

```
Enter 1st number: 12
Enter 2nd number: 15
The difference of 12 and 15 is 3
```

**2. Sorting 3 Numbers**

Write a program to accept 3 numbers from the user and print them in ascending order of value.

**Code**

``` python
a = int(input("Enter 1st number: "))
b = int(input("Enter 2nd number: "))
c = int(input("Enter 3rd number: "))

if b < a:
# Swapping the values of a and b
a, b = b, a

if c < b:
b, c = c, b
if b < a:
a, b = b, a

print("The numbers in sorted order:", a, ",", b, ",", c)
```

**Output**

```
Enter 1st number: 9
Enter 2nd number: 2
Enter 3rd number: 6
The numbers in sorted order: 2 , 6 , 9
```

**3. Divisibility**

Write a program to accept two numbers and test if the first number is divisible by the second number.

**Code**

``` python
a = int(input("Enter 1st number: "))
b = int(input("Enter 2nd number: "))

if a % b == 0:
print(a, "is divisible by", b)
else:
print(a, "is not divisible by", b)
```

**Output**

```
Enter 1st number: 9
Enter 2nd number: 2
9 is not divisible by 2

Enter 1st number: 9
Enter 2nd number: 3
9 is divisible by 3
```

## Iteration: for

Iteration statements, also known as Looping statements, allow repeated execution of a code block.

Python provides `for` and `while` statements to perform iteration.

The `for` statement can be used to iterate over the items of a sequence (`list`, `string`, `tuple`, `range`). It can also be used to iterate over unordered sequences like `set` and `dict`.

This process can be demonstrated using the below flowchart:

![Iteration in Flow of Code Python](images/0804a.png)

Let us go through some code examples to demonstrate how `for` statement can be used to iterate over sequences.

### List Iteration

**Code**

``` python
cars = ["Hyundai", "Honda",
"Ford", "Toyota",
"BMW", "Volkswagen"]
for make in cars:
print(make)
```

**Output**

```
Hyundai
Honda
Ford
Toyota
BMW
Volkswagen
```

### Tuple Iteration

**Code**

``` python
cars = ("Hyundai", "Honda",
"Ford", "Toyota",
"BMW", "Volkswagen")
for make in cars:
print(make)
```

**Output**

```
Hyundai
Honda
Ford
Toyota
BMW
Volkswagen
```

### String Iteration

**Code**

``` python
name = "python"
for char in name:
print(char)
```

**Output**

```
p
y
t
h
o
n
```

### Range Iteration

The `range` type represents an immutable sequence of numbers that is usually used in for loops for looping a certain number of times. `range` object always take the same (small) amount of memory, no matter the size of the range it represents, which is an advantage over a regular `list` or `tuple`.

**Syntax**: `range(stop)` or
`range(start, stop[, step])`

``` python
>>> range(10)
range(0, 10)
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 10, 2))
[1, 3, 5, 7, 9]
```

`range()` function is widely used in a `for` statement to control the number of iterations and provide the index value (`i`) of each iteration.

**Example #1**

Print multiples of 5 starting from 0 to 20.

**Code**

``` python
for i in range(5):
print(i*5)
```

**Output**

```
0
5
10
15
20
```

**Example #2**

Print all integers from 2 to 5 including the boundary values.

**Code**

``` python
for i in range(2, 6):
print(i)
```

**Output**

```
2
3
4
5
```

**Example #3**

Print all odd numbers between 2 and 10.

**Code**

``` python
for i in range(3, 10, 2):
print(i)
```

or

``` python
for i in range(2, 10):
if i % 2 != 0:
print(i)
```

**Output**

```
3
5
7
9
```

**Example #4**

Print the index of all occurrences of `o` in `python programming`.

**Code**

``` python
s = "python programming"
for i in range(len(s)):
if s[i] == "o":
print(i)
```

**Output**

```
4
9
```

### Exercises

Let us go through some programming problems which utilize the `for` iteration statement.

**1. Compound Interest**

Write a program to calculate the total compound interest payable for given principal, interest rate (compounded annually) and total time (in years).

**Code**

``` python
prin = float(input("Enter the principal amount: "))
rate = float(input("Enter the annual interest rate: "))
time = int(input("Enter the loan duration (in years): "))

amt = prin
for n in range(time):
amt += rate*amt/100

print("Total interest payable:", amt - prin)
```

**Output**

```
Enter the principal amount: 500000
Enter the annual interest rate: 5
Enter the loan duration (in years): 3
Total interest payable: 78812.5
```

**2. Factorial**

The factorial of a positive integer `n`, denoted by `n!`, is the product of all positive integers less than or equal to `n`.
`n! = n×(n-1)×(n-2)...3×2×1`
Write a program to calculate `n!` for a given `n` (assume `n` is greater than `0`).

**Code**

``` python
n = int(input("Enter n: "))

factorial = 1
for i in range(1, n+1):
factorial *= i

print("n! :", factorial)
```

**Output**

```
Enter n: 6
n! : 720
```

## Iteration: while

`while` statement repeatedly executes a code block as long as the test condition is satisfied.

Usually there is a statement at the end of the code block which updates the value of the variable being used in the test expression, so the the loop does not execute infinitely.

A flowchart of the process is provided below:

![while loop Iteration in Flow of Code](images/0805a.png)

For example, let us traverse a list and print the position(index) and value of each element until we reach the end of the list.

**Code**

``` python
cars = ["Hyundai", "Honda",
"Ford", "Toyota",
"BMW", "Volkswagen"]
i = 0
while i 0:
amt += rate*amt/100
time = time - 1

print("Total interest payable:", amt - prin)
```

**Output**

```
Enter the principal amount: 500000
Enter the annual interest rate: 5
Enter the loan duration (in years): 3
Total interest payable: 78812.5
```

**2. Factorial**

The factorial of a positive integer `n`, denoted by `n!`, is the product of all positive integers less than or equal to `n`.
`n! = n×(n-1)×(n-2)...3×2×1`
Write a program to calculate `n!` for a given `n` (assume `n` is greater than `0`).

**Code**

``` python
n = int(input("Enter n: "))

factorial = 1
while n > 0:
factorial *= n
n = n - 1

print("n! :", factorial)
```

**Output**

```
Enter n: 6
n! : 720
```

## Jump Statements

Jump statements are used to (abruptly) alter the flow of execution.

Some of the jump statements available in Python are:

### pass

A `pass` statement acts as a placeholder and performs null (no) operation.

Various reasons for using the keyword `pass` are provided below:

**1. Syntactical Requirement**

Using `pass` becomes a syntactical requirement for cases where the Python interpreter can raise a `SyntaxError` due to missing statements.

The below code will execute successfully without any operation in the loop

``` python
for i in range(6):
pass
```

whereas without `pass`

``` python
for i in range(6):
```

will throw the following `SyntaxError`

```
File "", line 1
for i in range(6):
^
SyntaxError: unexpected EOF while parsing
```

Similarly, inside an `if`

``` python
if 2 < 3:
pass
```

whereas without `pass`

``` python
if 2 < 3:
```

will throw the following `SyntaxError`

```
File "", line 1
if 2 < 3:
^
SyntaxError: unexpected EOF while parsing
```

**2. Skipping Code Execution**

`pass` can be used to skip code execution for certain cases.

For example,

**Code**

``` python
l = [2, 3, 4, 5, 6]
for i in l:
if i%3 == 0:
pass
else:
print(i, "is not divisible by 3")
```

**Output**

```
2 is not divisible by 3
4 is not divisible by 3
5 is not divisible by 3
```

**3. Placeholders**

`pass` can be used to create valid empty functions and classes as placeholders which can be modified in the future versions of code.

``` python
def emptyFunction():
pass

class EmptyClass:
pass
```

### break

The `break` statement is used to terminate the execution of immediately enclosing `for` or `while` statement.

The below code will terminate the `for` loop when `i` is equal to `4`

``` python
for i in range(10):
print(i)
if i == 4:
break
```

```
0
1
2
3
4
```

In a while statement,

``` python
i =0
while i <10:
print(i)
if i == 4:
break
i+=1
```

`break` will terminate the `while` loop when `i` is equal to `4`

```
0
1
2
3
4
```

### continue

`continue` statement is used to skip the execution of successive statements and start the next iteration.

The below code will skip all candidates for an interview who have less than 4 years of work experience.

``` python
people = [{"name": "ABC", "experience": 6},
{"name": "EFG", "experience": 2},
{"name": "JKL", "experience": 5},
{"name": "XYZ", "experience": 3},]
for candidate in people:
if candidate["experience"]<4:
continue
print(candidate["name"], "is selected for interview")
```

Output:

```
ABC is selected for interview
JKL is selected for interview
```

## Nested Loops

When a loop is present inside another loop, it is known as a nested loop.

For each iteration of the outer loop, the inner loop undergoes complete iteration. Thus, if the outer loop has to undergo `n` iterations and the inner loop has to undergo `m` iterations, the code block inside the inner loop executes `n x m` times.

Let us go through a nested loop example:

### Factorial

Write a program to print the factorial of all numbers in the range `1` to `10` (inclusive).

**Code**

``` python
for n in range(1, 11):
factorial = 1
for i in range(1, n+1):
factorial *= i
print(n,"! =", factorial)
```

**Output**

```
1 ! = 1
2 ! = 2
3 ! = 6
4 ! = 24
5 ! = 120
6 ! = 720
7 ! = 5040
8 ! = 40320
9 ! = 362880
10 ! = 3628800
```

### Nested Loop - break

A `break` statement inside the inner loop terminates only the inner loop whereas the outer loop is not affected.

To develop a better understanding, let us write a program to find all prime numbers between 2 and 40.

**Code**

``` python
for n in range(2, 40):
i = 2
while i < n/2:
if n%i == 0:
break
i+=1
if i>n/2:
print(n,"is prime")
```

**Output**

```
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
```

# Strings

## Strings: Introduction & Creation

A String (`str`) is an **immutable** sequence of Unicode characters which is used to handle textual data in Python.

They can be specified by enclosing within:

- Single quotes: `'embedded "double" quotes are allowed'`
- Double quotes: `"embedded 'single' quotes are allowed"`
- Triple quotes: `'''Three single quotes'''`, `"""Three double quotes"""`.

Triple quoted strings can also span multiple lines.

Some examples are provided below:

``` python
s = "I am a String"

s1 = """A
multiline
String"""

s2 = '''Also a
multiline
String'''
```

### Escape Characters

The backslash (`\`) character can be used in a string to escape characters that otherwise have a special meaning, such as newline, linefeed, or the quote character.

| Escape Sequence | Meaning |
|--|--|
| `\\` | Backslash (`\`) |
| `\'` | Single quote (`'`) |
| `\"` | Double quote (`"`) |
| `\a` | ASCII Bell (BEL) |
| `\b` | ASCII Backspace (BS) |
| `\f` | ASCII Form-feed (FF) |
| `\n` | ASCII Linefeed (LF) |
| `\r` | ASCII Carriage Return (CR) |
| `\t` | ASCII Horizontal Tab (TAB) |
| `\v` | ASCII Vertical Tab (VT) |

Although `\'` and `\"` can be used to specify quote characters, Python allows embedding double quotes inside a single-quoted string (`'My name is "Python".'`) and single quotes inside a double-quoted string (`"Python's World"`).

### Unicode Support

Python string objects support Unicode characters.

A unicode character can be specified as `\u` followed by the 4 letter unicode (`\uXXXX`).

``` python
>>> print("E = mc\u00B2")
E = mc²
```

In the above example, `\u00B2` is the unicode character which represents the 'SUPERSCRIPT TWO'.

### Other Types to String

In case you want to create a string object from other data types, just use the built-in `str()` function as follows:

``` python
>>> str(9)
'9'

>>> str(10.0)
'10.0'
```

## Accessing Characters of a String

Python strings are "immutable", i.e., the state (value) of the objects cannot be modified after they are created.

Using the standard `[ ]` syntax and zero-based indexing, characters can be accessed in a string.

If `s = "hello"`,

- `s[0]` will result in `h`
- `s[2]` will result in `l`
- `s[5]` will result in `IndexError: string index out of range` as the length of string is `5` (index `0` to `4`)
- `s[2] = 'p'` will result in `TypeError: 'str' object does not support item assignment` as `s` is immutable

Python also supports negative indexing, i.e., you can access the values of a string from right to left.

Index of `-1` denotes the last character of the string, `-2` is the second last character and so forth.

If `s = "hello"`,

- `s[-1]` will result in `o`
- `s[-4]` will result in `e`
- `s[-6]` will result in `IndexError: string index out of range` as the length of string is `5` (negative index `-1` to `-5`)

### Length of String

The built-in function `len()` returns the length of a string which is useful during string traversal or other string operations.

``` python
>>> len("hello")
5

>>> s = "sample text"
>>> len(s)
11

>>> p = "python"
>>> l = len(p)
>>> p[l-1]
'n'
```

## String Operations

We can perform various operations on a string (sequence of characters) such as slicing, membership, concatenation and repetition.

### Slicing

In Python, a character in a string can be easily accessed using its index.

``` python
>>> s = "Hello"
>>> s[1]
'e'
```

Python also provides a way to access a substring from a string. This substring is known as a **slice** and it can be obtained using the slice operator `[n:m]` which returns the part of the string from the start index (`n`) to the end index (`m`), including the first but excluding the last.

``` python
>>> s = "Hello"
>>> s[1:3]
'el'
```

If the start index (`n`) is omitted, the default value of `n` is set as `0` which denotes the beginning of the string. If the end index (`m`) is omitted, the substring ends at the last character of the string.

``` python
>>> s = "Hello"
>>> s[:3]
'Hel'
>>> s[3:]
'lo'
>>> s[:]
'Hello'
```

Negative indexing is also supported in the slice operator.

``` python
>>> s = "Hello"
>>> s[-4:-2]
'el'
```

In the above example, `-4` is