# make a sum variable and initialize it
sum = 0
# for-loop
# range = start-1, end 101 (not including 101-- so, 100), iterator = 1
for i in range(1,101):
# sum equals to the sum plus i
sum += i
# print out the sum
print (sum)
Category: Python
Python Program to Print out every odd number from 1-100
# for - loop
# start value = 1, end value = 101 (not including 101), iterator = 2
for i in range(1,101,2):
# print i
print (i)
How to Draw an n-sided Polygon in Python Using the Turtle Library
# import turtle library
import turtle
# get screen
Window = turtle.Screen()
# name turtle
bob = turtle.Turtle()
# prompt user for num of sides they want their polygon to be
sidesInput = int(input("Enter a number greater than or equal to 3: "))
# if the user inputed a number less than 3 for the number of sides they want in their polygon, print ERROR because a polygon cannot have less than 3 sides
if sidesInput < 3 :
print('ERROR')
# initialize variables
i = 0
forward = ''
left = ''
# using a while-loop
while i in range (sidesInput):
# make forward variable equal to 100
forward = 100
# make left variable 360 degrees divided by the number of sides inputed by user to get the angle at which to rotate to make the figure
left = 360/sidesInput
# while condition- make i = i + 1
i += 1
# turtle
# make bob go forward by forward variable
bob.forward(forward)
# make bob go left by left variable
bob.left(left)
Looping Examples-Graphics with the Turtle Library
# Example #1:
# import the turtle method
import turtle
# get the Screen
window = turtle.Screen
# initialize Turtle Variable
bob = turtle.Turtle()
# Make speed fast (0 is the fastest)
bob.speed(0)
# for loop
# start = 0, stop = 250, iteration = 1 (you don't have to put this because if no iteration is typed in the range, it assumes the iteration is 1.
for i in range(0,250):
# move bob forward and iterate by 1 to make bob loop
bob.forward(i+1)
# we are trying to make the base hexogonal and iterate it
# we use bob.left(60) to turn bob 60 degrees (the angle of each
side of a hexagon)
bob.left(60)
Output:

# Example #2:
# import Turtle Method
import turtle
# get Screen
window = turtle.Screen
# Initialize Turtle (I named mine 'bob')
bob = turtle.Turtle()
# make speed fast (zero = fastest)
bob.speed(0)
# for-loop
# start = 0, end = 500, iteration = 1
for i in range(0,500):
# move bob forward and iterate by 1
bob.forward(i+1)
# move bob left by 65 degrees (always go a couple of degrees off from the angle of a side of a hexagon if you want to create the hexagonal spiral like shape
bob.left(65)
Output:

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.

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!