๐ PYTHON โ DAY 6 STUDY MATERIAL
โจ Topic: while Loop in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is a Loop?
A loop is used to repeat a block of code multiple times until a condition becomes False.
โโโโโโโโโโโโโโโโโโโ
๐ What is while Loop?
The while loop executes a block of code as long as the condition is True.
โโโโโโโโโโโโโโโโโโโ
๐น Syntax of while Loop
while condition:
statement
โโโโโโโโโโโโโโโโโโโ
๐น Example: Print Numbers 1 to 5
i = 1
while i <= 5:
print(i)
i = i + 1
โโโโโโโโโโโโโโโโโโโ
๐น Example: Sum of Numbers
i = 1
sum = 0
while i <= 5:
sum = sum + i
i = i + 1
print(sum)
โโโโโโโโโโโโโโโโโโโ
๐น Infinite Loop
A loop that never ends is called an infinite loop.
Example (Avoid this):
while True:
print("Python")
โโโโโโโโโโโโโโโโโโโ
๐น Common Mistakes in while Loop
โ Forgetting to update loop variable
โ Wrong condition
โ Infinite loop
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 6
โ Print numbers from 1 to 10
โ Print even numbers using while loop
โ Find factorial of a number
โ Reverse a number
Example Program:
num = int(input("Enter a number: "))
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num = num // 10
print("Reverse:", rev)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 6 Goal
โ Understand repetition using while loop
โ Avoid infinite loops
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 7
๐ฅ for Loop in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: while Loop in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is a Loop?
A loop is used to repeat a block of code multiple times until a condition becomes False.
โโโโโโโโโโโโโโโโโโโ
๐ What is while Loop?
The while loop executes a block of code as long as the condition is True.
โโโโโโโโโโโโโโโโโโโ
๐น Syntax of while Loop
while condition:
statement
โโโโโโโโโโโโโโโโโโโ
๐น Example: Print Numbers 1 to 5
i = 1
while i <= 5:
print(i)
i = i + 1
โโโโโโโโโโโโโโโโโโโ
๐น Example: Sum of Numbers
i = 1
sum = 0
while i <= 5:
sum = sum + i
i = i + 1
print(sum)
โโโโโโโโโโโโโโโโโโโ
๐น Infinite Loop
A loop that never ends is called an infinite loop.
Example (Avoid this):
while True:
print("Python")
โโโโโโโโโโโโโโโโโโโ
๐น Common Mistakes in while Loop
โ Forgetting to update loop variable
โ Wrong condition
โ Infinite loop
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 6
โ Print numbers from 1 to 10
โ Print even numbers using while loop
โ Find factorial of a number
โ Reverse a number
Example Program:
num = int(input("Enter a number: "))
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num = num // 10
print("Reverse:", rev)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 6 Goal
โ Understand repetition using while loop
โ Avoid infinite loops
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 7
๐ฅ for Loop in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 7 STUDY MATERIAL
โจ Topic: for Loop in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is for Loop?
The for loop is used to iterate over a sequence such as a list, tuple, string, or range.
โโโโโโโโโโโโโโโโโโโ
๐น Syntax of for Loop
for variable in sequence:
statement
โโโโโโโโโโโโโโโโโโโ
๐น Using range() Function
range() generates a sequence of numbers.
range(start, stop, step)
Example:
for i in range(1, 6):
print(i)
Output:
1 2 3 4 5
โโโโโโโโโโโโโโโโโโ
๐น Example: Print Even Numbers
for i in range(2, 11, 2):
print(i)
โโโโโโโโโโโโโโโโโโโ
๐น Looping Through a String
for ch in "Python":
print(ch)
โโโโโโโโโโโโโโโโโโโ
๐น Nested for Loop
A for loop inside another for loop.
Example:
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
โโโโโโโโโโโโโโโโโโโ
๐น Difference Between for and while Loop
โข for loop is used when number of iterations is known
โข while loop is used when condition is unknown
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 7
โ Print numbers from 1 to 10
โ Print multiplication table
โ Print characters of a string
โ Create star pattern using nested loop
Example Program:
for i in range(1, 6):
print("*" * i)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 7 Goal
โ Master iteration using for loop
โ Use range() confidently
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 8
๐ฅ break, continue & pass Statements
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: for Loop in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is for Loop?
The for loop is used to iterate over a sequence such as a list, tuple, string, or range.
โโโโโโโโโโโโโโโโโโโ
๐น Syntax of for Loop
for variable in sequence:
statement
โโโโโโโโโโโโโโโโโโโ
๐น Using range() Function
range() generates a sequence of numbers.
range(start, stop, step)
Example:
for i in range(1, 6):
print(i)
Output:
1 2 3 4 5
โโโโโโโโโโโโโโโโโโ
๐น Example: Print Even Numbers
for i in range(2, 11, 2):
print(i)
โโโโโโโโโโโโโโโโโโโ
๐น Looping Through a String
for ch in "Python":
print(ch)
โโโโโโโโโโโโโโโโโโโ
๐น Nested for Loop
A for loop inside another for loop.
Example:
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
โโโโโโโโโโโโโโโโโโโ
๐น Difference Between for and while Loop
โข for loop is used when number of iterations is known
โข while loop is used when condition is unknown
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 7
โ Print numbers from 1 to 10
โ Print multiplication table
โ Print characters of a string
โ Create star pattern using nested loop
Example Program:
for i in range(1, 6):
print("*" * i)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 7 Goal
โ Master iteration using for loop
โ Use range() confidently
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 8
๐ฅ break, continue & pass Statements
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 8 STUDY MATERIAL
โจ Topic: break, continue & pass Statements
โโโโโโโโโโโโโโโโโโโ
๐ Control Statements in Python
Control statements are used to change the normal flow of loops.
Python provides three control statements:
โข break
โข continue
โข pass
โโโโโโโโโโโโโโโโโโโ
๐ break Statement
The break statement is used to terminate the loop immediately when a condition is met.
Example:
for i in range(1, 10):
if i == 5:
break
print(i)
Output:
1 2 3 4
โโโโโโโโโโโโโโโโโโโ
โญ continue Statement
The continue statement skips the current iteration and moves to the next one.
Example:
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1 2 4 5
โโโโโโโโโโโโโโโโโโ
โธ pass Statement
The pass statement is used as a placeholder where a statement is required but no action is needed.
Example:
for i in range(1, 5):
if i == 2:
pass
print(i)
โโโโโโโโโโโโโโโโโโโ
๐ Difference Between break, continue & pass
โข break โ Stops loop completely
โข continue โ Skips current iteration
โข pass โ Does nothing, avoids error
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 8
โ Stop loop when number equals 7
โ Skip printing number 5
โ Use pass inside empty if block
โ Combine loop with break & continue
Example Program:
for i in range(1, 11):
if i == 5:
continue
if i == 8:
break
print(i)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 8 Goal
โ Control loop execution
โ Understand loop flow clearly
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 9
๐ฅ Pattern Programs (Stars & Numbers)
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
React โค๏ธ If You Got It Right
โจ Topic: break, continue & pass Statements
โโโโโโโโโโโโโโโโโโโ
๐ Control Statements in Python
Control statements are used to change the normal flow of loops.
Python provides three control statements:
โข break
โข continue
โข pass
โโโโโโโโโโโโโโโโโโโ
๐ break Statement
The break statement is used to terminate the loop immediately when a condition is met.
Example:
for i in range(1, 10):
if i == 5:
break
print(i)
Output:
1 2 3 4
โโโโโโโโโโโโโโโโโโโ
โญ continue Statement
The continue statement skips the current iteration and moves to the next one.
Example:
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1 2 4 5
โโโโโโโโโโโโโโโโโโ
โธ pass Statement
The pass statement is used as a placeholder where a statement is required but no action is needed.
Example:
for i in range(1, 5):
if i == 2:
pass
print(i)
โโโโโโโโโโโโโโโโโโโ
๐ Difference Between break, continue & pass
โข break โ Stops loop completely
โข continue โ Skips current iteration
โข pass โ Does nothing, avoids error
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 8
โ Stop loop when number equals 7
โ Skip printing number 5
โ Use pass inside empty if block
โ Combine loop with break & continue
Example Program:
for i in range(1, 11):
if i == 5:
continue
if i == 8:
break
print(i)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 8 Goal
โ Control loop execution
โ Understand loop flow clearly
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 9
๐ฅ Pattern Programs (Stars & Numbers)
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
React โค๏ธ If You Got It Right
๐ PYTHON โ DAY 9 STUDY MATERIAL
โจ Topic: Pattern Programs (Stars & Numbers)
โโโโโโโโโโโโโโโโโโโ
๐ What are Pattern Programs?
Pattern programs use loops and logic to print designs using stars (*) or numbers.
They help improve loop control and logical thinking.
โโโโโโโโโโโโโโโโโโโ
โญ Star Pattern โ Right Triangle
Code:
for i in range(1, 6):
print("*" * i)
โโโโโโโโโโโโโโโโโโโ
โญ Star Pattern โ Inverted Triangle
Code:
for i in range(5, 0, -1):
print("*" * i)
โโโโโโโโโโโโโโโโโโโ
โญ Pyramid Star Pattern
Code:
n = 4
for i in range(n):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
โโโโโโโโโโโโโโโโโโโ
๐ข Number Pattern โ Increasing Numbers
1
12
123
1234
Code:
for i in range(1, 5):
for j in range(1, i + 1):
print(j, end="")
print()
โโโโโโโโโโโโโโโโโโโ
๐ข Number Pattern โ Same Number
1
22
333
4444
Code:
for i in range(1, 5):
print(str(i) * i)
โโโโโโโโโโโโโโโโโโโ
๐ก Logic Tips for Pattern Programs
โ Outer loop โ rows
โ Inner loop โ columns
โ Spaces control alignment
โ Practice regularly
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 9
โ Print hollow star rectangle
โ Print number pyramid
โ Print reverse number pattern
โ Create your own pattern
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 9 Goal
โ Master nested loops
โ Improve logical thinking
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 10
๐ฅ Functions in Python (Basics)
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: Pattern Programs (Stars & Numbers)
โโโโโโโโโโโโโโโโโโโ
๐ What are Pattern Programs?
Pattern programs use loops and logic to print designs using stars (*) or numbers.
They help improve loop control and logical thinking.
โโโโโโโโโโโโโโโโโโโ
โญ Star Pattern โ Right Triangle
Code:
for i in range(1, 6):
print("*" * i)
โโโโโโโโโโโโโโโโโโโ
โญ Star Pattern โ Inverted Triangle
Code:
for i in range(5, 0, -1):
print("*" * i)
โโโโโโโโโโโโโโโโโโโ
โญ Pyramid Star Pattern
Code:
n = 4
for i in range(n):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
โโโโโโโโโโโโโโโโโโโ
๐ข Number Pattern โ Increasing Numbers
1
12
123
1234
Code:
for i in range(1, 5):
for j in range(1, i + 1):
print(j, end="")
print()
โโโโโโโโโโโโโโโโโโโ
๐ข Number Pattern โ Same Number
1
22
333
4444
Code:
for i in range(1, 5):
print(str(i) * i)
โโโโโโโโโโโโโโโโโโโ
๐ก Logic Tips for Pattern Programs
โ Outer loop โ rows
โ Inner loop โ columns
โ Spaces control alignment
โ Practice regularly
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 9
โ Print hollow star rectangle
โ Print number pyramid
โ Print reverse number pattern
โ Create your own pattern
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 9 Goal
โ Master nested loops
โ Improve logical thinking
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 10
๐ฅ Functions in Python (Basics)
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 10 STUDY MATERIAL
โจ Topic: Functions in Python (Basics)
โโโโโโโโโโโโโโโโโโโ
๐ What is a Function?
A function is a block of reusable code that performs a specific task.
Functions help reduce code repetition and improve readability.
โโโโโโโโโโโโโโโโโโโ
๐น Why Use Functions?
โ Code reusability
โ Better organization
โ Easy debugging
โ Saves time and effort
โโโโโโโโโโโโโโโโโโโ
๐งฉ Syntax of a Function
def function_name():
statement
โโโโโโโโโโโโโโโโโโโ
๐น Example: Simple Function
def greet():
print("Hello Python")
greet()
Output:
Hello Python
โโโโโโโโโโโโโโโโโโโ
๐น Function with Parameters
Parameters are values passed to a function.
Example:
def greet(name):
print("Hello", name)
greet("Soham")
โโโโโโโโโโโโโโโโโโโ
๐น Function with Return Value
The return statement sends a value back to the caller.
Example:
def add(a, b):
return a + b
result = add(10, 20)
print(result)
โโโโโโโโโโโโโโโโโโโ
๐น Function Call
Calling a function means executing it.
Example:
add(5, 3)
โโโโโโโโโโโโโโโโโโโ
โ ๏ธ Important Points
โข Function name should be meaningful
โข Use indentation properly
โข return ends the function execution
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 10
โ Create a function to print your name
โ Create a function to add two numbers
โ Create a function to find square of a number
โ Create a function to check even or odd
Example Program:
def square(n):
return n * n
print(square(5))
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 10 Goal
โ Understand function basics
โ Write reusable code
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 11
๐ฅ Function Arguments (Types)
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: Functions in Python (Basics)
โโโโโโโโโโโโโโโโโโโ
๐ What is a Function?
A function is a block of reusable code that performs a specific task.
Functions help reduce code repetition and improve readability.
โโโโโโโโโโโโโโโโโโโ
๐น Why Use Functions?
โ Code reusability
โ Better organization
โ Easy debugging
โ Saves time and effort
โโโโโโโโโโโโโโโโโโโ
๐งฉ Syntax of a Function
def function_name():
statement
โโโโโโโโโโโโโโโโโโโ
๐น Example: Simple Function
def greet():
print("Hello Python")
greet()
Output:
Hello Python
โโโโโโโโโโโโโโโโโโโ
๐น Function with Parameters
Parameters are values passed to a function.
Example:
def greet(name):
print("Hello", name)
greet("Soham")
โโโโโโโโโโโโโโโโโโโ
๐น Function with Return Value
The return statement sends a value back to the caller.
Example:
def add(a, b):
return a + b
result = add(10, 20)
print(result)
โโโโโโโโโโโโโโโโโโโ
๐น Function Call
Calling a function means executing it.
Example:
add(5, 3)
โโโโโโโโโโโโโโโโโโโ
โ ๏ธ Important Points
โข Function name should be meaningful
โข Use indentation properly
โข return ends the function execution
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 10
โ Create a function to print your name
โ Create a function to add two numbers
โ Create a function to find square of a number
โ Create a function to check even or odd
Example Program:
def square(n):
return n * n
print(square(5))
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 10 Goal
โ Understand function basics
โ Write reusable code
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 11
๐ฅ Function Arguments (Types)
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โค1
๐ PYTHON โ DAY 11 STUDY MATERIAL
โจ Topic: Function Arguments (Types)
โโโโโโโโโโโโโโโโโโโ
๐ What are Function Arguments?
Arguments are values passed to a function when it is called.
They allow functions to work with different inputs.
โโโโโโโโโโโโโโโโโโโ
๐น 1. Positional Arguments
Arguments are passed in the same order as parameters.
Example:
def add(a, b):
print(a + b)
add(10, 5)
โโโโโโโโโโโโโโโโโโโ
๐น 2. Keyword Arguments
Arguments are passed using parameter names, order does not matter.
Example:
def greet(name, msg):
print(msg, name)
greet(msg="Hello", name="Soham")
โโโโโโโโโโโโโโโโโโโ
๐น 3. Default Arguments
Default values are used when no argument is passed.
Example:
def greet(name="User"):
print("Hello", name)
greet()
greet("Python")
โโโโโโโโโโโโโโโโโโโ
๐น 4. Variable-Length Arguments (*args)
Used when number of arguments is unknown.
Example:
def total(*nums):
print(sum(nums))
total(10, 20, 30)
โโโโโโโโโโโโโโโโโโโ
๐น 5. Keyword Variable-Length Arguments
Used to pass key-value pairs.
Example:
def details(**info):
print(info)
details(name="Soham", age=20)
โโโโโโโโโโโโโโโโโโโ
โ ๏ธ Important Notes
โข Positional arguments come first
โข Default arguments should be last
โข *args โ tuple
โข **kwargs โ dictionary
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 11
โ Use positional & keyword arguments
โ Create function with default value
โ Create function using *args
โ Create function using **kwargs
Example Program:
def marks(*m):
print(sum(m))
marks(60, 70, 80)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 11 Goal
โ Understand all types of function arguments
โ Write flexible functions
โโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 12
๐ฅ Recursion in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: Function Arguments (Types)
โโโโโโโโโโโโโโโโโโโ
๐ What are Function Arguments?
Arguments are values passed to a function when it is called.
They allow functions to work with different inputs.
โโโโโโโโโโโโโโโโโโโ
๐น 1. Positional Arguments
Arguments are passed in the same order as parameters.
Example:
def add(a, b):
print(a + b)
add(10, 5)
โโโโโโโโโโโโโโโโโโโ
๐น 2. Keyword Arguments
Arguments are passed using parameter names, order does not matter.
Example:
def greet(name, msg):
print(msg, name)
greet(msg="Hello", name="Soham")
โโโโโโโโโโโโโโโโโโโ
๐น 3. Default Arguments
Default values are used when no argument is passed.
Example:
def greet(name="User"):
print("Hello", name)
greet()
greet("Python")
โโโโโโโโโโโโโโโโโโโ
๐น 4. Variable-Length Arguments (*args)
Used when number of arguments is unknown.
Example:
def total(*nums):
print(sum(nums))
total(10, 20, 30)
โโโโโโโโโโโโโโโโโโโ
๐น 5. Keyword Variable-Length Arguments
Used to pass key-value pairs.
Example:
def details(**info):
print(info)
details(name="Soham", age=20)
โโโโโโโโโโโโโโโโโโโ
โ ๏ธ Important Notes
โข Positional arguments come first
โข Default arguments should be last
โข *args โ tuple
โข **kwargs โ dictionary
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 11
โ Use positional & keyword arguments
โ Create function with default value
โ Create function using *args
โ Create function using **kwargs
Example Program:
def marks(*m):
print(sum(m))
marks(60, 70, 80)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 11 Goal
โ Understand all types of function arguments
โ Write flexible functions
โโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 12
๐ฅ Recursion in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 12 STUDY MATERIAL
โจ Topic: Recursion in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is Recursion?
Recursion is a technique where a function calls itself to solve a problem.
It is useful for problems that can be broken into smaller sub-problems.
โโโโโโโโโโโโโโโโโโโ
๐น Two Important Parts of Recursion
1๏ธโฃ Base Condition โ Stops recursion
2๏ธโฃ Recursive Call โ Function calls itself
Without base condition โ infinite recursion โ
โโโโโโโโโโโโโโโโโโโ
๐น Syntax of Recursive Function
def function_name():
if base_condition:
return value
return function_name(smaller_problem)
โโโโโโโโโโโโโโโโโโโ
๐ข Example: Factorial using Recursion
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
print(factorial(5))
Output:
120
โโโโโโโโโโโโโโโโโโโ
๐ Example: Fibonacci Series
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
print(fib(6))
โโโโโโโโโโโโโโโโโโโ
โ ๏ธ Important Notes
โข Always define a base case
โข Recursive calls increase memory usage
โข Not suitable for very large problems
โโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 12
โ Find factorial of a number
โ Print Fibonacci series
โ Calculate sum of digits using recursion
โ Reverse a number using recursion
Example Program:
def sum_digits(n):
if n == 0:
return 0
return n % 10 + sum_digits(n // 10)
print(sum_digits(123))
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 12 Goal
โ Understand recursive thinking
โ Write correct recursive functions
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 13
๐ฅ Strings in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: Recursion in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is Recursion?
Recursion is a technique where a function calls itself to solve a problem.
It is useful for problems that can be broken into smaller sub-problems.
โโโโโโโโโโโโโโโโโโโ
๐น Two Important Parts of Recursion
1๏ธโฃ Base Condition โ Stops recursion
2๏ธโฃ Recursive Call โ Function calls itself
Without base condition โ infinite recursion โ
โโโโโโโโโโโโโโโโโโโ
๐น Syntax of Recursive Function
def function_name():
if base_condition:
return value
return function_name(smaller_problem)
โโโโโโโโโโโโโโโโโโโ
๐ข Example: Factorial using Recursion
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
print(factorial(5))
Output:
120
โโโโโโโโโโโโโโโโโโโ
๐ Example: Fibonacci Series
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
print(fib(6))
โโโโโโโโโโโโโโโโโโโ
โ ๏ธ Important Notes
โข Always define a base case
โข Recursive calls increase memory usage
โข Not suitable for very large problems
โโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 12
โ Find factorial of a number
โ Print Fibonacci series
โ Calculate sum of digits using recursion
โ Reverse a number using recursion
Example Program:
def sum_digits(n):
if n == 0:
return 0
return n % 10 + sum_digits(n // 10)
print(sum_digits(123))
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 12 Goal
โ Understand recursive thinking
โ Write correct recursive functions
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 13
๐ฅ Strings in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 13 STUDY MATERIAL
โจ Topic: Strings in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is a String?
A string is a sequence of characters enclosed in single (' ') or double (" ") quotes.
Example:
name = "Python"
msg = 'Hello'
โโโโโโโโโโโโโโโโโโโ
๐ข String Indexing
Each character in a string has an index number, starting from 0.
Example:
text = "Python"
text[0] โ P
text[1] โ y
text[-1] โ n
โโโโโโโโโโโโโโโโโโโ
โ๏ธ String Slicing
Used to extract a portion of a string.
Syntax:
string[start:end]
Example:
text = "Python"
print(text[0:3]) # Pyt
print(text[2:]) # thon
print(text[:4]) # Pyth
โโโโโโโโโโโโโโโโโโ
๐ String Looping
Example:
for ch in "Python":
print(ch)
โโโโโโโโโโโโโโโโโโโ
๐งฎ String Length
Use len() to find string length.
Example:
text = "Python"
print(len(text))
โโโโโโโโโโโโโโโโโโโ
๐ Strings are Immutable
Strings cannot be changed once created.
Example:
text = "Python"
text[0] = 'J' โ Error
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 13
โ Print each character of a string
โ Reverse a string
โ Find length of a string
โ Extract substring
Example Program:
text = input("Enter a string: ")
print(text[::-1])
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 13 Goal
โ Understand string basics
โ Use indexing and slicing confidently
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 14
๐ฅ String Methods
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: Strings in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is a String?
A string is a sequence of characters enclosed in single (' ') or double (" ") quotes.
Example:
name = "Python"
msg = 'Hello'
โโโโโโโโโโโโโโโโโโโ
๐ข String Indexing
Each character in a string has an index number, starting from 0.
Example:
text = "Python"
text[0] โ P
text[1] โ y
text[-1] โ n
โโโโโโโโโโโโโโโโโโโ
โ๏ธ String Slicing
Used to extract a portion of a string.
Syntax:
string[start:end]
Example:
text = "Python"
print(text[0:3]) # Pyt
print(text[2:]) # thon
print(text[:4]) # Pyth
โโโโโโโโโโโโโโโโโโ
๐ String Looping
Example:
for ch in "Python":
print(ch)
โโโโโโโโโโโโโโโโโโโ
๐งฎ String Length
Use len() to find string length.
Example:
text = "Python"
print(len(text))
โโโโโโโโโโโโโโโโโโโ
๐ Strings are Immutable
Strings cannot be changed once created.
Example:
text = "Python"
text[0] = 'J' โ Error
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 13
โ Print each character of a string
โ Reverse a string
โ Find length of a string
โ Extract substring
Example Program:
text = input("Enter a string: ")
print(text[::-1])
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 13 Goal
โ Understand string basics
โ Use indexing and slicing confidently
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 14
๐ฅ String Methods
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 14 STUDY MATERIAL
โจ Topic: String Methods in Python
โโโโโโโโโโโโโโโโโโโ
๐ What are String Methods?
String methods are built-in functions used to manipulate and format strings.
โโโโโโโโโโโโโโโโโโโ
๐ค Common String Methods
๐น upper() โ Converts string to uppercase
Example:
text = "python"
print(text.upper())
๐น lower() โ Converts string to lowercase
print(text.lower())
๐น title() โ Capitalizes first letter of each word
print("hello world".title())
๐น capitalize() โ Capitalizes first character
print("python".capitalize())
โโโโโโโโโโโโโโโโโโโ
โ๏ธ Trim Methods
๐น strip() โ Removes spaces from both sides
๐น lstrip() โ Removes left spaces
๐น rstrip() โ Removes right spaces
Example:
text = " python "
print(text.strip())
โโโโโโโโโโโโโโโโโโโ
๐ Replace & Split
๐น replace() โ Replaces part of string
print("hello python".replace("python", "world"))
๐น split() โ Splits string into list
print("a,b,c".split(","))
โโโโโโโโโโโโโโโโโโโ
๐ Search Methods
๐น find() โ Returns index of substring
print("python".find("t"))
๐น count() โ Counts occurrences
print("banana".count("a"))
โโโโโโโโโโโโโโโโโโโ
โ Check Methods
๐น isalpha() โ Checks alphabets
๐น isdigit() โ Checks digits
๐น isalnum() โ Checks alphanumeric
Example:
print("Python123".isalnum())
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 14
โ Convert string to uppercase
โ Count vowels in string
โ Replace a word in sentence
โ Split full name into first and last name
Example Program:
text = input("Enter text: ")
print(text.upper())
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 14 Goal
โ Master common string methods
โ Manipulate strings effectively
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 15
๐ฅ Lists in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: String Methods in Python
โโโโโโโโโโโโโโโโโโโ
๐ What are String Methods?
String methods are built-in functions used to manipulate and format strings.
โโโโโโโโโโโโโโโโโโโ
๐ค Common String Methods
๐น upper() โ Converts string to uppercase
Example:
text = "python"
print(text.upper())
๐น lower() โ Converts string to lowercase
print(text.lower())
๐น title() โ Capitalizes first letter of each word
print("hello world".title())
๐น capitalize() โ Capitalizes first character
print("python".capitalize())
โโโโโโโโโโโโโโโโโโโ
โ๏ธ Trim Methods
๐น strip() โ Removes spaces from both sides
๐น lstrip() โ Removes left spaces
๐น rstrip() โ Removes right spaces
Example:
text = " python "
print(text.strip())
โโโโโโโโโโโโโโโโโโโ
๐ Replace & Split
๐น replace() โ Replaces part of string
print("hello python".replace("python", "world"))
๐น split() โ Splits string into list
print("a,b,c".split(","))
โโโโโโโโโโโโโโโโโโโ
๐ Search Methods
๐น find() โ Returns index of substring
print("python".find("t"))
๐น count() โ Counts occurrences
print("banana".count("a"))
โโโโโโโโโโโโโโโโโโโ
โ Check Methods
๐น isalpha() โ Checks alphabets
๐น isdigit() โ Checks digits
๐น isalnum() โ Checks alphanumeric
Example:
print("Python123".isalnum())
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 14
โ Convert string to uppercase
โ Count vowels in string
โ Replace a word in sentence
โ Split full name into first and last name
Example Program:
text = input("Enter text: ")
print(text.upper())
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 14 Goal
โ Master common string methods
โ Manipulate strings effectively
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 15
๐ฅ Lists in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 15 STUDY MATERIAL
โจ Topic: Lists in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is a List?
A list is a collection of items stored in a single variable.
Lists are ordered, mutable (changeable) and allow duplicate values.
โโโโโโโโโโโโโโโโโโโ
๐น Creating a List
Example:
nums = [10, 20, 30, 40]
names = ["Python", "Java", "C"]
โโโโโโโโโโโโโโโโโโโ
๐ข List Indexing
Example:
nums = [10, 20, 30]
nums[0] โ 10
nums[-1] โ 30
โโโโโโโโโโโโโโโโโโโ
โ๏ธ List Slicing
Example:
nums = [1, 2, 3, 4, 5]
print(nums[1:4]) # [2, 3, 4]
โโโโโโโโโโโโโโโโโโโ
โ๏ธ Modify List Elements
Example:
nums = [10, 20, 30]
nums[1] = 25
print(nums)
โโโโโโโโโโโโโโโโโโโ
๐ Looping Through List
Example:
for n in nums:
print(n)
โโโโโโโโโโโโโโโโโโโ
๐งฎ List Length
Example:
print(len(nums))
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 15
โ Create a list of numbers
โ Access elements using index
โ Change list elements
โ Print all list elements using loop
Example Program:
nums = [5, 10, 15, 20]
for n in nums:
print(n)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 15 Goal
โ Understand list basics
โ Use lists confidently
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 16
๐ฅ List Methods
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: Lists in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is a List?
A list is a collection of items stored in a single variable.
Lists are ordered, mutable (changeable) and allow duplicate values.
โโโโโโโโโโโโโโโโโโโ
๐น Creating a List
Example:
nums = [10, 20, 30, 40]
names = ["Python", "Java", "C"]
โโโโโโโโโโโโโโโโโโโ
๐ข List Indexing
Example:
nums = [10, 20, 30]
nums[0] โ 10
nums[-1] โ 30
โโโโโโโโโโโโโโโโโโโ
โ๏ธ List Slicing
Example:
nums = [1, 2, 3, 4, 5]
print(nums[1:4]) # [2, 3, 4]
โโโโโโโโโโโโโโโโโโโ
โ๏ธ Modify List Elements
Example:
nums = [10, 20, 30]
nums[1] = 25
print(nums)
โโโโโโโโโโโโโโโโโโโ
๐ Looping Through List
Example:
for n in nums:
print(n)
โโโโโโโโโโโโโโโโโโโ
๐งฎ List Length
Example:
print(len(nums))
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 15
โ Create a list of numbers
โ Access elements using index
โ Change list elements
โ Print all list elements using loop
Example Program:
nums = [5, 10, 15, 20]
for n in nums:
print(n)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 15 Goal
โ Understand list basics
โ Use lists confidently
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 16
๐ฅ List Methods
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 16 STUDY MATERIAL
โจ Topic: List Methods in Python
โโโโโโโโโโโโโโโโโโโ
๐ What are List Methods?
List methods are built-in functions used to add, remove, and manage list elements.
โโโโโโโโโโโโโโโโโโโ
โ Adding Elements
๐น append() โ Adds element at the end
Example:
nums = [1, 2, 3]
nums.append(4)
๐น insert() โ Adds element at specific index
nums.insert(1, 10)
๐น extend() โ Adds multiple elements
nums.extend([5, 6])
โโโโโโโโโโโโโโโโโโโ
โ Removing Elements
๐น remove() โ Removes specific value
nums.remove(10)
๐น pop() โ Removes element by index
nums.pop()
nums.pop(1)
๐น clear() โ Removes all elements
nums.clear()
โโโโโโโโโโโโโโโโโโโ
๐ Other Useful List Methods
๐น sort() โ Sorts list
nums.sort()
๐น reverse() โ Reverses list
nums.reverse()
๐น count() โ Counts occurrence
nums.count(2)
๐น index() โ Returns index of value
nums.index(3)
โโโโโโโโโโโโโโโโโโโ
๐ง List Copy
๐น copy() โ Creates shallow copy
new_list = nums.copy()
โโโโโโโโโโโโโโโโโโโ
โ ๏ธ Important Notes
โข Lists are mutable
โข sort() changes original list
โข Use sorted() to keep original list
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 16
โ Add elements using append & insert
โ Remove elements using pop & remove
โ Sort a list
โ Reverse a list
Example Program:
nums = [4, 2, 1, 3]
nums.sort()
print(nums)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 16 Goal
โ Master list operations
โ Manage list data efficiently
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 17
๐ฅ Tuples in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: List Methods in Python
โโโโโโโโโโโโโโโโโโโ
๐ What are List Methods?
List methods are built-in functions used to add, remove, and manage list elements.
โโโโโโโโโโโโโโโโโโโ
โ Adding Elements
๐น append() โ Adds element at the end
Example:
nums = [1, 2, 3]
nums.append(4)
๐น insert() โ Adds element at specific index
nums.insert(1, 10)
๐น extend() โ Adds multiple elements
nums.extend([5, 6])
โโโโโโโโโโโโโโโโโโโ
โ Removing Elements
๐น remove() โ Removes specific value
nums.remove(10)
๐น pop() โ Removes element by index
nums.pop()
nums.pop(1)
๐น clear() โ Removes all elements
nums.clear()
โโโโโโโโโโโโโโโโโโโ
๐ Other Useful List Methods
๐น sort() โ Sorts list
nums.sort()
๐น reverse() โ Reverses list
nums.reverse()
๐น count() โ Counts occurrence
nums.count(2)
๐น index() โ Returns index of value
nums.index(3)
โโโโโโโโโโโโโโโโโโโ
๐ง List Copy
๐น copy() โ Creates shallow copy
new_list = nums.copy()
โโโโโโโโโโโโโโโโโโโ
โ ๏ธ Important Notes
โข Lists are mutable
โข sort() changes original list
โข Use sorted() to keep original list
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 16
โ Add elements using append & insert
โ Remove elements using pop & remove
โ Sort a list
โ Reverse a list
Example Program:
nums = [4, 2, 1, 3]
nums.sort()
print(nums)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 16 Goal
โ Master list operations
โ Manage list data efficiently
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 17
๐ฅ Tuples in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 17 STUDY MATERIAL
โจ Topic: Tuples in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is a Tuple?
A tuple is a collection of items stored in a single variable.
Tuples are ordered and immutable (cannot be changed).
โโโโโโโโโโโโโโโโโโโ
๐น Creating a Tuple
Example:
t = (10, 20, 30)
names = ("Python", "Java", "C")
Single element tuple:
x = (10,) โ comma is required
โโโโโโโโโโโโโโโโโโโ
๐ข Tuple Indexing
Example:
t = (10, 20, 30)
t[0] โ 10
t[-1] โ 30
โโโโโโโโโโโโโโโโโโโ
โ๏ธ Tuple Slicing
Example:
t = (1, 2, 3, 4, 5)
print(t[1:4])
โโโโโโโโโโโโโโโโโโโ
๐ Looping Through Tuple
Example:
for item in t:
print(item)
โโโโโโโโโโโโโโโโโโโ
๐ Tuple Immutability
Tuple elements cannot be modified.
Example:
t = (10, 20, 30)
t[0] = 5 โ Error
โโโโโโโโโโโโโโโโโโโ
๐งฎ Tuple Methods
๐น count() โ Counts occurrence
๐น index() โ Returns index
Example:
t = (1, 2, 2, 3)
print(t.count(2))
print(t.index(3))
โโโโโโโโโโโโโโโโโโโ
๐ Convert Tuple to List
Example:
t = (1, 2, 3)
lst = list(t)
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 17
โ Create tuple of numbers
โ Access elements using index
โ Loop through tuple
โ Convert tuple to list
Example Program:
t = (5, 10, 15)
for i in t:
print(i)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 17 Goal
โ Understand tuple basics
โ Know difference between list & tuple
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 18
๐ฅ Sets in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: Tuples in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is a Tuple?
A tuple is a collection of items stored in a single variable.
Tuples are ordered and immutable (cannot be changed).
โโโโโโโโโโโโโโโโโโโ
๐น Creating a Tuple
Example:
t = (10, 20, 30)
names = ("Python", "Java", "C")
Single element tuple:
x = (10,) โ comma is required
โโโโโโโโโโโโโโโโโโโ
๐ข Tuple Indexing
Example:
t = (10, 20, 30)
t[0] โ 10
t[-1] โ 30
โโโโโโโโโโโโโโโโโโโ
โ๏ธ Tuple Slicing
Example:
t = (1, 2, 3, 4, 5)
print(t[1:4])
โโโโโโโโโโโโโโโโโโโ
๐ Looping Through Tuple
Example:
for item in t:
print(item)
โโโโโโโโโโโโโโโโโโโ
๐ Tuple Immutability
Tuple elements cannot be modified.
Example:
t = (10, 20, 30)
t[0] = 5 โ Error
โโโโโโโโโโโโโโโโโโโ
๐งฎ Tuple Methods
๐น count() โ Counts occurrence
๐น index() โ Returns index
Example:
t = (1, 2, 2, 3)
print(t.count(2))
print(t.index(3))
โโโโโโโโโโโโโโโโโโโ
๐ Convert Tuple to List
Example:
t = (1, 2, 3)
lst = list(t)
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 17
โ Create tuple of numbers
โ Access elements using index
โ Loop through tuple
โ Convert tuple to list
Example Program:
t = (5, 10, 15)
for i in t:
print(i)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 17 Goal
โ Understand tuple basics
โ Know difference between list & tuple
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 18
๐ฅ Sets in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 18 STUDY MATERIAL
โจ Topic: Sets in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is a Set?
A set is a collection of unique elements stored in a single variable.
Sets are unordered and do not allow duplicate values.
โโโโโโโโโโโโโโโโโโโ
๐น Creating a Set
Example:
s = {10, 20, 30}
names = {"Python", "Java", "C"}
Duplicate values are removed automatically:
s = {1, 2, 2, 3}
Output โ {1, 2, 3}
โโโโโโโโโโโโโโโโโโโ
โ Add Elements to Set
๐น add() โ Adds single element
s.add(40)
๐น update() โ Adds multiple elements
s.update([50, 60])
โโโโโโโโโโโโโโโโโโโ
โ Remove Elements from Set
๐น remove() โ Removes element (error if not found)
s.remove(20)
๐น discard() โ Removes element (no error)
s.discard(100)
๐น pop() โ Removes random element
s.pop()
โโโโโโโโโโโโโโโโโโโ
๐ Set Operations
๐น union() โ Combines sets
๐น intersection() โ Common elements
๐น difference() โ Remaining elements
Example:
a = {1, 2, 3}
b = {3, 4, 5}
print(a.union(b))
print(a.intersection(b))
print(a.difference(b))
โโโโโโโโโโโโโโโโโโโ
๐งฎ Check Membership
Example:
print(2 in a)
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 18
โ Create a set
โ Add and remove elements
โ Perform union & intersection
โ Remove duplicate values from list using set
Example Program:
lst = [1, 2, 2, 3, 4]
s = set(lst)
print(s)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 18 Goal
โ Understand set properties
โ Perform set operations
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 19
๐ฅ Dictionaries in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: Sets in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is a Set?
A set is a collection of unique elements stored in a single variable.
Sets are unordered and do not allow duplicate values.
โโโโโโโโโโโโโโโโโโโ
๐น Creating a Set
Example:
s = {10, 20, 30}
names = {"Python", "Java", "C"}
Duplicate values are removed automatically:
s = {1, 2, 2, 3}
Output โ {1, 2, 3}
โโโโโโโโโโโโโโโโโโโ
โ Add Elements to Set
๐น add() โ Adds single element
s.add(40)
๐น update() โ Adds multiple elements
s.update([50, 60])
โโโโโโโโโโโโโโโโโโโ
โ Remove Elements from Set
๐น remove() โ Removes element (error if not found)
s.remove(20)
๐น discard() โ Removes element (no error)
s.discard(100)
๐น pop() โ Removes random element
s.pop()
โโโโโโโโโโโโโโโโโโโ
๐ Set Operations
๐น union() โ Combines sets
๐น intersection() โ Common elements
๐น difference() โ Remaining elements
Example:
a = {1, 2, 3}
b = {3, 4, 5}
print(a.union(b))
print(a.intersection(b))
print(a.difference(b))
โโโโโโโโโโโโโโโโโโโ
๐งฎ Check Membership
Example:
print(2 in a)
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 18
โ Create a set
โ Add and remove elements
โ Perform union & intersection
โ Remove duplicate values from list using set
Example Program:
lst = [1, 2, 2, 3, 4]
s = set(lst)
print(s)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 18 Goal
โ Understand set properties
โ Perform set operations
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 19
๐ฅ Dictionaries in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 19 STUDY MATERIAL
โจ Topic: Dictionaries in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is a Dictionary?
A dictionary stores data in keyโvalue pairs.
Dictionaries are unordered, mutable, and keys must be unique.
โโโโโโโโโโโโโโโโโโโ
๐น Creating a Dictionary
Example:
student = {
"name": "Soham",
"age": 20,
"course": "Python"
}
โโโโโโโโโโโโโโโโโโโ
๐ Access Dictionary Values
Example:
print(student["name"])
print(student.get("age"))
โโโโโโโโโโโโโโโโโโโ
โ๏ธ Modify Dictionary
Example:
student["age"] = 21
student["city"] = "Pune"
โโโโโโโโโโโโโโโโโโโ
โ Remove Dictionary Items
๐น pop() โ Removes specific key
student.pop("city")
๐น popitem() โ Removes last item
student.popitem()
๐น clear() โ Removes all items
student.clear()
โโโโโโโโโโโโโโโโโโโ
๐ Loop Through Dictionary
Example:
for key in student:
print(key, student[key])
โโโโโโโโโโโโโโโโโโโ
๐งฎ Dictionary Methods
๐น keys() โ Returns all keys
๐น values() โ Returns all values
๐น items() โ Returns key-value pairs
Example:
print(student.keys())
print(student.values())
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 19
โ Create student dictionary
โ Add and update values
โ Loop through dictionary
โ Delete a key
Example Program:
student = {"name": "Amit", "age": 22}
for k, v in student.items():
print(k, v)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 19 Goal
โ Understand key-value storage
โ Use dictionaries effectively
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 20
๐ฅ Dictionary Methods & Nested Dictionary
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: Dictionaries in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is a Dictionary?
A dictionary stores data in keyโvalue pairs.
Dictionaries are unordered, mutable, and keys must be unique.
โโโโโโโโโโโโโโโโโโโ
๐น Creating a Dictionary
Example:
student = {
"name": "Soham",
"age": 20,
"course": "Python"
}
โโโโโโโโโโโโโโโโโโโ
๐ Access Dictionary Values
Example:
print(student["name"])
print(student.get("age"))
โโโโโโโโโโโโโโโโโโโ
โ๏ธ Modify Dictionary
Example:
student["age"] = 21
student["city"] = "Pune"
โโโโโโโโโโโโโโโโโโโ
โ Remove Dictionary Items
๐น pop() โ Removes specific key
student.pop("city")
๐น popitem() โ Removes last item
student.popitem()
๐น clear() โ Removes all items
student.clear()
โโโโโโโโโโโโโโโโโโโ
๐ Loop Through Dictionary
Example:
for key in student:
print(key, student[key])
โโโโโโโโโโโโโโโโโโโ
๐งฎ Dictionary Methods
๐น keys() โ Returns all keys
๐น values() โ Returns all values
๐น items() โ Returns key-value pairs
Example:
print(student.keys())
print(student.values())
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 19
โ Create student dictionary
โ Add and update values
โ Loop through dictionary
โ Delete a key
Example Program:
student = {"name": "Amit", "age": 22}
for k, v in student.items():
print(k, v)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 19 Goal
โ Understand key-value storage
โ Use dictionaries effectively
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 20
๐ฅ Dictionary Methods & Nested Dictionary
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 20 STUDY MATERIAL
โจ Topic: Dictionary Methods & Nested Dictionary
โโโโโโโโโโโโโโโโโโ
๐ Important Dictionary Methods
๐น update() โ Adds or updates key-value pairs
Example:
student = {"name": "Soham"}
student.update({"age": 21})
๐น get() โ Returns value of key
print(student.get("name"))
๐น setdefault() โ Returns value, adds key if not present
student.setdefault("city", "Pune")
โโโโโโโโโโโโโโโโโโโ
โ Removing Dictionary Data
๐น del โ Deletes key
del student["age"]
๐น clear() โ Removes all data
student.clear()
โโโโโโโโโโโโโโโโโโโ
๐งฉ Nested Dictionary
A dictionary inside another dictionary is called a nested dictionary.
Example:
students = {
1: {"name": "Amit", "age": 20},
2: {"name": "Soham", "age": 21}
}
Access nested value:
print(students[1]["name"])
โโโโโโโโโโโโโโโโโโโ
๐ Loop Through Nested Dictionary
Example:
for id, info in students.items():
print(id, info["name"], info["age"])
โโโโโโโโโโโโโโโโโโโ
๐งฎ Copy Dictionary
๐น copy() โ Creates shallow copy
new_dict = student.copy()
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 20
โ Use update() and setdefault()
โ Create nested dictionary
โ Access and print nested values
โ Loop through nested dictionary
Example Program:
data = {1: {"name": "Raj", "marks": 80}}
print(data[1]["marks"])
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 20 Goal
โ Master dictionary methods
โ Work with nested dictionaries
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 21
๐ฅ Revision + Practice (Week Review)
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: Dictionary Methods & Nested Dictionary
โโโโโโโโโโโโโโโโโโ
๐ Important Dictionary Methods
๐น update() โ Adds or updates key-value pairs
Example:
student = {"name": "Soham"}
student.update({"age": 21})
๐น get() โ Returns value of key
print(student.get("name"))
๐น setdefault() โ Returns value, adds key if not present
student.setdefault("city", "Pune")
โโโโโโโโโโโโโโโโโโโ
โ Removing Dictionary Data
๐น del โ Deletes key
del student["age"]
๐น clear() โ Removes all data
student.clear()
โโโโโโโโโโโโโโโโโโโ
๐งฉ Nested Dictionary
A dictionary inside another dictionary is called a nested dictionary.
Example:
students = {
1: {"name": "Amit", "age": 20},
2: {"name": "Soham", "age": 21}
}
Access nested value:
print(students[1]["name"])
โโโโโโโโโโโโโโโโโโโ
๐ Loop Through Nested Dictionary
Example:
for id, info in students.items():
print(id, info["name"], info["age"])
โโโโโโโโโโโโโโโโโโโ
๐งฎ Copy Dictionary
๐น copy() โ Creates shallow copy
new_dict = student.copy()
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 20
โ Use update() and setdefault()
โ Create nested dictionary
โ Access and print nested values
โ Loop through nested dictionary
Example Program:
data = {1: {"name": "Raj", "marks": 80}}
print(data[1]["marks"])
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 20 Goal
โ Master dictionary methods
โ Work with nested dictionaries
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 21
๐ฅ Revision + Practice (Week Review)
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 21 STUDY MATERIAL
โจ Topic: Revision + Practice (Week Review)
โโโโโโโโโโโโโโโโโโโ
๐ Topics Covered (Day 1 โ Day 20)
โ Python Introduction & Installation
โ Variables & Data Types
โ Operators
โ Conditional Statements
โ while Loop & for Loop
โ break, continue, pass
โ Pattern Programs
โ Functions & Arguments
โ Recursion
โ Strings & String Methods
โ Lists & List Methods
โ Tuples
โ Sets
โ Dictionaries & Nested Dictionary
โโโโโโโโโโโโโโโโโโโ
๐ง Quick Revision Points
๐น Variables are dynamically typed
๐น Indentation is mandatory in Python
๐น Lists are mutable, tuples are immutable
๐น Sets store unique values
๐น Dictionaries store key-value pairs
๐น Functions reduce code repetition
โโโโโโโโโโโโโโโโโโโ
๐งช Practice Programs โ Day 21
1๏ธโฃ Check whether a number is even or odd
num = int(input("Enter number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
โโโโโโโโโโโโโโโโโโโ
2๏ธโฃ Find sum of elements in a list
nums = [10, 20, 30]
print(sum(nums))
โโโโโโโโโโโโโโโโโโโ
3๏ธโฃ Reverse a string
text = input("Enter string: ")
print(text[::-1])
โโโโโโโโโโโโโโโโโโโ
4๏ธโฃ Count frequency using dictionary
text = "python"
freq = {}
for ch in text:
freq[ch] = freq.get(ch, 0) + 1
print(freq)
โโโโโโโโโโโโโโโโโโโ
โญ Challenge Tasks
โ Print star pyramid
โ Create simple calculator using functions
โ Remove duplicates from list
โ Store student data using dictionary
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 21 Goal
โ Revise all fundamentals
โ Identify weak topics
โ Build confidence in basics
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 22
๐ฅ File Handling in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: Revision + Practice (Week Review)
โโโโโโโโโโโโโโโโโโโ
๐ Topics Covered (Day 1 โ Day 20)
โ Python Introduction & Installation
โ Variables & Data Types
โ Operators
โ Conditional Statements
โ while Loop & for Loop
โ break, continue, pass
โ Pattern Programs
โ Functions & Arguments
โ Recursion
โ Strings & String Methods
โ Lists & List Methods
โ Tuples
โ Sets
โ Dictionaries & Nested Dictionary
โโโโโโโโโโโโโโโโโโโ
๐ง Quick Revision Points
๐น Variables are dynamically typed
๐น Indentation is mandatory in Python
๐น Lists are mutable, tuples are immutable
๐น Sets store unique values
๐น Dictionaries store key-value pairs
๐น Functions reduce code repetition
โโโโโโโโโโโโโโโโโโโ
๐งช Practice Programs โ Day 21
1๏ธโฃ Check whether a number is even or odd
num = int(input("Enter number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
โโโโโโโโโโโโโโโโโโโ
2๏ธโฃ Find sum of elements in a list
nums = [10, 20, 30]
print(sum(nums))
โโโโโโโโโโโโโโโโโโโ
3๏ธโฃ Reverse a string
text = input("Enter string: ")
print(text[::-1])
โโโโโโโโโโโโโโโโโโโ
4๏ธโฃ Count frequency using dictionary
text = "python"
freq = {}
for ch in text:
freq[ch] = freq.get(ch, 0) + 1
print(freq)
โโโโโโโโโโโโโโโโโโโ
โญ Challenge Tasks
โ Print star pyramid
โ Create simple calculator using functions
โ Remove duplicates from list
โ Store student data using dictionary
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 21 Goal
โ Revise all fundamentals
โ Identify weak topics
โ Build confidence in basics
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 22
๐ฅ File Handling in Python
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 22 STUDY MATERIAL
โจ Topic: File Handling in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is File Handling?
File handling allows Python programs to read data from files and write data to files permanently.
โโโโโโโโโโโโโโโโโโโ
๐ Types of Files
โ Text files (.txt)
โ Binary files (.bin, .dat)
โโโโโโโโโโโโโโโโโโโ
๐ Opening a File
Syntax:
file = open("filename", "mode")
Common modes:
"r" โ Read
"w" โ Write
"a" โ Append
"x" โ Create
"rb" โ Read binary
"wb" โ Write binary
โโโโโโโโโโโโโโโโโโโ
๐ Reading from a File
Example:
file = open("data.txt", "r")
print(file.read())
file.close()
โโโโโโโโโโโโโโโโโโโ
โ๏ธ Writing to a File
Example:
file = open("data.txt", "w")
file.write("Hello Python")
file.close()
โโโโโโโโโโโโโโโโโโโ
โ Append Data to File
Example:
file = open("data.txt", "a")
file.write("\nWelcome")
file.close()
โโโโโโโโโโโโโโโโโโโ
๐ Using with Statement
Automatically closes the file.
Example:
with open("data.txt", "r") as file:
print(file.read())
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 22
โ Create a text file
โ Write data into file
โ Read file content
โ Append data to file
Example Program:
with open("test.txt", "w") as f:
f.write("Python File Handling")
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 22 Goal
โ Understand file operations
โ Read & write files safely
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 23
๐ฅ File Methods & File Modes
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: File Handling in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is File Handling?
File handling allows Python programs to read data from files and write data to files permanently.
โโโโโโโโโโโโโโโโโโโ
๐ Types of Files
โ Text files (.txt)
โ Binary files (.bin, .dat)
โโโโโโโโโโโโโโโโโโโ
๐ Opening a File
Syntax:
file = open("filename", "mode")
Common modes:
"r" โ Read
"w" โ Write
"a" โ Append
"x" โ Create
"rb" โ Read binary
"wb" โ Write binary
โโโโโโโโโโโโโโโโโโโ
๐ Reading from a File
Example:
file = open("data.txt", "r")
print(file.read())
file.close()
โโโโโโโโโโโโโโโโโโโ
โ๏ธ Writing to a File
Example:
file = open("data.txt", "w")
file.write("Hello Python")
file.close()
โโโโโโโโโโโโโโโโโโโ
โ Append Data to File
Example:
file = open("data.txt", "a")
file.write("\nWelcome")
file.close()
โโโโโโโโโโโโโโโโโโโ
๐ Using with Statement
Automatically closes the file.
Example:
with open("data.txt", "r") as file:
print(file.read())
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 22
โ Create a text file
โ Write data into file
โ Read file content
โ Append data to file
Example Program:
with open("test.txt", "w") as f:
f.write("Python File Handling")
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 22 Goal
โ Understand file operations
โ Read & write files safely
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 23
๐ฅ File Methods & File Modes
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
Forwarded from TECH BY WEB CODER
30 Pattern In Python.pdf
5 MB
Important Web Development And Programming Language Related Project๐
๐๐ป๐๐ป๐๐ป๐๐ป๐๐ป๐๐ป๐๐ป๐๐ป๐๐ป
Topic :- Top 30 Patterns in Python (Star, Alphabet, And Number)
๐๐ป๐๐ป๐๐ป๐๐ป๐๐ป๐๐ป๐๐ป๐๐ป๐๐ป
Topic :- Top 30 Patterns in Python (Star, Alphabet, And Number)
๐1
๐ PYTHON โ DAY 23 STUDY MATERIAL
โจ Topic: File Methods & File Modes
โโโโโโโโโโโโโโโโโโโ
๐ File Modes in Python
๐น "r" โ Read mode (file must exist)
๐น "w" โ Write mode (creates new / overwrites file)
๐น "a" โ Append mode (adds data at end)
๐น "x" โ Create file (error if file exists)
๐น "r+" โ Read + Write
๐น "w+" โ Write + Read
โโโโโโโโโโโโโโโโโโโ
๐ Important File Methods
๐น read() โ Reads entire file
๐น readline() โ Reads one line
๐น readlines() โ Reads all lines as list
Example:
with open("data.txt", "r") as f:
print(f.readline())
โโโโโโโโโโโโโโโโโโโ
โ๏ธ Writing Multiple Lines
Example:
with open("data.txt", "w") as f:
f.writelines(["Python\n", "Java\n", "C\n"])
โโโโโโโโโโโโโโโโโโโ
๐ File Cursor Position
๐น tell() โ Returns current position
๐น seek() โ Changes position
Example:
with open("data.txt", "r") as f:
print(f.tell())
f.seek(0)
โโโโโโโโโโโโโโโโโโโ
๐งน Closing a File
๐น close() โ Closes file manually
๐น with statement โ Closes automatically (recommended)
โโโโโโโโโโโโโโโโโโโ
โ ๏ธ Common File Errors
โ FileNotFoundError
โ PermissionError
โ Wrong mode usage
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 23
โ Read file line by line
โ Write multiple lines to file
โ Use tell() and seek()
โ Try different file modes
Example Program:
with open("demo.txt", "w+") as f:
f.write("Hello Python")
f.seek(0)
print(f.read())
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 23 Goal
โ Master file modes
โ Use file methods confidently
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 24
๐ฅ Exception Handling (try, except)
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: File Methods & File Modes
โโโโโโโโโโโโโโโโโโโ
๐ File Modes in Python
๐น "r" โ Read mode (file must exist)
๐น "w" โ Write mode (creates new / overwrites file)
๐น "a" โ Append mode (adds data at end)
๐น "x" โ Create file (error if file exists)
๐น "r+" โ Read + Write
๐น "w+" โ Write + Read
โโโโโโโโโโโโโโโโโโโ
๐ Important File Methods
๐น read() โ Reads entire file
๐น readline() โ Reads one line
๐น readlines() โ Reads all lines as list
Example:
with open("data.txt", "r") as f:
print(f.readline())
โโโโโโโโโโโโโโโโโโโ
โ๏ธ Writing Multiple Lines
Example:
with open("data.txt", "w") as f:
f.writelines(["Python\n", "Java\n", "C\n"])
โโโโโโโโโโโโโโโโโโโ
๐ File Cursor Position
๐น tell() โ Returns current position
๐น seek() โ Changes position
Example:
with open("data.txt", "r") as f:
print(f.tell())
f.seek(0)
โโโโโโโโโโโโโโโโโโโ
๐งน Closing a File
๐น close() โ Closes file manually
๐น with statement โ Closes automatically (recommended)
โโโโโโโโโโโโโโโโโโโ
โ ๏ธ Common File Errors
โ FileNotFoundError
โ PermissionError
โ Wrong mode usage
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 23
โ Read file line by line
โ Write multiple lines to file
โ Use tell() and seek()
โ Try different file modes
Example Program:
with open("demo.txt", "w+") as f:
f.write("Hello Python")
f.seek(0)
print(f.read())
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 23 Goal
โ Master file modes
โ Use file methods confidently
โโโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 24
๐ฅ Exception Handling (try, except)
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
๐ PYTHON โ DAY 24 STUDY MATERIAL
โจ Topic: Exception Handling in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is an Exception?
An exception is an error that occurs during program execution, which interrupts normal flow.
Examples:
ZeroDivisionError
ValueError
TypeError
โโโโโโโโโโโโโโโโโโโ
๐ก Why Use Exception Handling?
โ Prevent program crash
โ Handle errors gracefully
โ Improve program reliability
โโโโโโโโโโโโโโโโโโโ
๐น try-except Block
Syntax:
try:
risky_code
except:
error_handling_code
Example:
try:
x = int(input("Enter number: "))
print(10 / x)
except:
print("Error occurred")
โโโโโโโโโโโโโโโโโโโ
๐น Handling Specific Exceptions
Example:
try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")
โโโโโโโโโโโโโโโโโโโ
๐น Multiple except Blocks
Example:
try:
x = int(input())
except ValueError:
print("Invalid input")
except ZeroDivisionError:
print("Division error")
โโโโโโโโโโโโโโโโโโโ
๐น else Block
Executes if no exception occurs.
Example:
try:
print(10 / 2)
except:
print("Error")
else:
print("Success")
โโโโโโโโโโโโโโโโโโโ
๐น finally Block
Always executes (used for cleanup).
Example:
try:
print(10 / 2)
finally:
print("Done")
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 24
โ Handle divide by zero
โ Handle invalid input
โ Use else and finally
โ Write safe calculator
Example Program:
try:
a = int(input("Enter a: "))
b = int(input("Enter b: "))
print(a / b)
except Exception as e:
print("Error:", e)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 24 Goal
โ Handle runtime errors
โ Write crash-free programs
โโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 25
๐ฅ User-Defined Exceptions
โจ Stay Connected | Keep Coding
๐ TechByWebCoder
โจ Topic: Exception Handling in Python
โโโโโโโโโโโโโโโโโโโ
๐ What is an Exception?
An exception is an error that occurs during program execution, which interrupts normal flow.
Examples:
ZeroDivisionError
ValueError
TypeError
โโโโโโโโโโโโโโโโโโโ
๐ก Why Use Exception Handling?
โ Prevent program crash
โ Handle errors gracefully
โ Improve program reliability
โโโโโโโโโโโโโโโโโโโ
๐น try-except Block
Syntax:
try:
risky_code
except:
error_handling_code
Example:
try:
x = int(input("Enter number: "))
print(10 / x)
except:
print("Error occurred")
โโโโโโโโโโโโโโโโโโโ
๐น Handling Specific Exceptions
Example:
try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")
โโโโโโโโโโโโโโโโโโโ
๐น Multiple except Blocks
Example:
try:
x = int(input())
except ValueError:
print("Invalid input")
except ZeroDivisionError:
print("Division error")
โโโโโโโโโโโโโโโโโโโ
๐น else Block
Executes if no exception occurs.
Example:
try:
print(10 / 2)
except:
print("Error")
else:
print("Success")
โโโโโโโโโโโโโโโโโโโ
๐น finally Block
Always executes (used for cleanup).
Example:
try:
print(10 / 2)
finally:
print("Done")
โโโโโโโโโโโโโโโโโโโ
๐ Practice Tasks โ Day 24
โ Handle divide by zero
โ Handle invalid input
โ Use else and finally
โ Write safe calculator
Example Program:
try:
a = int(input("Enter a: "))
b = int(input("Enter b: "))
print(a / b)
except Exception as e:
print("Error:", e)
โโโโโโโโโโโโโโโโโโโ
๐ฏ Day 24 Goal
โ Handle runtime errors
โ Write crash-free programs
โโโโโโโโโโโโโโโโโโ
๐ Next Topic โ Day 25
๐ฅ User-Defined Exceptions
โจ Stay Connected | Keep Coding
๐ TechByWebCoder