More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
post
page
Python IDE Dashboard

Victor Vasarely’s Artwork revisited using Python

VasarelyIn this blog post we are looking at a specific painting from Victor Vasarely. The painting is from a serie of paintings called “Alphabet Plastique” and was painted in the 70’s.

We are going to try to recreate this painting using the turtle library in Python.

Did you know?


Victor Vasarely is a unique artist in the history of twentieth century art. Famous during his lifetime, he distinguished himself from contemporary art with the creation of a new movement: optical art. The evolution of his life of work is inherently coherent, progressing from graphic art to the artist’s determination to promote a social art that is accessible to all.
Source:www.fondationvasarely.fr

First attempt


Your Challenge


Search the internet for some artwork from Vasarely’s “Alphabet Plastique” collection and use this as a source of inspiration. Using the turtle library in Python (See script above), create your own piece of artwork inspired from your research.
Victor Vasarely - Selection

Tagged with: , , ,

Pixel Art in Python

smiley pixel art

Learning Objectives


In this blog post we are going to investigate how to use lists and list of lists with Python to create some 2D pixel art graphics.

List?


In Python, a list is used to save collection of values. For instance, to save all the different days of the week we could declare a variable called “daysOfTheWeek” and assign a list as follows:

daysOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

Based on this what do you think len(daysOfTheWeek) would return?
What’s about daysOfTheWeek[0]?
Or daysOfTheWeek[3] ?

Now look at this program used to display all the days of the week:

When using lists you can use a range of methods to add or remove values in the list. For instance:

Multi-dimensional Lists?


Sometimes you need more than on dimension to a list. In Python you can achieve this by creating a list of lists!

For instance for our pixel art project we want to store each pixel on a line within a list:
e.g.:

line1=[1,0,1,0,1,0]
line2=[0,1,0,1,0,1]
line3=[1,0,1,0,1,0]

We can then store all these lines into a list. Let’s call this list “grid”

grid=[line1, line2, line3]

A quicker way to do this is to do it all in one line as follows:

grid = [[1,0,1,0,1,0],[0,1,0,1,0,1],[1,0,1,0,1,0]]

We can then access any cell within this grid using the following command:

print grid[2][4] #This would return the value of the 5th of the 3rd line!

Let’s apply this to our pixel art project


Challenge #1:


Change this code to create the pixel art of a space invader:
spaceInvader

Challenge #2:


Tweak this code to allow colours to be used in your pixel art.
To do so you will first create a new list called colourPalette to store a collection of colours of your choice: e.g.

colourPalette = ["#FF0000#", "#00FF00", "#0000FF", "#000000", "#FFFFFF"]

More changes will be required for you to be able to recreate this pixel art:
Applepix

Traffic Lights Challenge

Did you know?

Traffic Lights
An embedded system is a computer system with a dedicated function within a larger mechanical or electrical system, often with real-time computing constraints.

A burglar alarm, an in-car cruise control system, a speedometer on a bike, a traffic lights control system are all examples of embedded system.

One application of computing is to create the algorithms used by these control systems. For this challenge we will focus on a traffic lights system.

Our Traffic Lights


Our system consists of two traffic lights used at a crossing.

The diagram below describes the sequence that these traffic lights have to follow:
traffic-light-sequence

We have started the code to control the first traffic light.

Your Challenge


Tweak this code to control the second traffic light. The second traffic light sequence should match the one described in the diagram above. However both traffic lights cannot be green at the same time!

Extension


Check this article to see how “smart” traffic lights help control the flow of traffic more effectively.

Tagged with: , ,

Python Shapes using Turtle

Learning Objectives

In this challenge we are going to use x and y coordinates to draw shapes on the screen. We will be using the turtle library to draw on the screen.

X and Y coordinates? Quadrant?


xy-coordinates
Check the above picture. Can you state three facts about X and Y coordinates and about quadrants?

The canvas we are drawing on (using Python Turtle) is 400 pixels wide by 400 pixels high.
Look at the canvas below to understand how (x,y) coordinates work:

Challenge #1

Look and test the following code used to draw a “+” shape.

Your task is to tweak this code to draw the following shapes:
PythonShapes

Challenge #2

Look and test the following code used to draw a square using the for loop.

Your task is to tweak this code to draw the following shapes:
PythonShapes2

Extension


Once you have completed all the shapes above you can try this more advanced challenge.

Tagged with: , ,

My E-Mail Validation Script

The aim of this challenge is to write a computer program that asks the end-user to type their e-mail address. The program should decide whether the e-mail being entered is a valid one or not.

Some of the validation checks that your program should complete are as follows:

  • The email should contain one and only one “@” sign
  • The email should contain at least one “.” sign located after the “@” sign. However it may contains more than one “.” sign for emails ending in .co.uk for instance
  • The email cannot contain any space or “#” signs
  • The “@” sign cannot be in the first position and there should be at least 1 character between the “@” and the “.” sign.
  • The email cannot end with a “.” sign

Can you think of any other rules to make your validation script even more robust?

Learning Objectives

By Completing this challenge you are going to develop your string manipulation skills!

Let’s Get Started

Look at the following code used to find out if the email contains an “@” sign.

Comlete this code to implement all of the validation check mentionned above.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: ,

Fancy a game of Poker Dice?

diceThe aim of this challenge is to create a simplified game of Poker Dice using only three dice.
The computer will generate three random numbers between 1 and 6. The user scores points as follows:

  • Three of a kind: +50pts
  • Straight (e.g. 3,4,5): +30pts
  • Three odd numbers: +15pts
  • Three even numbers: +15pts
  • One pair: +10pts

Step 1: Using random numbers

Start your game by generating three random numbers and displaying these to the end user.
You will need to import the randint function from the random python library. To do so you can use the following code:

from random import randint

#to generate a random number between 1 and 10 use the following instruction:
myNumber = randint(1,10)

Step 2: Odd or Even?

Think about the characteristic of an odd number aka a number which cannot be divided evenly by 2.

Using the mod (remainder: % sign in Python) we can determine if a number can be divided evenly by 2.

Based on this we can create a function (sub routine) called isOdd that will take a number as a parameter and return True if this number is odd, False if not.

Try this yourself or check the following code.

def isOdd(number):
    if (number % 2 == 1):
    	return True
    else:
        return False

Can you make up another function to findout if a number is even?

Step 3: Complete the game…

You will need to reuse the two functions, isOdd() and isEven() that you created in the previous step.

Complete the code

Check the code below and complete it further:

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: ,

Prime Number Check in Python

Did you know?

A prime number is a number that has exactly two factors (1 and itself). Which means that a prime number can be divided evenly only by 1, or itself. A prime number must be a whole number greater than 1.

2, 3, 5, 7, 11, 13, 17… are all examples of prime numbers.

Your challenge:

Write a program that prompts the user to enter a number between 1 and 9999. The program will inform the end-user whether their number is a prime number or not.

Hint:

In Python you can easily calculate the remainder of a division using the % sign.

For instance 7 % 2 would return 1.

So to check if a number “a” can be divided evenly by another number “b” you can check if the remainder of a/b is null using if a%b==0

Solution

So let’s look at the following code used to check if a number is a prime number or not.

Extended Challenges

  1. Tweak this code to display all the prime numbers between 1 and 9999.
  2. Write another program that takes a user input and find out if the number being entered is either odd or even.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Barcode Generator – Using Python

barcodeFor this challenge we are investigating how barcodes are produced. Though there are different standards to produce barcodes, they all follow a similar approach based on binary code!

Did you know?

On a barcode the pattern made using black and white lines is in fact binary code! A black line represents a 1 and a white line a 0.

So the following binary code “0101” consists of a thin white line, a thin black line, a thin white line and a thin black line.

The following binary code “1110” however consists of a large black line (as thick as 3 lines) and a thin white line.

So to produce our own simplified bar codes all we need to know is convert each digit of the actual number into binary (using 4 digit binary codes, aka a nibble)

Decimal Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001

Using Python to generate random barcodes

Your Challenge

Adapt this script to ask the end-user to enter their initials. Use the ASCII table to convert these initials into ASCII and then into binary code to then generate a barcode matching those initials.

Tagged with:

Random Cloud Generator using Python

cloudCan computers produce artwork? In this challenge we are looking at how to write a piece of code to generate a “pretty” graphic.

Our first script will generate a random cloud on a blue sky by drawing several (15 in total) white circles of different sizes and position on the canvas. It uses the randomize library to randomly alter the size and position of these circles.

Your Challenge


Use a similar script to generate a random forest as follows:

forest

Extended Challenge


How could you change your script so that each tree has a different shade of green:

forest2

Tagged with: ,

Recursive vs. Iterative Algorithms

The purpose of this blog post is to highlight the differnce between two types of algorithms: Iterative and Recursive algorithms.

The challenge we will focus on is to define a function that returns the result of 1+2+3+4+….+n where n is a parameter.

The Iterative Approach

The following code uses a loop – in this case a counting loop, aka a “For Loop”.
This is the main characteristic of iterative code: it uses loops.

# iterative Function (Returns the result of: 1 +2+3+4+5+...+n)
def iterativeSum(n):
    total=0
    for i in range(1,n+1):  
        total += i
    return total

The Recursive Approach

The following code uses a function that calls itself. This is the main characteristic of a recursive approach.

# Recursive Function (Returns the result of: 1 +2+3+4+5+...+n)
def recursiveSum(n):
    if (n > 1):
        return n + recursiveSum(n - 1)
    else:
        return n

You can visualise/trace this recursive function on recursivevisualizer.com

Let’s see both approaches in action

Your Challenge:

Tweak both functions above to:

  • add up only even numbers: e.g. 2+4+6+8+….+n-2+n
  • add up only odd numbers: e.g. 1+3+5+…+n-2+n
  • add up numbers, counting in 5: e.g. 5+10+15+…+n-5+n