Installation of Python and Python Notebook
What is Python?
Python is a high-level, interpreted, object-oriented programming language.
It’s easy to learn, free to use, and has a rich collection of libraries for data science, AI, web
development, and more.
Steps to Install Python:
1. Go to [Link]
2. Download the latest version (Python 3.x)
3. During installation:
o Check “Add Python to PATH”
o Click “Install Now”
4. Verify installation:
5. python --version
Output:
Python 3.12.3
Installing and Using Jupyter Notebook:
Jupyter Notebook is an interactive coding environment that lets you write code and see the
output immediately.
Installation:
pip install notebook
To open Jupyter Notebook:
jupyter notebook
Python Objects
Concept:
In Python, everything is an object (numbers, strings, lists, functions, etc.).
Every object has:
1. Type – the kind of object (int, str, list, etc.)
2. Value – the data it holds
3. Identity – unique memory address
Example:
x=5
print(type(x)) # <class 'int'>
print(id(x)) # unique identity number
Numbers and Booleans
Numeric Types:
Type Example Description
int 5, -10 Whole numbers
float 3.14, -2.5 Decimal numbers
complex 2+3j Complex numbers
Example:
a = 10
b = 2.5
c = 3 + 4j
print(a + b) # 12.5
print([Link]) # 3.0
print([Link]) # 4.0
Boolean Type:
Booleans have only two values: True or False
x = (10 > 5)
print(x) # True
Strings
Strings are sequences of characters enclosed in quotes.
Examples:
s = "Python"
print(s[0]) # P
print(s[-1]) # n
print(s[1:4]) # yth
Note: Strings are immutable, meaning their content cannot be changed.
Container Objects
Container objects hold multiple values.
Type Syntax Mutable Example
List [] Yes [1,2,3]
Tuple () No (1,2,3)
Set {} Yes {1,2,3}
Dictionary {key:value} Yes {'a':10}
Example:
lst = [1,2,3]
tpl = (4,5,6)
st = {7,8,9}
d = {'name':'Alice', 'age':20}
Mutability of Objects
• Mutable: Can be changed after creation (e.g., list, dict, set)
• Immutable: Cannot be changed (e.g., int, str, tuple)
Example:
lst = [1, 2, 3]
lst[0] = 10 # OK
s = "hello"
# s[0] = 'H' Error (strings are immutable)
Operators
Arithmetic Operators
Operator Example Result
+ 10 + 5 15
- 10 - 5 5
* 10 * 5 50
/ 10 / 3 3.333
// 10 // 3 3
% 10 % 3 1
** 2 ** 3 8
Bitwise Operators
Operate on binary bits:
Operator Example Meaning
& 5&3 AND
` ` 5
Operator Example Meaning
^ 5^3 XOR
~ ~5 NOT
<< 5 << 1 Left Shift
>> 5 >> 1 Right Shift
Comparison Operators
==, !=, >, <, >=, <=
a = 10
b=5
print(a > b) # True
Assignment Operators
=, +=, -=, *=, /=, %=, **=
x=5
x += 3 # 8
Operator Precedence and Associativity
Order of execution:
1. Parentheses ()
2. Exponentiation **
3. Multiplication, Division, Floor Division, Modulus
4. Addition, Subtraction
5. Comparisons
6. Logical operators
Example:
x = 10 + 2 * 5 # 20
y = (10 + 2) * 5 # 60
Conditional Statements
Used to perform decisions in code.
Example:
x = 10
if x > 15:
print("Greater than 15")
elif x == 10:
print("Equal to 10")
else:
print("Less than 10")
Loops
While Loop
i=1
while i <= 5:
print(i)
i += 1
For Loop
for i in range(1,6):
print(i)
1 Break and Continue Statements
Example:
for i in range(1, 6):
if i == 3:
continue # skip 3
if i == 5:
break # stop loop
print(i)
1 Range Function
Used to generate sequences of numbers.
print(list(range(5))) # [0,1,2,3,4]
print(list(range(2,10,2))) # [2,4,6,8]
UNIT II – STRING OBJECTS AND LIST OBJECTS
String Object Basics
• Strings are immutable sequences of characters.
• They support indexing and slicing.
Example:
s = "Python"
print(s[0]) # P
print(s[-1]) # n
print(s[1:4]) # yth
String Methods
Method Description Example
upper() Converts to uppercase "abc".upper() → "ABC"
lower() Converts to lowercase "ABC".lower() → "abc"
title() Capitalizes each word "hello world".title()
strip() Removes spaces " hello ".strip()
replace(a,b) Replaces substring "hello".replace("h","H")
find(sub) Finds substring index "python".find("th")
count(sub) Counts substring "banana".count("a")
startswith() Checks prefix "python".startswith("py")
endswith() Checks suffix "hello".endswith("o")
Splitting and Joining Strings
Splitting:
data = "apple,banana,grape"
lst = [Link](",")
print(lst) # ['apple', 'banana', 'grape']
Joining:
new_str = "-".join(lst)
print(new_str) # apple-banana-grape
String Formatting
Using .format()
name = "Amit"
age = 22
print("My name is {} and I am {} years old".format(name, age))
Using f-string
print(f"My name is {name} and I am {age} years old")
List Object Basics
Lists are mutable collections that can store mixed data types.
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple
fruits[1] = "mango"
print(fruits) # ['apple', 'mango', 'cherry']
List Methods
Method Description Example
append(x) Adds an element at the end [Link](5)
insert(i,x) Inserts element at index [Link](1,99)
remove(x) Removes element [Link](2)
pop() Removes last element [Link]()
sort() Sorts the list [Link]()
reverse() Reverses the list [Link]()
count(x) Counts occurrences [Link](10)
Example:
lst = [3,1,4,2]
[Link]()
print(lst) # [1,2,3,4]
List as Stack and Queue
Stack (LIFO – Last In First Out):
stack = []
[Link](10)
[Link](20)
print([Link]()) # 20
Queue (FIFO – First In First Out):
from collections import deque
q = deque([1,2,3])
[Link](4)
print([Link]()) # 1
List Comprehensions
A concise way to create lists.
Example 1:
squares = [x**2 for x in range(6)]
print(squares) # [0,1,4,9,16,25]
Example 2 (with condition):
even = [x for x in range(10) if x%2==0]
print(even) # [0,2,4,6,8]