Turtle Graphics

After this lesson, you will be able to create basic graphics in Python using the Turtle Library.

Input:

import turtle
window = turtle.Screen()
window.bgcolor('black')

Bob = turtle.Turtle()
Bob.begin_fill()
Bob.color('white')

Bob.forward(100)
Bob.left(90)
Bob.forward(100)
Bob.left(90)
Bob.forward(100)
Bob.left(90)
Bob.forward(100)
Bob.up()
Bob.forward(100)
Bob.end_fill()
Bob.color('black')

Output:

import turtle
window = turtle.Screen()
window.bgcolor('black')

Whenever you want to use the Turtle Library, use import turtle.

In order for the window (where the turtle is being executed) to show up, you just write the window variable name = turtle.Screen().

Since I wanted to change the color of my window, I used window.bgcolor(‘color’) to change my background color. I used window.bgcolor(‘black’) to change my background color black. If you want to use different string colors, this link has a variety of different colors that can be used. Using the link, just click on the color you like and put it in the string.

Bob = turtle.Turtle()
Bob.begin_fill()
Bob.color('white')

For this part of the code, I named my turtle ‘Bob’ in Bob = turtle.Turtle().

I wanted my turtle to be filled in so I used Bob.begin_fill() to begin filling Bob.

I then selected white to be the color that I wanted Bob to be filled in with using Bob.color(‘white’).

Bob.forward(100)
Bob.left(90)

Bob.forward(100)
Bob.left(90)

Bob.forward(100)
Bob.left(90)
Bob.forward(100)
Bob.up()
Bob.forward(100)
Bob.color('black')
Bob.end_fill()
Bob.color('black')

This is the part where I started to tell bob where to go and what to do. I used Bob.forward(100) to make him go in the forward direction for 100 units. Here’s how the output of the first line of this part of the code looked like:

I then used Bob.left(90) to make Bob turn left 90 degrees and made him go forward again by 100 units. Here’s how this looked like: Since I chose to fill the object in, it looks like a triangle, which is half of a square. It looks like this because we are halfway done.

I kept repeating the process till I got a square:

The arrow at the bottom of the square was slightly annoying to me, so to move the arrow away from the square, I used Bob.up() to basically lift Bob’s tail up and moved it forward a hundred units. I then ended the fill color using Bob.end_fill().I then changed the background color to black to make the white arrow black and blend it in with the background.

HTML Basics

You can copy (Ctrl C) and paste (Ctrl V) this into Notepad for Windows or any other equivalent:

<!DOCTYPE html>
<html>
	<head>
		<title>This is the title</title>
	</head>
	<body>
		This is the content
	</body>
</html>

Don’t type this in, this should be your output when you save it as an html doc and open it:

This is the title
	
	
		This is the content

This is how html input looks like on Notepad ++:

To get the output, click save as on Notepad or Notepad ++ and save the document as an html file. For Notepad, when typing the name of the file, add .html at the end of it. Ex : HTMLdocs.html . For Notepad ++ scroll threw the options till you find HypertextMarkupLanguage or html doc.

Every HTML document starts with <!DOCTYPE html>. The <> is called a tag. Each starting tag <> must have an ending tag </>.

For example, if you make the starting tag <html>, you must close the tag in the end with </html>.

This document is split into the two main categories, <head> and <body>.

In the head tag, we can write the title using the <title> and write the name of the title, which is “This is the title”. In the body tag, we can write content.

Hello, World! (Java)

“Hello, World!” is the first program written by most programmers.

If you are anywhere from being a beginner to being more experienced in Java, and if you have Windows or Mac, I recommend installing Eclipse IDE here. If you do not have Windows or Mac, I recommend creating an account on Repl.it here.

Eclipse:

Once you download Eclipse, go to:

File–>New–>Java Project–>(Name the Project)–> FINISH.

Click File->New->Java Project

Then, right click on the file and click New–>Class–>(Name the Class).

Right click file-> New Class

When you name the class, be sure to not put any characters or numbers as the beginning of the name. Also, do not include any spaces in the name. Finally, select the public static void main(String [] args) option and press FINISH. Now you are all set! 🙂

Click –>Run, Run to run your program!

Here’s how it works:

Input:

// This is a comment in Java, it does not affect the program or its output
// System.out.println() is used to print something out in java and go to the next line
// Quotes are used to denote that we are printing out a string
// You must put semicolons at the end of each line (except for comments, of course)

System.out.println("Hello, World!");

Output:

Hello, World

In Java, only double quotes (” “) are allowed for Strings. Therefore if you want to print something along with double quotes in java, you would have to do this:

Input:

// print something out with double quotes in java
System.out.println("\"Hello, World!\"");

Output:

"Hello, World!"

Hello, World! (Python)

“Hello, World! ” is usually the first program written by a programmer:

# HELLO WORLD (INPUT)- this is a comment in Python, this will not affect the program or its output

# the "print" symbol is used to print something out in the display

# we must put "Hello, World!" in quotes because it is a string

print("Hello, World!")

(OUTPUT)
Hello, World!

Notice that the quotes from print(“Hello, World!”) don’t get printed out in the output. This is because this message is read as a string. Strings in python are denoted by single (‘ ‘), double (” “), or triple (”’ ”’) quotes.

However, if you do want quotes to show up in the output, you have to put opposite quotes (either single quotes inside double quotes or double quotes inside single quotes) in the print statement :

So I’d use print(‘ “Hello, World! ” ‘) if I wanted to print out “Hello, World!” or print(” ‘Hello, World!’ “) if I wanted to print out ‘Hello, World!’.

# (INPUT)

print('"Hello, World!"')
OUTPUT:
"Hello, World!"

or,

# (INPUT)
print("'Hello, World!'")
OUTPUT:
'Hello, World!'

The triple quotes (”’ ”’), mentioned earlier, are used to print something in seperate lines. \n can also be used for this. For example:

# (INPUT)
# printing something in seperate lines using triple quotes ('''  ''')
print(''' Hello
World!''')
OUTPUT:
Hello
World!

Or another way to print out the same thing:

# (INPUT)
# printing something in seperate lines using backslash n- \n
print('Hello\nWorld!')
OUTPUT:
Hello
World!
Design a site like this with WordPress.com
Get started