• Simple syntax → easy model building
• Huge libraries → NumPy, Pandas,
Chapter: Advanced Python TensorFlow
– Detailed Descriptive Notes • Strong community support
• Fast prototyping
• Handles large datasets efficiently
1. Introduction to Jupyter Notebook
Definition:
5. Applications of Python
Jupyter Notebook is an open-source interactive
• Artificial Intelligence
computing environment that allows users to write
• Machine Learning
and execute Python code in small blocks called
• Web Development (Django, Flask)
cells, along with text, images, and visualizations.
• Game Development
Key Features:
• Automation Scripts
• Interactive execution (run code cell-by-cell)
• Data Science
• Supports Markdown (formatted notes)
• Cyber Security
• Used widely in Data Science & AI
• Allows visualization (graphs, charts)
6. Python Character Set
• Auto-saving of work
Definition:
Uses:
Character set refers to valid characters that can
• Teaching programming
be used in Python code.
• Data analysis
• Machine learning experiments
Types:
1. Letters → A–Z, a–z
2. Digits → 0–9
2. Introduction to Python
3. Special symbols → + - * / = () [] {}
Definition:
4. Whitespace → space, tab, newline
Python is a high-level, interpreted, general-
purpose programming language known for its
7. Python Statements
simplicity and readability.
Definition:
Example:
A statement is an instruction that Python
print("Hello World")
interpreter can execute.
Important Points:
• Developed by Guido van Rossum (1991)
Types of Statements
• Easy syntax similar to English
1. Single Line Statement
• No need to declare variable types
x = 10
3. Features of Python
2. Multi-line Statements
1. Simple and Easy to Learn
(a) Using Backslash ()
2. Interpreted Language (no compilation
total = 10 + 20 + 30 + \
required)
40 + 50
3. Platform Independent
(b) Using Parentheses ()
4. Object-Oriented Programming
total = (10 + 20 + 30 +
5. Large Standard Library
40 + 50)
6. Dynamic Typing
(c) Using Square Brackets []
7. Open Source
numbers = [1, 2, 3,
4, 5]
4. Why Python for AI?
Reasons:
8. Python Comments Constants are fixed values that do not change
Definition: during execution.
Comments are non-executable statements used Note:
to explain code. Python does not enforce constants but uses
Types: naming convention (uppercase).
1. Single-line Comment PI = 3.14
# This is a comment
2. Multi-line Comment 12. Data Types
''' Definition:
This is a multi-line comment Data type specifies the type of data stored in a
''' variable.
Data
Description Example
9. Identifiers and Keywords Type
Identifiers int Integer 10
Definition: float Decimal 10.5
Identifiers are names given to variables,
str String "Hello"
functions, classes, etc.
bool Boolean True
Rules:
• Cannot start with digit Mutable
list [1,2,3]
• Cannot use keywords collection
• No special characters except _ Immutable
tuple (1,2,3)
• Case-sensitive collection
Example: Key-value
student_name = "Lokesh" dict {"a":1}
pair
Keywords 13. Operators (IMPORTANT – TABULAR
Definition: FORMAT)
Keywords are reserved words with predefined
Definition:
meanings.
Operators are symbols used to perform
Examples:
operations on variables and values.
if, else, while, for, break, True, False, None
1. Arithmetic Operators
10. Variables
Operator Meaning Example Output
Definition:
+ Addition 5+2 7
A variable is a named memory location used to
store data. - Subtraction 5-2 3
Example: * Multiplication 5*2 10
x = 10 / Division 5/2 2.5
name = "Python" % Modulus 5%2 1
Features: // Floor Division 5//2 2
• No need to declare type
** Exponent 2**3 8
• Can change value anytime
2. Relational (Comparison) Operators
11. Constants
Definition: Operator Meaning Example
== Equal to a == b
Operator Meaning Example Input Function
!= Not equal a != b Takes input from user (always string)
name = input("Enter your name: ")
> Greater than a>b
< Less than a<b
Output Function
>= Greater or equal a >= b Displays output
<= Less or equal a <= b print("Hello", name)
3. Logical Operators 15. Type Conversion
Operator Meaning Example Definition:
and Both true a>0 and b>0 Conversion of one data type into another.
or Any true a>0 or b>0
Types:
not Reverse condition not(a>0)
Function Use
4. Assignment Operators int() Convert to integer
Operator Example Meaning float() Convert to decimal
= x=5 Assign str() Convert to string
+= x += 2 x = x+2 bool() Convert to boolean
-= x -= 2 x = x-2
Example:
*= x *= 2 x = x*2
x = "10"
/= x /= 2 x = x/2
y = int(x)
5. Bitwise Operators 1. Implicit Type Conversion
Operator Meaning Definition
& AND Implicit type conversion is the automatic
| OR conversion of one data type into another by
^ XOR Python itself, without any user intervention.
It is also called Type Promotion.
~ NOT
<< Left shift
Why Python does this?
>> Right shift
Python converts data types automatically to:
• avoid data loss
6. Membership Operators • maintain accuracy
Operator Meaning Example • ensure smooth calculations
in Present in sequence 'a' in "apple" • Example 1: int → float
not in Not present 'x' not in "apple" • x = 10 # int
y = 5.5 # float
7. Identity Operators z = x + y # int automatically
converted to float
Operator Meaning Example
print(z)
is Same object a is b print(type(z))
is not Different object a is not b 2. Explicit Type Conversion (Type Casting)
Definition
14. Input() and Output()
Explicit type conversion is the manual conversion • Allows duplicate values
of one data type into another using built-in • Can store mixed data types
functions.
Done by the programmer intentionally Example:
list1 = [10, 20, 30, "Python", True]
Why we need it? print(list1)
• To take numeric input from user
• To perform calculations 1. Creating a List
• To change data format Definition:
• To control program behavior Creating a list means defining a list and storing
elements in it.
1. int() Conversion
Methods to Create List
• Converts to integer (a) Direct Method
• Example: list1 = [1, 2, 3, 4]
• x = "10"
y = int(x)
(b) Empty List
print(y)
print(type(y))
list2 = []
(c) Using list() function
2. float() Conversion
list3 = list((1, 2, 3))
• Converts to decimal
• Example: Example Program:
• x = "5.5" list1 = [10, 20, 30]
y = float(x) print("List is:", list1)
print(y)
print(type(y))
2. Indexing in a List
3. str() Conversion Definition:
Indexing means accessing elements using their
Converts to string position (index number).
• Example: Types of Indexing:
• x = 100 • Positive Indexing → starts from 0
y = str(x)
print(y) • Negative Indexing → starts from -1
print(type(y))
Example:
INTRODUCTION TO LIST
list1 = [10, 20, 30, 40]
Definition
print(list1[0]) # 10
A List in Python is an ordered, mutable
print(list1[-1]) # 40
(changeable) collection of elements, where
elements can be of different data types.
3. Accessing Elements of a List
Lists are written using square brackets [ ].
Definition:
Accessing elements means retrieving values from
Key Features of List
a list using index.
• Ordered (maintains sequence)
• Mutable (can be changed)
Repeating means duplicating elements of a list
Syntax: multiple times.
list_name[index]
Operator Used:
Example Program: *
fruits = ["apple", "banana", "mango"]
print(fruits[1]) # banana Example Program:
list1 = [1, 2]
Access Multiple Elements: list2 = list1 * 3
print(fruits[0], fruits[2]) print(list2)
Output:
4. Slicing a List [1, 2, 1, 2, 1, 2]
Definition:
Slicing means extracting a portion (subset) of a 7. Membership Operators in a List
list. Definition:
Membership operators are used to check whether
Syntax: an element exists in a list or not.
list[start : end : step]
end is excluded Operators:
• in
• not in
Examples:
list1 = [10, 20, 30, 40, 50]
Example Program:
print(list1[1:4]) # [20, 30, 40] list1 = [10, 20, 30]
print(list1[:3]) # [10, 20, 30] print(20 in list1) # True
print(list1[::2]) # [10, 30, 50] print(50 not in list1) # True
5. Concatenating Two Lists 8. Traversing a List
Definition: Definition:
Concatenation means joining two or more lists Traversing means visiting each element of the list
into one list. one by one.
Operator Used: (a) Using for loop
+ Example:
list1 = [10, 20, 30]
Example Program: for i in list1:
list1 = [1, 2, 3] print(i)
list2 = [4, 5, 6]
list3 = list1 + list2 (b) Using range()
print(list3) Example:
Output: list1 = [10, 20, 30]
[1, 2, 3, 4, 5, 6]
for i in range(len(list1)):
6. Repeating Elements in a List print(list1[i])
Definition:
Syntax:
Built-in Functions and Methods of List list_name.extend(iterable)
Definition Example:
Built-in functions and methods are predefined list1 = [1, 2]
functions provided by Python that help us [Link]([3, 4])
perform various operations on lists such as adding, print(list1)
removing, searching, sorting, etc. Output:
[1, 2, 3, 4]
1. len()
Definition: 5. insert()
Returns the total number of elements in a list. Definition:
Syntax: Inserts an element at a specific position (index).
len(list_name) Syntax:
Example: list_name.insert(index, element)
list1 = [10, 20, 30, 40] Example:
print(len(list1)) list1 = [10, 20, 30]
Output: [Link](1, 15)
4 print(list1)
Output:
2. list() [10, 15, 20, 30]
Definition:
Used to create a list from another iterable (like 6. count()
tuple, string, etc.). Definition:
Syntax: Returns the number of times an element appears
list(iterable) in the list.
Example: Syntax:
t = (1, 2, 3) list_name.count(element)
list1 = list(t) Example:
print(list1) list1 = [1, 2, 2, 3, 2]
3. append() print([Link](2))
Definition: Output:
Adds an element at the end of the list. 3
Syntax:
list_name.append(element) 7. index()
Example: Definition:
list1 = [1, 2, 3] Returns the index (position) of the first
[Link](4) occurrence of an element.
print(list1) Syntax:
Output: list_name.index(element)
[1, 2, 3, 4] Example:
4. extend() list1 = [10, 20, 30, 20]
print([Link](20))
Definition: Output:
Adds multiple elements (another iterable) to the 1
list.
8. remove() 11. sort()
Definition: Definition:
Removes the first occurrence of a specified Sorts the list in ascending order by default.
element. Syntax:
Syntax: list_name.sort()
list_name.remove(element) Example:
Example: list1 = [3, 1, 2]
list1 = [10, 20, 30, 20] [Link]()
[Link](20) print(list1)
print(list1) Output:
Output: [1, 2, 3]
[10, 30, 20]
Descending Order:
9. pop() [Link](reverse=True)
Definition:
Removes and returns the element at a specific 12. sorted()
index (default is last element). Definition:
Syntax: Returns a new sorted list without changing the
list_name.pop(index) original list.
Example: Syntax:
list1 = [10, 20, 30] sorted(list_name)
[Link](1)
print(list1) Example:
Output: list1 = [3, 1, 2]
[10, 30] new_list = sorted(list1)
print("Original:", list1)
Default Pop: print("Sorted:", new_list)
list1 = [10, 20, 30]
[Link]() INTRODUCTION TO TUPLE
print(list1) Definition
Output: A Tuple is an ordered, immutable (unchangeable)
[10, 20] collection of elements in Python, written using
parentheses ( ).
10. reverse() Once a tuple is created, its elements cannot
Definition: be modified, added, or deleted.
Reverses the order of elements in the list.
Syntax: Example
list_name.reverse() t = (10, 20, 30)
Example: print(t)
list1 = [1, 2, 3]
[Link]() CHARACTERISTICS OF TUPLES
print(list1) 1. Ordered
Output: o Elements maintain their position.
[3, 2, 1]
2. Immutable t = (10, 20, 30, 40)
o Cannot change values after print(t[0]) # 10
creation. print(t[-1]) # 40
3. Allows Duplicates
t = (1, 2, 2, 3) 2. Slicing
4. Heterogeneous Definition:
o Can store different data types. Slicing is used to extract a portion of a tuple.
t = (10, "Python", True)
5. Faster than Lists Syntax:
o Due to immutability.
tuple[start : end : step]
end is excluded
CREATING TUPLES
1. Using Parentheses Example:
t1 = (1, 2, 3) t = (10, 20, 30, 40, 50)
print(t[1:4]) # (20, 30, 40)
2. Without Parentheses (Tuple Packing) print(t[:3]) # (10, 20, 30)
t2 = 10, 20, 30 print(t[::2]) # (10, 30, 50)
3. Single Element Tuple (Important) 3. Concatenation
Must use comma Definition:
t3 = (5,) Combining two tuples into one.
4. Empty Tuple Operator:
t4 = () +
Example:
5. Using tuple() Function t1 = (1, 2)
t5 = tuple([1, 2, 3]) t2 = (3, 4)
t3 = t1 + t2
OPERATIONS ON TUPLES print(t3)
Output:
1. Accessing Elements of a Tuple (1, 2, 3, 4)
Definition:
Accessing means retrieving elements using index 4. Repetition
values. Definition:
Repeating tuple elements multiple times.
Types:
• Positive Indexing → starts from 0 Operator:
• Negative Indexing → starts from -1 *
Example:
Syntax: t = (1, 2)
tuple_name[index] print(t * 3)
Output:
Example: (1, 2, 1, 2, 1, 2)
5. Using Membership Operators
Explanation:
Definition: The tuple has 4 elements, so len() returns 4.
Used to check whether an element exists in a
tuple. 2. tuple()
Operators: Definition:
• in Used to create a tuple from another iterable (like
• not in list, string, etc.).
Example:
t = (10, 20, 30) Syntax:
print(20 in t) # True tuple(iterable)
print(50 not in t) # True
Example 1 (List → Tuple):
6. Traversing a Tuple list1 = [1, 2, 3]
Definition: t = tuple(list1)
Traversing means visiting each element one by print(t)
one. Output:
(a) Using for loop (1, 2, 3)
t = (10, 20, 30)
Example 2 (String → Tuple):
for i in t: t = tuple("ABC")
print(i) print(t)
(b) Using range() Output:
t = (10, 20, 30) ('A', 'B', 'C')
for i in range(len(t)):
print(t[i]) Explanation:
Each character becomes a separate element in the
Built-in Functions of Tuple tuple.
Definition
Built-in functions of tuples are predefined 3. count()
functions in Python used to perform operations Definition:
like counting elements, converting data types, Returns the number of times a specific element
searching values, etc. appears in the tuple.
1. len() Syntax:
Definition: tuple_name.count(element)
Returns the total number of elements present in
a tuple. Example:
Syntax: t = (1, 2, 2, 3, 2)
len(tuple_name) print([Link](2))
Output:
Example: 3
t = (10, 20, 30, 40)
print(len(t)) Explanation:
Output: The number 2 appears 3 times.
4
Example Structure
4. index() mypackage/
Definition: [Link]
Returns the index (position) of the first [Link]
occurrence of an element.
2. Installing Packages
Syntax: Definition
tuple_name.index(element) Installing a package means downloading and
setting up external libraries so they can be used in
Example: Python.
t = (10, 20, 30, 20)
print([Link](20)) Using pip (Python Package Manager)
Output: Syntax:
1 pip install package_name
Explanation: Examples
The first occurrence of 20 is at index 1. pip install numpy
pip install pandas
5. sorted() pip install matplotlib
Definition: pip install scikit-learn
Returns a new sorted list from the tuple elements pip install nltk
(does NOT return a tuple). pip install opencv-python
Syntax: Check Installed Packages
sorted(tuple_name) pip list
Example: 3. Importing Libraries
t = (3, 1, 2)
result = sorted(t) 1. Importing Entire Library
print("Sorted:", result) Syntax:
print(type(result)) import library_name
Example:
1. Python Libraries and Packages import math
Definition of Library print([Link](25))
A library is a collection of pre-written modules,
functions, and classes that can be used to perform 2. Importing Library with Alias
specific tasks without writing code from scratch. Definition:
Example: math, numpy, pandas Giving a short name (alias) to a library.
Definition of Package Syntax:
A package is a collection of multiple modules import library_name as alias_name
organized in folders.
In simple terms:
• Module → single file
• Package → folder of modules
3. Matplotlib
3. Importing Specific Functions ‘ Definition:
Syntax: Matplotlib is used for data visualization (graphs
from library_name import function_name and charts).
Example: 4. Scikit-Learn
from math import sqrt Definition:
print(sqrt(16)) Scikit-learn is used for machine learning
algorithms.
4. Importing All Functions
Syntax: Features:
from library_name import * • Classification
• Regression
Example: • Clustering
from math import * 5. NLTK (Natural Language Toolkit)
print(sqrt(36)) Definition:
Not recommended in large programs (can NLTK is used for processing human language
cause confusion) (text).
4. Important Python Libraries Features:
• Text analysis
1. NumPy (Numerical Python) • Tokenization
Definition: • Sentiment analysis
NumPy is used for numerical computations and
working with arrays. 6. OpenCV
Definition:
Features: OpenCV is used for image processing and
• Fast calculations computer vision.
• Supports multi-dimensional arrays
• Used in AI & Data Science Features:
• Face detection
Mathematical Operation: • Image editing
• Video processing
2. Pandas
Definition:
Pandas is used for data analysis and data
manipulation.
Features:
• Handles tables (dataframes)
• Data cleaning
• Data filtering