0% found this document useful (0 votes)
2 views73 pages

Pyhton

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 73

How To Type Python ?

• Interactive
You type directly to Python one line at a time and it responds
• Script
You enter a sequence of statements (lines) into a file using a text
editor and tell Python to execute the statements in the file

1. Logical Operators
1.1 Operators
Key Points:
Python is a high-level programming language used for writing human-readable code
that is translated into binary for the computer to execute.
Arithmetic operations like addition, subtraction, multiplication, and division are
intuitive and easy to perform.

A. Arithmetic Operators and Behavior:

Operator Operation Example Result


+ Addition 5+3 8

- Subtraction 10 - 4 6
* Multiplication 7*2 14

/ Division (float result) 5/2 2.5

// Floor Division (integer part) 5 // 2 2


% Modulus (remainder) 5%2 1

** Exponentiation 2 ** 3 8

B. Notes:
Division ( / ) always results in a float.
Use // for integer division.
Parentheses and BODMAS rules determine operation precedence.
Avoid leaving operators without operands (e.g., 8+ → Error).

1.2 Assignment Operators


Used to assign values to variables.
Example: = , += , -= , *= , /= , etc.

Short-hand Assignment:

x=5
x += 3 # Equivalent to x = x + 3
print(x) # Output: 8

Multiple Assignments in One Line:

a, b = 10, 20
print(a, b) # Output: 10 20

1.3 Relational (Comparison) Operators


Used to compare two values.
Return Boolean values: True or False .
Examples:
Equal to ( == ), Not equal to ( != ), Greater than ( > ), Less than ( < ), Greater than or
equal to ( >= ), Less than or equal to ( <= ).

Example:

a=5
b = 10

print(a == b) # Output: False


print(a != b) # Output: True
print(a < b) # Output: True
1.4 Logical Operators
Combine multiple conditions.
Include:
and : True if both conditions are true.
or : True if at least one condition is true.
not : Reverses the Boolean value.

Truth Table:

A B A and B A or B not A
True True True True False
True False False True False
False True False True True
False False False False True

Example:

a = 10
b = 20

print(a > 5 and b < 30) # Output: True


print(a > 15 or b < 25) # Output: True
print(not (a > 15)) # Output: True

1.5 Unary Operators


Operate on a single operand.
Example: Negation ( - ).

Example:

a = 10
print(-a) # Output: -10
1.6 Precedence and Associativity of Operators
Precedence: Determines the order in which operators are evaluated.
Associativity: Determines the order of evaluation when operators have the same
precedence.

Operator Precedence Table (Highest to Lowest):

Operator Description
(X*Y) Parenthesis
** Exponentiation
*, /, //, % Multiplication, Division, Modulus
+, - Addition, Subtraction
<, >, <=, >= Relational Operators
==, != Equality Operators
not Logical NOT
and Logical AND
or Logical OR

Example:

result = 10 + 2 * 3 ** 2
print(result) # Output: 28 (Exponentiation > Multiplication > Addition)

2. Math in Python
2.1 Importing the Math Module
To use the math module, you must import it first:

Examples:
01. Direct Import
import math
print(math.sqrt(25)) # Output: 5.0

02. Using an Alias (Shortcut Name)

import math as m
print(m.sqrt(25)) # Output: 5.0

03. Importing Specific Functions

from math import sqrt, pow


print(sqrt(25)) # Output: 5.0
print(pow(2, 3)) # Output: 8.0

2.2 Commonly Used Methods in the Math Module


The math module contains various useful methods. Here are some of the most commonly
used ones:

Function Description Example


math.sqrt(x) Returns the square root of x . math.sqrt(25) → 5.0

math.pow(x, y) Returns x raised to the power y . math.pow(2, 3) → 8.0

math.ceil(x) Returns the smallest integer greater than or math.ceil(4.2) → 5


equal to x .
math.floor(x) Returns the largest integer less than or equal math.floor(4.7) → 4
to x .
math.exp(x) Returns e raised to the power x . math.exp(1) →
2.7182818

math.log(x[, Returns the natural logarithm of x or the math.log(10, 10) → 1.0


base]) logarithm with specified base.
math.sin(x) Returns the sine of x (in radians). math.sin(math.pi/2) →
1.0

math.cos(x) Returns the cosine of x (in radians). math.cos(0) → 1.0


Function Description Example
math.tan(x) Returns the tangent of x (in radians). math.tan(math.pi/4) →
1.0

2.3 Commonly Used Constants in the Math Module


The math module also provides several useful constants:

Constant Description Example


math.pi Value of π (pi). math.pi → 3.14159

math.e Value of Euler’s number e . math.e → 2.71828

math.inf Positive infinity. math.inf


math.nan Not a number (NaN). math.nan

3. Strings in Python
3.1 What Are Strings?
A string is a sequence of characters enclosed in single ( ' ) or double ( " ) quotes.
Escape sequences like backslashes ( \ ) allow you to use special characters within
strings.

Examples:

String Type Code Output


Regular String 'Python' or "Python" Python

Escape Special Quotes 'It\'s Python' It's Python

Using Backslash "She said \"Hi!\"" She said "Hi!"

3.2 String Concatenation and Repetition


Concatenation:
Combines two strings using the + operator.

Example: 'Python' + 'Rocks' → PythonRocks

Repetition:
Repeats a string using the * operator.

Example: 'Hi' * 3 → HiHiHi

3.3. New Line and Raw Strings

Concept Syntax Result


New Line Character "Hello\nWorld!" Hello
World!
Raw String r"Hello\nWorld!" Hello\nWorld!

Notes:

Use \n to introduce a new line in a string.


Use r before a string to treat escape sequences (like \n ) as normal text.

3.4. Strings as Variables


1. Defining Strings:

Strings in Python are enclosed in either single ( ' ) or double ( " ) quotation marks.
Examples:

"shiva"
'shiva'

2. Assigning a String to a Variable:


Syntax:

variable_name = "string_value"

Example:

name = 'shiva'
name # Output: 'shiva'

3.5 Slicing & Navigation Strings


1. Accessing a Substring:

Use the slice syntax with start index and end index separated by a colon ( : ). The end
index is not included.

name = 'youtube'
name[2:5] # Output: 'utu'

2. Omitting Slice Indices:

If the start or end index is omitted, the slice starts from the beginning or goes to the end
of the string, respectively.

print(s[:2]) # Output: Mo
print(s[8:]) # Output: thon
print(s[:]) # Output: Monty Python

3.Using in as a Logical Operator

The in keyword checks if a substring exists within a string and returns a Boolean value.

fruit = 'banana'
print('n' in fruit) # Output: True
print('m' in fruit) # Output: False
if 'a' in fruit:
print('Found it!') # Output: Found it!

4. Index Error:

Attempting to access an index beyond the string's length results in an error.

zot = 'abc'
print(zot[5]) # IndexError: string index out of range

5. String Length
The len() function returns the length of a string.

fruit = 'banana'
print(len(fruit)) # Output: 6

6. Looking Inside Strings

Individual characters in a string can be accessed using an index in square brackets. The
index starts at 0 and can be an expression.

fruit = 'banana'
letter = fruit[1]
print(letter) # Output: a

x=3
w = fruit[x - 1]
print(w) # Output: n

3.6 Negative Indexing:


Python supports negative indexing, where -1 refers to the last character.

Indexing youtube

Positive 0123456
Indexing youtube

Negative -7 -6 -5 -4 -3 -2 -1

1. Examples of Negative Indexing:

Single character:

name[-1] # Output: 'e'

Slicing with negative indices:

name[-5:-2] # Output: 'utu'

3.7 Concatenating Strings:


Combine strings using the + operator.
Example:

a = 'Hello'
b = a + 'There'
print(b) # Output: HelloThere

c = a + ' ' + 'There'


print(c) # Output: Hello There

---
## 3.8 String Library Methods in Python
Here’s a **table of string methods** that were mentioned in the chapter, along with their
descriptions and examples:
Based on the content of the chapter and the image you provided, here’s a **comprehensive list of
essential string methods** that were mentioned in the chapter, including the ones from the image
and additional ones like `len()`:
### **Essential String Methods**
| **Method** | **Description** | **Example**
|
| ---------------------------------------- | ---------------------------------------------------------------------------------
------------------------- | ------------------------------------------------- |
| **`len()`** | Returns the length of the string. |
`len("banana")` → `6` |
| **`str.capitalize()`** | Converts the first character of the string to uppercase.
| `"hello".capitalize()` → `"Hello"` |
| **`str.replace(old, new, count)`** | Replaces all occurrences of `old` with `new`. Optionally, only
the first `count` occurrences are replaced. | `"hello".replace("l", "x")` → `"hexxo"` |
| **`str.lower()`** | Converts the string to lowercase. |
`"Hello".lower()` → `"hello"` |
| **`str.upper()`** | Converts the string to uppercase. |
`"Hello".upper()` → `"HELLO"` |
| **`str.endswith(suffix, start, end)`** | Checks if the string ends with `suffix`. Optionally, specify a
start and end index. | `"hello".endswith("lo")` → `True` |
| **`str.startswith(prefix, start, end)`** | Checks if the string starts with `prefix`. Optionally, specify a
start and end index. | `"hello".startswith("he")` → `True` |
| **`str.find(sub, start, end)`** | Returns the index of the first occurrence of `sub`. Returns `-1` if
not found. | `"banana".find("na")` → `2` |
| **`str.lstrip(chars)`** | Removes leading (left) whitespace or specified characters.
| `" hello ".lstrip()` → `"hello "` |
| **`str.rstrip(chars)`** | Removes trailing (right) whitespace or specified characters.
| `" hello ".rstrip()` → `" hello"` |
| **`str.strip(chars)`** | Removes both leading and trailing whitespace or specified characters.
| `" hello ".strip()` → `"hello"` |
| **`str.split(sep, maxsplit)`** | Splits the string into a list based on the separator `sep`.
Optionally, limit the number of splits. | `"hello,world".split(",")` → `["hello", "world"]` |

### **Key Notes:**


1. **Immutable**: Strings in Python are immutable, meaning these methods do not modify the
original string but instead return a new string.
2. **Case Sensitivity**: Methods like `find()`, `startswith()`, and `endswith()` are case-sensitive.
3. **Whitespace**: Methods like `strip()`, `lstrip()`, and `rstrip()` remove spaces, tabs, and newline
characters by default.
4. **Optional Parameters**: Many methods (e.g., `find()`, `replace()`, `split()`) accept optional
parameters like `start`, `end`, or `count` for more precise control.
---
## 3.9 String Comparison
Certainly! String comparison in Python is based on lexicographical order, which is similar to how
words are ordered in a dictionary. Python compares strings character by character using their
Unicode values. Here are more examples to illustrate string comparison:

#### Example 1: Equality Check (`==`)


The `==` operator checks if two strings are exactly the same.

```python
word1 = "apple"
word2 = "apple"
word3 = "banana"

print(word1 == word2) # Output: True


print(word1 == word3) # Output: False

Example 2: Less Than ( < )


The < operator checks if the first string comes before the second string in lexicographical
order.

word1 = "apple"
word2 = "banana"
word3 = "apricot"

print(word1 < word2) # Output: True (because 'apple' comes before 'banana')
print(word1 < word3) # Output: True (because 'apple' comes before 'apricot')
print(word2 < word3) # Output: False (because 'banana' comes after 'apricot')

Example 3: Greater Than ( > )


The > operator checks if the first string comes after the second string in lexicographical
order.

word1 = "banana"
word2 = "apple"
word3 = "apricot"

print(word1 > word2) # Output: True (because 'banana' comes after 'apple')
print(word1 > word3) # Output: True (because 'banana' comes after 'apricot')
print(word2 > word3) # Output: False (because 'apple' comes before 'apricot')

Example 4: Case Sensitivity


String comparison is case-sensitive. Uppercase letters have lower Unicode values than
lowercase letters, so they come first in lexicographical order.

word1 = "Apple"
word2 = "apple"
word3 = "Banana"

print(word1 < word2) # Output: True (because 'Apple' comes before 'apple')
print(word1 < word3) # Output: True (because 'Apple' comes before 'Banana')
print(word2 < word3) # Output: False (because 'apple' comes after 'Banana')

Example 5: Mixed Comparisons


You can chain multiple comparisons together.

word1 = "apple"
word2 = "banana"
word3 = "cherry"

print(word1 < word2 < word3) # Output: True (because 'apple' < 'banana' < 'cherry')
print(word1 < word3 < word2) # Output: False (because 'apple' < 'cherry' is True, but 'cherry' <
'banana' is False)

Example 6: Comparing Strings with Different Lengths


When comparing strings of different lengths, Python compares them character by
character until it finds a difference.

word1 = "apple"
word2 = "applet"
word3 = "app"
print(word1 < word2) # Output: True (because 'apple' is shorter and comes before 'applet')
print(word1 < word3) # Output: False (because 'apple' comes after 'app')
print(word2 < word3) # Output: False (because 'applet' comes after 'app')

Example 7: Using <= and >=


The <= and >= operators check if one string is less than or equal to, or greater than or
equal to, another string.

word1 = "apple"
word2 = "apple"
word3 = "banana"

print(word1 <= word2) # Output: True (because they are equal)


print(word1 <= word3) # Output: True (because 'apple' comes before 'banana')
print(word1 >= word2) # Output: True (because they are equal)
print(word1 >= word3) # Output: False (because 'apple' comes before 'banana')

Example 8: Comparing Strings with Numbers


Strings that contain numbers are compared character by character, not numerically.

word1 = "100"
word2 = "99"
word3 = "101"

print(word1 < word2) # Output: True (because '1' < '9' in Unicode)
print(word1 < word3) # Output: True (because '100' is compared character by character)
print(word2 < word3) # Output: True (because '99' is compared character by character)

Example 9: Comparing Strings with Special Characters


Special characters and symbols are also compared based on their Unicode values.

word1 = "apple!"
word2 = "apple"
word3 = "apple#"
print(word1 < word2) # Output: False (because '!' has a higher Unicode value than the end of
'apple')
print(word1 < word3) # Output: True (because '!' comes before '#')

Example 10: Comparing Empty Strings


An empty string is considered less than any non-empty string.

word1 = ""
word2 = "apple"

print(word1 < word2) # Output: True (because an empty string is less than any non-empty string)
print(word1 > word2) # Output: False

4. Variables and Data Types in Python


4.1 What is a Variable?
A variable in Python acts as a tag to reference a value stored in memory.
Variables are created when a value is assigned to them.
Example:

x = 10 # Variable `x` references the value 10

Names of the variables :


Must start with a letter or underscore _
Must consist of letters, numbers, and underscores
Case Sensitive

Examples:
Good: spam , eggs , spam23 , _speed
Bad: 23spam , #sign , var.12
Different: spam , Spam , SPAM

4.2 Memory Area of Variables


Python stores variables in memory and uses object IDs to keep track of them.
If multiple variables reference the same data, they point to the same memory location.

Getting the Address of a Variable

Use the id() function to retrieve the memory address of a variable.


Example:

a = 42
print(id(a)) # Outputs the memory address of the variable `a`

4.3 How Variables Work in Python


01. Assigning one variable to another:

a = 42
b=a
print(id(a), id(b)) # Both variables point to the same memory address

02. Updating a variable:


When you change a variable's value, it points to a new memory location.

a = 42
a = 50 # `a` now points to a new memory location

4.4 Garbage Collection


Python automatically handles memory management through Garbage Collection.
Unused data (data not referenced by any variable) is cleared from memory.
Example:

x = [1, 2, 3]
x = None # The previous list [1, 2, 3] is eligible for garbage collection
4.5 Constants
Constants are the Fixed values such as numbers, letters, and strings, are called “constants”
because their value does not change

Numeric constants are as you expect


String constants use single quotes (') or double quotes (")

print(123)
# output : 123
print(98.6)
# output : 98.6
print('Hello world')
# output : Hello world

Conventionally written in uppercase letters.


Example:

PI = 3.14159
GRAVITY = 9.8

4.6 Data Type of a Variable


Use the type() function to determine the data type of a variable.
Example:

a=5
print(type(a)) # Output: <class 'int'>

4.7 Data Types in Python


4.7.1 Why are Data Types Important?

Data types specify the kind of data a variable can hold.


They determine what operations can be performed on the data.

4.7.2 Common Data Types**

Type Description Example


NoneType Represents the absence of a value. a = None

Numbers Includes integers, floats, and complex numbers. x = 42 , y = 3.14 , z =


2+3j

Boolean Represents True or False . flag = True

List Ordered and mutable collection. lst = [1, 2, 3]

Tuple Ordered and immutable collection. tpl = (1, 2, 3)

Set Unordered collection of unique elements. s = {1, 2, 3}

String Sequence of characters. name = "Alice"

Range Immutable sequence of numbers. range(start, stop, range(5) , range(5,-1,+1)


step)

Dictionary Collection of key-value pairs. d = {'a': 1, 'b': 2}

1. None Type

Represents a null value.


Example:

a = None
print(type(a)) # Output: <class 'NoneType'>

2. Numbers

Integer ( int ): Whole numbers.


Example:

x = 10
Float ( float ): Decimal numbers.
Example:

y = 3.14

Note : There is a Type Called Complex, won't go through .

3. Booleans

Represents truth values: True or False .


Example:

is_valid = True

4. Sequence Data Types

Type Description Example


List Mutable, ordered collection. lst = [1, 2, 3]

Tuple Immutable, ordered collection. tpl = (1, 2, 3)

String Immutable sequence of characters. name = "Python"

Range Immutable sequence of numbers. range(5)

5. Sets

Unordered collection of unique elements.


Example:

s = {1, 2, 3, 3} # Output: {1, 2, 3}

6. Dictionary
Collection of key-value pairs.
Example:

d = {'name': 'Alice', 'age': 25}

4.8 Type Conversion


Convert data from one type to another using built-in functions.
Examples:

a = 5.6
b = int(a) # Converts float to int
c = str(b) # Converts int to string

4.9 Swapping Two Variables in Python


# Example with strings
a = "hello"
b = "world"

# Using a temporary variable


temp = a
a=b
b = temp

print("a =", a) # Output: a = world


print("b =", b) # Output: b = hello

4.10 Using Previous Outputs:


Python allows you to use the output of a previous operation by using the underscore
( _ ) variable.
Example:
_ + y # Previous output (5) + y (3)
# Result: 8

4.11 Literals
Literals are constant values assigned to constant variables.

5. User Input in Python


5.1 How to Get User Input
Python provides a straightforward way to get user input using the input() function. This
function displays a prompt message to the user and waits for their response.

Example

name = input("Please enter your name: ")


x = input("Enter the first number: ")
y = input("Enter the second number: ")
z = x + y # Concatenates the strings
print(z)

5.2 The input() Function


The input() function reads user input as a string.
Prompt Message: The string argument provided to input() is displayed to the user.
Usage:

name = input("Enter your name: ")


print("Welcome,", name)

Important Note
The input() function always returns a string. To perform operations like arithmetic, you
need to convert the input using type-casting functions such as int() or float() .

5.3 Types of Input Data


By default, all input values are strings. You can convert the input into other types if needed.

Example: Converting to Integer

x = input("Enter a number: ") # Input is a string


a = int(x) # Convert the string to an integer
print(a + 10) # Perform arithmetic

5.4 When to Use Index Values


If you want to extract only the first character of a user’s input, you can use indexing with the
result of the input() function.

Examples

ch = input("Enter a character: ")[0] # Direct indexing


print("First character:", ch)

5.5 The eval() Function


The eval() function evaluates the user input as a Python expression.
Usage

result = eval(input("Enter an expression: "))


print("Result:", result)

Example
If the user enters 3 + 4 * 2 , the eval() function calculates the result and returns 11 .

Important Note
Be cautious when using eval() with untrusted input, as it can execute arbitrary code and
pose a security risk.

5.6 Passing Values from the Command Line


Python allows you to pass arguments to scripts through the command line using the
sys.argv list.

How It Works

01. The first argument ( sys.argv[0] ) is the name of the script.


02. Subsequent arguments can be accessed using their indices.

Example: Command-Line Arguments


Suppose the script MyCode.py contains:

import sys

x = int(sys.argv[1]) # First argument


y = int(sys.argv[2]) # Second argument
print("Sum:", x + y)

To execute the script:

python3 MyCode.py 5 10
Output:

Sum: 15

6. Lists, Tuples, Sets, and Dictionaries in Python


6.1 Lists in Python
A. What is a List?

A list is a collection used to store multiple items in a single variable.


Defined using square brackets [] .
Lists are mutable, meaning their values can be changed.

B. Key Features:

01. Indexing and Slicing:


Indexing starts from 0.
Negative indexing is also supported.
Example:

my_list = [10, 20, 30, 40, 50]


my_list[0] # Output: 10
my_list[-1] # Output: 50
my_list[1:4] # Output: [20, 30, 40]

02. Mixed Data Types:


A list can store integers, strings, floats, or even other lists.
Example:

mixed_list = [1, "Hello", 3.14, [5, 6, 7]]

03. List of Strings:


Example:
names = ["Alice", "Bob", "Charlie"]

04. List of Lists:


Example:

nested_list = [[1, 2], [3, 4]]

6.2 Methods and Functions:

Method Description Example


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

clear() Removes all elements from the list. my_list.clear()

insert() Inserts an element at the specified position. my_list.insert(1, 15)


remove() Removes the first occurrence of the specified my_list.remove(20)
element.
pop() Removes and returns the element at the specified my_list.pop(2)
index.
extend() Extends the list by appending elements from my_list.extend([70,
another list. 80])
sort() Sorts the list in ascending order. my_list.sort()

count() Counts the occurrences of an element. (1, 2, 2).count(2) → 2

index() Returns the index of the first occurrence. (1, 2, 3).index(2) → 1


add() Adds an element to the set. my_set.add(4)

union() Returns a new set with elements from both sets. set1.union(set2)

intersection() Returns a set with common elements from both set1.intersection(set2)


sets.

Built-in Functions:
min() : Returns the smallest value.
Example: min([10, 20, 30]) → 10
max() : Returns the largest value.
Example: max([10, 20, 30]) → 30
sum() : Returns the sum of all values.
Example: sum([10, 20, 30]) → 60

6.3 Tuples in Python


A. What is a Tuple?

A tuple is similar to a list but immutable (cannot be modified).


Defined using parentheses () .

B. Key Features:

01. Accessing Elements:


Use indexing like lists.
Example:

my_tuple = (10, 20, 30)


my_tuple[1] # Output: 20

02. Immutable Nature:


Example:

my_tuple[0] = 15 # Error: Tuples do not support item assignment

C. When to Use Tuples?

Use tuples when you need a collection of values that should not change.
Tuples are faster to iterate than lists.

6.4 Sets in Python


A. What is a Set?

A set is a collection of unique elements.


Defined using curly braces {} .
B. Key Features:

01. Unique Elements:


Duplicates are automatically removed.
Example:

my_set = {1, 2, 2, 3}
print(my_set) # Output: {1, 2, 3}

02. No Indexing:
Sets do not support indexing or slicing.
03. Unordered:
The order of elements is not guaranteed.

6.5 Dictionaries in Python


A. What is a Dictionary?

A dictionary is a collection of key-value pairs.


Defined using curly braces {} .

B. Key Features:

01. Keys and Values:


Keys must be unique and immutable (e.g., strings, numbers).
Values can be of any data type.
Example:

my_dict = {1: 'Alice', 2: 'Bob'}

02. Accessing Values:


Use the key to fetch a value.
Example:
my_dict[1] # Output: 'Alice'

03. Using get() :


Avoids errors if the key doesn’t exist.
Example:

my_dict.get(3, 'Not Found') # Output: 'Not Found'

C. Adding and Removing Elements:


01. Add/Update a Value:

my_dict['Charlie'] = 'Python'

02. Delete a Value:

del my_dict['Bob']

D. Nested Dictionaries:
Dictionaries can contain other dictionaries.
Example:

nested_dict = {
'Python': ['PyCharm', 'VS Code'],
'Java': {'JSE': 'NetBeans', 'JEE': 'Eclipse'}
}
nested_dict['Java']['JEE'] # Output: 'Eclipse'

E. Combining Lists into a Dictionary:


Use the zip() method.
Example:
keys = ['Name', 'Age']
values = ['Alice', 25]
my_dict = dict(zip(keys, values))

7. Conditional Statements in Python


7.1 If Statement
The if statement is the most basic form of conditional execution in Python. It evaluates a
condition and executes a block of code if the condition is true.

Syntax:

if condition:
statement

How It Works:

The condition is evaluated.


If the condition is true, the indented block (or suite) of code is executed.
If the condition is false, the block is skipped.

Example:

age = 18
if age >= 18:
print("You are eligible to vote.")

In this case, since the condition ( age >= 18 ) is true, the message "You are eligible to vote."
will be printed.

7.2 Indentation in Python


Python uses indentation to define blocks of code that belong to conditional statements,
loops, functions, etc. Proper indentation is crucial for Python code to execute correctly.
What is Indentation?
Indentation refers to the spaces or tabs at the beginning of a line of code. It helps the
interpreter understand which statements belong to a specific block of code.

Indentation improves code readability.


Python uses indentation (instead of braces {} ) to define the scope of code blocks.

Example:

x=5
if x > 3:
print("x is greater than 3") # This statement is part of the if block

The print() statement is indented, indicating it belongs to the if block.

7.3 Else Block


An else block can be used in conjunction with an if statement. The else block runs when
the condition in the if statement is false.

How It Works:

If the if condition evaluates to false, the else block will execute.


If the if condition is true, the else block is skipped.

Syntax:

if condition:
statement1
else:
statement2

Example:

age = 16
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")

Since the condition age >= 18 is false, the else block will execute, printing "You are not
eligible to vote."

7.4 Nested If and Else Statements


Python allows you to nest if and else statements inside one another. This is useful when
you need to evaluate multiple conditions at different levels.

Syntax:

if condition1:
if condition2:
statement1
else:
statement2
else:
statement3

Example:

x = 10
y=5

if x > 5:
if y > 3:
print("Both conditions are true.")
else:
print("The second condition is false.")
else:
print("The first condition is false.")

If x > 5 is true, Python will check the second condition y > 3 and print the appropriate
message based on the result.
7. 5 if , elif , and else Statements
elif stands for "else if," and it is used to check multiple conditions in a chain. You can use if ,
elif , and else to check different conditions and execute different blocks of code based on
the first condition that evaluates to true.

Syntax:

if condition1:
statement1
elif condition2:
statement2
else:
statement3

How It Works:

The if statement is evaluated first.


If the if condition is false, the elif statement(s) are evaluated in order.
If none of the if or elif conditions are true, the else block is executed.

Example:

x = 15

if x > 20:
print("x is greater than 20")
elif x > 10:
print("x is greater than 10 but less than or equal to 20")
else:
print("x is less than or equal to 10")

In this example:

Since x is 15, the second condition ( x > 10 ) is true, so the message "x is greater than 10
but less than or equal to 20" will be printed.

Summary of Key Concepts


if , elif , else statements: Control the flow of code based on conditions.
Indentation: Crucial for defining blocks of code.
Nested statements: Allow for complex decision-making logic.

8. Loops in Python
8.1 For Loop
A for loop is used to iterate over a sequence (such as a list, tuple, string, or range). It is
commonly used when the number of iterations is predetermined or when iterating over
collections of data.

A. Syntax of a For Loop

for variable in iterable:


# Statements to execute for each iteration

B. Key Points About For Loops

The iterable can be a sequence (e.g., list, tuple, string) or an object that implements the
__iter__ method.
For each iteration, the variable takes the value of the next item in the iterable.
The loop ends when there are no items left to iterate over.

C. Example: Iterating Over a Range

for i in range(1, 6): # Iterates from 1 to 5 (inclusive)


print("Iteration:", i)

Output:

Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5

D. Example: Iterating Over a String

text = "Python"
for char in text:
print(char, end=" ")

Output:

Python

8.2 While Loop


The while loop executes a block of code repeatedly as long as the specified condition is
true. It is commonly used when the number of iterations depends on a condition rather than
being predetermined.

A. Syntax of a While Loop

while condition:
# Statements to execute
# Update the condition

B. Key Points About While Loops**

The condition is checked before each iteration.


If the condition is False at the start, the loop body will not execute even once.
Ensure the condition eventually becomes False to avoid an infinite loop.

C. Example: Basic While Loop**

i=1
while i <= 5:
print("Count:", i)
i += 1

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

For vs. While Loops

Feature For Loop While Loop


Use Case Known number of iterations Unknown or condition-based
iterations
Syntax Simplicity Compact and easy for Requires manual condition setup
iterables
Termination Automatic based on the Needs a condition and update
iterable
Example Use Iterating through a list Waiting for user input
Case

8.3 For-Else and While-Else Loops


In Python, both for and while loops support an else clause. The else block executes only if
the loop terminates normally (i.e., not due to a break statement).

A. For-Else Example

for i in range(1, 6):


if i == 3:
print("Breaking the loop")
break
print(i)
else:
print("Loop completed without a break")

Output:

1
2
Breaking the loop

In this example, the else block does not execute because the loop is terminated using
break .

B. While-Else Example

i=1
while i <= 5:
if i == 3:
print("Breaking the loop")
break
print(i)
i += 1
else:
print("Loop completed without a break")

Output:

1
2
Breaking the loop

8.4 Control Flow Statements in Loops


Control flow statements like break, continue, and pass modify the behavior of loops.

A. break Statement
Exits the loop prematurely when a condition is met.
for i in range(1, 6):
if i == 3:
break
print(i)

Output:

1
2

B. continue Statement
Skips the rest of the code for the current iteration and moves to the next iteration.

for i in range(1, 6):


if i == 3:
continue
print(i)

Output:

1
2
4
5

C. pass Statement
Does nothing and serves as a placeholder.

for i in range(1, 6):


if i == 3:
pass
else:
print(i)

Output:
1
2
4
5

8.5 Nested Loops


You can nest one loop inside another to handle multi-dimensional data or perform complex
operations.

Example: Nested For Loop

for i in range(1, 4):


for j in range(1, 4):
print(f"({i}, {j})", end=" ")
print()

Output:

(1, 1) (1, 2) (1, 3)


(2, 1) (2, 2) (2, 3)
(3, 1) (3, 2) (3, 3)

Using Loops for Pattern Printing

1.Printing a Square

n=4
for i in range(n):
for j in range(n):
print("#", end=" ")
print()

Output:
####
####
####
####

2 .Printing a Right Triangle

n=5
for i in range(1, n + 1):
for j in range(1, i + 1):
print("*", end=" ")
print()

Output:

*
**
***
****
*****

9. Arrays
9.1 What is an Array?
An array is a data structure that stores multiple elements of the same data type.
Unlike lists, arrays enforce type consistency, meaning all elements must belong to the
same type.
Arrays in Python are dynamic, meaning their size can increase or decrease during
program execution.
To use arrays in Python, you must import the array module:

import array as arr

When creating an array, you must specify two things:


01. Type Code: Defines the data type of the array.
02. Values: The elements you want to store in the array.

9.2 Type Codes


The type code defines the type of data stored in an array and determines memory
allocation for each element.

Type Code Type Size (bytes) Example


'i' Signed integer 2 or 4 -5, 0, 10

'I' Unsigned integer 2 or 4 0, 5, 10


'f' Floating-point 4 1.5, -2.3

'd' Double-precision float 8 2.34, -5.67

9.3 Creating Arrays


Static Array Example
You can create an array with predefined values:

import array as arr

# Create an array of integers


my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array) # Output: array('i', [1, 2, 3, 4, 5])

Dynamic Array Example (Adding Elements)


You can create an empty array and add values dynamically using the append() function:

# Create an empty array


dynamic_array = arr.array('i', [])

# Add elements to the array


dynamic_array.append(10)
dynamic_array.append(20)
print(dynamic_array) # Output: array('i', [10, 20])

9.4 Common Array Functions


Here’s a list of commonly used array functions with descriptions:

Function Description Example


append(x) Adds an element x to the end of the array. my_array.append(6)

insert(i, x) Inserts x at index i . my_array.insert(2, 10)


pop([i]) Removes and returns the element at index my_array.pop() or
i (last if not specified). my_array.pop(2)

remove(x) Removes the first occurrence of x . my_array.remove(3)

index(x) Returns the index of the first occurrence of my_array.index(2)


x.

reverse() Reverses the array in place. my_array.reverse()

buffer_info() Returns the address and size of the array. my_array.buffer_info()


typecode Returns the type code of the array. print(my_array.typecode)

len() Returns the number of elements in the print(len(my_array))


array.

9.5 Indexing in Arrays


Array elements can be accessed using their index, starting at 0 . Negative indexing is also
supported to access elements from the end:

# Accessing elements
print(my_array[1]) # Output: 2
print(my_array[-1]) # Output: 5

9.6 Fetching All Values of an Array


You can retrieve all elements of an array using loops:
# Using a for loop
for value in my_array:
print(value, end=" ") # Output: 1 2 3 4 5

# Using a while loop


index = 0
while index < len(my_array):
print(my_array[index])
index += 1

9.8 Creating Arrays Through User Input


You can create arrays dynamically by accepting user input:

import array as arr

# Create an empty array


user_array = arr.array('i', [])

# Get the size of the array


size = int(input("Enter the number of elements: "))

# Add elements to the array


for _ in range(size):
element = int(input("Enter an element: "))
user_array.append(element)

print("Array created:", user_array)

9.9 Searching for Elements in an Array


Using Loops
A loop can be used to find the index of an element:

search_element = 3
for index, value in enumerate(my_array):
if value == search_element:
print(f"Element found at index {index}")
break
else:
print("Element not found")

Using index() Function


The index() function is a simpler way to locate an element:

print(my_array.index(3)) # Output: 2

9.10 Example: Array Operations


import array as arr

# Create an array
my_array = arr.array('i', [1, 2, 3, 4, 5])

# Add elements
my_array.append(6)
my_array.insert(2, 10)

# Remove elements
my_array.remove(3)

# Reverse the array


my_array.reverse()

# Print details
print("Array:", my_array)
print("Length:", len(my_array))
print("Buffer Info:", my_array.buffer_info())

9.11 Nested Example: User Input and Search


import array as arr
# Create an empty array
user_array = arr.array('i', [])

# Take input for the array


size = int(input("Enter the number of elements: "))
for _ in range(size):
element = int(input("Enter an element: "))
user_array.append(element)

# Search for an element


search_element = int(input("Enter the element to search: "))
if search_element in user_array:
print(f"Element {search_element} found at index {user_array.index(search_element)}")
else:
print("Element not found")

10. NumPy
Introduction to NumPy
NumPy (Numerical Python) is a powerful library in Python used for working with arrays,
linear algebra, Fourier transforms, and matrices. It provides high-performance,
multidimensional array objects and tools for working with these arrays efficiently.

Creating Arrays in NumPy


NumPy provides six main methods for creating arrays:

1. array()
Creates an array from a list, tuple, or iterable.
You can specify the data type explicitly or let NumPy infer it.

from numpy import array

# Create an integer array


arr = array([1, 2, 3, 4, 5])
print(arr) # Output: [1 2 3 4 5]
print(arr.dtype) # Output: int32
# Create an array with mixed types (converts to float automatically)
arr = array([1, 2, 3, 4, 5.0])
print(arr.dtype) # Output: float64

# Explicitly specify the data type


arr = array([1, 2, 3, 4, 5], float)
print(arr.dtype) # Output: float64

2. linspace()
Generates evenly spaced values over a specified range.
By default, divides the range into 50 parts unless a different number is specified.

Syntax:

linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

from numpy import linspace

# Generate 16 values between 0 and 15


arr = linspace(0, 15, 16)
print(arr)

# Default behavior (50 parts)


arr = linspace(0, 15)
print(arr)

3. arange()
Similar to Python’s built-in range() , but returns a NumPy array.
Allows non-integer steps.

Syntax:

arange(start, stop, step, dtype=None)


from numpy import arange

# Generate an array with step size 2


arr = arange(1, 15, 2)
print(arr)

# Using a floating-point step


arr = arange(1, 15, 2.5)
print(arr)

4. logspace()
Generates numbers spaced evenly on a logarithmic scale.
Base 10 is used by default.

Syntax:

logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)

from numpy import logspace

# Generate an array with 5 values in log scale


arr = logspace(1, 40, 5)
print(arr)

5. zeros()
Creates an array filled with zeros.

Syntax:

zeros(shape, dtype=float, order='C')

from numpy import zeros


arr = zeros(5) # Creates an array with 5 zeros
print(arr)

6. ones()
Creates an array filled with ones.

Syntax:

ones(shape, dtype=float, order='C')

from numpy import ones

arr = ones(5) # Creates an array with 5 ones


print(arr)

# Specify integer type


arr = ones(4, int)
print(arr)

Array Operations
1. Adding Values to Arrays
Perform vectorized operations to add a scalar or another array to an array.

from numpy import array

arr = array([1, 2, 3, 4, 5])

# Add a scalar
arr = arr + 5
print(arr) # Output: [6 7 8 9 10]

# Add two arrays


arr2 = array([10, 20, 30, 40, 50])
result = arr + arr2
print(result) # Output: [16 27 38 49 60]
2. Mathematical Functions
Perform mathematical operations element-wise.

from numpy import sin, cos, log, sqrt

arr = array([1, 2, 3, 4])

print(sin(arr)) # Sine of each element


print(cos(arr)) # Cosine of each element
print(log(arr)) # Natural logarithm of each element
print(sqrt(arr)) # Square root of each element

3. Aggregation Functions
sum() , min() , and max() can be used to calculate the total, minimum, and maximum
values.

print(arr.sum()) # Sum of all elements


print(arr.min()) # Minimum value
print(arr.max()) # Maximum value

Array Copying and Aliasing


1. Aliasing
When two variables reference the same array, modifying one will affect the other.

arr1 = array([1, 2, 3])


arr2 = arr1 # Both reference the same array

arr2[0] = 10
print(arr1) # Output: [10 2 3]

2. Shallow Copy
Use view() to create a shallow copy.
arr1 = array([1, 2, 3])
arr2 = arr1.view() # Separate memory location

arr2[0] = 10
print(arr1) # Output: [ 1 2 3]
print(arr2) # Output: [10 2 3]

3. Deep Copy
Use copy() to create an independent copy.

arr1 = array([1, 2, 3])


arr2 = arr1.copy() # Independent copy

arr2[0] = 10
print(arr1) # Output: [1 2 3]
print(arr2) # Output: [10 2 3]

Multidimensional Arrays
1. Two-Dimensional Arrays
A two-dimensional array is an array of arrays.

from numpy import array

arr = array([
[1, 2, 3],
[4, 5, 6]
])

print(arr.ndim) # Number of dimensions (2)


print(arr.shape) # Shape (2, 3)
print(arr.size) # Total elements (6)

2. Reshaping Arrays
Use reshape(r, c) to change the dimensions.

arr = array([1, 2, 3, 4, 5, 6])


arr2 = arr.reshape(2, 3) # 2 rows, 3 columns
print(arr2)

Matrices
Creating Matrices
Convert an array to a matrix using matrix() .

from numpy import matrix

mat = matrix('1 2 3; 4 5 6')


print(mat)

Matrices in NumPy
A matrix is a specialized 2D array where rows and columns are explicitly defined. NumPy
provides the matrix() function to create and manipulate matrices. Matrices are particularly
useful in mathematical computations, especially in linear algebra.

Creating a Matrix
There are several ways to create a matrix in NumPy:

01. Using the matrix() Function


You can directly create a matrix using the matrix() function by providing the
elements in a string format. Rows are separated by semicolons ( ; ), and elements in
each row are separated by spaces.

from numpy import matrix

mat = matrix('1 2 3; 4 5 6; 7 8 9')


print(mat)
# Output:
# [[1 2 3]
# [4 5 6]
# [7 8 9]]

02. Converting an Array to a Matrix


If you already have a 2D array, you can convert it into a matrix using the matrix()
function.

from numpy import array, matrix

arr = array([[1, 2], [3, 4]])


mat = matrix(arr)
print(mat)

03. Creating a Matrix of Zeros, Ones, or Random Numbers


Use NumPy’s zeros() , ones() , or random functions and then convert them into a
matrix.

from numpy import zeros, ones, matrix


from numpy.random import rand

zero_matrix = matrix(zeros((2, 2)))


one_matrix = matrix(ones((3, 3)))
random_matrix = matrix(rand(3, 3)) # Random values in the range [0, 1]

print(zero_matrix)
print(one_matrix)
print(random_matrix)

Attributes of a Matrix
Shape: Returns the dimensions of the matrix as (rows, columns) .
Size: Total number of elements in the matrix.
Transpose: Flips rows and columns using .T .
Data Type: Use .dtype to find the data type of the elements.
from numpy import matrix

mat = matrix('1 2 3; 4 5 6')


print("Shape:", mat.shape) # Output: (2, 3)
print("Size:", mat.size) # Output: 6
print("Transpose:\n", mat.T) # Flips rows and columns
print("Data Type:", mat.dtype) # Output: int32

Matrix Arithmetic Operations


01. Addition and Subtraction
Matrices of the same shape can be added or subtracted element-wise.

mat1 = matrix('1 2 3; 4 5 6')


mat2 = matrix('6 5 4; 3 2 1')

print("Addition:\n", mat1 + mat2)


print("Subtraction:\n", mat1 - mat2)

02. Matrix Multiplication


Use the * operator or the dot() method for matrix multiplication.

mat1 = matrix('1 2; 3 4')


mat2 = matrix('5 6; 7 8')

print("Matrix Multiplication:\n", mat1 * mat2) # Element-wise multiplication


print("Using dot():\n", mat1.dot(mat2)) # Dot product

03. Element-wise Operations


Perform element-wise multiplication or division using the multiply() function.

from numpy import multiply

mat1 = matrix('1 2; 3 4')


mat2 = matrix('2 3; 4 5')
print("Element-wise Multiplication:\n", multiply(mat1, mat2))

Special Matrices
01. Identity Matrix
Use the identity() function to create a square identity matrix.

from numpy import identity

identity_matrix = identity(3)
print(identity_matrix)

02. Diagonal Matrix


Use the diag() function to create or extract diagonal elements.

from numpy import diag

diag_matrix = diag([1, 2, 3]) # Creates a diagonal matrix


print(diag_matrix)

mat = matrix('1 2 3; 4 5 6; 7 8 9')


print("Diagonal Elements:", diag(mat)) # Extracts diagonal elements

03. Zero and Ones Matrix


Use the zeros() and ones() functions as shown earlier.

Matrix Functions
Here are some commonly used functions in matrix operations:

Function Description
transpose() Transposes the matrix (flips rows and columns).
sum() Computes the sum of all elements or along a specific axis.
mean() Returns the average of the elements in the matrix.
Function Description
min() Finds the smallest value in the matrix.
max() Finds the largest value in the matrix.
det() Computes the determinant of a square matrix.
inv() Computes the inverse of a square matrix.
dot() Performs matrix multiplication.
eig() Computes the eigenvalues and eigenvectors of a matrix.

Example: Determinant and Inverse of a Matrix


To perform advanced linear algebra operations, you can use NumPy’s linalg module.

from numpy.linalg import det, inv

# Define a square matrix


mat = matrix('1 2; 3 4')

# Determinant
print("Determinant:", det(mat))

# Inverse
print("Inverse:\n", inv(mat))

Matrix Reshaping
Reshape a 1D array or matrix into a new shape using reshape() .
Be cautious about matching the total number of elements in the old and new shapes.

arr = matrix('1 2 3 4 5 6')


reshaped_matrix = arr.reshape(2, 3) # 2 rows, 3 columns
print(reshaped_matrix)

Matrix Multiplication Rules


When multiplying two matrices:
01. The number of columns in the first matrix must match the number of rows in the
second matrix.
02. The resulting matrix will have dimensions (rows of the first matrix) x (columns of the second
matrix) .

mat1 = matrix('1 2; 3 4')


mat2 = matrix('5 6; 7 8')

# Valid multiplication
result = mat1 * mat2
print(result)

11. Functions
11.1 Introduction to Functions
A function is a block of code designed to perform a specific task.
Functions help in breaking down complex problems into smaller, manageable pieces,
making code more readable, reusable, and easier to debug.
Functions can take input arguments, execute operations, and optionally return values.

Syntax for Defining a Function:

def function_name(parameters):
# Function body
return value # Optional

Example:

def add_numbers(a, b):


sum = a + b
return sum

result = add_numbers(4, 5)
print(result) # Output: 9

Functions must be called to execute. Without calling, they will not run.
11.2 What are Arguments in Python?
Arguments are the values passed to a function when it is called.
Formal Arguments: Variables used in the function definition.
Actual Arguments: Values passed during the function call.

Example:

def greet(name): # 'name' is the formal argument


print(f"Hello, {name}")

greet('Alice') # 'Alice' is the actual argument

11.2 Return Statement in Python


The return statement is used to:
Exit a function.
Return a value to the caller.
A function without a return statement returns None by default.

Example:

def square(x):
return x * x

result = square(4)
print(result) # Output: 16

A function can also return multiple values as a tuple:

def add_sub(x, y):


return x + y, x - y

result1, result2 = add_sub(10, 5)


print(result1, result2) # Output: 15, 5
11.3 Passing Arguments in Python
Python uses a "pass by object reference" mechanism.

Immutable Objects (e.g., int , float , str ): Changes inside the function do not affect the
original object.
Mutable Objects (e.g., list , dict ): Changes inside the function affect the original
object.

Example with Mutable Object:

def update(lst):
print("Before modification:", lst)
lst[1] = 25
print("After modification:", lst)

my_list = [10, 20, 30]


update(my_list)
print(my_list) # Output: [10, 25, 30]

Explanation:

The list's memory reference is passed to the function, so changes inside the function
modify the original list.

11.4 Types of Actual Arguments


4.1. Positional Arguments

The order of arguments during the function call should match the parameter order in
the definition.

def person(name, age):


print(f"Name: {name}, Age: {age}")

person('Alice', 30) # Output: Name: Alice, Age: 30

4.2 Keyword Arguments


Argument names are explicitly mentioned during the function call.
The order of arguments can vary.

def person(name, age):


print(f"Name: {name}, Age: {age}")

person(age=30, name='Alice') # Output: Name: Alice, Age: 30

4.3. Default Arguments

Parameters can have default values in the function definition.


These arguments become optional during the function call.

def person(name, age=25):


print(f"Name: {name}, Age: {age}")

person('Alice') # Output: Name: Alice, Age: 25


person('Bob', age=40) # Output: Name: Bob, Age: 40

Rule: Default arguments must come after non-default arguments.

4.5 Variable-Length Arguments

A function can accept an arbitrary number of arguments using *args for positional
arguments and **kwargs for keyword arguments.

Example with *args :

def sum_numbers(a, *b):


total = a
for num in b:
total += num
print(f"Total: {total}")

sum_numbers(5, 10, 15, 20) # Output: Total: 50

Example with `kwargs`**:


def display_info(**info):
for key, value in info.items():
print(f"{key}: {value}")

display_info(name='Alice', age=30, city='New York')


# Output:
# name: Alice
# age: 30
# city: New York

Here’s how you can organize your notes for Section 11.5 Scope of Variables based on the
lecture content:

11.5 Scope of Variables


5.1 Scope of Variables
The scope of a variable refers to the region in the program where the variable can be
accessed.
There are two types of variable scope:
01. Local Scope: Variables defined inside a function.
02. Global Scope: Variables defined outside a function.

5.2 Local Variables


A local variable is a variable defined inside a function.
It can only be accessed within the function where it is defined.
Local variables are destroyed once the function completes execution.
Local variables have higher priority over global variables with the same name.

Example:

def func():
x = 10 # Local variable
print(x)
func() # Output: 10

Here, x is a local variable and can only be accessed inside the func() function.

5.3 Global Variables


A global variable is a variable defined outside a function.
It can be accessed both inside and outside functions.
Global variables retain their value throughout the program's execution.

Example:

x = 10 # Global variable

def func():
print(x) # Accessing global variable

func() # Output: 10

Here, x is a global variable and can be accessed inside the func() function.

5.4 Global Keyword


The global keyword is used to access and modify a global variable inside a function.
Without the global keyword, a new local variable is created instead of modifying the
global variable.

Example:

x = 10 # Global variable

def func():
global x # Using global keyword
x = 15 # Modifying global variable
print(x)
func() # Output: 15
print(x) # Output: 15 (global variable is modified)

Here, the global keyword ensures that the global variable x is modified inside the
function.

5.5 Global() Function


The global() function is used to access and modify global variables inside a function.
It returns a dictionary of all global variables, which can be accessed using the variable
name as a key.

Example:

x = 10 # Global variable

def func():
x = 15 # Local variable
print("Local x:", x) # Output: Local x: 15

y = globals()['x'] # Accessing global variable


print("Global x:", y) # Output: Global x: 10

globals()['x'] = 20 # Modifying global variable

func()
print("Updated Global x:", x) # Output: Updated Global x: 20

Here, globals()['x'] is used to access and modify the global variable x inside the
function.

11.6 Anonymous Functions (Lambda Functions)


A lambda function is a small, anonymous function defined using the lambda keyword.
Syntax: lambda arguments: expression

Example:
square = lambda x: x * x
print(square(5)) # Output: 25

add = lambda x, y: x + y
print(add(3, 7)) # Output: 10

11.7 Recursion in Python


Recursion is a fundamental programming technique where a function calls itself to solve
smaller instances of a larger problem. It is especially useful for tasks that can be broken
down into similar sub-problems, such as mathematical computations or data structure
traversal. In this section, we will explore recursion, its limitations, and its practical
applications in Python.

7.1 What is Recursion?


Recursion occurs when a function calls itself either directly or indirectly. For example, a
function calculating a factorial can call itself with a smaller value until it reaches the base
case.

7.2 Key Features of Recursion


10. Calling a Function Within Itself

Recursion allows a function to call itself to repeat a task.

11. Infinite Recursion Without a Base Case

Without a condition to terminate the recursion, the program will continue calling itself
indefinitely, eventually leading to a crash.

12. Default Recursion Limit in Python

Python has a default recursion limit of 1000, preventing excessive function calls that
can lead to system crashes.

13. Modifying Recursion Limit


You can change the recursion limit using the sys module:

import sys
sys.setrecursionlimit(2000) # Set the recursion limit to 2000

14. Accessing the Current Limit

Use sys.getrecursionlimit() to check the current recursion limit.

7.3 Example: Factorial Calculation


Let’s calculate the factorial of a number using recursion:

def factorial(n):
if n == 0:
return 1 # Base case: factorial of 0 is 1
else:
return n * factorial(n - 1) # Recursive call

result = factorial(5)
print(result) # Output: 120

Explanation:

The base case ensures that the recursion stops when n reaches 0 .
Each recursive call computes a smaller factorial until the base case is reached.

7.4 Changing Recursion Limit


Python allows you to customize the recursion limit using the sys module.

import sys

# Check the current recursion limit


print("Default recursion limit:", sys.getrecursionlimit()) # Output: 1000

# Set a new recursion limit


sys.setrecursionlimit(2000)
print("New recursion limit:", sys.getrecursionlimit()) # Output: 2000

7.5 Practical Applications of Recursion


15. Breaking Down Complex Problems:
Recursion simplifies the solution of complex problems by dividing them into smaller,
manageable sub-problems.
16. Tree and Graph Traversal:
Algorithms like Depth-First Search (DFS) and Preorder Traversal use recursion.
17. Mathematical Computations:
Tasks like finding the greatest common divisor (GCD) or solving the Tower of Hanoi
problem use recursion effectively.

7.6 Best Practices


18. Always Define a Base Case

Ensure your recursive function has a clear stopping condition to prevent infinite
recursion.

19. Optimize for Performance

Recursive functions can be slower and consume more memory compared to iterative
solutions. Use them only when they simplify the logic.

20. Use Global Variables Carefully

If a variable is defined outside the recursive function, declare it as global to modify it


inside the function.

7.7 Example: Fibonacci Series Using Recursion

def fibonacci(n):
if n <= 1:
return n # Base cases: fibonacci(0) = 0, fibonacci(1) = 1
else:
return fibonacci(n - 1) + fibonacci(n - 2) # Recursive calls

n=6
for i in range(n):
print(fibonacci(i), end=" ") # Output: 0 1 1 2 3 5

Advantages of Recursion
Simplifies the logic for solving problems with a repetitive structure.
Reduces code complexity for problems like tree traversals or divide-and-conquer
algorithms.

Disadvantages of Recursion
Can lead to stack overflow if not implemented correctly.
Less memory-efficient compared to iterative solutions due to maintaining a call stack.

12. Exception Handling in Python


12.1 Understanding Errors in Python
Errors are an inevitable part of programming, and Python categorizes them into three main
types:

21. Compile-Time Errors:

These occur due to syntax mistakes, like missing colons or improper indentation.
Detected before the code is executed.
Example:

if x > 5 # Missing colon


print("Error!")

22. Logical Errors:


Happen when the code runs without syntax issues, but the output is incorrect due to a
flawed logic.
Example:

a, b = 5, 10
print("Sum:", a * b) # Incorrect logic (should be addition, not multiplication)

23. Runtime Errors:

Arise during code execution, typically caused by unforeseen user input or external
factors.
Example:

a = 10 / 0 # Division by zero raises a runtime error

12.1 Graceful Handling of Runtime Errors


Runtime errors can crash a program, which is undesirable, especially for critical
applications. Python provides tools like try-except blocks to handle such errors gracefully.

Try-Except Block
The try block contains code that might raise an error, and the except block handles the
error. This approach ensures the program doesn’t terminate unexpectedly.

Example:

try:
a = 10 / int(input("Enter a number: "))
print("Result:", a)
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")

12.3 Assigning Exceptions to Variables


In Python, you can assign an exception to a variable to access detailed information about it.

Example:

try:
a = 10 / 0
except ZeroDivisionError as e:
print("Exception:", e)

Output:

Exception: division by zero

12.4 The Finally Block


The finally block guarantees the execution of specific code, regardless of whether an
exception occurred. It is especially useful for tasks like closing files or releasing resources.

Example:

try:
f = open("example.txt", "r")
# Perform file operations
except FileNotFoundError:
print("File not found.")
finally:
print("Closing file.")
f.close() # Ensures the file is closed

12.5 Handling Multiple Exceptions


You can handle different exceptions separately using multiple except blocks, or use a
generic except block to catch all unforeseen errors.

Example:
try:
a = int(input("Enter a number: "))
b = 10 / a
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
except ValueError:
print("Error: Invalid input, please enter a number.")
except Exception as e:
print("An unexpected error occurred:", e)

Best Practices for Exception Handling


24. Anticipate potential errors:

Identify sections of your code that might raise exceptions.


Use try-except blocks to handle those cases.

25. Close resources properly:

Use the finally block to release resources like file handles or database connections.
Avoid resource leaks to ensure the stability of your application.

26. Be specific:

Handle specific exceptions with separate except blocks.


Use a generic except Exception block as a last resort.

13. Working with Files in Python


1. Reading Files
Opening a File
Before reading a file, you need to open it using the open() function. This function returns a
file handle, which is used to perform operations on the file.

fhand = open('mbox.txt', 'r')


filename : The name of the file (e.g., 'mbox.txt' ).
mode : The mode in which the file is opened. Use 'r' for reading.

Example:

fhand = open('mbox.txt', 'r')


print(fhand) # Output: <_io.TextIOWrapper name='mbox.txt' mode='r' encoding='UTF-8'>

File Handles
A file handle is an object that Python uses to interact with the file. It provides methods for
reading, writing, and closing the file.

Reading Line by Line


You can treat a file handle as a sequence of lines. Use a for loop to iterate through each line
in the file.

fhand = open('mbox.txt')
for line in fhand:
print(line) # Prints each line in the file

Reading the Entire File


You can read the entire content of a file into a single string using the read() method.

fhand = open('mbox.txt')
inp = fhand.read()
print(len(inp)) # Prints the total number of characters in the file
print(inp[:20]) # Prints the first 20 characters

Searching Through a File


You can search for specific lines in a file using conditions inside a for loop.
fhand = open('mbox.txt')
for line in fhand:
if line.startswith('From:'):
print(line)

Handling Missing Files


If a file does not exist, Python raises a FileNotFoundError . Use a try-except block to handle
this gracefully.

try:
fhand = open('missing.txt')
except FileNotFoundError:
print('File not found!')

2. Writing Files
Opening a File for Writing
To write to a file, open it in write mode ( 'w' ). If the file already exists, its contents will be
overwritten. If it doesn't exist, a new file will be created.

fout = open('output.txt', 'w')

Writing Data to a File


Use the write() method to add data to a file. This method returns the number of characters
written.

fout = open('output.txt', 'w')


fout.write('Hello, world!\n')
fout.close() # Always close the file after writing

Appending to a File
To add data to an existing file without overwriting it, open the file in append mode ( 'a' ).

fout = open('output.txt', 'a')


fout.write('This is a new line.\n')
fout.close()

3. Common File Operations


Counting Lines
You can count the number of lines in a file using a loop.

fhand = open('mbox.txt')
count = 0
for line in fhand:
count += 1
print('Line Count:', count)

Skipping Lines with continue


Use the continue statement to skip lines that don't meet certain criteria.

fhand = open('mbox.txt')
for line in fhand:
if not line.startswith('From:'):
continue
print(line)

Using in to Filter Lines


You can search for specific substrings within lines using the in keyword.

fhand = open('mbox.txt')
for line in fhand:
if '@uct.ac.za' in line:
print(line)
4. Error Handling
Handling Bad File Names
Always validate user input when dealing with file names to avoid errors.

fname = input('Enter the file name: ')


try:
fhand = open(fname)
except FileNotFoundError:
print('File cannot be opened:', fname)
quit()

5. Summary

Operation Description
Opening a File Use open(filename, mode) to open a file. Modes: 'r' (read), 'w'
(write), 'a' (append).
Reading Line by Line Use a for loop to iterate through each line in the file.
Reading the Entire Use read() to read the entire file into a string.
File
Writing to a File Use write() to add data to a file. Always close the file with close() .
Appending to a File Use mode 'a' to add data without overwriting the file.
Counting Lines Use a loop to count the number of lines in a file.
Skipping Lines Use continue to skip lines that don't meet certain criteria.
Searching for Use in to filter lines containing specific substrings.
Substrings
Error Handling Use try-except to handle missing files or invalid input.

6. Quick Reference Table


Method/Function Description
open(filename, mode) Opens a file and returns a file handle.
read() Reads the entire file content as a string.
readline() Reads a single line from the file.
write(data) Writes data to a file.
close() Closes the file.
rstrip() Removes trailing whitespace (including newlines) from a string.
startswith(prefix) Checks if a string starts with a specific prefix.
in Checks if a substring exists within a string.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy