Using Python to Make Art with Math

<a href="https://simpleprogrammer.com/need-learn-math-programmer/" target="_blank">Math</a>&nbsp;can be intimidating.

Math can be intimidating.

Depending on the teacher and how it is taught, it can be an infuriating combination of inscrutable and boring.

But, there’s a beauty to math—a symmetry to the intelligence and logic behind numbers. I love math, and I want other people to love it too.

One neat way to make math more approachable and show its beauty visually is to combine it with something called “generative art.” Generative art is where you create a few, usually simple rules which are often math- or geometry-based, and then you tell a computer to process these rules.

Since computers are great for processing instructions quickly, and on a much greater scale than a human could, the designs that are created are often more complex and interesting than you might expect from such simple rules.

This example shows a bunch of floating particles that move with a mesmerizingly natural motion. They float around, link up with other particles, and change directions all on their own. It’s a variation on a “flocking algorithm,” and the amazing thing is that most of this natural motion comes from simply having each particle follow the rules of “don’t run into anybody,” “stay with the flock,” and “go in roughly the same direction as those near you.”

Python is a great option for creating these generative art projects; it is used by data scientists, mathematicians, and engineers (among many others) as an open source option for processing numerical calculations and generating visualizations.

It is also extremely easy to read and write clear code, which makes it an ideal language for outlining the simple rules needed to create this generative art.

One of the simplest mathematical constructs you can create with these rules is an integer sequence, which is an ordered list of integers (whole numbers, including: positive, negative, and zero). Usually, the relationship between these integers is spelled out in some sort of logical way with a set of rules in order to help someone figure out what the next number in the pattern is.

In this article, we are going to take the mathematics of integer sequences, supercharge them with the power of programming in Python, and use them to create some interesting and beautiful generative art.

It is a fun exercise, and the calculations and code samples are simple enough that they should be engaging for programmers, mathematicians, and artists alike.

This is not a beginner’s Python article, though, so I will assume you have some familiarity with Python’s syntax—or, at least, a willingness to pick it up as you go along—as well as how to run Python programs. If you’re not sure how to run them, and you’d like to write code in an application with a big giant “Play” button, the Mu text editor is great for beginners.

The particular sequence I want to talk about this time is the Recamán sequence. The rules are deceptively simple, but when the numbers are given visual or auditory shape, the results can be interesting and even a little spooky.

The Recamán Sequence

Here are the rules:

  1. Start at zero.
  2. Every step you take will be 1 bigger than the last step you took.
  3. If it’s possible to step backward (negatively), do so. Otherwise step forward.
  4. Backward steps are only allowed if the resulting location is positive (greater than zero) and if we’ve never been to that number before.

Let’s do the first few as examples.

We start at zero.

  • 0

The next step size will be 1. Stepping backward would put us at -1, which is not allowed, so we’ll step forward.

  • 0 -> 1

The next step size is 2. Stepping backward would put us at -1. That’s still not allowed, so forward we must go.

  • 0 -> 1 -> 3

The next step size is 3. Stepping backward puts us at 0. Since we’ve already been to 0 (our first starting point), this is not a valid move. I promise the sequence gets interesting soon, but for now, we step forward.

  • 0 -> 1 -> 3 -> 6

The next step size is 4. Stepping backward lands us at 2. Since 2 is positive, and we haven’t seen it yet, we can take our first legitimate backward step!

  • 0 -> 1 -> 3 -> 6 -> 2

Hopefully you’re beginning to see how the rules work. Now, this is kind of interesting to think about, but I’m not sure I would call a list of five numbers beautiful. That’s where we’ll lean a little bit harder on art and code. Luckily Python provides both of these to us in a fun and adorable module in its Standard Library: Turtle.

Introducing Turtle Graphics

Turtle Graphics was originally a key feature of the Logo programming language.

It’s a relatively simple Domain-Specific Language (DSL) where there is an avatar—traditionally shaped like a little turtle—on the screen, and you give it instructions on where to go: forward, left, or right. As it moves, it draws a line wherever it goes with its tail, although you can tell it to pick its tail up or put it down as necessary. It can even jump positions and change colors!

Python also includes a “turtle” library packaged along with it in its standard library. Let’s take a look at a sample program to see how it works. Create a file called “example1.py” with the following contents.

import turtle

window = turtle.Screen()
joe = turtle.Turtle()

joe.forward(50)
joe.left(90)
joe.forward(100)

turtle.done()

Here are the important bits:

  • On line 1, we import the “turtle” module. This will have many of the functions that we’re going to use, and we couldn’t use them unless we imported the module first. We’ll use these functions by writing “turtle.function_name,” as you’ll see.
  • On line 3, we create a Window. This is the window that will pop up when you run your code. It holds all of the stuff we’re going to be drawing.
  • On line 4, we actually create our Turtle. We are going to call him “joe” and store him in a variable so that we can give him commands later.
  • Lines 6-8 are us giving “joe” commands. You see the “dot” syntax that should be getting pretty familiar.
  • Line 10 just keeps the window from closing when “joe” is done with his drawing.

If you run your code, you should see something like the following:

Side note: If you want the real, old-fashioned Logo experience, you can add “joe.shape(“turtle”)” to your code, right under the line where you define “joe.” Isn’t he cute?

Okay, now that you’ve seen what “turtle” is all about, let’s get back to the sequence we were working on.

Coding the Sequence

Like anything good, we’re definitely not going to get a perfect result on the first go. We’ll need to do some iteration. I’ll take you through three passes at this art project, and each one will get a little more complicated and a little more visually interesting. After that, I have some potential ideas for further iteration that you can try if this gets your creative spark going. Let’s get to it!

First Try

The code that we write will be very similar to the English we would use to describe the steps for the sequence. Remember the two rules: Go backward when possible, otherwise go forward, and increase the step size by one after each step.

Create a new file named “recaman1.py.” We’ll start with those basic rules and then figure out how to make it actually work. I’m naming our new turtle Euler, after some guy.

import turtle

window = turtle.Screen()
euler = turtle.Turtle() # A good mathy name for our turtle
euler.shape("turtle")

current = 0 # Here's how we know where we are
seen = set() # Here's where we'll keep track of where we've been

Step increases by 1 each time

for step_size in range(1, 100):

backwards = current - step_size

# Step backwards if its positive and we've never been there before
if backwards &gt; 0 and backwards not in seen:
    euler.backward(step_size)
    current = backwards
    seen.add(current)

# Otherwise, go forwards
else:
    euler.forward(step_size)
    current += step_size
    seen.add(current)

turtle.done()

However, when we run it, it doesn’t look very good. In fact, it looks like maybe somebody gave Euler too much coffee.

Second Try

So, that was pretty reasonable. The code seems to read just like we might explain it to someone, which is good.

I’m afraid that the linear motion is just a little boring, though. This is when we want to put our artists’ caps on (do artists wear caps?) and figure out a little more creative way to get Euler from point A to point B.

Luckily turtles don’t have to move in straight lines! They can also move in arcs and circles. Let’s have him bounce from spot to spot on the number line! To make a turtle draw a circle or partial arc, we’ll use the “circle” command, which causes the turtle to follow a circle where the imaginary center is “radius” units to the turtle’s left.

That means we’ll have to orient our turtle before drawing, depending on whether he’s going forward or backward, using the “setheading” command.

Remember that you can find all the turtle commands in the official documentation, just in case you’re curious.

import turtle

window = turtle.Screen()
euler = turtle.Turtle() # A good mathy name for our turtle
euler.shape("turtle")

current = 0 # Here's how we know where we are
seen = set() # Here's where we'll keep track of where we've been

Step increases by 1 each time

for step_size in range(1, 100):

backwards = current - step_size

# Step backwards if its positive and we've never been there before
if backwards &gt; 0 and backwards not in seen:
    euler.setheading(90) # 90 degrees is pointing straight up
    euler.circle(step_size/2, 180)  # 180 degrees means "draw a semicircle"
    current = backwards
    seen.add(current)

# Otherwise, go forwards
else:
    euler.setheading(270)  # 270 degrees is straight down
    euler.circle(step_size/2, 180)
    current += step_size
    seen.add(current)

turtle.done()

That’s neat, but for the first little while, it seems like he just wiggles around in one place and the lines are very close together. Also, he’s not going to be using the whole left half of the screen!

Let’s do one more iteration together, where we make it even nicer to look at.

Third Try

The goals for this iteration are to make the picture bigger, and to give him more room to work.

import turtle

window = turtle.Screen()
window.setup(width=800, height=600, startx=10, starty=0.5)
euler = turtle.Turtle() # A good mathy name for our turtle
euler.shape("turtle")
scale = 5 # This isn't a turtle module setting. This is just for us.

Move the little buddy over to the left side to give him more room to work

euler.penup()
euler.setpos(-390, 0)
euler.pendown()

current = 0 # Here's how we know where we are
seen = set() # Here's where we'll keep track of where we've been

Step increases by 1 each time

for step_size in range(1, 100):

backwards = current - step_size

# Step backwards if its positive and we've never been there before
if backwards &gt; 0 and backwards not in seen:
    euler.setheading(90)  # 90 degrees is pointing straight up
    # 180 degrees means "draw a semicircle"
    euler.circle(scale * step_size/2, 180)
    current = backwards
    seen.add(current)

# Otherwise, go forwards
else:
    euler.setheading(270)  # 270 degrees is straight down
    euler.circle(scale * step_size/2, 180)
    current += step_size
    seen.add(current)

turtle.done()

As you can see, we’ve added a scaling factor which you can tune to whatever you think works best. I arrived at this value by trying a couple and picking my favorite. We also shifted him over so he starts at the left side of the screen. Since he can never go negative, we know he will only go right from wherever he starts.

Further Tweaking

By now, I think you get the gist, and you’re hopefully starting to see the magic of integer sequences: out of a few simple rules (and with the help of a tireless reptile assistant), you can make some truly interesting and captivating results.

You’ve got all the tools you need to do something even cooler. Here are some ideas to get your creative juices flowing:

  • What if you change the color of the lines? What if you change it continuously?
  • What if you skew the drawing to an angle to take up more of the screen?
  • What if you have multiple turtles drawing sequences in tandem?
  • What if you vary the predictable sequence with a little bit of randomness?
  • What if you incorporate user input (mouse clicks, key presses, etc.) to affect how the turtles behave?
  • What if you make a turtle that draws but is driven by something really slow, like the cycle of the moon?
  • Is there a way you could incorporate a turtle with an IoT project or a web application?
Other Incarnations of the Sequence

If you found the Recamán sequence particularly fascinating, you’re not alone. There are a ton of different incarnations out there people have created, combining the lines with color, sound, and more. Here are a few of my favorite.

This slightly spooky version with sound:


This Numberphile video about the sequence is really interesting:


The Coding Train also did a couple videos on coding this up if you want a cool video walkthrough with an amazing teacher and don’t mind writing JavaScript:

In fact, P5.js (and its Java-based predecessor, Processing) are both great alternatives for making art and animations with code; they come with dedicated editors to help you do that, and they allow support for sound and other add-ins!

Get Your Turtles in Gear

Hopefully this tutorial was enough to spark your interest in using code to generate art, and hopefully (if you were before), you are no longer too intimidated to look to mathematics as a source for your artistic ideas.

Geometry, never-ending constants, golden ratios, Fibonacci spirals, fractals, and number theory are all goldmines of awesome visual projects just waiting to be programmatically generated, and now you have all the tools you need to get your own set of turtles and start generating!


By :  Ryan Palo


Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Description
Learn Hands-On Python Programming By Creating Projects, GUIs and Graphics

Python is a dynamic modern object -oriented programming language
It is easy to learn and can be used to do a lot of things both big and small
Python is what is referred to as a high level language
Python is used in the industry for things like embedded software, web development, desktop applications, and even mobile apps!
SQL-Lite allows your applications to become even more powerful by storing, retrieving, and filtering through large data sets easily
If you want to learn to code, Python GUIs are the best way to start!

I designed this programming course to be easily understood by absolute beginners and young people. We start with basic Python programming concepts. Reinforce the same by developing Project and GUIs.

Why Python?

The Python coding language integrates well with other platforms – and runs on virtually all modern devices. If you’re new to coding, you can easily learn the basics in this fast and powerful coding environment. If you have experience with other computer languages, you’ll find Python simple and straightforward. This OSI-approved open-source language allows free use and distribution – even commercial distribution.

When and how do I start a career as a Python programmer?

In an independent third party survey, it has been revealed that the Python programming language is currently the most popular language for data scientists worldwide. This claim is substantiated by the Institute of Electrical and Electronic Engineers, which tracks programming languages by popularity. According to them, Python is the second most popular programming language this year for development on the web after Java.

Python Job Profiles
Software Engineer
Research Analyst
Data Analyst
Data Scientist
Software Developer
Python Salary

The median total pay for Python jobs in California, United States is $74,410, for a professional with one year of experience
Below are graphs depicting average Python salary by city
The first chart depicts average salary for a Python professional with one year of experience and the second chart depicts the average salaries by years of experience
Who Uses Python?

This course gives you a solid set of skills in one of today’s top programming languages. Today’s biggest companies (and smartest startups) use Python, including Google, Facebook, Instagram, Amazon, IBM, and NASA. Python is increasingly being used for scientific computations and data analysis
Take this course today and learn the skills you need to rub shoulders with today’s tech industry giants. Have fun, create and control intriguing and interactive Python GUIs, and enjoy a bright future! Best of Luck
Who is the target audience?

Anyone who wants to learn to code
For Complete Programming Beginners
For People New to Python
This course was designed for students with little to no programming experience
People interested in building Projects
Anyone looking to start with Python GUI development
Basic knowledge
Access to a computer
Download Python (FREE)
Should have an interest in programming
Interest in learning Python programming
Install Python 3.6 on your computer
What will you learn
Build Python Graphical User Interfaces(GUI) with Tkinter
Be able to use the in-built Python modules for their own projects
Use programming fundamentals to build a calculator
Use advanced Python concepts to code
Build Your GUI in Python programming
Use programming fundamentals to build a Project
Signup Login & Registration Programs
Quizzes
Assignments
Job Interview Preparation Questions
& Much More

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

Description
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Description
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.

Dropbox: https://bit.ly/2AW7FYF

Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more: