Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Tuesday, 6 October 2026

Python 3.15 Lazy Imports: Faster Startup, Lower Memory, and More Control

 

Python 3.15 introduces explicit lazy imports through PEP 810, giving developers a new way to control when modules are actually imported.

Instead of loading every dependency when a program starts, lazy imports allow Python to defer the work until the imported name is actually needed.

For large applications with many dependencies, this could make application startup significantly faster.

What Are Lazy Imports?

Traditionally, when Python encounters an import such as:

import pandasimport requestsimport json

the modules are imported immediately.

With Python 3.15's explicit lazy import syntax, you can write:

lazy import pandaslazy import requestslazy import json

The modules aren't fully loaded at that point. The import is deferred until the corresponding name is actually used.

The basic idea is:

Don't pay the cost of loading a dependency until you actually need it.


Why Does This Matter?

Modern Python applications can have extremely large dependency trees.

A single application might depend on:

  • Pandas
  • NumPy
  • Cloud SDKs
  • Database drivers
  • Machine learning frameworks
  • LLM providers
  • Vector databases
  • Observability tools
  • Authentication libraries
  • Optional integrations

But a particular execution path might only use a small percentage of those dependencies.

Traditional imports can make the application pay the startup cost for dependencies that may never be used.

Lazy imports provide a way to avoid some of that unnecessary work.


Python 3.15 Lazy Import Example

Traditional imports:

import jsonfrom pathlib import Pathimport pandas as pdimport requests

Python 3.15:

lazy import jsonlazy from pathlib import Pathlazy import pandas as pdlazy import requests

The important difference is the lazy keyword.

The actual module loading is deferred until the imported name is accessed.


How Much Faster Can It Be?

PEP 810 reports significant improvements in certain real-world workloads.

The reported results include:

Up to 50–70% faster startup

Some command-line workloads can see substantial reductions in startup time when many imported dependencies aren't actually needed.

Around 30–40% lower memory usage

Large applications can also benefit from reduced memory consumption when unused dependencies are no longer loaded immediately.

However, these numbers aren't guarantees for every Python application.

The actual improvement depends heavily on the application's dependency tree and which modules are eventually used.


Where Lazy Imports Can Make the Biggest Difference

Lazy imports are particularly interesting for applications that have:

Large dependency trees + small execution paths

For example, imagine a CLI application with 100 dependencies.

A particular command might only need 10 of them.

With traditional imports, many of those dependencies may be initialized before the application can even execute the command.

With lazy imports, Python can defer some of that work until it becomes necessary.

This can be especially useful for:

  • CLI applications
  • Developer tools
  • Large Python services
  • Plugin systems
  • Data science applications
  • AI/ML applications
  • Applications with optional features

Lazy Imports and AI/GenAI Applications

This is particularly interesting for modern AI applications.

A single AI application might integrate with:

LLM providers
       ↓
Vector databases
       ↓
Cloud SDKs
       ↓
Database drivers
       ↓
Observability
       ↓
Optional integrations

But an individual execution may only use one or two of these components.

For example, an application might support OpenAI, Anthropic, multiple vector databases, AWS, Azure, and several observability platforms.

Loading everything during startup can be expensive.

Lazy imports give developers another tool for controlling that startup cost.


Lazy Imports vs Importing Inside a Function

Python developers have traditionally used this technique:

def process_data():    import pandas as pd    return pd.DataFrame(...)

This provides function-level deferral.

Explicit lazy imports are different:

lazy import pandas

They are designed for module-level imports.

Therefore, lazy imports don't completely replace the traditional technique of importing inside a function.

If you need very specific function-level control, a local import can still be appropriate.


What About __lazy_modules__?

Python 3.15 also provides mechanisms such as:

__lazy_modules__

This can help projects adopt lazy imports without necessarily rewriting every import statement individually.

That can be particularly useful for larger existing codebases where changing hundreds or thousands of import statements isn't practical.


There Is a Trade-Off

Lazy imports aren't a free performance upgrade.

One important consequence is that some problems can be detected later.

With traditional imports:

import some_module

an import failure generally happens during startup.

With a lazy import:

lazy import some_module

the failure may not occur until the imported name is actually accessed.

That means errors can move from startup time to usage time.

Import-time side effects can also behave differently because module initialization is deferred.

Developers therefore need to consider whether a dependency is safe and appropriate to import lazily.


Lazy Imports Are Optional

One of the most important points about Python 3.15's feature is that lazy imports don't replace normal imports.

Traditional imports remain the normal behavior.

You explicitly opt into lazy importing when you want it:

lazy import module

This makes the feature more of a developer control mechanism than a change to Python's entire import system.


Python 3.15 Release Timing

Python 3.15 was originally scheduled for a October 1, 2026 final release.

However, last-minute issues related to lazy imports resulted in an additional release candidate being prepared.

Python 3.15.0rc3 was released on October 2, with the final release scheduled for:

October 9, 2026

It's an interesting detail because one of Python 3.15's headline features was also involved in the final release delay.


Should You Use Lazy Imports?

It depends on your application.

Lazy imports are worth investigating if your project has:

  • A large dependency tree
  • Slow startup times
  • High memory usage
  • Many optional dependencies
  • Large CLI tools
  • Plugin architectures
  • Features that are rarely used

For a small Python script with a handful of dependencies, the benefit may be negligible.

For a large application, however, reducing unnecessary initialization can have a meaningful impact.


The Bigger Picture

Python has historically made it easy to import modules, but developers have had fewer language-level options for explicitly controlling when those imports become active.

PEP 810 changes that.

The goal isn't:

Make every Python import lazy.

The goal is:

Give developers more control over when dependency costs are paid.

For increasingly large Python applications — particularly applications combining data science, cloud services, AI, databases, and multiple optional integrations — that control could become increasingly valuable.

Final Takeaway

Python 3.15's lazy imports are not about changing how every Python program works.

They're about giving developers another performance tool.

If your application spends significant time loading dependencies that aren't immediately needed, explicit lazy imports could help reduce:

Startup time ↓
Memory usage ↓
Unnecessary initialization ↓

while giving developers more control over how and when dependencies are loaded.

Python's import system just became a little more flexible — and Python 3.15 is getting interesting.

Tuesday, 22 September 2026

Hands-on Python Tutorial

 




Hands-on Python Tutorial by Dr. Andrew N. Harrington is a practical learning resource designed to introduce beginners to programming through Python. Harrington is associated with the Computer Science Department at Loyola University Chicago, and the tutorial is listed as one of his research outputs.

Rather than treating programming as a collection of syntax rules, the tutorial takes a hands-on approach. Readers learn Python concepts by working through examples involving variables, strings, functions, dictionaries, loops, files, graphics, and flow control.

Introduction to Python

The tutorial begins with the fundamentals of Python programming and the Python interpreter. Beginners are introduced to the interactive programming environment and gradually learn how Python statements are executed.

Early topics include:

  • Python interpreter and IDLE

  • Variables and assignment

  • Numbers and arithmetic

  • Strings

  • Printing and input

  • Functions

  • Dictionaries

  • Loops and sequences

  • Floating-point numbers

  • Basic program structure

This progression makes the material suitable for learners who are starting programming from the beginning.


Download the PDF for free: http://anh.cs.luc.edu/python/hands-on/3.1/Hands-onPythonTutorial.pdf

Learning Through Practical Examples

One of the main strengths of the tutorial is its emphasis on experimentation. Instead of only explaining what a programming feature means, the material encourages readers to type, execute, and modify Python code.

This approach helps beginners understand an important programming skill: learning by observing what a program actually does.

For example, after learning about variables, a learner can immediately change values and observe how the program behaves. The same approach continues with strings, functions, loops, and other Python features.

Variables, Data Types, and Functions

The tutorial introduces the fundamental building blocks used in almost every Python program.

Readers learn how variables refer to objects, how Python handles different types of data, and how operations can be performed on those objects.

Functions are also introduced as a way of organizing reusable pieces of code. This gives beginners an early understanding of how larger programs can be divided into smaller, manageable components.

Strings and Dictionaries

Strings receive considerable attention throughout the tutorial. Beginners explore string operations and progressively move toward more advanced string-related concepts.

Dictionaries are another important topic. They introduce a powerful way of organizing information through key-value relationships and help prepare learners for more structured Python programming.

Loops and Sequences

The tutorial introduces loops and sequences as essential tools for repetitive tasks.

Learners work with concepts such as:

  • Iteration

  • Sequences

  • Loop-based processing

  • Tuples

  • Repeated operations

  • Controlling program flow

These concepts are particularly important because they allow programs to process collections of information efficiently.

Objects and Methods

The second major section moves beyond basic syntax into Python's object-oriented model.

Readers encounter:

  • Objects

  • Classes

  • Methods

  • String methods

  • Object behavior

This provides an early introduction to one of Python's most important programming concepts: objects combine data and behavior.

Understanding methods also helps learners move from simply writing individual statements toward interacting with Python's built-in objects.

Practical Mini-Projects

The tutorial includes practical examples such as Mad Libs, graphics, and file processing.

These examples are useful because they demonstrate how individual Python concepts can be combined to create something more meaningful.

For beginners, this transition is important. Learning syntax is one thing; understanding how syntax can be combined to solve a problem is another.

Graphics with Python

An interesting part of the tutorial is its introduction to graphics.

Graphics provide a visual way for beginners to understand programming concepts. Instead of seeing only text output, learners can use Python to create visual results.

This can make concepts such as coordinates, repetition, and program control easier to understand.

Working with Files

The tutorial also introduces file processing.

File handling is an essential programming skill because real applications frequently need to read information from or write information to files.

Learning this topic helps beginners move beyond small interactive programs toward programs capable of processing persistent data.

Flow of Control

A later section focuses on flow of control, including conditional statements and different types of loops.

This section helps learners understand how a program decides:

  • Which statements should execute

  • When a block should execute

  • How many times an operation should repeat

  • How different conditions affect program behavior

These concepts form the foundation for almost every useful application.

Dynamic Web Pages

The tutorial also contains an introductory section on dynamic web pages, including web-page basics, composing web pages with Python, and CGI-based dynamic pages.

This gives learners an early look at how programming can interact with web technologies.

Although modern Python web development has evolved significantly, the section is valuable historically and conceptually because it demonstrates how Python can be used beyond standalone scripts.

Learning Structure

The tutorial follows a gradual progression:

Python Basics → Data and Functions → Objects and Methods → Graphics and Files → Flow Control → Web Programming

This structure makes it possible for beginners to build their knowledge step by step instead of being introduced to advanced concepts immediately.

Who Should Read This Tutorial?

Hands-on Python Tutorial is particularly suitable for:

  • Complete programming beginners

  • Students learning Python for the first time

  • Computer science students

  • Learners who prefer practical examples

  • Teachers looking for introductory Python material

  • Programmers who want a structured refresher

It is especially useful for learners who prefer learning by doing rather than reading theory alone.

Strengths

1. Beginner-Friendly Progression

The tutorial starts with basic programming concepts and gradually introduces more sophisticated ideas.

2. Practical Orientation

Examples, exercises, graphics, file handling, and programming tasks make the material more hands-on.

3. Strong Python Fundamentals

The tutorial covers many fundamentals that remain important for Python programmers, including functions, strings, dictionaries, loops, objects, and methods.

4. Broader Programming Perspective

It does not stop at syntax. The material introduces files, graphics, object-oriented concepts, and web programming.

5. Freely Accessible Resource

The tutorial has been distributed online as a learning resource, and the author's Loyola publication record identifies it as a 2015 research output.

Limitations

The tutorial is based on an older Python 3.1-era version, so some examples and development practices should be considered historically dated when compared with modern Python.

For current Python development, learners should supplement it with modern Python documentation and current libraries.

However, the underlying programming concepts remain useful for learning fundamentals.

Download the PDF for free: http://anh.cs.luc.edu/python/hands-on/3.1/Hands-onPythonTutorial.pdf

Final Verdict

Hands-on Python Tutorial is a practical and structured introduction to Python programming. Its biggest strength is its hands-on philosophy: learners are encouraged to interact with Python, experiment with code, and gradually build more complex programs.

The tutorial covers a surprisingly broad range of topics, moving from basic Python syntax to functions, dictionaries, loops, objects, graphics, files, flow control, and introductory web programming.

For someone beginning their Python journey, it can serve as a strong fundamentals-first learning resource, especially when combined with modern Python tools and documentation.

\

๐Ÿ Stanford Code in Place 2026: Learn Python for Free

 




๐Ÿ Stanford Code in Place 2026: Learn Python for Free

Stanford Code in Place 2026 is a popular international programming course from Stanford University. It is based on Stanford's introductory computer science curriculum and is designed to help beginners learn programming using Python.

๐ŸŽ“ What is Code in Place?

Code in Place is a beginner-friendly online learning program where students learn the fundamentals of programming through practical exercises and projects. You don't need previous programming experience to start learning.


Apply Now: https://codeinplace.stanford.edu/apply/cipx/student?

๐Ÿ What Will You Learn?

The course introduces important programming concepts such as control flow, variables, functions, loops, lists, dictionaries, and graphics. These concepts help learners develop programming logic and build a strong foundation in Python.

๐Ÿ’ป Why Learn Python?

Python is one of the most widely used programming languages. After learning the basics, you can explore areas such as Data Science, Artificial Intelligence, Machine Learning, Web Development, Automation, and Software Development.

๐ŸŒŽ Who Can Join?

The course is suitable for beginners, students, aspiring developers, and anyone interested in learning programming. It is especially useful for people who want to start their coding journey with a structured curriculum.

๐Ÿš€ How to Apply?

If applications are open for the relevant 2026 cohort, you can apply through the official Stanford Code in Place application page.

Apply Now: https://codeinplace.stanford.edu/apply/cipx/student?

⭐ Final Thoughts

If you want to start learning Python and programming from the fundamentals, Stanford Code in Place provides a structured way to begin. Learning programming step by step can also prepare you for advanced fields such as AI, ML, and Data Science.

Saturday, 19 September 2026

14 Best GitHub Profiles Every Python, Data Science & AI Developer Should Follow

 


GitHub is more than just a platform for storing code. It's a place to learn from experienced developers, explore open-source projects, and improve your programming skills.

Whether you're learning Python, exploring Data Science, building AI applications, or contributing to open source, following the right GitHub profiles can help you discover valuable resources.

In this blog, let's explore 14 GitHub profiles worth following in 2026.


๐Ÿ Python & Data Science GitHub Profiles

1. Wes McKinney — Creator of pandas

๐Ÿ”— GitHub: https://github.com/wesm

Wes McKinney is the creator of pandas, one of the most widely used Python libraries for data analysis.

Explore his GitHub profile to learn more about his work in data analysis and scientific computing.

Perfect for: Python, Pandas, Data Analysis.


2. Jake VanderPlas — Scientific Python & Data Science

๐Ÿ”— GitHub: https://github.com/jakevdp

Jake VanderPlas is known for his contributions to the scientific Python ecosystem and data science education.

His work covers scientific computing, visualization, and machine learning.

Perfect for: NumPy, Scientific Python, Data Science.


3. Tirthajyoti Sarkar — Machine Learning & Python

๐Ÿ”— GitHub: https://github.com/tirthajyoti

Explore Python projects, machine learning resources, and data science-related repositories.

This profile can be useful for developers looking for practical learning materials.

Perfect for: Machine Learning, Python, Data Science.


4. Andrej Karpathy — AI & Deep Learning

๐Ÿ”— GitHub: https://github.com/karpathy

Andrej Karpathy is known for his work in deep learning and AI education.

His repositories include projects and educational resources that help developers understand modern AI concepts.

Perfect for: Deep Learning, Neural Networks, AI.


5. Manu Joseph — Machine Learning

๐Ÿ”— GitHub: https://github.com/manujosephv

Explore machine learning-related projects, including work associated with PyTorch Tabular.

Perfect for: Machine Learning, PyTorch, Data Science.


๐Ÿค– AI & Machine Learning GitHub Profiles

6. Hugging Face — Open-Source AI

๐Ÿ”— GitHub: https://github.com/huggingface

Hugging Face provides open-source tools and libraries for machine learning, natural language processing, and AI development.

Developers can explore popular projects such as Transformers and other machine learning tools.

Perfect for: NLP, LLMs, Transformers, AI.


7. OpenAI — AI Research & Tools

๐Ÿ”— GitHub: https://github.com/openai

Explore OpenAI's public GitHub repositories, which include open-source projects and developer tools.

Perfect for: AI Development, Machine Learning, Open Source.


8. Microsoft — Cloud, AI & Developer Tools

๐Ÿ”— GitHub: https://github.com/microsoft

Microsoft maintains a wide range of open-source repositories covering AI, cloud computing, developer tools, and programming languages.

Perfect for: AI, Cloud Computing, Software Development.


9. Google — AI & Open Source

๐Ÿ”— GitHub: https://github.com/google

Explore Google's public repositories, covering software development, AI, machine learning, and other open-source projects.

Perfect for: AI, Machine Learning, Open Source.


๐Ÿ’ป Programming & Open Source GitHub Profiles

10. Sindre Sorhus — Open Source Developer

๐Ÿ”— GitHub: https://github.com/sindresorhus

Sindre Sorhus is a prolific open-source developer known for a large collection of JavaScript and Node.js packages.

Perfect for: JavaScript, Node.js, Open Source.


11. freeCodeCamp — Programming Education

๐Ÿ”— GitHub: https://github.com/freeCodeCamp

freeCodeCamp offers free programming education and maintains open-source learning resources.

Developers can explore educational content and contribute to the project.

Perfect for: Programming, Web Development, Beginners.


12. Real Python — Python Learning Resources

๐Ÿ”— GitHub: https://github.com/realpython

Real Python is a popular Python education platform.

Its GitHub profile provides access to public repositories and learning-related resources.

Perfect for: Python, Tutorials, Programming Education.


13. The Algorithms — Algorithms in Multiple Languages

๐Ÿ”— GitHub: https://github.com/TheAlgorithms

The Algorithms organization provides algorithm implementations in multiple programming languages.

It's a useful place to explore algorithms, data structures, and programming concepts.

Perfect for: Data Structures, Algorithms, Problem Solving.


14. Donnemartin — Data Engineering & Python

๐Ÿ”— GitHub: https://github.com/donnemartin

Explore repositories related to Python, software development, and data engineering.

Perfect for: Python, Data Engineering, Software Development.


๐ŸŽฏ Why Should You Follow GitHub Profiles?

Following developers and organizations on GitHub can help you:

✅ Discover real-world coding projects
✅ Learn from open-source contributors
✅ Improve your programming skills
✅ Explore new Python libraries
✅ Understand software development practices
✅ Find resources for AI and Data Science
✅ Contribute to open-source projects

GitHub is one of the best places to learn by exploring real code.


๐Ÿš€ How to Start Learning from GitHub

If you're a beginner, follow these simple steps:

Step 1: Choose a GitHub profile related to your interests.

Step 2: Explore their repositories.

Step 3: Read the README files to understand each project.

Step 4: Run the code on your local machine or in Jupyter Notebook.

Step 5: Try modifying the project and building something of your own.

Step 6: Contribute to open source when you're ready.

Thursday, 17 September 2026

Python Turtle Graphics - RGB & HSV Colors

 



Code:

import turtle import math t = turtle.Turtle() screen = turtle.Screen() screen.bgcolor("black") t.speed(0) t.hideturtle() t.width(2) for i in range(80): y = -240 + i * 6 x1 = 100 * math.sin(i * 0.25) x2 = -x1 t.color("cyan") t.penup() t.goto(x1, y) t.pendown() t.dot(7) t.color("magenta") t.penup() t.goto(x2, y) t.pendown() t.dot(7) t.color("white") t.penup() t.goto(x1, y) t.pendown() t.goto(x2, y) screen.mainloop()


Explanation:

1. Import Libraries
import turtle
import math
turtle → Used to draw graphics and shapes.
math → Provides mathematical functions such as sin().

2. Create Turtle and Screen
t = turtle.Turtle()
Creates a Turtle object named t.
This turtle acts as the drawing pen.
screen = turtle.Screen()
Creates the drawing window.
screen.bgcolor("black")
Sets the background color to black.

3. Configure the Turtle
t.speed(0)
Sets the turtle's drawing speed to maximum.
t.hideturtle()
Hides the turtle arrow/cursor while drawing.
t.width(2)
Sets the pen width to 2 pixels.

4. Create the Wave Using a Loop
for i in range(80):
Runs the loop 80 times.
i takes values from 0 to 79.
Each iteration creates one pair of dots and one connecting line.

5. Calculate the Vertical Position
y = -240 + i * 6
Calculates the y coordinate for each row.
Starting position is -240.
Every iteration moves upward by 6.

For example:

i = 0   → y = -240
i = 1   → y = -234
i = 2   → y = -228
...

This creates 80 horizontal levels.

6. Calculate the First Wave Position
x1 = 100 * math.sin(i * 0.25)
math.sin() generates a smooth wave-like movement.
i * 0.25 controls how quickly the wave changes.
100 controls the horizontal size/amplitude of the wave.

So x1 continuously moves left and right.

7. Create the Opposite Wave
x2 = -x1
Makes x2 the exact opposite of x1.
This creates a mirror effect.

For example:

x1 = 80
x2 = -80

So the two dots appear symmetrically around the center.

๐Ÿ”ต 8. Draw the Cyan Dot
t.color("cyan")
Changes the turtle drawing color to cyan.
t.penup()
Lifts the pen so movement doesn't create a line.
t.goto(x1, y)
Moves the turtle to the calculated (x1, y) position.
t.pendown()
Places the pen down again.
t.dot(7)
Draws a circular dot with size 7.

So this creates the left cyan wave.

๐ŸŸฃ 9. Draw the Magenta Dot
t.color("magenta")
Changes the drawing color to magenta.
t.penup()
Stops drawing while moving.
t.goto(x2, y)
Moves to the mirrored position (x2, y).
t.pendown()
Enables drawing again.
t.dot(7)
Creates the magenta dot.

This creates the right-side mirrored wave.

⚪ 10. Connect Both Dots
t.color("white")
Changes the line color to white.
t.penup()
Prevents a line from being drawn while moving to the first dot.
t.goto(x1, y)
Moves back to the cyan dot's position.
t.pendown()
Starts drawing.
t.goto(x2, y)
Draws a white horizontal line from the cyan dot to the magenta dot.

๐Ÿ” 11. Repeat the Pattern

The loop repeats everything:

for i in range(80):

Each repetition creates:

๐Ÿ”ต────────────๐ŸŸฃ
 ๐Ÿ”ต──────────๐ŸŸฃ
  ๐Ÿ”ต────────๐ŸŸฃ
    ๐Ÿ”ต────๐ŸŸฃ
      ๐Ÿ”ต๐ŸŸฃ

Because sin() changes the x positions, the endpoints form two mirrored waves.

๐Ÿ–ฅ️ 12. Keep the Window Open
screen.mainloop()
Keeps the Turtle window open.
Without this, the window may close immediately after the program finishes.

๐ŸŽฏ Main Concept

The most important line is:

x1 = 100 * math.sin(i * 0.25)

It uses the sine function to create the wave, while:

x2 = -x1

creates its mirror image. The result is a symmetrical neon-style wave pattern made from dots and connecting lines.













Wednesday, 16 September 2026

๐Ÿ Python Turtle Art ๐ŸŒ€ | Coding Meets Creativity!

 



Code :

import turtle import math t = turtle.Turtle() screen = turtle.Screen() screen.bgcolor("black") t.speed(0) t.hideturtle() t.width(2) for i in range(80): y = -240 + i * 6 x1 = 100 * math.sin(i * 0.25) x2 = -x1 t.color("cyan") t.penup() t.goto(x1, y) t.pendown() t.dot(7) t.color("magenta") t.penup() t.goto(x2, y) t.pendown() t.dot(7) t.color("white") t.penup() t.goto(x1, y) t.pendown() t.goto(x2, y) screen.mainloop()













Explanation:


1. Import the Turtle Module
import turtle

This imports Python’s built-in turtle module, which allows us to create graphics, drawings, and patterns using a turtle cursor.

2. Import the Math Module
import math

The math module provides mathematical functions. Here, we use math.sin() to create the wave effect.

3. Create a Turtle Object
t = turtle.Turtle()

This creates a Turtle object named t. The turtle will be responsible for drawing the dots and lines.

4. Create the Drawing Screen
screen = turtle.Screen()

This creates the Turtle graphics window and allows us to control its properties.

5. Set the Background Color
screen.bgcolor("black")

This changes the background of the Turtle window to black, making the colored pattern stand out.

6. Set Turtle Speed
t.speed(0)

speed(0) sets the turtle to its fastest drawing speed.

7. Hide the Turtle Cursor
t.hideturtle()

This hides the turtle icon so that only the artwork is visible.

8. Set the Line Width
t.width(2)

This sets the thickness of the lines drawn by the turtle to 2.

๐Ÿ”„ Creating the Pattern
9. Run the Loop 80 Times
for i in range(80):

The loop runs 80 times, with i taking values from 0 to 79.

Each iteration creates one horizontal level of the pattern.

10. Calculate the Y Coordinate
y = -240 + i * 6

This calculates the vertical position of the current row.

The pattern starts at -240.
Each iteration moves 6 pixels upward.
As i increases, the pattern moves from bottom to top.

11. Calculate the First X Coordinate
x1 = 100 * math.sin(i * 0.25)

This is the main mathematical part of the program.

math.sin() generates a smooth wave-like value.

100

controls the approximate width/amplitude of the wave.

i * 0.25

controls how quickly the wave changes.

Together, they create the left-side movement of the dots.

12. Calculate the Opposite X Coordinate
x2 = -x1

This creates the mirror position of x1.

For example:

x1 = 80
x2 = -80

So the two dots appear symmetrically on opposite sides.

๐Ÿ”ต Drawing the Cyan Dot
13. Set the Color to Cyan
t.color("cyan")

The turtle's drawing color is changed to cyan.

14. Lift the Pen
t.penup()

This prevents the turtle from drawing a line while moving to the next position.

15. Move to the First Position
t.goto(x1, y)

The turtle moves to the calculated (x1, y) coordinate.

16. Put the Pen Down
t.pendown()

The turtle is now ready to draw.

17. Draw the Cyan Dot
t.dot(7)

This creates a cyan dot with a diameter of 7 pixels.

๐ŸŸฃ Drawing the Magenta Dot
18. Change the Color
t.color("magenta")

The turtle's color is changed to magenta.

19. Lift the Pen
t.penup()

The pen is lifted so the turtle can move without drawing an unwanted line.

20. Move to the Second Position
t.goto(x2, y)

The turtle moves to the mirrored (x2, y) coordinate.

21. Put the Pen Down
t.pendown()

The turtle is ready to draw again.

22. Draw the Magenta Dot
t.dot(7)

This creates a magenta dot with a diameter of 7 pixels.

⚪ Connecting the Two Dots
23. Set the Color to White
t.color("white")

The color is changed to white for the connecting line.

24. Lift the Pen
t.penup()

The pen is lifted to avoid drawing an unwanted line while moving back to the first point.

25. Move to the First Point
t.goto(x1, y)

The turtle returns to the first dot's position.

26. Put the Pen Down
t.pendown()

The turtle is now ready to draw the connecting line.

27. Connect the Two Points
t.goto(x2, y)

The turtle draws a white horizontal line from the first point to the second point.

Each row therefore looks approximately like:

๐Ÿ”ต────────────๐ŸŸฃ

Because the x positions change using the sine function, these lines form a beautiful wave-like pattern.

๐Ÿ–ฅ️ Keep the Window Open
28. Start the Main Event Loop
screen.mainloop()

This keeps the Turtle graphics window open and allows it to continue handling events.

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 (1378) Python Coding Challenge (1271) Python Library (21) Python Mathematics (19) Python Mistakes (51) Python Pattern Challenge (18) Python Quiz (653) 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)