Tuesday, 21 April 2026
Monday, 20 April 2026
π Day 26/150 – Print Numbers from 1 to N in Python
π Day 26/150 – Print Numbers from 1 to N in Python
Printing numbers from 1 to N is one of the most basic and important programming exercises. It helps you understand loops, iteration, and how Python executes repeated tasks.
Let’s explore different ways to achieve this π
πΉ Method 1 – Using for Loop
The most common and beginner-friendly approach.
n = 10 for i in range(1, n + 1): print(i)
✅ Explanation:
-
range(1, n + 1)generates numbers from 1 to N - The loop prints each number one by one
πΉ Method 2 – Taking User Input
Make the program dynamic by taking input from the user.
✅ Explanation
input()takes value from the userint()converts it into an integer- Loop prints numbers accordingly
πΉ Method 3 – Using while Loop
A condition-based approach.
n = 10 i = 1 while i <= n: print(i) i += 1
✅ Explanation:
-
Starts from
i = 1 -
Runs until
i <= n -
Increments
iafter each iteration
πΉ Method 4 – Using List Comprehension
A more compact and Pythonic way.
n = 10 numbers = [i for i in range(1, n + 1)] print(numbers)
✅ Explanation:
- Creates a list of numbers from 1 to N
- Prints all values at once
π― Final Thoughts
-
Use
for loopfor simple iteration ✅ -
Use
while loopwhen working with conditions π - Use list comprehension for compact code π§
π Day 25/150 – Check Alphabet, Digit, or Special Character in Python
π Day 25/150 – Check Alphabet, Digit, or Special Character in Python
This is a very practical problem that helps you understand character classification in Python. It’s commonly used in input validation, password checking, and text processing.
π Goal
Given a character, determine whether it is:
- π€ Alphabet (A–Z, a–z)
- π’ Digit (0–9)
- ⚡ Special Character (anything else like @, #, $, etc.)
πΉ Method 1 – Using if-elif-else
char = 'A' if char.isalpha(): print("Alphabet") elif char.isdigit(): print("Digit") else: print("Special Character")
π§ Explanation:
- isalpha() → checks if character is a letter
- isdigit() → checks if it’s a number
- Anything else → special character
π Best for: Clean and beginner-friendly logic
πΉ Method 2 – Taking User Input
π§ Explanation:
- Makes program interactive
- Works for real-time inputs
π Best for: Practical use
πΉ Method 3 – Using Function
def check_char(c): if c.isalpha(): return "Alphabet" elif c.isdigit(): return "Digit" else: return "Special Character" print(check_char('@'))
π§ Explanation:
- Function makes code reusable
- Returns result instead of printing
π Best for: Modular code
πΉ Method 4 – Using Lambda Function
check = lambda c: "Alphabet" if c.isalpha() else "Digit" if c.isdigit() else "Special Character" print(check('5'))
π§ Explanation:
- One-line compact logic
- Uses nested conditional expressions
π Best for: Short expressions
⚡ Key Takeaways
- isalpha() → Alphabet
- isdigit() → Digit
- Else → Special Character
- Always validate input length
π‘ Pro Tip
Try extending this:
- Count alphabets, digits, and symbols in a string
- Build a password strength checker
- Analyze text data
April Python Bootcamp Day 13
Python Coding April 20, 2026 Bootcamp, Python No comments
What is a Module?
A module is a single Python file (.py) that contains functions, variables, or classes which can be reused in other programs.
Example:
If you create a file named utils.py, it becomes a module.
Why modules are important:
- Promote code reusability
- Help in organizing large codebases
- Reduce redundancy
What is a Package?
A package is a folder that contains multiple modules.
Structure example:
my_package/
├── module1.py
├── module2.py
└── __init__.py
Packages allow you to structure your project logically and scale your code efficiently.
Importing Modules
There are multiple ways to import modules:
1. Import Entire Module
import math
print(math.sqrt(16))
2. Import Specific Functions
from math import sqrt, ceil
print(sqrt(16))
print(ceil(4.2))
3. Using Module Alias
import math as m
print(m.factorial(5))
Built-in Modules in Python
Python provides many built-in modules that simplify development.
Math Module
Used for mathematical operations.
import math
print(math.sqrt(16))
print(math.ceil(4.2))
print(math.floor(4.9))
print(math.pow(2, 3))
print(math.factorial(5))
Common functions:
- sqrt()
- ceil()
- floor()
- pow()
- factorial()
Random Module
Used for generating random values.
import random
print(random.random())
print(random.randint(1, 10))
print(random.choice([1, 2, 3, 4]))
Shuffling example:
lst = [1, 2, 3, 4]
random.shuffle(lst)
print(lst)
Other function:
- uniform(a, b) → random float between a and b
Datetime Module
Used for working with dates and time.
from datetime import datetime, date, timedelta
print(datetime.now())
print(date.today())
d = datetime.now()
print(d.strftime("%Y-%m-%d"))
new_date = d + timedelta(days=5)
print(new_date)
Key functionalities:
- Current date and time
- Formatting dates
- Date arithmetic
OS Module
Used for interacting with the operating system.
import os
print(os.getcwd())
print(os.listdir())
Common operations:
- Get current directory
- List files
- Create/delete folders
- Rename files
Key Takeaways
- Modules help reuse code
- Packages help organize large projects
- Built-in modules save development time
- Proper imports improve code readability
Assignment Questions
Basic Level
- Import the math module and find square root of a number
- Import specific functions from math and use ceil() and floor()
- Generate a random number between 1 and 50
- Print current date using datetime
- Print current working directory using os
Intermediate Level
- Generate a list of 5 random numbers using random.randint()
- Shuffle a list of numbers using random.shuffle()
- Format current date as DD-MM-YYYY
- Create a program to add 7 days to current date
- List all files in your current directory
Advanced Level
- Create your own module with functions (e.g., add, subtract) and import it
- Create a package with at least 2 modules and use them
- Build a mini project using math and random (e.g., number guessing game)
- Write a script to organize files in a folder using os
- Combine datetime and os to log file creation time
Summary
Modules and packages are fundamental for writing scalable Python applications.
- Modules allow code reuse
- Packages provide structure
- Built-in modules handle complex operations easily
Understanding this concept is essential before moving into:
- Large projects
- Frameworks
- Real-world software development
April Python Bootcamp Day 12
Python Coding April 20, 2026 Bootcamp, Python No comments
Lambda Functions
A lambda function is a small, anonymous function written in a single line. It does not require a function name or the def keyword.
Syntax
lambda arguments: expression
Key characteristics:
- No function name
- No def keyword
- Only one expression allowed
- Used for short and simple operations
Normal Function vs Lambda Function
Normal Function
def add(a, b):
return a + b
print(add(4, 5))
Lambda Function
add1 = lambda a, b: a + b
print(add1(2, 3))
Another example:
square = lambda x: x ** 2
print(square(5))
Lambda functions are useful when you need a quick function for a short period of time.
Higher Order Functions
A higher order function is a function that:
- Takes another function as input, or
- Returns a function as output
Common examples include:
- map()
- filter()
- sorted()
map() Function
Applies a function to every element of an iterable.
nums = [1, 2, 3, 4, 5]
result = list(map(lambda x: x * 2, nums))
print(result)
Output:
[2, 4, 6, 8, 10]
filter() Function
Filters elements based on a condition.
nums = [1, 2, 3, 4, 5]
even = list(filter(lambda x: x % 2 == 0, nums))
print(even)
Output:
[2, 4]
sorted() with Lambda
Used for custom sorting logic.
students = [("Piyush", 20), ("Rahul", 18), ("Amar", 24)]
sorted_list = sorted(students, key=lambda x: len(x[0]))
print(sorted_list)
This sorts based on the length of names.
Key Points About Lambda Functions
- Best for short and simple logic
- Can take multiple arguments
- Limited to a single expression
- Not suitable for complex logic
Recursion
Recursion is a technique where a function calls itself to solve a problem.
Rules of Recursion
Every recursive function must have:
-
Base Case
Condition where recursion stops -
Recursive Case
Function calling itself
Without a base case, recursion will lead to infinite calls and crash the program.
Example: Print Numbers
def print_nums(n):
if n > 5: # Base case
return
print(n, end=" ")
print_nums(n + 1)
print_nums(1)
Output:
1 2 3 4 5
Example: Factorial Using Recursion
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
print(factorial(5))
When to Use Recursion
- When a problem can be broken into smaller subproblems
- Tree structures
- Backtracking problems
- Divide and conquer algorithms
When Not to Use Recursion
- When it increases complexity unnecessarily
- Risk of stack overflow
- When a loop provides a simpler solution
Assignment Questions
Basic Level
- Create a lambda function to add two numbers
- Write a lambda function to find square of a number
- Use map() with lambda to multiply all elements of a list by 3
- Use filter() to get all odd numbers from a list
- Sort a list of integers using lambda
Intermediate Level
- Use map() to convert a list of strings to uppercase
- Use filter() to remove negative numbers from a list
- Sort a list of tuples based on the second value
- Write a recursive function to print numbers from 1 to n
- Modify recursion example to print numbers in reverse
Advanced Level
- Write a recursive function to calculate factorial
- Write a recursive function to calculate Fibonacci series
- Create a function using both lambda and map() to square a list
- Implement a recursive function to find sum of digits of a number
- Compare recursion vs loop for factorial and analyze performance
Summary
- Lambda functions provide a concise way to write small functions
- Higher order functions like map(), filter(), and sorted() enhance functional programming
- Recursion is powerful but must be used carefully
- Choosing between recursion and iteration depends on problem complexity
Popular Posts
-
What you'll learn Understand why version control is a fundamental tool for coding and collaboration Install and run Git on your local ...
-
In today’s world, data is not just digital — it’s geospatial . Every day, satellites capture massive amounts of imagery about our planet. ...
-
Explanation: π§ 1. Operator Precedence (Priority Rules) In Python, logical operators follow this order: and → evaluated first or → evaluat...
-
Explanation: πΉ Step 1: Create List x = [1, 2, 3] A list is created π x = [1, 2, 3] πΉ Step 2: Start Loop for i in x: Python starts itera...
-
Explanation: πΉ 1. Variable Assignment clcoding is a variable used to store a value. πΉ 2. int() Function int() converts a value into an i...
-
Explanation: πΉ Step 1: Understand the Expression x and [] or [0] This uses short-circuit evaluation Python evaluates left → right πΉ Step...
-
π Day 22/150 – Simple Interest in Python Calculating Simple Interest (SI) is a fundamental concept in both mathematics and programming. It...
-
Code Explanation: πΉ Step 1: Create Tuple a = (1, [2, 3]) A tuple is created → (1, [2, 3]) Tuple is immutable ❌ But it contains a list [2,...
-
Master Data Science with Python: Exploring Coursera's "Python for Applied Data Science AI" Python has become a cornerstone f...
-
Machine learning is powerful — but understanding it through theory alone is not enough. The real learning happens when you apply algorithm...

.png)
.png)
.png)
