top of page

Some Coding eexmaples to try:

​

​

>>> from turtle import *

>>> forward(200)

>>> for i in range(100):

...     forward(3.1416)

...     right(1.8)

...

>>> forward(200)

​

Next:

>>> from turtle import *

>>> color('red')

>>>

>>> for i in range(20):

...     forward(200)

...     left(108)

...

​

Try this:

>>> from turtle import *

>>> color('red')

>>>

>>> for i in range(10):

...     left(108)

...     forward(200)

...

>>> bye()

>>>

​

or try this:

>>> for i in range(6):

...     forward(200)

...     left(120)

Caesar Shift in Python (Brilliant)

 

alphabet = "abcdefghijklmnopqrstuvwxyz"

# convert between letters and numbers up to 26
def number_to_letter(i):
   return alphabet[i%26] # %26 does the wrap-around

def letter_to_number(l):
   return alphabet.find(l) # index in the alphabet

# How to encode a single character (letter or not)
def caesar_shift_single_character(l, amount):
   i = letter_to_number(l)
   if i == -1: # character not found in alphabet
       return "" # remove it, it's space or punctuation
   else:
       return number_to_letter(i + amount) # Caesar shift

# How to encode a full text
def caesar_shift(text, amount):
   shifted_text = ""
   for char in text.lower(): # also convert uppercase letters to lowercase
       shifted_text += caesar_shift_single_character(char, amount)
   return shifted_text

### MAIN PROGRAM ###

message = """
Once upon a midnight dreary, while I pondered, weak and weary,
Over many a quaint and curious volume of forgotten lore—
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door—
"'Tis some visitor," I muttered, "tapping at my chamber door—
Only this and nothing more."
"""

code = caesar_shift(message, 2)
print(code)

#functions

 

from math import sin
#change the list below to whatever x values you wish to see evaluated.
#Make sure you keep it in list format (include [] around your list!)
x_vals = [0, 0.5, 1, 1.5, 2]   
for x in x_vals:
   print("When x =", x)
   try:
        # the function we want to see what happens as x approaches 0
       print("y = " , x * sin(1/x), "\n")
   except  ZeroDivisionError: 
       print("Error! Cannot Divide By Zero!\n")

 

#Previously:

from math import sin
#change the list below to whatever x values you wish to see evaluated.
#Make sure you keep it in list format (include [] around your list!)
x_vals = [0, 0.5, 1, 1.5, 2]   
for x in x_vals:
   print("When x =", x)
   try:
        # the function we want to see what happens as x approaches 0
       print("y = " , sin(1/x), "\n")
   except  ZeroDivisionError: 
       print("Error! Cannot Divide By Zero!\n")

 

#first version of sine

 

from math import sin

# Change the list below to whatever x values you wish to see evaluated.
# Make sure you keep it in list format (include [] around your list!)
x_vals = [0, 0.5, 1, 1.5, 2]  

for x in x_vals:
   print("When x =", x)
   try:
        # the function we want to see what happens as x approaches 0
       print("y = " , sin(x) /x, "\n")
   except  ZeroDivisionError: 
       print("Error! Cannot Divide By Zero!\n")

bottom of page