Saturday, 10 October 2026

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

 


Code Explanation:

Step 1: Define the Base Class

class Base:

This creates a parent class named Base. Other classes can inherit its attributes and methods.

Step 2: Create a Class Attribute
x = 1

The class attribute x is initialized to 1.
Initially:
Base.x = 1


Step 3: Define __init_subclass__()
def __init_subclass__(cls):

Python automatically calls __init_subclass__() when a new class inherits from Base or from a subclass that inherits this method.
The parameter cls refers to the newly created subclass.
For example, when class A is created:
cls → A

Step 4: Update the Subclass Attribute
cls.x += 2

This is equivalent to:
cls.x = cls.x + 2

Python looks up the current value of cls.x, adds 2, and assigns the result to the subclass.
If the subclass does not have its own x, Python initially finds the inherited value.

Step 5: Create Class A
class A(Base):    pass

Class A inherits from Base.
When A is created, Python automatically calls:
Base.__init_subclass__(A)


Initially, A inherits Base.x, which is 1.
So:
A.x = 1 + 2
A.x = 3


The augmented assignment creates an x attribute directly on A. It does not change Base.x.
Now:
Base.x = 1
A.x    = 3


Step 6: Create Class B
class B(A):    pass

Class B inherits from A.
The inherited __init_subclass__() method runs again, this time with cls referring to B.
Since B does not initially define its own x, Python finds A.x, which is 3.
Therefore:
B.x = 3 + 2
B.x = 5

Now each class has its own effective value:
Base.x = 1
A.x    = 3
B.x    = 5

Step 7: Print the Values
print(Base.x, A.x, B.x)

Python prints the three class attributes in order:
- Base.x → 1
- A.x → 3
- B.x → 5

Final output:

1 3 5


300 Days Python Coding Challenges with Explanation

๐Ÿ Python Pattern Challenge — Day 22

 


๐Ÿ Python Pattern Challenge — Day 22

Pattern printing is a great way to improve your Python loops, conditions, repetition, and logical thinking. For Day 22, let's try something different by combining numbers and stars in the same pattern.

๐ŸŽฏ Today's Challenge

Write a Python program to print:


The pattern alternates between numbers and *, making it simple but different from the usual star-only patterns.

Best and cleanest code will be rewarded! ๐Ÿ†


Solution 1 — Using Nested Loops

for i in range(1, 7): for j in range(1, i + 1): if j % 2 == 1: print((j + 1) // 2, end=" ") else: print("*", end=" ") print()





How it works

The outer loop controls the number of rows:

for i in range(1, 7):

The inner loop controls how many elements are printed in each row:

for j in range(1, i + 1):

Then we check whether the position is odd or even:

if j % 2 == 1:

Odd positions contain numbers:

1 2 3

Even positions contain stars:

* * *

Solution 2 — Using a Simple List

items = [1, "*", 2, "*", 3, "*"] for i in range(1, len(items) + 1): print(*items[:i])




Output

1 1 * 1 * 2 1 * 2 * 1 * 2 * 3 1 * 2 * 3 *





This is a very clean approach because every row simply takes one more element from the list.


Solution 3 — Using a Function

def pattern(n): for i in range(1, n + 1): for j in range(1, i + 1): print( (j + 1) // 2 if j % 2 else "*", end=" " ) print() pattern(6)







Using a function makes the pattern reusable.

Try:

pattern(10)

to generate more rows.

⚡ Short & Clean Code

a = [1, "*", 2, "*", 3, "*"] for i in range(1, 7): print(*a[:i])




๐Ÿ”ฅ Just a few lines are enough to create the complete pattern.


๐Ÿš€ Challenge Yourself

Can you modify this pattern:

  • Continue it up to 5 or 10 numbers?
  • Replace * with #?
  • Create the same pattern using a while loop?
  • Reverse the pattern?
  • Take the number of rows using input()?
  • Create your own pattern by mixing numbers, letters, and symbols?

Drop your solution below! ๐Ÿ‘‡

22 Days. 22 Patterns. Stronger Python Logic. ๐Ÿ๐Ÿ”ฅ

Learn • Practice • Grow with CLCODING ๐Ÿš€


Projects: 107 Pattern Plots Using Python

Python Coding Challenge - Question with Answer (ID 101026)

 




Explanation:

๐ŸŸข Step 1: Create a List
x = [2, 4, 6, 8]

- A list named x is created with four elements.
- The elements have indexes 0, 1, 2, and 3.
Index Value
0 2
1 4
2 6
3 8

๐ŸŸข Step 2: Understand pop(1)
x.pop(1)

- The pop() method removes and returns the element at the specified index.
- Index 1 contains the value 4.
- Python returns 4 and removes it from the list.
Updated list:
[2, 6, 8]



๐ŸŸข Step 3: Understand pop(0)
x.pop(0)

- The updated list is [2, 6, 8].
- Index 0 contains 2.
- Python returns 2 and removes it.
Updated list:
[6, 8]

๐ŸŸข Step 4: Understand x[-1]
x[-1]
- Negative indexing accesses elements from the end of a list.
- x[-1] returns the last element.
- The current list is [6, 8], so x[-1] returns 8.

๐ŸŸข Step 5: Evaluate the Expression
x.pop(1) + x.pop(0) * x[-1



Python evaluates multiplication before addition, so the expression becomes:
\[
4 + 2 \times 8
\]

First, multiplication:
\[
2 \times 8 = 16
\]

Then, addition:
\[
4 + 16 = 20
\]

The final list after both pop() operations is [6, 8].

✅ Final Output
20

Books: Python for Aerospace & Satellite Data Processing

Friday, 9 October 2026

Calculus (Free PDF)

 



Calculus by Gilbert Strang: Understanding the Mathematics of Change

Introduction

Calculus is one of the most important branches of mathematics. It helps us understand how quantities change, how motion works, how areas and volumes can be calculated, and how complex systems behave. Its applications range from physics and engineering to economics, data science, and artificial intelligence.

Calculus by Gilbert Strang is a well-known textbook designed to help students understand these ideas through intuitive explanations, practical examples, and a structured progression of topics.

Written by Gilbert Strang, a mathematics professor at MIT, the book was first published by Wellesley-Cambridge Press in 1991. It covers both single-variable and multivariable calculus, connecting mathematical concepts with applications in science and engineering. The textbook is also available through MIT OpenCourseWare, making it a valuable resource for independent learners.

Download the PDF for free: https://ocw.mit.edu/courses/res-18-001-calculus-fall-2023/mitres_18_001_f17_full_book.pdf

What Makes This Book Special?

Many students approach calculus as a subject filled with rules, symbols, and lengthy calculations. Strang takes a more conceptual approach, emphasizing what calculus means and why its ideas are useful.

The book introduces concepts through familiar situations such as motion, velocity, distance, and changing quantities. It then develops the mathematical tools needed to analyze these situations.

Another strength is its connection between calculus and computation. Readers are encouraged to see mathematics as a way to understand real problems, not merely as a collection of exercises.

Key Topics Covered in the Book

1. Introduction to Calculus

The opening chapters introduce the relationship between changing quantities and the mathematical ideas used to describe them.

Examples involving motion and distance help readers understand how an object's position changes over time. These intuitive starting points prepare students for more formal topics such as derivatives and integrals.

2. Derivatives and Rates of Change

Derivatives describe how quickly a quantity changes at a particular point. They are essential for understanding motion, growth, optimization, and the behavior of functions.

The book explores slopes, tangent lines, limits, continuity, and the rules used to calculate derivatives. It also explains how derivatives can reveal important features of a graph.

These concepts are useful in physics, economics, engineering, and machine learning, where understanding changing quantities is often essential.

3. Applications of Derivatives

Calculus becomes especially powerful when its concepts are applied to practical problems.

This section explores maximum and minimum problems, graph analysis, linear approximation, and numerical methods such as Newton's method.

These techniques help answer questions such as how to maximize efficiency, minimize costs, approximate complicated functions, or find solutions to equations.

4. Integrals and Accumulation

Integration provides a way to calculate accumulated quantities, including areas, volumes, and total changes.

The book develops the ideas behind antiderivatives, definite and indefinite integrals, numerical integration, and the fundamental relationship between differentiation and integration.

These concepts are important for calculating physical quantities, analyzing probability distributions, and understanding many mathematical models used in science.

5. Exponential Functions and Logarithms

Exponential functions and logarithms appear in many models of growth and decay. They are used to describe population changes, compound interest, radioactive decay, and other processes.

Strang explores their mathematical properties and applications, helping readers understand why these functions are so important in scientific and quantitative work.

6. Techniques and Applications of Integration

The book moves beyond the basics to explore integration techniques and their applications to areas, volumes, curve lengths, probability, mass, moments, force, work, and energy.

These topics demonstrate how integration connects abstract mathematical ideas with measurable quantities in the physical world.

7. Infinite Series

Infinite series help represent functions and approximate complicated mathematical expressions.

The book introduces convergence, power series, and Taylor series, developing tools that are useful in numerical computation and mathematical modeling.

These concepts also provide background for understanding approximation methods used in scientific computing and machine learning.

8. Vectors and Matrices

Vectors and matrices connect calculus with linear algebra and multidimensional geometry.

Readers explore vector operations, projections, determinants, systems of linear equations, and related concepts. This material is particularly useful for students preparing to study optimization, computational mathematics, or machine learning.

9. Partial Derivatives and Multivariable Calculus

Many real-world problems involve several changing variables. Multivariable calculus extends the ideas of derivatives and integrals to these situations.

The book covers partial derivatives, gradients, tangent planes, directional derivatives, constrained optimization, and multiple integrals.

These concepts are fundamental to understanding how machine learning models are optimized and how functions behave across multiple dimensions.

10. Vector Calculus and Further Applications

The later material introduces vector fields, line integrals, surface integrals, and major results such as Green's theorem, the divergence theorem, and Stokes' theorem.

These topics are important in advanced physics, electromagnetism, fluid mechanics, and engineering.

The book's complete topic structure is documented in the author's table of contents and MIT OpenCourseWare materials.

Why Is Calculus Important for Data Science and AI?

Calculus provides much of the mathematical foundation behind modern machine learning.

  • Gradient-based optimization: Derivatives help learning algorithms determine how model parameters should change to reduce prediction errors.

  • Deep learning: Gradients and the chain rule are central to training neural networks through backpropagation.

  • Probability and statistics: Integration helps describe continuous probability distributions and calculate probabilities.

  • Scientific computing: Numerical methods help solve problems that are difficult to handle analytically.

  • Mathematical modeling: Calculus helps describe continuous change in systems ranging from physical processes to economic behavior.

For learners pursuing data science or AI, this book can build the conceptual foundation needed to understand these techniques. However, readers will still need dedicated resources on probability, statistics, linear algebra, and machine learning.

Who Should Read This Book?

This book is particularly suitable for:

  • College and university students studying mathematics, engineering, physics, or computer science.

  • Data science learners who want to strengthen their mathematical foundations.

  • Aspiring machine learning engineers preparing to study optimization and neural networks.

  • Self-learners looking for a structured introduction to single-variable and multivariable calculus.

  • Teachers and educators seeking an application-oriented calculus textbook.

  • Science and engineering students who need calculus for quantitative problem-solving.

Students who are unfamiliar with algebra, functions, and trigonometry should review those subjects before beginning the more advanced chapters.

Strengths of the Book

Conceptual explanations: The book emphasizes understanding mathematical ideas and their significance.

Practical applications: Examples connect calculus with motion, geometry, probability, physics, and engineering.

Broad topic coverage: It progresses from introductory concepts to multivariable calculus and vector calculus.

Computational perspective: The material encourages readers to connect mathematics with numerical methods and computation.

Accessible learning resources: MIT OpenCourseWare provides the textbook alongside study guides, an instructor's manual, and supplementary learning materials.

Limitations to Consider

Although the book is comprehensive, it may not be the easiest choice for every beginner.

First, calculus requires consistent practice. Reading the explanations without solving problems will not be enough to develop fluency.

Second, some topics require a reasonable foundation in algebra, trigonometry, and functions. Learners who are missing these prerequisites may need additional introductory material.

Finally, this is a mathematics textbook rather than a Python programming or machine learning project guide. Students who want to implement calculus concepts in code should complement it with computational exercises and practical programming resources.

Hard Copy: Calculus

Download the PDF for free: https://ocw.mit.edu/courses/res-18-001-calculus-fall-2023/mitres_18_001_f17_full_book.pdf

Final Verdict

Calculus by Gilbert Strang is a valuable resource for anyone who wants to understand the mathematics of change and accumulation. Its combination of conceptual explanations, applications, and broad mathematical coverage makes it useful for both formal study and independent learning.

For data science and AI learners, its greatest benefit is the foundation it provides for understanding derivatives, gradients, integration, approximation, and optimization. These ideas become much easier to appreciate when learners understand the mathematics behind them rather than relying entirely on software libraries.

If your goal is to build strong mathematical foundations for engineering, scientific computing, or machine learning, this book is well worth considering.


✨ Python Turtle The Neon Lotus Firework

 


Code:

import turtle import math import time screen = turtle.Screen() screen.setup(700, 700) screen.bgcolor("#02020a") screen.tracer(0) t = turtle.Turtle() t.hideturtle() t.speed(0) t.width(2) colors = [ "#ff1744", "#ff4081", "#d500f9", "#7c4dff", "#00e5ff", "#00ff9d" ] # ๐ŸŒธ Lotus petals for layer in range(13): t.color(colors[layer % len(colors)]) size = 55 + layer * 11 for petal in range(12): rotation = petal * 30 + layer * 2 t.penup() for i in range(81): a = math.radians(i * 4.5) # Smooth pointed petal r = size * math.sin(a) r *= 0.75 + 0.25 * math.sin(a * 2) x = r * math.cos(a) y = r * math.sin(a) rot = math.radians(rotation) X = x * math.cos(rot) - y * math.sin(rot) Y = x * math.sin(rot) + y * math.cos(rot) t.goto(X, Y) if i == 0: t.pendown() screen.update() time.sleep(0.012) time.sleep(0.08) # ✨ Sparkling center for r in range(40, 2, -2): t.penup() t.goto(0, -r) t.dot(r, colors[r % len(colors)]) screen.update() time.sleep(0.08) t.penup() t.goto(0, -6) t.dot(12, "white") turtle.done()


























Explanation:

1. Import Libraries
import turtleimport mathimport time

- turtle → draws the lotus flower.
- math → performs mathematical and trigonometric calculations.
- time → controls animation speed.

2. Create the Screen
screen = turtle.Screen()screen.setup(700, 700)screen.bgcolor("#02020a")screen.tracer(0)

- Creates a 700 × 700 drawing window.
- Sets a dark background.
- tracer(0) disables automatic screen updates.

3. Configure the Turtle
t = turtle.Turtle()t.hideturtle()t.speed(0)t.width(2)

- Creates the drawing turtle.
- Hides the turtle cursor.
- speed(0) sets maximum drawing speed.
- width(2) sets the line thickness.

4. Define Neon Colors
colors = [    "#ff1744", "#ff4081",    "#d500f9", "#7c4dff",    "#00e5ff", "#00ff9d"]

- Stores six neon colors.
- The colors repeat across the lotus layers.

5. Create Lotus Layers
for layer in range(13):

- Creates 13 layers of petals.
- Each layer uses a different size and rotation.

6. Select the Layer Color
t.color(colors[layer % len(colors)])

- Selects a color for the current layer.
- The modulo operator % repeats the colors when needed.

7. Calculate Petal Size
size = 55 + layer * 11

- Starts with a size of 55.
- Increases the size by 11 for each new layer.
- Creates an expanding lotus effect.

8. Create 12 Petals
for petal in range(12):

- Generates 12 petal curves in each layer.

9. Calculate Petal Rotation
rotation = petal * 30 + layer * 2

- petal * 30 separates 12 petals evenly around 360°.
- layer * 2 slightly rotates each new layer.

10. Lift the Pen
t.penup()

- Prevents unwanted lines when moving to the starting position.

11. Generate Petal Points
for i in range(81):

- Generates 81 points for each petal curve.
- More points help create a smooth shape.

12. Calculate the Angle
a = math.radians(i * 4.5)

- Converts degrees into radians.
- The angle progresses from 0° to 360°.

13. Create the Petal Radius
r = size * math.sin(a)

- Calculates the distance from the center.
- The sine function helps form the pointed petal shape.

14. Smooth the Petal Shape
r *= 0.75 + 0.25 * math.sin(a * 2)

- Modifies the radius using another sine wave.
- Adds a smooth, curved appearance to the petals.

15. Calculate X Coordinate
x = r * math.cos(a)

- Calculates the horizontal position of the point.

16. Calculate Y Coordinate
y = r * math.sin(a)

- Calculates the vertical position.
- Together, x and y create the petal curve.

17. Convert the Rotation Angle
rot = math.radians(rotation)

- Converts the petal's rotation angle into radians.

18. Rotate the X Coordinate
X = x * math.cos(rot) - y * math.sin(rot)

- Calculates the rotated horizontal position.

19. Rotate the Y Coordinate
Y = x * math.sin(rot) + y * math.cos(rot)

- Calculates the rotated vertical position.
- Together, these formulas rotate each petal around the center.

20. Move to the Calculated Point
t.goto(X, Y)

- Moves the turtle to the new coordinates.

21. Start Drawing the Petal
if i == 0:    t.pendown()

- Lowers the pen at the first point.
- The following points are connected to draw the curve.

22. Animate the Petals
screen.update()time.sleep(0.012)

- Refreshes the screen.
- Adds a short delay to show the drawing animation.

23. Pause Between Petals
time.sleep(0.08)

- Adds a small pause after each petal.
- Makes the drawing process easier to watch.

24. Create the Sparkling Center
for r in range(40, 2, -2):

- Creates circles with decreasing sizes.
- The radius decreases from 40 to 4.
t.penup()t.goto(0, -r)t.dot(r, colors[r % len(colors)])

- Moves to the center area.
- Draws colorful dots of different sizes.
- Produces a layered, sparkling center.

25. Animate the Center Glow
screen.update()time.sleep(0.08)

- Updates the screen after each circle.
- Creates a gradual glowing effect.

26. Add the White Core
t.penup()t.goto(0, -6)t.dot(12, "white")

- Adds a white dot in the center.
- Creates a bright highlight.

27. Finish the Drawing
turtle.done()

- Keeps the Turtle graphics window open.

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (347) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (16) C (78) C# (12) C++ (83) cloud (1) Course (93) Coursera (305) Cybersecurity (36) data (10) Data Analysis (47) Data Analytics (31) data management (16) Data Science (435) Data Strucures (19) Deep Learning (222) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Gadgets (1) Generative AI (78) Git (13) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (407) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1380) Python Coding Challenge (1272) Python Library (22) Python Mathematics (20) Python Mistakes (51) Python Pattern Challenge (19) Python Quiz (655) Python Tips (112) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (22) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)