Sunday, 5 April 2026

Python Coding challenge - Day 1121| What is the output of the following Python Code?

 


Code Explanation:

1. Class Definition Phase
class Test:
    x = 10
✅ What happens:
A class named Test is created.
A class variable x is defined and assigned value 10.

๐Ÿ‘‰ At this point:

Test.x = 10

๐Ÿ“Œ 2. Constructor (__init__) Definition
def __init__(self):
    self.x = self.x + 5
✅ What happens:
This runs every time an object is created.
self.x refers to:
First tries instance variable
If not found → falls back to class variable

๐Ÿ“Œ 3. Creating First Object (t1)
t1 = Test()

Step-by-step:
๐Ÿ”น Step 1: Object is created
Python creates a new object t1.
๐Ÿ”น Step 2: __init__ runs
self.x = self.x + 5
self.x → no instance variable yet
So Python looks at class variable → Test.x = 10

๐Ÿ‘‰ Calculation:

self.x = 10 + 5 = 15
๐Ÿ”น Step 3: Instance variable created

Now:

t1.x = 15   (instance variable)

๐Ÿ“Œ 4. Creating Second Object (t2)
t2 = Test()
Step-by-step:

Same process repeats:

self.x → still no instance variable
Uses class variable again → 10

๐Ÿ‘‰ Calculation:

self.x = 10 + 5 = 15

Now:

t2.x = 15

๐Ÿ“Œ 5. Important Concept: Class vs Instance Variable

At this point:

Variable Value
Test.x 10
t1.x 15
t2.x 15

๐Ÿ‘‰ Key idea:

self.x = ... creates a new instance variable
It does NOT modify the class variable

๐Ÿ“Œ 6. Final Print Statement
print(t1.x, t2.x, Test.x)
Values:
t1.x → 15
t2.x → 15
Test.x → 10

✅ Final Output
15 15 10

Python Coding Challenge - Question with Answer (ID -050426)

 


Code Explanation:

๐Ÿ”ธ 1. List Creation (a)
A list [1,2] is created
Stored at some memory location
Variable a points to that location

๐Ÿ”ธ 2. List Creation (b)
Another list [1,2] is created
This is a different object in memory
Variable b points to a new location

๐Ÿ”ธ 3. Equality Check (a == b)
== compares values inside objects
Both lists have same elements [1,2]
✅ Result → True

๐Ÿ”ธ 4. Identity Check (a is b)
is compares memory location (object identity)
a and b are different objects
❌ Result → False

๐Ÿ”ธ 5. Final Output
True False

Book: 100 Python Automation Projects for Smart Developers

Saturday, 4 April 2026

๐Ÿš€ Day 12/150 – Check Leap Year in Python

 

Welcome back to the 150 Days of Python series! ๐ŸŽฏ
Today, we’re solving a real-world logic problem — checking whether a year is a leap year.

This is a great exercise to understand:

  • Conditional statements
  • Logical operators (and, or)
  • Writing clean, readable code

 Problem Statement

Write a Python program to check whether a given year is a Leap Year or Not.


 Understanding Leap Year Logic

Before coding, let’s understand the rules:

A year is a leap year if:

✔ Divisible by 4
❌ But NOT divisible by 100
✔ EXCEPTION: If divisible by 400, then it is a leap year


Year Result
2024 ✅ Leap Year
1900 ❌ Not Leap Year
2000 ✅ Leap Year


 Method 1 – Using if-elif-else

year = 2024 if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): print("Leap Year") else: print("Not a Leap Year")








 Code Explanation:

  • year % 4 == 0
    ๐Ÿ‘‰ Checks if year is divisible by 4
  • year % 100 != 0
    ๐Ÿ‘‰ Ensures it is NOT divisible by 100
  • year % 400 == 0
    ๐Ÿ‘‰ Special case: makes century years valid
  • and → both conditions must be true
  • or → at least one condition must be true

๐Ÿ‘‰ This full condition ensures accurate leap year calculation

 Method 2 – Taking User Input

year = int(input("Enter a year: ")) if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): print("Leap Year") else: print("Not a Leap Year")






๐Ÿ” What’s new here?
  • input() → takes user input
  • int() → converts string input into integer

๐Ÿ‘‰ Always convert input when dealing with numbers!

Method 3 – Using a Function

def check_leap_year(year): if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): return "Leap Year" else: return "Not a Leap Year" print(check_leap_year(2024))







๐Ÿ” Why use a function?
  • Code becomes reusable
  • Cleaner structure
  • Easy to test

๐Ÿ‘‰ You can call this function anytime with different values


Method 4 – Using Python calendar Module

import calendar year = 2024 if calendar.isleap(year): print("Leap Year") else: print("Not a Leap Year")








๐Ÿ” Explanation:
  • calendar.isleap(year)
    ๐Ÿ‘‰ Built-in Python function
    ๐Ÿ‘‰ Returns True or False

๐Ÿ‘‰ This is the simplest and most reliable method

Important Things to Remember

✔ Always use correct logical condition
✔ Don’t forget parentheses ( ) in conditions
✔ Convert input using int()
✔ Built-in functions save time and reduce errors


Summary

MethodConcept
if-elseBasic logic
User InputReal-world usage
FunctionReusability
calendar modulePythonic approach

Python Coding Challenge - Question with Answer (ID -040426)

 






Code Explanation:

๐Ÿ”ธ 1. List Creation
clcoding = [1, 2, 3]
A list named clcoding is created.
It contains three integer elements: 1, 2, and 3.
This list will be used for iteration in the loop.

๐Ÿ”ธ 2. For Loop Declaration
for i in clcoding:
A for loop starts.
It iterates over each element of the list clcoding.
On each iteration, the current value is stored in the variable i.

๐Ÿ‘‰ Iterations happen like this:

1st → i = 1
2nd → i = 2
3rd → i = 3

๐Ÿ”ธ 3. Pass Statement
pass
pass is a null statement (does nothing).
It is used as a placeholder when a statement is syntactically required but no action is needed.
So, during each loop iteration, nothing happens.

๐Ÿ”ธ 4. Print Statement
print(i)
This is outside the loop.
After the loop finishes, i still holds the last value assigned in the loop.

๐Ÿ‘‰ Final value of i = 3

๐Ÿ”น Output
3

Book:  BIOMEDICAL DATA ANALYSIS WITH PYTHON

Friday, 3 April 2026

๐Ÿš€ Day 9/150 – Check Positive or Negative Number in Python

 

1️⃣ Method 1 – Using If-Else Conditions

The simplest and most common approach.

num = 10 if num > 0: print("Positive number") elif num < 0: print("Negative number") else: print("Zero")








Output
Positive number

✔ Easy to understand
✔ Best for beginners

2️⃣ Method 2 – Taking User Input

Make the program interactive.

num = float(input("Enter a number: ")) if num > 0: print("Positive number") elif num < 0: print("Negative number") else: print("Zero")



✔ Works with decimal numbers
✔ Useful in real-world applications

3️⃣ Method 3 – Using a Function

Reusable and clean approach.

def check_number(n): if n > 0: return "Positive" elif n < 0: return "Negative" else: return "Zero" print(check_number(-5))





✔ Reusable logic
✔ Cleaner code

4️⃣ Method 4 – Using Lambda Function

One-line solution using lambda.

check = lambda n: "Positive" if n > 0 else ("Negative" if n < 0 else "Zero") print(check(0))





✔ Compact code

✔ Useful for quick operations

๐ŸŽฏ Key Takeaways

Today you learned:

  • Conditional statements (if, elif, else)
  • Handling user input
  • Writing reusable functions
  • Using lambda expressions

๐Ÿš€ Day 2/150 – Add Two Numbers in Python


๐Ÿš€ Day 2/150 – Add Two Numbers in Python

Let’s explore different ways to do it.

1️⃣ Basic Addition (Direct Method)

This is the most straightforward way.

a = 10 b = 5 result = a + b print(result)






✅ Simple
✅ Beginner-friendly
✅ Most commonly used

If you're just starting Python, this is your foundation.

2️⃣ Taking User Input

Now let’s make it interactive.
a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) print("Sum:", a + b)



Why int()?

Because input() always returns a string.
We convert it into an integer before adding.

๐Ÿ’ก This teaches:

Type conversion

Real-world program interaction

3️⃣ Using a Function

Functions make your code reusable.

def add_numbers(x, y): return x + y print(add_numbers(10, 5))

Why use functions?

Clean code

Reusability

Better structure

Important for larger projects

This is how professionals write code.

4️⃣ Using Lambda (One-Line Function)

For short operations, Python allows anonymous functions.

add = lambda x, y: x + y print(add(10, 5))




When to use?

Quick operations

Functional programming

Passing functions as arguments

Short. Elegant. Powerful.

5️⃣ Using sum() Built-in Function

numbers = [10, 5] print(sum(numbers))




sum() is useful when adding multiple values.

Example:

print(sum([1, 2, 3, 4, 5]))

This is more scalable.

6️⃣ Using Recursion

def add(a, b): if b == 0: return a return add(a + 1, b - 1) print(add(10, 5))








This method doesn’t use + directly in the usual way.

It demonstrates:

Recursion

Base case

Recursive case

Stack behavior

⚠️ Not practical for real-world addition, but great for understanding logic.

๐ŸŽฏ What Should You Actually Use?

SituationBest Method
Normal programs        
a + b
Reusable logicFunction
Many numberssum()
Interview discussionRecursion / Bitwise
Functional programmingLambda

๐Ÿ’ญ Why Learn Multiple Ways?

Because programming isn’t about memorizing syntax.

It’s about:

  • Understanding concepts

  • Improving problem-solving

  • Writing clean code

  • Thinking differently

The more ways you know, the sharper your logic becomes.


Popular Posts

Categories

100 Python Programs for Beginner (119) AI (275) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (11) BI (10) Books (262) Bootcamp (11) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (31) data (6) Data Analysis (34) Data Analytics (22) data management (15) Data Science (366) Data Strucures (21) Deep Learning (173) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (20) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (73) Git (10) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (42) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (314) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (14) PHP (20) Projects (34) Python (1376) Python Coding Challenge (1156) Python Mathematics (1) Python Mistakes (51) Python Quiz (534) Python Tips (6) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (51) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)