Sunday, 8 November 2026

Reinforcement Learning: Mathematics and algorithms from MDPs to RLHF, InstructGPT, and DeepSeek-R1, with Python (FREE PDF)

 


Reinforcement Learning: From Mathematics and MDPs to RLHF, InstructGPT, and DeepSeek-R1 — A Complete Learning Guide

Introduction

Reinforcement Learning (RL) is one of the most important areas of Artificial Intelligence. It focuses on teaching intelligent systems how to make decisions through interaction, experience, and feedback.

Unlike traditional supervised learning, where models learn from labeled examples, reinforcement learning allows an agent to explore an environment, take actions, receive rewards, and improve its future decisions.

This approach is used in robotics, autonomous systems, game-playing agents, recommendation systems, and modern Large Language Models.

The book Reinforcement Learning: Mathematics and Algorithms from MDPs to RLHF, InstructGPT, and DeepSeek-R1, with Python, by Dipesh Gautam, provides a comprehensive learning path from the mathematical foundations of reinforcement learning to its applications in modern generative AI.

One of its most valuable features is the combination of mathematical explanations, algorithmic concepts, and practical Python implementations. It aims to make the mathematics behind reinforcement learning understandable while showing how the algorithms work in practice.

The book is particularly relevant for Machine Learning students, AI developers, researchers, and anyone interested in understanding how modern AI systems learn to make better decisions.


Download the PDF for free: Reinforcement Learning

What Is Reinforcement Learning?

Reinforcement Learning is a machine learning approach in which an agent learns to make decisions by interacting with an environment.

The agent observes the current situation, selects an action, and receives feedback in the form of a reward.

The objective is to learn a strategy that produces better long-term outcomes.

Consider a simple example of a robot learning to navigate a room.

The robot starts in a particular location and needs to reach a destination. It can move in different directions, but some movements may bring it closer to the goal while others may create obstacles.

Initially, the robot may make incorrect decisions. Through repeated interactions and feedback, it gradually learns which actions are more useful.

This learning process is the foundation of reinforcement learning.

What Makes This Book Special?

One of the strongest features of this book is its connection between traditional reinforcement learning and modern AI applications.

Many introductory resources focus mainly on classical reinforcement learning algorithms. However, this book extends the discussion toward Reinforcement Learning from Human Feedback, InstructGPT, and DeepSeek-R1-style training.

The book also emphasizes mathematical understanding rather than presenting algorithms as black boxes.

Readers are introduced to the probability and statistical concepts behind reinforcement learning, along with detailed explanations of important algorithmic results.

Another important feature is its practical approach. The theoretical concepts are paired with modular Python and PyTorch implementations that can be explored using small environments.

This makes it easier to understand how an algorithm works, how its components interact, and how mathematical ideas translate into executable code.

Key Topics Covered

Mathematical Foundations of Reinforcement Learning

The book begins with the mathematical concepts required to understand reinforcement learning.

Probability and statistics play an important role because reinforcement learning involves uncertainty, random actions, rewards, and changing environments.

Readers explore important probability concepts and statistical identities that support the derivation of reinforcement learning algorithms.

This foundation helps learners understand why different algorithms behave differently and how their learning processes can be analyzed.

Markov Decision Processes (MDPs)

Markov Decision Processes are among the most important foundations of reinforcement learning.

An MDP provides a structured way to represent decision-making problems involving states, actions, rewards, and transitions.

For example, consider an autonomous vehicle navigating through a road network.

Its current location and surrounding traffic conditions represent the state. Moving forward, turning, or stopping are possible actions. The resulting position and feedback describe the outcome of an action.

MDPs provide a framework for representing these decisions and studying how an agent can select actions to achieve its objectives.

Understanding MDPs is essential before exploring more advanced reinforcement learning algorithms.

Markov Reward Processes

Markov Reward Processes extend the idea of Markov systems by introducing rewards associated with states and transitions.

They help describe how an agent experiences rewards while moving through a sequence of states.

This concept is important because reinforcement learning does not simply focus on reaching the next state. It also considers the overall value of future rewards.

Readers can use this foundation to understand how an agent evaluates different situations and estimates the long-term usefulness of its experiences.

Bellman Equations

Bellman equations are fundamental to reinforcement learning.

They describe how the value of a decision-making situation can be understood through its immediate reward and the expected value of future situations.

These concepts form the foundation of many reinforcement learning algorithms.

The book explores Bellman expectation and optimality equations and develops their mathematical foundations step by step.

Understanding these ideas helps learners recognize how an agent estimates future rewards and improves its decision-making strategy.

Dynamic Programming

Dynamic Programming is an important approach for solving sequential decision-making problems.

It uses previously calculated information to solve larger problems more efficiently.

In reinforcement learning, dynamic programming methods are closely connected to value estimation and policy improvement.

Important concepts include policy evaluation, policy improvement, policy iteration, and value iteration.

These techniques are useful when the environment's transition and reward structure is known or can be modeled.

Monte Carlo Methods

Monte Carlo methods learn from complete experiences or episodes.

Instead of requiring a complete mathematical description of the environment, these methods use sampled experiences to estimate values.

For example, an agent playing a game can complete several rounds and use the rewards collected from those rounds to improve its future decisions.

Monte Carlo learning is an important step toward understanding how reinforcement learning algorithms learn directly from experience.

Temporal-Difference Learning

Temporal-Difference (TD) learning combines ideas from dynamic programming and Monte Carlo methods.

It allows an agent to update its estimates using information from its current experience without necessarily waiting for an entire episode to finish.

This makes TD learning useful in ongoing decision-making environments.

The topic is particularly important because several popular reinforcement learning algorithms build upon temporal-difference learning.

Q-Learning and Deep Q-Networks

Q-Learning is a model-free reinforcement learning algorithm that learns the value of taking particular actions in different states.

It helps an agent determine which actions are likely to produce better future rewards.

Deep Q-Networks (DQN) extend this idea by using neural networks to estimate action values.

This makes it possible to work with more complex environments where representing every state-action combination in a traditional table is impractical.

DQN is an important milestone in deep reinforcement learning and has been used in game-playing and other complex decision-making tasks.

Policy Gradient Methods

Policy Gradient methods directly optimize an agent's policy.

A policy describes how an agent selects actions based on its current state.

Instead of only learning action values, these methods improve the decision-making strategy itself.

They are particularly useful in environments involving large or continuous action spaces.

This section helps readers understand the transition from value-based reinforcement learning to policy-based learning.

Actor-Critic Methods

Actor-Critic methods combine two important components.

The actor is responsible for selecting actions, while the critic evaluates the quality of those decisions.

Both components work together to improve the agent's behavior.

This approach combines ideas from value-based and policy-based reinforcement learning.

Actor-Critic methods are widely used in modern deep reinforcement learning because they provide a flexible framework for learning complex policies.

Proximal Policy Optimization (PPO)

Proximal Policy Optimization is a popular policy optimization algorithm.

It is designed to improve an agent's policy while avoiding excessively large updates that could make learning unstable.

PPO is especially important in modern AI because it has been used in reinforcement learning pipelines for training and improving language models.

Understanding PPO provides a useful foundation for exploring RLHF and modern LLM post-training.

Reinforcement Learning from Human Feedback (RLHF)

Reinforcement Learning from Human Feedback is an approach that uses human preferences to guide the behavior of AI models.

Traditional language model training focuses heavily on predicting the next token. However, a model that predicts text accurately does not automatically produce responses that meet human expectations.

RLHF introduces human preference information into the training process.

For example, human evaluators may compare two AI-generated answers and indicate which response is more helpful, accurate, or appropriate.

This feedback can be used to train a reward model, which helps guide the optimization of the language model.

RLHF has become an important part of modern LLM alignment and post-training.

InstructGPT

InstructGPT is an important example of applying human feedback techniques to language model training.

The approach focuses on improving how language models follow user instructions.

Its training process connects supervised fine-tuning, human preference comparisons, reward modeling, and reinforcement learning.

This topic helps readers understand how a general-purpose language model can be further trained to produce responses that better match human expectations.

It also demonstrates how traditional reinforcement learning techniques can be adapted to modern language model development.

DeepSeek-R1 and Modern Reasoning Models

DeepSeek-R1 brought considerable attention to reinforcement learning as a technique for improving reasoning capabilities in Large Language Models.

Its approach demonstrates how reinforcement learning can encourage models to develop stronger problem-solving behavior, particularly in areas such as mathematics and coding.

The book explores DeepSeek-R1-style training and connects modern reasoning-oriented AI systems with the foundations of reinforcement learning.

Readers can develop an understanding of how reward-based training, policy optimization, and verifiable feedback contribute to the development of reasoning models.

This makes the book especially relevant for learners interested in the latest developments in Generative AI.

Python and PyTorch Implementations

One of the most practical aspects of the book is its focus on implementation.

Rather than studying algorithms only from a theoretical perspective, readers can explore how reinforcement learning techniques are translated into Python code.

The implementations use Python and PyTorch and are designed around small, inspectable environments.

This approach helps learners understand the relationship between mathematical concepts, algorithmic steps, and actual program execution.

Python is particularly useful for reinforcement learning because of its extensive ecosystem of scientific computing and machine learning libraries.

PyTorch provides tools for building neural networks, calculating gradients, and implementing deep reinforcement learning algorithms.

Through practical experimentation, learners can understand how an agent interacts with an environment, updates its policy, evaluates rewards, and improves its behavior.

Practical Skills You Can Develop

After studying this book and implementing its examples, readers can develop an understanding of:

  • Mathematical foundations of reinforcement learning.

  • Probability and statistics for sequential decision-making.

  • Markov Decision Processes.

  • Markov Reward Processes.

  • Bellman expectation and optimality concepts.

  • Dynamic programming techniques.

  • Monte Carlo learning.

  • Temporal-Difference learning.

  • Q-Learning and Deep Q-Networks.

  • Policy Gradient algorithms.

  • Actor-Critic architectures.

  • Proximal Policy Optimization.

  • Reinforcement Learning from Human Feedback.

  • Reward modeling and human preference learning.

  • InstructGPT training approaches.

  • DeepSeek-R1-style reinforcement learning.

  • Python and PyTorch implementations.

Who Should Read This Book?

Machine Learning Students: Learners who want to understand reinforcement learning from its mathematical foundations to advanced algorithms.

AI Engineers: Developers interested in building intelligent systems that learn through interaction and feedback.

Deep Learning Enthusiasts: Readers who want to explore neural-network-based reinforcement learning and policy optimization.

NLP Developers: Professionals interested in RLHF, language model alignment, and instruction-following systems.

Generative AI Researchers: Researchers exploring the connection between reinforcement learning and modern reasoning models.

Python Developers: Programmers who want to implement reinforcement learning algorithms using Python and PyTorch.

A background in Python, linear algebra, probability, and basic machine learning will be helpful. Readers who are new to reinforcement learning should study the foundational chapters carefully before moving to the advanced LLM training material.

Strengths of the Book

  • Connects classical reinforcement learning with modern Generative AI.

  • Explains mathematical foundations and algorithmic concepts.

  • Covers MDPs, Bellman equations, and dynamic programming.

  • Introduces major value-based and policy-based algorithms.

  • Explores DQN, PPO, and Actor-Critic methods.

  • Includes RLHF, InstructGPT, and DeepSeek-R1-style training.

  • Combines theory with Python and PyTorch implementations.

  • Uses small environments to make algorithm behavior easier to inspect.

  • Provides a learning path from foundational concepts to advanced applications.

Limitations to Consider

This book covers a broad and technically demanding subject. Readers without a background in probability, statistics, and machine learning may find some mathematical derivations challenging.

The practical implementations require familiarity with Python and a willingness to experiment with PyTorch.

Another consideration is that the field of reinforcement learning for LLMs evolves rapidly. New training methods, optimization strategies, and reasoning architectures continue to appear, so readers should supplement the book with current research papers and technical documentation.

It is also important to remember that reproducing a complete modern LLM training pipeline requires significantly more computational resources than running the small educational examples in a textbook.

Reinforcement Learning and the Future of Artificial Intelligence

Reinforcement Learning is becoming increasingly important in the development of intelligent systems.

Traditional applications focused on teaching agents to navigate environments, play games, control robots, and optimize decisions.

Modern applications extend these ideas to language models and AI reasoning systems.

RLHF helps align language model responses with human preferences, while reinforcement learning with verifiable rewards can encourage better performance on tasks where outcomes can be checked.

These developments demonstrate that reinforcement learning is no longer limited to traditional game-playing or robotics applications.

It is becoming an important part of how modern AI systems are trained, evaluated, and improved.

Understanding the foundations of RL helps learners follow these developments with greater technical clarity.

Download the PDF for free: Reinforcement Learning

Hard Copy: Reinforcement Learning: Mathematics and algorithms from MDPs to RLHF, InstructGPT, and DeepSeek-R1, with Python (FREE PDF)
Kindle: Reinforcement Learning: Mathematics and algorithms from MDPs to RLHF, InstructGPT, and DeepSeek-R1, with Python (FREE PDF)

Final Verdict

Reinforcement Learning: Mathematics and Algorithms from MDPs to RLHF, InstructGPT, and DeepSeek-R1, with Python is a valuable learning resource for anyone interested in understanding the evolution of reinforcement learning.

Its biggest strength is the connection between mathematical foundations, classical algorithms, practical Python implementations, and modern LLM training techniques.

The progression from Markov Decision Processes and Bellman concepts to DQN, PPO, RLHF, InstructGPT, and DeepSeek-R1-style training gives readers a broader understanding of how reinforcement learning supports modern Artificial Intelligence.

For Machine Learning students, Python developers, AI engineers, and researchers, this book offers an opportunity to explore both the theory and practical implementation of reinforcement learning.

It is especially useful for learners who want to understand not just what modern AI training methods do, but also the mathematical and algorithmic ideas behind them.


Agent-Based Models (Quantitative Applications in the Social Sciences)

 


Agent-Based Models: Understanding Complex Systems Through Individual Behavior

Introduction

Agent-Based Modeling (ABM) is a powerful computational approach used to understand complex systems by simulating the behavior and interactions of individual entities.

In social systems, individual people make decisions, interact with others, respond to their surroundings, and influence the behavior of larger groups. When thousands or millions of these interactions occur together, unexpected patterns can emerge.

For example, traffic congestion can develop from the decisions of individual drivers. The spread of information can emerge from interactions between people. Economic trends can develop from the combined decisions of consumers, businesses, and investors.

Agent-Based Models help researchers understand these complex situations by creating virtual environments in which individual entities, called agents, follow specific behavioral rules and interact with one another.

Agent-Based Models, written by Nigel Gilbert, is part of SAGE's Quantitative Applications in the Social Sciences series. The book introduces the theoretical foundations, methodological principles, and practical development of agent-based models, particularly for social science research.

It is an excellent resource for students, researchers, data scientists, and anyone interested in computational social science, simulation, and complex systems.


ABM as a Free Tool for Exploring Complexity : ABM as a Free Tool for Exploring Complexity

What Is Agent-Based Modeling?

Agent-Based Modeling is a computational modeling technique in which a system is represented through individual autonomous entities called agents.

Each agent has its own characteristics, behaviors, and decision-making rules.

Agents interact with other agents and their environment. These interactions continuously change the state of the simulated system.

For example, imagine a virtual city containing hundreds of people.

Each person can be represented as an agent with individual characteristics, such as age, occupation, location, preferences, and behavior.

The agents can move around the city, interact with other people, make decisions, and respond to changing conditions.

By observing these interactions, researchers can study larger patterns such as population movement, traffic congestion, social networks, and economic behavior.

The central idea is that complex patterns at the group level can emerge from relatively simple individual behaviors.

What Makes This Book Special?

One of the most valuable aspects of this book is its focus on understanding how to design, develop, and evaluate agent-based models.

Rather than presenting ABM only as a programming technique, Nigel Gilbert explains its theoretical and methodological foundations.

The book discusses how to identify agents, define their behaviors, represent environments, introduce randomness, and determine how interactions should occur over time.

It also explains how researchers can verify whether a model works correctly and validate whether its behavior reasonably represents the real-world system being studied.

Another important feature is its practical orientation. Readers are introduced to NetLogo as a modeling environment and can follow a worked example to understand how an agent-based simulation is developed.

The book is especially useful for social scientists who want to explore computational modeling without requiring extensive previous programming experience.

Key Topics Covered

The Idea of Agent-Based Modeling

The book begins by introducing the basic concept of agent-based modeling and explaining why it is useful for studying complex systems.

Readers explore how individual entities can be represented computationally and how their interactions can generate larger patterns.

The chapter also introduces examples from different disciplines and compares ABM with other modeling approaches.

An important concept is emergence, which describes how large-scale patterns can develop from interactions between individual agents.

For example, traffic jams may emerge even when no individual driver intends to create congestion.

Agents, Environments, and Timescales

Agents are the central components of an agent-based model.

This section explains how agents are represented, what characteristics they possess, and how their behavior is determined.

The environment provides the space or context in which agents operate.

An environment may represent a geographical area, a virtual network, a marketplace, or a social setting.

The book also discusses randomness and time.

Randomness allows agents to behave differently under similar conditions, while time determines when decisions, interactions, and state changes occur.

Population learning is another important concept because agents or groups can adapt their behavior through experience and interaction.

Designing an Agent-Based Model

Designing an effective ABM requires more than simply creating agents and allowing them to interact.

Researchers must first identify a specific research question and decide which parts of the real-world system need to be represented.

This chapter introduces the process of developing an agent-based model, from defining the research problem to selecting agents, specifying their behavior, and determining the structure of the environment.

It also demonstrates how a model can be developed through a worked example.

This section is particularly valuable because it helps readers translate an abstract research question into a computational simulation.

Developing an Agent-Based Model

After designing the model, the next step is implementation.

This chapter explores the practical tools, programming languages, libraries, frameworks, and environments available for developing agent-based simulations.

It introduces NetLogo and demonstrates how to build a model step by step.

Readers also explore the development of a collectivities model, which helps demonstrate how individual agents can form larger social structures through interaction.

The chapter discusses debugging and testing, which are essential for ensuring that a simulation behaves as intended.

Verification and Validation

Verification and validation are two critical stages in agent-based modeling.

Verification focuses on determining whether the model has been implemented correctly.

Researchers check whether the programming logic, agent behaviors, interactions, and simulation rules work as expected.

Validation focuses on determining whether the model adequately represents the real-world phenomenon being studied.

A model may execute without programming errors but still fail to represent actual social behavior accurately.

The book introduces techniques for examining model behavior and evaluating its credibility.

These concepts are important because simulation results should not automatically be treated as evidence about the real world.

Using Agent-Based Models in Research

Agent-Based Models are useful across many areas of social science research.

This section explains how researchers can plan an ABM project, organize the research process, and communicate their findings.

It also discusses how simulation results can be reported in academic publications.

An important application is public policy.

Researchers can use agent-based simulations to explore how individuals and organizations might respond to different policies.

For example, a model could investigate how changes in transportation policies influence commuting patterns or how public health interventions affect interactions within a population.

These simulations can help researchers explore possible outcomes before implementing real-world interventions.

Important Concepts in Agent-Based Modeling

Emergent Behavior

Emergent behavior occurs when large-scale patterns develop through the interactions of individual agents.

For example, the formation of social groups, traffic congestion, and market trends can emerge from individual decisions.

Agent Autonomy

Autonomy means that agents can make their own decisions based on their internal rules, characteristics, and available information.

Different agents do not necessarily behave in identical ways.

Agent Interaction

Agents can communicate, cooperate, compete, exchange resources, or influence one another.

These interactions are essential for creating realistic social simulations.

Environment

The environment defines the conditions in which agents operate.

It may include geographical locations, available resources, social networks, rules, and external influences.

Randomness

Randomness introduces variation into agent behavior and interactions.

It helps represent situations in which outcomes are not completely predictable.

Adaptation and Learning

Some models allow agents to modify their behavior based on experience, feedback, or interactions with other agents.

This makes it possible to explore how individual and collective behavior changes over time.

Applications of Agent-Based Models

Social Science Research

ABM can help researchers understand how individual behavior influences larger social structures.

It can be applied to social networks, group formation, cooperation, cultural change, and collective decision-making.

Economics

Economic systems involve interactions between consumers, companies, workers, and financial institutions.

Agent-based simulations can help researchers investigate market behavior, competition, financial instability, and the emergence of economic patterns.

Public Health

Researchers can represent individuals as agents and simulate their interactions to explore the possible spread of infectious diseases.

Models can also help investigate how changes in individual behavior or public health interventions influence population-level outcomes.

Traffic and Transportation

Each vehicle or driver can be represented as an agent with individual movement and decision-making rules.

These models can help explore traffic congestion, road usage, transportation planning, and pedestrian movement.

Environmental Studies

ABM can be used to investigate interactions between people, natural resources, and environmental systems.

For example, researchers can simulate how communities respond to resource shortages or how individual decisions influence land-use changes.

Political Science

Agent-based simulations can help researchers study voting behavior, political participation, opinion formation, collective action, and the spread of political information.

Business and Organizational Research

Companies can use agent-based modeling to explore customer behavior, organizational interactions, supply chains, and market dynamics.

Tools Used for Agent-Based Modeling

The book introduces NetLogo as a practical environment for developing agent-based simulations.

Other tools and technologies are also useful when exploring ABM.

NetLogo: A popular environment for building and experimenting with agent-based models. It is particularly suitable for educational and research applications.

Python: Python can be used to build agent-based simulations with libraries such as Mesa.

Mesa: A Python framework designed specifically for agent-based modeling. It provides tools for defining agents, environments, model behavior, and simulation processes.

Repast: A toolkit for developing agent-based simulations, particularly in scientific and social science research.

AnyLogic: A simulation platform that supports agent-based, discrete-event, and system-dynamics modeling.

These tools allow researchers and developers to explore complex systems through computational experiments.

Practical Skills You Can Develop

After studying this book, readers can develop an understanding of:

  • Agent-based modeling fundamentals.

  • Defining agents and their characteristics.

  • Designing virtual environments.

  • Creating individual behavioral rules.

  • Modeling interactions between agents.

  • Understanding emergence and collective behavior.

  • Introducing randomness into simulations.

  • Designing computational experiments.

  • Building simulations using NetLogo.

  • Debugging and verifying simulation models.

  • Validating models against real-world observations.

  • Planning agent-based research projects.

  • Reporting simulation results.

  • Applying ABM to social and economic policy research.

Who Should Read This Book?

Social Science Students: Students interested in sociology, economics, political science, psychology, and social research.

Data Scientists: Professionals interested in simulation-based analysis and complex systems.

Machine Learning Students: Learners who want to explore computational modeling beyond traditional predictive algorithms.

Researchers: Academics interested in studying social behavior, collective decisions, and emerging patterns.

Economics Students: Learners interested in market behavior, individual decision-making, and economic simulations.

AI and Simulation Enthusiasts: Anyone interested in autonomous agents, multi-agent systems, and computational experiments.

The book is designed to be accessible to readers with limited programming experience, making it a useful starting point for people from different academic backgrounds.

Strengths of the Book

  • Clear introduction to agent-based modeling.

  • Strong focus on social science applications.

  • Explains agents, environments, and interactions.

  • Discusses practical model design and development.

  • Introduces NetLogo through a worked example.

  • Covers verification and validation.

  • Explains how to plan and report ABM research.

  • Connects computational simulation with real-world social questions.

Limitations to Consider

This book is primarily an introduction to agent-based modeling in the social sciences rather than an advanced programming manual.

Readers looking for extensive Python implementation examples may need additional resources, especially those focused on libraries such as Mesa.

The book also emphasizes research methodology and model design. Readers interested in advanced multi-agent reinforcement learning, distributed simulation, or large-scale AI agent architectures will need more specialized material.

Another important consideration is that simulation results depend on the assumptions and behavioral rules used to construct the model. A well-designed simulation still requires careful validation before its results can support real-world conclusions.

Hard Copy: Agent-Based Models (Quantitative Applications in the Social Sciences)


Final Verdict

Agent-Based Models by Nigel Gilbert is a valuable introductory resource for understanding how computational simulations can explain complex social behavior.

Its greatest strength is its focus on the relationship between individual actions and collective outcomes.

By learning how agents interact, adapt, and respond to their environments, readers can understand how large-scale social patterns emerge from relatively simple behavioral rules.

The book also provides practical guidance on designing, developing, verifying, and validating agent-based simulations.

For students and researchers interested in computational social science, economics, sociology, simulation, and complex systems, this book is an excellent starting point.


Python Coding Challenge - Question with Answer (ID 091126)

 




Explanation:

Step 1: Understand the Outer Lambda
lambda x: ...

- lambda creates an anonymous function.
- The outer lambda accepts one argument, x.
- In this code, x = 2.

Step 2: Understand the Inner Lambda
lambda y: x**y

- This creates another lambda function inside the outer lambda.
- It accepts one argument, y.
- It calculates x raised to the power of y.
- It can access x from the outer lambda.

Step 3: Execute the Inner Lambda
(lambda y: x**y)(3)

- The inner lambda is immediately called with 3.
- Therefore, y = 3.
- The expression becomes:
2 ** 3


Step 4: Execute the Outer Lambda
(lambda x: ...)(2)

- The outer lambda is called with 2.
- Therefore, x = 2.
- It executes the inner lambda using y = 3.
Step 5: Calculate the Result
\[
2^3 = 2 \times 2 \times 2 = 8
\]

3. Final Output
8

๐Ÿ Python Pattern Challenge — Day 43

 





๐Ÿ Python Pattern Challenge — Day 43 | Repeating Star Sequence Pattern

Welcome to Day 43 of the Python Pattern Challenge! ๐Ÿš€

Today, we're creating a simple yet interesting Repeating Star Sequence Pattern using Python. This pattern combines single stars, increasing star groups, and repeating sequences to create a unique geometric design.

Unlike traditional pyramid patterns, this challenge uses different star arrangements across multiple sections, making it a fun exercise for understanding loops, spacing, and pattern logic.

๐ŸŽฏ Today's Challenge

Write a Python program to print exactly the following pattern:

* *** ***** * * *** ***** * *** *****





๐Ÿ’ป Python Solution

pattern = [ (4, 1), (3, 3), (2, 5), (4, 1), (4, 1), (3, 3), (2, 5), (4, 1), (3, 3), (2, 5) ] for spaces, stars in pattern: print(" " * spaces + "*" * stars)





๐Ÿง  How It Works

1. Storing the pattern

The pattern list contains pairs of values representing the number of spaces and stars in each row.

2. Controlling the spacing

" " * spaces

This adds the required leading spaces to position each row.

3. Repeating the stars

"*" * stars

The multiplication operator repeats the star symbol according to the specified count.

4. Printing each row

print(" " * spaces + "*" * stars)

This combines spaces and stars to generate the complete pattern.

๐Ÿš€ Challenge Yourself

  • Increase the number of star groups.

  • Replace stars with numbers or alphabets.

  • Create your own repeating sequence.

  • Try generating the pattern using nested loops.

  • Experiment with different spacing arrangements.

๐Ÿ’ก Key Takeaway

Python pattern printing is a great way to understand string repetition, loops, lists, and logical thinking. Small changes in spacing and repetition can produce completely different designs.

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

Learn • Practice • Grow with CLCODING


Python Coding Challenge - Question with Answer (ID 081126)

 


800 Days Python Coding Challenges with Explanation

Explanation:

Step 1: Initialize Variable
x = 5

- A variable x is created.
- Its initial value is 5.

Step 2: Understand the Walrus Operator (:=)
(x := x + 3)

- := is called the Walrus Operator or Assignment Expression.
- It calculates the value and assigns it to the variable at the same time.
Calculation:
x = 5 + 3x = 8

So, the first expression returns 8.

Step 3: Evaluate the Second Expression
(x := x - 2)
- Now x is already 8.
- We subtract 2 and assign the result back to x.
Calculation:
x = 8 - 2x = 6

So, the second expression returns 6.

Step 4: Perform Multiplication
8 * 6
Both expressions are evaluated from left to right.
\[
8 \times 6 = 48
\]

3. Final Output
48

4. Key Concepts to Remember
Concept Explanation
:= Assigns and returns a value in an expression
x + 3 Updates x from 5 to 8
x - 2 Updates x from 8 to 6
* Multiplies the two returned values

Final Answer: 48


Saturday, 7 November 2026

Algorithms for Optimization (FREE PDF)

 



Algorithms for Optimization — A Complete Guide to Computational Optimization Techniques

Introduction

Optimization is an important concept in computer science, mathematics, engineering, and Artificial Intelligence. It focuses on finding the best possible solution to a problem while considering different objectives, limitations, and constraints.

From improving machine learning models to designing autonomous systems, optimization algorithms help solve complex problems efficiently.

Algorithms for Optimization, written by Mykel J. Kochenderfer and Tim A. Wheeler, is a comprehensive textbook published by The MIT Press. It introduces practical computational techniques for solving optimization problems and explains how different algorithms can be applied to real-world engineering challenges.

The book takes an engineering-oriented approach, connecting mathematical concepts with computational implementations. It covers a wide range of optimization methods, including gradient descent, stochastic optimization, genetic algorithms, constrained optimization, surrogate models, and optimization under uncertainty.

With detailed explanations, illustrations, examples, and exercises, this book is particularly useful for advanced undergraduate students, graduate students, researchers, and professionals interested in optimization and computational problem-solving.

Download the PDF for free:

 https://drive.google.com/file/d/1Rx7MAekKZbyNVc5B2MNfR0HXXIEJCksp/view


What Makes This Book Special?

One of the strongest features of this book is its broad coverage of optimization techniques.

Instead of concentrating on only one algorithm or optimization strategy, it explores several approaches and explains when different methods can be useful.

The authors introduce both traditional mathematical optimization techniques and modern computational approaches.

Another important feature is its practical programming focus. The book includes implementations using Julia, a programming language designed for high-performance numerical and scientific computing.

It also includes test functions, mathematical appendices, exercises, and detailed explanations that help readers understand the behavior of different optimization algorithms.

The book is especially useful for learners interested in understanding the relationship between optimization theory and practical engineering applications.

Key Topics Covered

Introduction to Optimization

The book begins by introducing the fundamentals of optimization and explaining how optimization problems are formulated.

Readers learn how to identify objectives, constraints, and possible solutions.

Important concepts include optimization problem formulation, objective functions, feasible solutions, constraints, local and global minima, and optimization processes.

This chapter establishes the foundation for understanding how optimization algorithms operate.

Derivatives and Gradients

Derivatives and gradients are important tools in many numerical optimization methods.

This chapter introduces how changes in input variables influence objective functions and how this information can guide an algorithm toward better solutions.

Topics include numerical differentiation, multidimensional derivatives, gradients, and automatic differentiation.

These concepts are particularly important in machine learning because many training algorithms use gradient-based optimization.

Bracketing Methods

Bracketing methods help identify promising regions in which an optimal solution may exist.

The book introduces several search techniques used in one-dimensional optimization, including Fibonacci Search, Golden Section Search, Quadratic Fit Search, Bisection Method, and Shubert-Piyavskii Method.

These methods help readers understand how optimization algorithms narrow down the search space and locate useful solutions.

Local Descent Methods

Local descent methods are designed to improve a solution by repeatedly moving toward a better region of the search space.

This chapter introduces descent directions, line searches, approximate line searches, and trust-region methods.

Readers learn how optimization algorithms make iterative improvements and how termination conditions determine when the search should stop.

These concepts are important in numerical optimization and machine learning model training.

First-Order Optimization Methods

First-order methods use gradient information to guide the optimization process.

The book explores several widely used algorithms, including Gradient Descent, Conjugate Gradient, Momentum, Nesterov Momentum, Adagrad, RMSProp, Adadelta, Adam, and Hypergradient Descent.

These algorithms are particularly relevant to deep learning, where optimization techniques are used to train neural networks and improve predictive performance.

Understanding these methods helps readers recognize why different optimizers behave differently during model training.

Second-Order Optimization Methods

Second-order optimization methods use additional information about the shape and curvature of an objective function.

This chapter covers Newton's Method, the Secant Method, and Quasi-Newton Methods.

Readers explore how curvature information can influence optimization decisions and how these techniques differ from first-order approaches.

These methods are useful in numerical optimization, scientific computing, and advanced mathematical modeling.

Direct Optimization Methods

Direct methods search for better solutions without requiring derivative information.

The book introduces several algorithms, including Cyclic Coordinate Search, Powell's Method, Hooke-Jeeves Method, Generalized Pattern Search, Nelder-Mead Simplex Method, and Divided Rectangles.

These methods are valuable when objective functions are difficult to differentiate or when derivative information is unavailable.

Stochastic Optimization Methods

Some optimization problems involve randomness, uncertainty, or noisy objective evaluations.

Stochastic optimization methods use probabilistic or randomized strategies to explore the solution space.

Important techniques include Simulated Annealing, Cross-Entropy Method, Natural Evolution Strategies, Covariance Matrix Adaptation, and Mesh Adaptive Direct Search.

These algorithms are useful in complex optimization problems where traditional local search methods may struggle.

Population-Based Optimization

Population-based methods work with multiple candidate solutions rather than improving only one solution at a time.

This chapter introduces evolutionary and swarm-based optimization techniques.

Topics include Genetic Algorithms, Differential Evolution, Particle Swarm Optimization, Firefly Algorithm, Cuckoo Search, and Hybrid Optimization Methods.

These approaches are particularly interesting for engineering design, parameter tuning, and problems involving complex search spaces.

Constrained Optimization

Many real-world optimization problems have limitations that must be respected.

For example, an engineering design may need to minimize cost while satisfying safety, material, and performance requirements.

This chapter explores constrained optimization techniques, including Lagrange multipliers, penalty methods, augmented Lagrangian methods, and interior-point methods.

It also introduces duality and inequality constraints.

Understanding these concepts is important for solving practical optimization problems where not every solution is acceptable.

Linear Constrained Optimization

Linear optimization focuses on problems where the objective and constraints can be represented using linear relationships.

The book introduces linear optimization problem formulation and the Simplex Algorithm.

It also discusses dual certificates and their role in understanding optimization solutions.

These concepts are widely used in operations research, logistics, resource allocation, and production planning.

Multiobjective Optimization

Real-world problems frequently involve multiple objectives that may conflict with each other.

For example, a manufacturing system may need to reduce cost while improving product quality and production speed.

This chapter explores multiobjective optimization and the concept of Pareto optimality.

Important topics include Pareto optimality, constraint methods, weight methods, multiobjective population methods, and preference elicitation.

These techniques help decision-makers understand trade-offs between competing objectives.

Sampling Plans and Surrogate Models

Optimization algorithms sometimes require many expensive evaluations of an objective function.

Sampling plans help select useful points for evaluating a system, while surrogate models approximate expensive functions.

The book introduces full factorial sampling, random sampling, stratified sampling, and quasi-random sequences.

It then explores surrogate modeling techniques, including linear models, basis functions, and model selection.

These methods are useful in scientific experiments, engineering simulations, and expensive computational optimization problems.

Probabilistic Surrogate Models and Surrogate Optimization

Probabilistic surrogate models provide predictions while also representing uncertainty about those predictions.

The book introduces Gaussian distributions, Gaussian Processes, and methods for fitting probabilistic models.

It also explores surrogate optimization strategies such as prediction-based exploration, error-based exploration, lower confidence bound exploration, probability of improvement, expected improvement, and safe optimization.

These techniques are important in Bayesian optimization, hyperparameter tuning, and experimental design.

Optimization Under Uncertainty and Uncertainty Propagation

In many engineering applications, the exact behavior of a system cannot be predicted with complete certainty.

This section explores how optimization algorithms can account for uncertain inputs and outcomes.

Readers study set-based uncertainty, probabilistic uncertainty, sampling methods, Taylor approximations, polynomial chaos, and Bayesian Monte Carlo.

These techniques are useful in risk analysis, scientific simulations, engineering design, and decision-making systems.

Discrete and Expression Optimization

Not all optimization problems involve continuous variables.

Some problems require selecting discrete values, assigning resources, or choosing combinations from a finite set of possibilities.

This chapter introduces integer programming, rounding, cutting planes, branch and bound, dynamic programming, and ant colony optimization.

The book also explores expression optimization using genetic programming, grammatical evolution, and probabilistic grammars.

These methods demonstrate how optimization can be applied to problems involving both numerical and symbolic structures.

Multidisciplinary Optimization

The final major chapter explores optimization problems involving multiple interconnected engineering disciplines.

In complex systems, changes in one component may influence several other components.

This chapter introduces disciplinary analyses, interdisciplinary compatibility, and different multidisciplinary design optimization architectures.

These approaches are useful in aerospace engineering, autonomous systems, complex product design, and other engineering applications.

Practical Skills You Can Develop

After studying the book and implementing its examples, readers can develop experience in:

  • Understanding optimization problem formulation.

  • Implementing gradient-based optimization methods.

  • Working with first-order and second-order algorithms.

  • Applying stochastic optimization techniques.

  • Understanding genetic algorithms and swarm intelligence.

  • Solving constrained and linear optimization problems.

  • Exploring multiobjective optimization.

  • Building surrogate models.

  • Understanding Gaussian Processes and Bayesian optimization.

  • Working with optimization under uncertainty.

  • Applying discrete optimization methods.

  • Implementing numerical algorithms using Julia.

  • Comparing optimization strategies for different problem types.

Who Should Read This Book?

Computer Science Students: Learners interested in algorithm design, computational mathematics, and optimization techniques.

Machine Learning Engineers: Professionals who want to understand the optimization algorithms used during model training and hyperparameter tuning.

Data Scientists: Learners interested in optimization, statistical modeling, and efficient computational methods.

Engineering Students: Students working in aerospace, electrical, mechanical, and other engineering disciplines.

Operations Research Professionals: People interested in resource allocation, constrained optimization, and decision-making.

AI Researchers: Researchers who want to explore optimization techniques for intelligent systems and complex computational problems.

A background in calculus, linear algebra, probability, and programming will make the book easier to understand. Familiarity with Julia is useful, although the book includes an introduction to the language.

Strengths of the Book

  • Comprehensive coverage of optimization algorithms.

  • Practical engineering perspective.

  • Wide variety of gradient-based, stochastic, and derivative-free methods.

  • Strong relevance to machine learning and scientific computing.

  • Introduction to uncertainty modeling and Gaussian Processes.

  • Programming implementations using Julia.

  • Exercises and illustrations for reinforcing concepts.

Limitations to Consider

This is a mathematically intensive textbook rather than a casual introduction to programming.

Readers without a background in calculus, linear algebra, and probability may find some chapters challenging.

The book uses Julia for its computational implementations. Python developers may need to translate examples into NumPy, SciPy, or other Python libraries to integrate the concepts into their existing workflows.

It also covers a large number of algorithms, so readers interested in a specific area, such as deep learning optimization or Bayesian optimization, may want to supplement it with more specialized resources.

Kindle:Algorithms for Optimization (FREE PDF)

Hard Copy: Algorithms for Optimization (FREE PDF)


Download the PDF for free:

 https://drive.google.com/file/d/1Rx7MAekKZbyNVc5B2MNfR0HXXIEJCksp/view


Final Verdict

Algorithms for Optimization is a valuable textbook for understanding how computational methods solve complex optimization problems.

Its greatest strength is the variety of techniques it covers, ranging from gradient descent and Newton's Method to genetic algorithms, Gaussian Processes, and multidisciplinary optimization.

The book is particularly relevant to machine learning, engineering, scientific computing, and operations research because it explains the methods used to improve systems under different conditions and constraints.

For the best learning experience, implement the algorithms, experiment with different objective functions, and compare their behavior on practical optimization problems.


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

 


Code Explanation:

1. Creating a Class
class Counter:

- Defines a class named Counter.
- A class acts as a blueprint for creating objects.
- Here, the class is designed to maintain and update a counter value.

2. Defining a Class Variable
    count = 5

- Creates a class variable named count.
- Its initial value is 5.
- This variable belongs to the class rather than an individual object.
- It can be accessed using Counter.count.

3. Using the Class Method Decorator
    @classmethod

- @classmethod is a built-in Python decorator.
- It converts the following method into a class method.
- A class method receives the class itself as its first argument instead of an instance.

4. Defining the Class Method
    def update(cls, n):

- Defines a method named update().
- cls refers to the current class, which is Counter.
- n represents the value that needs to be added.
- Unlike self, which refers to an object, cls refers to the class.

5. Updating the Class Variable
        cls.count += n

- Accesses the class variable using cls.count.
- Adds the value of n to the existing count.
- Updates the class variable directly.
- If count is initially 5 and n is 3, the new value becomes 8.

6. Calling the Class Method
Counter.update(3)

- Calls the update() method directly through the class.
- No object creation is required.
- The value 3 is passed to the parameter n.
- The method updates Counter.count from 5 to 8.

7. Printing the Updated Value
print(Counter.count)

- Accesses the updated class variable.
- The value is now 8.
- The print() function displays the result.

Final Output
Correct Output
8

Molecular Structure Visualization in Python Using RDKit

 

Molecular Structure Visualization in Python Using RDKit

Introduction

Python is widely used in scientific computing, artificial intelligence, machine learning, and data science. But did you know that Python can also help you visualize chemical molecules and explore their structures?

One of the most powerful libraries for this purpose is RDKit.

RDKit is an open-source cheminformatics toolkit that allows developers, researchers, and computational chemists to work with molecular structures, chemical fingerprints, molecular descriptors, and chemical data.

With just a few lines of Python code, you can convert a chemical representation into a visual molecular structure.

In this tutorial, we will learn how to install RDKit, understand SMILES notation, and generate a molecular structure image using Python.

What Is RDKit?

RDKit is an open-source software toolkit designed for cheminformatics and molecular data processing.

It provides Python APIs for working with chemical information and molecular structures.

Key Features of RDKit

  • Molecular Visualization: Generate 2D molecular structure diagrams.

  • SMILES Processing: Convert SMILES strings into molecular objects.

  • Molecular Descriptors: Calculate molecular properties and descriptors.

  • Chemical Fingerprints: Generate molecular fingerprints for similarity analysis.

  • Substructure Searching: Find specific chemical patterns inside molecules.

  • Molecular Manipulation: Create, modify, and analyze chemical structures.

RDKit is especially useful in computational chemistry, pharmaceutical research, cheminformatics, and AI-driven drug discovery.

1. Install RDKit

First, install RDKit using pip.

%pip install rdkit

If you are working in Jupyter Notebook, execute this command in a notebook cell.

2. Import Required Libraries

Next, import the required modules.

from rdkit import Chem
from rdkit.Chem import Draw

Understanding the imports:

  • Chem: Provides tools for creating and manipulating molecular structures.

  • Draw: Provides functionality for generating molecular structure images.

3. Create a Molecular Structure Using SMILES

SMILES stands for Simplified Molecular Input Line Entry System.

It is a text-based notation used to represent chemical structures.

Instead of manually drawing a molecule, we can describe its atoms, bonds, and connectivity using a SMILES string.

Let's use the following SMILES representation:

smiles = "CC(=O)OC1=CC=CC=C1C(=O)O"

This represents Aspirin (Acetylsalicylic Acid).

Now convert the SMILES string into an RDKit molecular object.

mol = Chem.MolFromSmiles(smiles)

The MolFromSmiles() function parses the SMILES string and creates a molecular object.

4. Visualize the Molecular Structure

RDKit provides MolToImage() to generate a 2D molecular diagram.

from IPython.display import display

display(
    Draw.MolToImage(
        mol,
        size=(450, 300)
    )
)

This generates a visual representation of the aspirin molecule.

The diagram displays:

  • Carbon atoms and their connectivity

  • Oxygen atoms

  • Single and double chemical bonds

  • The benzene ring

  • Functional groups

You can change the image dimensions using the size parameter.

5. Complete Python Code

Here is the complete implementation in one place:

from rdkit import Chem
from rdkit.Chem import Draw
from IPython.display import display

# SMILES representation of Aspirin
smiles = "CC(=O)OC1=CC=CC=C1C(=O)O"

# Convert SMILES into a molecular object
mol = Chem.MolFromSmiles(smiles)

# Check whether the molecule was parsed
if mol is not None:
    display(Draw.MolToImage(mol, size=(450, 300)))
else:
    print("Invalid SMILES string")

This code can be executed directly in Jupyter Notebook.

6. Important RDKit Functions

Here are some useful RDKit functions worth exploring:

FunctionPurpose
Chem.MolFromSmiles()Create a molecule from SMILES
Chem.MolToSmiles()Convert a molecule into SMILES
Draw.MolToImage()Generate a 2D molecular image
Chem.AddHs()Add explicit hydrogen atoms
Chem.RemoveHs()Remove explicit hydrogen atoms
Chem.MolFromMolBlock()Read a molecule from a MolBlock
Chem.FindMolChiralCenters()Identify chiral centers

7. Real-World Applications of RDKit

RDKit is useful across several scientific and technological fields.

Drug Discovery

Researchers use molecular representations and descriptors to investigate potential drug candidates.

Computational Chemistry

Chemists can process and analyze molecular structures programmatically.

Molecular Similarity

Chemical fingerprints can help compare molecules and identify structurally similar compounds.

Machine Learning

Molecular descriptors and fingerprints can be used as input features for machine learning models.

Artificial Intelligence in Chemistry

RDKit can be integrated into AI pipelines for molecular property prediction, chemical data processing, and generative molecular research.

8. Why Python Developers Should Learn RDKit

Python is not limited to web development, automation, or data analysis.

Its scientific ecosystem allows developers to work in specialized fields such as chemistry, biology, healthcare, and pharmaceutical research.

Learning RDKit introduces developers to:

  • Scientific Python programming

  • Molecular data processing

  • Chemical structure visualization

  • Computational chemistry

  • Molecular machine learning

  • AI-assisted drug discovery

It is an excellent library for developers interested in combining Python with scientific research.

Conclusion

RDKit demonstrates how powerful Python can be beyond traditional programming applications.

With only a few lines of code, we can convert a SMILES string into a molecular object and generate a 2D chemical structure diagram.

Whether you are a Python developer, data scientist, computational chemist, or AI researcher, RDKit is a valuable tool to explore.

Start experimenting with molecular structures and discover how Python connects programming with the world of chemistry!

Explore more Python libraries and scientific computing resources at CLCODING.

๐ŸŒ https://whatsapp.com/channel/0029Va5BbiT9xVJXygonSX0G

Python Coding Challenge - Question with Answer (ID 071126)

 


Explanation:

Step 1: Import partial
from functools import partial

- partial is a function from Python's functools module.
- It allows us to create a new function by fixing one or more arguments of an existing function.

Step 2: Create a Partial Function
f = partial(pow, 3)

- pow() normally accepts two or three arguments:
  pow(base, exponent)pow(base, exponent, modulus)
- Here, partial(pow, 3) fixes the first argument (base) as 3.
- Now, f() behaves like this:
  f(exponent)
  
   is equivalent to:
  pow(3, exponent)

Step 3: Execute the Function
print(f(2), f(2, 5))

First call:
f(2)

Equivalent to:
pow(3, 2)

Calculation:
\[
3^2 = 9
\]

Second call:
f(2, 5)
Equivalent to:
pow(3, 2, 5)

Calculation:
\[
3^2 \bmod 5 = 9 \bmod 5 = 4
\]

Final Output
9 4

Python Turtle — The Hypnotic Rainbow Vortex

 


Code:

import turtle import time import colorsys s = turtle.Screen() s.setup(750, 750) s.bgcolor("black") s.title("Rainbow Hypnotic Vortex") s.tracer(0) t = turtle.Turtle() t.hideturtle() t.speed(0) t.pensize(2) for i in range(180): t.pencolor(colorsys.hsv_to_rgb(i / 180, 1, 1)) t.penup() t.goto(0, 0) t.setheading(i * 7) t.pendown() length = 80 + i * 1.8 for _ in range(2): t.forward(length) t.circle(35 + i * 0.15, 90) s.update() time.sleep(0.025) turtle.done()













Explanation:

1. Import Libraries
import turtleimport timeimport colorsys

- turtle – draws the vortex pattern.
- time – adds animation delays.
- colorsys – generates rainbow colors using HSV color values.

2. Create the Screen
s = turtle.Screen()s.setup(750, 750)s.bgcolor("black")s.title("Rainbow Hypnotic Vortex")s.tracer(0)


- Creates a 750 × 750 graphics window.
- Sets the background to black.
- Adds the window title.
- tracer(0) disables automatic screen updates.

3. Configure the Turtle
t = turtle.Turtle()t.hideturtle()t.speed(0)t.pensize(2)
- Creates the drawing turtle.
- Hides the turtle cursor.
- Sets the fastest drawing speed.
- Sets the pen thickness to 2.

4. Create the Main Loop
for i in range(180):

Repeats the drawing process 180 times to create multiple rotating patterns.

5. Generate Rainbow Colors
t.pencolor(colorsys.hsv_to_rgb(i / 180, 1, 1))

- Converts HSV color values into RGB.
- i / 180 gradually changes the hue.
- 1 represents full saturation.
- 1 represents full brightness.
- Produces a continuous rainbow color effect.

6. Set the Drawing Position
t.penup()t.goto(0, 0)t.setheading(i * 7)t.pendown()

- Lifts the pen to avoid unwanted lines.
- Moves the turtle to the center.
- Rotates the direction by 7° on each iteration.
- Puts the pen down to start drawing.

7. Calculate the Pattern Length
length = 80 + i * 1.8

- Starts with a length of 80 units.
- Increases the length by 1.8 units per iteration.
- Creates progressively larger patterns.

8. Draw the Curved Vortex
for _ in range(2):    t.forward(length)    t.circle(35 + i * 0.15, 90)

- Repeats the drawing movement twice.
- forward(length) draws a straight line.
- circle() creates a curved arc.
- The circle radius gradually increases with i.
- Combines straight and curved movements to create the vortex effect.

9. Animate the Drawing
s.update()time.sleep(0.025)

- s.update() refreshes the screen manually.
- time.sleep(0.025) pauses for 0.025 seconds.
- Makes the drawing animation visible.

10. Finish the Program
turtle.done()



Keeps the Turtle graphics window open after the artwork is complete.




Output:


Popular Posts

Categories

100 Python Programs for Beginner (119) AI (359) Android (25) AngularJS (1) API (2) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (20) C (78) C# (12) C++ (83) cloud (1) Course (97) Coursera (307) Cybersecurity (37) data (10) Data Analysis (48) Data Analytics (31) data management (16) Data Science (443) Data Strucures (21) Deep Learning (228) Django (17) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Gadgets (1) Generative AI (80) Git (14) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (417) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (36) Python (1405) Python Bug (1) Python Coding Challenge (1302) Python Library (29) Python Mathematics (24) Python Mistakes (51) Python Pattern Challenge (38) Python Quiz (684) Python Tips (115) Python Turtle (1) Questions (3) R (72) React (7) Scripting (3) security (5) Selenium Webdriver (4) Software (21) SQL (56) Udemy (24) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)