? Module 1_ Introduction to Python
? Module 1_ Introduction to Python
✅ Detailed Notes
🔹 What is Python?
Python is a high-level, interpreted, general-purpose programming language known for its
simple syntax and readability. It supports multiple paradigms, including:
● Procedural
● Object-Oriented
● Functional programming
🔹 Features of Python
● Easy to learn & use
● Interpreted language
● Dynamically typed
● Portable (cross-platform)
🔹 Python Applications
● Web Development (e.g., Django, Flask)
● Game Development
● Desktop Applications
🔹 Python Versions
● Python 2.x – Legacy
❓ Short Q&A
Q1. Who developed Python?
Ans: Guido van Rossum in 1991.
✅
b) Bjarne Stroustrup
c) Guido van Rossum
d) James Gosling
✅
b) Compiled
c) High-level
d) Machine-level
✅
c) Procedural
d) All of the above
✅
b) Data Science
c) Firmware flashing
d) AI
🔁 Flowchart: Python Execution Process
[ Write Code (.py file) ]
↓
[ Python Interpreter Reads ]
↓
[ Converts to Bytecode (.pyc) ]
↓
[ Bytecode Sent to PVM (Python Virtual Machine) ]
↓
[ Code Executed Line by Line ]
🐍 Python Programming
|
----------------------------------------------------
| | | | |
Features Applications Paradigms Versions Developer
| | | | |
Easy Syntax Web Dev Procedural Python 2.x Guido van
Rossum
Open Source Data Sci OOP Python 3.x
Interpreted AI/ML Functional
GUI Support Automation
✨ Summary:
● 🐍 Python is a high-level, interpreted, and object-oriented programming language.
● 💡 It has easy syntax similar to English, making it beginner-friendly.
● 🛠️ Python supports multiple programming paradigms (OOP, functional, procedural).
● 🔄 It is dynamically typed — no need to declare variable types.
● 📚 Widely used in Web Development, Data Science, AI, Automation, etc.
✅ Conclusion:
● 🌟 Python is a powerful, versatile, and easy-to-learn language that forms the base of
many modern applications.
● 🚀 Learning Python gives you the key to enter many tech fields.
_________________________________________________________________________
● Numbers:
● Strings:
● Booleans:
● Lists:
● Dictionaries:
● Sets:
🔹 Variable Assignment
Python allows variables to be assigned values without explicit declaration. The interpreter
assigns the data type based on the value.
x = 10 # int
y = 3.14 # float
z = "Python" # string
🔹 Type Conversion
Python allows implicit or explicit type conversion:
●
Explicit Conversion: Using functions like int(), float(), str() for manual conversion.
Example:
num_str = "10"
num_int = int(num_str) # explicit conversion to int
●
❓ Short Q&A
Q1. What is a variable in Python?
Ans: A variable is a name that refers to a memory location used to store a value.
✅
a) int
b) float
c) str
d) complex
✅
a) 13
b) 13.5
c) 10.0
d) Error
✨ Summary:
📦 Variables store data in memory using assignment (=).
🧮 Data Types:
int 🔢 (e.g., 5)
bool 🔘 (True/False)
○ + (Addition)
○ - (Subtraction)
○ * (Multiplication)
○ / (Division)
○ // (Floor Division)
○ % (Modulus)
○ ** (Exponentiation)
Example:
a = 10
b=5
print(a + b) # 15
print(a - b) # 5
2.
3. Comparison Operators: Used to compare two values.
○ == (Equal to)
Example:
x = 10
y = 20
print(x == y) # False
print(x < y) # True
4.
5. Logical Operators: Used to combine conditional statements.
Example:
a = True
b = False
print(a and b) # False
print(a or b) # True
6.
7. Assignment Operators: Used to assign values to variables.
○ = (Assign)
Example:
c=5
c += 3 # c = c + 3, so c becomes 8
8.
9. Bitwise Operators: Used to perform bit-level operations.
○ & (AND)
○ | (OR)
○ ^ (XOR)
○ ~ (NOT)
Example:
a = 10 # 1010 in binary
b = 4 # 0100 in binary
print(a & b) # 0 (bitwise AND)
10.
11.Identity Operators: Used to check if two variables refer to the same object in memory.
○ is not (Returns True if both variables do not refer to the same object)
Example:
x = [1, 2, 3]
y = [1, 2, 3]
print(x is y) # False (different memory locations)
12.
13.Membership Operators: Used to test if a value is in a sequence (like a list or string).
Example:
a = [1, 2, 3]
print(2 in a) # True
14.
❓ Short Q&A
Q1. What is the difference between = and == in Python?
Ans: = is an assignment operator, used to assign a value to a variable. == is a comparison
operator, used to compare two values for equality.
Q3. What is the purpose of logical operators like and, or, and not?
Ans: Logical operators are used to combine conditional statements or reverse the results of a
condition.
✅
a) 2.5
b) 2
c) 3
d) Error
✅
a) Checks if the two objects are equal
b) Checks if the two objects are not the same
c) Compares the values of two objects
d) None of the above
✨ Summary:
● ➕ Arithmetic Operators: +, -, *, /, **, %
● ⚖️ Relational/Comparison: ==, !=, >, <, >=, <=
● 🔁 Assignment: =, +=, -=, etc.
● 🤔 Logical: and, or, not
● 🧠 Bitwise: &, |, ^, ~, <<, >>
● 🔍 Membership: in, not in
✅ Conclusion:
● ⚙️ Operators allow you to perform operations, compare values, and create logical
conditions in programs.
1. If Statements: Used to execute a block of code only if a certain condition is true.
Syntax:
if condition:
# block of code
○
Example:
a = 10
if a > 5:
print("a is greater than 5")
○
2. If-Else Statements: Used to execute one block of code if the condition is true, and
another block if it is false.
Syntax:
if condition:
# block of code if true
else:
# block of code if false
○
Example:
a=4
if a > 5:
print("a is greater than 5")
else:
print("a is not greater than 5")
○
3. If-Elif-Else Statements: Used to check multiple conditions.
Syntax:
if condition1:
# block of code if condition1 is true
elif condition2:
# block of code if condition2 is true
else:
# block of code if neither condition1 nor condition2 is true
○
Example:
a=3
if a > 5:
print("a is greater than 5")
elif a == 3:
print("a is 3")
else:
print("a is less than 5")
○
4. While Loops: Used to repeatedly execute a block of code as long as the condition is
true.
Syntax:
while condition:
# block of code
○
Example:
i=1
while i <= 5:
print(i)
i += 1
○
5. For Loops: Used to iterate over a sequence (such as a list, tuple, or range).
Syntax:
for variable in sequence:
# block of code
○
Example:
for i in range(5):
print(i)
○
6. Break Statement: Used to break out of the current loop.
Example:
for i in range(5):
if i == 3:
break
print(i)
○
7. Continue Statement: Used to skip the current iteration of the loop and continue with the
next iteration.
Example:
for i in range(5):
if i == 3:
continue
print(i)
○
8. Pass Statement: Used as a placeholder for future code.
Example:
for i in range(5):
if i == 3:
pass # Placeholder, do nothing
print(i)
○
❓ Short Q&A
Q1. What is the difference between if and if-else statements?
Ans: The if statement executes code only when a condition is true, while if-else provides
an alternative block of code that executes when the condition is false.
x = 10
if x > 5:
print("Greater")
else:
print("Smaller")
✅
a) Smaller
b) Greater
c) Error
d) Nothing
✅
b) pass
c) break
d) exit
for i in range(3):
if i == 1:
continue
print(i)
✅
a) 0 1 2
b) 0 2
c) 1 2
d) 1
4. Which loop will execute its block of code at least once, even if the condition is false?
✅
a) for loop
b) while loop
c) if loop
d) do-while loop
✨ Summary:
● 🤖 Used for decision making.
● ✅ Executes different code blocks depending on conditions.
🧾 Syntax
if condition:
# do this
elif another_condition:
# do something else
else:
# default action
✅ Conclusion:
●
● 🧭 Control Flow lets your program make intelligent decisions, just like human thinking.
■ Example: a = 10
■ Example: b = 10.5
■ Example: c = 3 + 4j
■ Example: b = b'Hello'
■ Example: mv = memoryview(b'Hello')
■ Example: x = None
❓ Short Q&A
Q1. What is the difference between a list and a tuple in Python?
Ans: A list is mutable, meaning its elements can be changed, while a tuple is immutable,
meaning once created, its elements cannot be modified.
Q2. What is a dictionary in Python?
Ans: A dictionary is a collection of key-value pairs where each key is unique, and each key
maps to a value.
✅ a) list
b) tuple
c) frozenset
d) str
x = (1, 2, 3)
x[0] = 5
a) [1, 2, 3]
✅
b) (1, 2, 3)
c) Error
d) 5
✅
a) John
b) "John"
c) "name"
d) Error
✅
b) tuple
c) set
d) dict
✅
b) Null
c) No value
d) Empty
[Create List]
↓
[Print List]
End
------------------------------------------------
| | | | |
| | | | |
complex range
✨ Summary:
● 🔁 Loops repeat code until a condition is met.
● 🔄 for loop: for iterating over a sequence.
● 📍 while loop: continues while a condition is True.
● 🛑 break: exit loop early.
● ⏭️ continue: skip current iteration.
● 🤫 pass: do nothing (placeholder).
✅ Conclusion:
● 🧩 Loops make programs efficient, shorter, and help handle repetition easily.
🧠 Module 6: Functions
✅ Detailed Notes
🔹 What are Functions in Python?
A function is a block of reusable code that performs a specific task. It allows us to write code
once and reuse it multiple times, improving code readability and reducing redundancy.
Syntax:
def function_name(parameters):
# function body
return value
○
Example:
def greet(name):
○
2. Function Parameters:
○ Functions can accept input values known as parameters (also called arguments).
Example:
def add(a, b):
return a + b
○
3. Return Statement:
○ Built-in Functions: Functions that come with Python, like print(), len(), etc.
Syntax:
lambda arguments: expression
○
Example:
square = lambda x: x ** 2
○
7. Recursion:
Example:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
○
❓ Short Q&A
Q1. What is the purpose of the return statement in a function?
Ans: The return statement is used to return a result or output from the function back to the
caller.
Q4. How can you define a function that accepts an arbitrary number of arguments?
Ans: You can define a function that accepts an arbitrary number of arguments using *args for
non-keyword arguments and **kwargs for keyword arguments.
def greet(name):
print(greet("Alice"))
✅
a) Hello, Alice
b) "Hello, Alice!"
c) greet("Alice")
d) Error
✅
b) define
c) def
d) func
return a * b
print(multiply(3))
a) 3
✅
b) 6
c) 6
d) Error
✅
b) def function(args, kwargs):
*c) def function(args):
d) def function(args):
✅
a) lambda function(args):
b) lambda args: expression
c) def lambda(args):
d) lambda expression(args):
[Define Function]
[Call Function]
↓
[Return Value]
End
----------------------------------------------------
| | | | |
| | | | |
def function pos, kw, def return value built-in, user calls itself
✨ Summary:
🧵 A string is a sequence of characters inside quotes.
📐 Strings are immutable (cannot be changed after creation).
🛠️ Useful Methods: .lower(), .upper(), .replace(), .find(), .split(), etc.
🔢 Indexing and slicing supported: "Hello"[1:4] → 'ell'
✅ Conclusion:
📝 Strings are essential for handling text-based data — very common in every application
○ If the condition is True, the code block inside the if will execute.
Syntax:
if condition:
# code to execute
○
2. elif statement:
Syntax:
if condition1:
# code
elif condition2:
# code
○
3. else statement:
Syntax:
else:
# code
○
Example:
x = 10
if x > 5:
elif x == 5:
else:
Syntax:
for item in sequence:
# code to execute
○
Example:
for i in range(5):
print(i)
2.
3. while loop:
Syntax:
while condition:
# code to execute
○
Example:
count = 0
print(count)
count += 1
4.
Example:
for i in range(10):
if i == 5:
break
print(i)
○
2. continue:
Example:
for i in range(5):
if i == 2:
continue
print(i)
○
3. pass:
Example:
for i in range(5):
pass
○
❓ Short Q&A
Q1. What is the purpose of the elif statement?
Ans: The elif statement allows checking multiple conditions sequentially. If the previous if
or elif conditions are False, it checks the next one.
x = 10
if x > 5:
else:
✅
a) x is equal to 5
b) x is greater than 5
c) Error
d) x is less than 5
✅
a) continue
b) break
c) pass
d) exit
for i in range(3):
if i == 1:
continue
print(i)
a) 1
✅
b) 0 1 2
c) 0 2
d) Error
✅
a) if condition then:
b) if condition:
c) if (condition) then:
d) if: condition
No
↓
[Code Block 2] --> [Exit]
-----------------------------------------------
| | | |
| | | |
✨ Summary:
● 🔄 Converts one data type to another.
● 🔢 Example:
○ int("5") → 5
○ str(10) → "10"
○ float("3.14") → 3.14
● ✅ Ensures correct operations (e.g., adding int + int not int + str)
✅ Conclusion:
● 🧪 Type casting helps avoid errors and ensures compatibility between different data
types.
🧠 Module 8: Functions
✅ Detailed Notes
🔹 What are Functions?
A function is a block of code that only runs when it is called. Functions allow you to organize
and reuse code. They can accept inputs (parameters), process them, and return outputs
(results).
🔸 Defining a Function
Syntax to define a function:
def function_name(parameters):
# code to execute
return result
1.
Example:
def greet(name):
print(greet("Alice"))
2.
🔸 Function Parameters
1. Positional Parameters:
Example:
def add(a, b):
return a + b
2.
3. Keyword Parameters:
○ You can pass arguments to a function using the parameter name. The order
doesn't matter here.
Example:
def add(a, b):
return a + b
print(add(b=3, a=2)) # Outputs: 5
4.
5. Default Parameters:
○ Default values can be set for parameters, making them optional when calling the
function.
Example:
def greet(name, message="Hello"):
6.
7. Variable-length Parameters:
Example:
def greet(*args, **kwargs):
print("Args:", args)
print("Kwargs:", kwargs)
8.
🔸 Returning Values
● Functions can return values using the return keyword.
return a * b
result = multiply(3, 4)
print(result) # Outputs: 12
●
Example:
square = lambda x: x ** 2
print(square(5)) # Outputs: 25
●
❓ Short Q&A
Q1. What is a function in Python?
Ans: A function is a block of code that performs a specific task. It can accept inputs
(parameters) and return outputs (results).
Q3. Can you explain the use of the return statement in a function?
Ans: The return statement is used to send back a result from a function. It allows the function
to output a value after performing its task.
print(greet("Alice"))
✅
a) Hello, Alice!
b) Hi, Alice!
c) Alice!
d) Error
2. How do you define a function in Python?
✅
a) function(){}
b) def function_name():
c) function_name()
d) create function function_name():
multiply = lambda x, y: x * y
print(multiply(3, 4))
a) 7
✅
b) 12
c) 12
d) Error
def func(*args):
print(arg)
func(1, 2, 3)
a) 1
✅
b) 1 2 3
c) 1 2 3
d) Error
✅
b) It outputs a value from the function
c) It exits the function and sends a result back to the caller
d) It defines the function
🔁 Flowchart: Functions
Start
[Define Function]
[Call Function]
[Execute Code]
------------------------------------------------
| | |
| | |
Executes code
✨ Summary:
⚙️ A block of reusable code that performs a task.
🔑 Defined using def keyword.
📥 Takes parameters and returns output using return.
🧱 Example:
def greet(name): return "Hello " + name
✅ Conclusion:
🛠️ Functions make code modular, readable, reusable, and easier to manage.
🔸 Using Modules
1. Importing a Module:
Example:
import math
2.
3. Import Specific Functions or Variables:
Example:
from math import pi
4.
5. Renaming Modules:
Example:
import math as m
6.
🔸 Creating Your Own Module
1. Defining a Module:
○ Any Python file (.py) can be a module. To create your own module, define
functions and variables in a .py file.
return a + b
return a - b
2.
3. Using Your Module:
○ Once you have created a module, you can import it into another script using the
import statement.
Example:
import mymodule
4.
Example:
import os
🔸 Packages
1. Creating a Package:
3.
❓ Short Q&A
Q1. What is the difference between a module and a package in Python?
Ans: A module is a single Python file containing code, whereas a package is a collection of
modules stored in a directory.
import math
print(math.pow(2, 3))
✅
a) Prints 2 raised to the power of 3
b) Prints 8
c) Prints 6
d) Error
4. Which of the following Python modules help in working with JSON data?
a) os
b) datetime
✅ c) json
d) math
5. What is the purpose of the __init__.py file in a Python package?
a) It contains the main function of the package
✅
b) It defines the functions of the package
c) It marks the directory as a package
d) It is not necessary for a package
[Define Functions/Variables]
[Import Module]
[Use Functions/Variables]
[Exit]
|
-----------------------------------------------------------
| | |
| Module |
✨ Summary:
● 🧯 Prevents program from crashing on error.
● 🔐 Use try, except, else, finally blocks.
❗ Example:
try:
x = 10 / 0
except ZeroDivisionError:
✅ Conclusion:
●
● 🔒 Exception handling makes code robust, user-friendly, and safe from unexpected
crashes.
__________________________________________________________________
📚 You’ve put in the effort, revised with focus, and prepared every concept with clarity.
Now, go to your exam with confidence in your heart ❤️, clarity in your mind 🧠, and faith in
your hard work 💪.
📝👩💻 These notes and this journey were lovingly created by
Shasmeen Zahra
💖
– with dedication, heart, and hope for your success.