Turtle programming in Python
First we import the turtle module. Then create a window, next we create turtle object and using
turtle method we can draw in the drawing board. “Turtle” is a Python feature like a drawing
board, which lets us command a turtle to draw all over it! We can use functions like
turtle.forward(…) and turtle.right(…) which can move the turtle around.Commonly used turtle
methods are :
Some turtle method
METHOD PARAMETER DESCRIPTION
Turtle() None It creates and returns a new turtle object
forward() amount It moves the turtle forward by the specified amount
backward() amount It moves the turtle backward by the specified amount
right() angle It turns the turtle clockwise
left() angle It turns the turtle counter clockwise
penup() None It picks up the turtle’s Pen
pendown() None Puts down the turtle’s Pen
up() None Picks up the turtle’s Pen
down() None Puts down the turtle’s Pen
color() Color name Changes the color of the turtle’s pen
fillcolor() Color name Changes the color of the turtle will use to fill a polygon
heading() None It returns the current heading
position() None It returns the current position
goto() x, y It moves the turtle to position x,y
begin_fill() None Remember the starting point for a filled polygon
end_fill() None It closes the polygon and fills with the current fill color
dot() None Leaves the dot at the current position
stamp() None Leaves an impression of a turtle shape at the current location
shape() shapename Should be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’
Plotting using Turtle
To make use of the turtle methods and functionalities, we need to import turtle.”turtle” comes
packed with the standard Python package and need not be installed externally.The roadmap for
executing a turtle program follows 4 steps:
1. Import the turtle module
2. Create a turtle to control.
3. Draw around using the turtle methods.
4. Run turtle.done().
5. So as stated above, before we can use turtle, we need to import it.We import it as :
from turtle import *
# or
import turtle
6. After importing the turtle library and making all the turtle functionalities available to us,
we need to create a new drawing board(window) and a turtle. Let’s call the window as
wn and the turtle as skk. So we code as:
wn = turtle.Screen()
wn.bgcolor("light green")
wn.title("Turtle")
skk = turtle.Turtle()
7. Now that we have created the window and the turtle, we need to move the turtle. To
move forward 100 pixels in the direction skk is facing, we code:
skk.forward(100)
8. We have moved skk 100 pixels forward, Awesome! Now we complete the program with
the done() function and We’re done!
turtle.done()
9. So, we have created a program that draws a line 100 pixels long. We can draw various
shapes and fill different colors using turtle methods. There’s plethora of functions and
programs to be coded using the turtle library in python. Let’s learn to draw some of the
basic shapes.
So, Code will be:
import turtle
wn = turtle.Screen()
wn.bgcolor("light green")
wn.title("Turtle")
skk = turtle.Turtle()
skk.forward(100)
turtle.done()
So, Output will be:
# using Turtle Programming
import turtle
skk = turtle.Turtle()
for i in range(4):
skk.forward(50)
skk.right(90)
turtle.done()
# Python program to draw star
# using Turtle Programming
import turtle
star = turtle.Turtle()
for i in range(50):
star.forward(50)
star.right(144)
turtle.done()
# Python program to draw hexagon
# using Turtle Programming
import turtle
polygon = turtle.Turtle()
side_length = 70
angle = 360.0 / num_sides
for i in range(num_sides):
polygon.forward(side_length)
polygon.right(angle)
turtle.done()
import turtle #Inside_Out
wn = turtle.Screen()
wn.bgcolor("light green")
skk = turtle.Turtle()
skk.color("blue")
def sqrfunc(size):
for i in range(4):
skk.fd(size)
skk.left(90)
size = size + 5
sqrfunc(6)
sqrfunc(26)
sqrfunc(46)
sqrfunc(66)
sqrfunc(86)
sqrfunc(106)
sqrfunc(126)
sqrfunc(146)
Encryption Decryption In Python
Cryptography
The science of writing secret codes is called cryptography. For thousands of years cryptography
has made secret messages that only the sender and recipient could read, even if someone
captured the messenger and read the coded message. A secret code system is called a cipher. The
cipher used by the program in this chapter is called the Caesar cipher.
In cryptography, we call the message that we want to be secret the plaintext. The plaintext could
look like this:
Hello there! The keys to the house are hidden under the flower pot.
Converting the plaintext into the encoded message is called encrypting the plaintext. The
plaintext is encrypted into the ciphertext. The ciphertext looks like random letters, and we cannot
understand what the original plaintext was just by looking at the ciphertext. Here is the previous
example encrypted into ciphertext:
Yvccf kyviv! Kyv bvpj kf kyv yfljv riv yzuuve leuvi kyv wcfnvi gfk.
But if you know about the cipher used to encrypt the message, you can decrypt the ciphertext
back to the plaintext. (Decryption is the opposite of encryption.)
Many ciphers also use keys. Keys are secret values that let you decrypt ciphertext that was
encrypted using a specific cipher. Think of the cipher as being like a door lock. You can only
unlock it with a particular key.
The Caesar Cipher
The key for the Caesar Cipher will be a number from 1 to 26. Unless you know the key (that is,
know the number used to encrypt the message), you won’t be able to decrypt the secret code.
The Caesar Cipher was one of the earliest ciphers ever invented. In this cipher, you encrypt a
message by taking each letter in the message (in cryptography, these letters are called symbols
because they can be letters, numbers, or any other sign) and replacing it with a “shifted” letter. If
you shift the letter A by one space, you get the letter B. If you shift the letter A by two spaces,
you get the letter C. Figure 14-1 is a picture of some letters shifted over by three spaces.
Figure 14-1: Shifting over letters by three spaces. Here, B becomes E.
To get each shifted letter, draw out a row of boxes with each letter of the alphabet. Then draw a
second row of boxes under it, but start a certain number (this number is the key) of spaces over.
After the letters at the end, wrap around back to the start of the boxes. Here is an example with
the letters shifted by three spaces:
Figure 14-2: The entire alphabet shifted by three spaces.
The number of spaces you shift is the key in the Caesar Cipher. The example above shows the
letter translations for the key 3.
If you encrypt the plaintext “Howdy” with a key of 3, then:
The “H” becomes “K”.
The letter “o” becomes “r”.
The letter “w” becomes “z”.
The letter “d” becomes “g”.
The letter “y” becomes “b”.
The ciphertext of “Hello” with key 3 becomes “Krzgb”.
We will keep any non-letter characters the same. To decrypt “Krzgb” with the key 3, we go from
the bottom boxes back to the top:
The letter “K” becomes “H”.
The letter “r” becomes “o”.
The letter “z” becomes “w”.
The letter “g” becomes “d”.
The letter “b” becomes “y”.
ASCII, and Using Numbers for Letters
How do we implement this shifting of the letters as code? We can do this by representing each
letter as a number called an ordinal, and then adding or subtracting from this number to form a
new ordinal (and a new letter). ASCII (pronounced “ask-ee” and stands for American Standard
Code for Information Interchange) is a code that connects each character to a number between 32
and 126.
The capital letters “A” through “Z” have the ASCII numbers 65 through 90. The lowercase
letters “a” through “z” have the ASCII numbers 97 through 122. The numeric digits “0” through
“9” have the ASCII numbers 48 through 57. Table 14-1 shows all the ASCII characters and
ordinals.
Modern computers use UTF-8 instead of ASCII. But UTF-8 is backwards compatible with
ASCII, so the UTF-8 ordinals for ASCII characters are the same as ASCII’s ordinals.
Table 14-1: The ASCII Table
32 (space) 48 0 64 @ 80 P 96 ` 112 p
33 ! 49 1 65 A 81 Q 97 a 113 q
34 " 50 2 66 B 82 R 98 b 114 r
35 # 51 3 67 C 83 S 99 c 115 s
36 $ 52 4 68 D 84 T 100 d 116 t
37 % 53 5 69 E 85 U 101 e 117 u
38 & 54 6 70 F 86 V 102 f 118 v
39 ' 55 7 71 G 87 W 103 g 119 w
40 ( 56 8 72 H 88 X 104 h 120 x
41 ) 57 9 73 I 89 Y 105 i 121 y
42 * 58 : 74 J 90 Z 106 j 122 z
43 + 59 ; 75 K 91 [ 107 k 123 {
44 , 60 < 76 L 92 \ 108 l 124 |
45 - 61 = 77 M 93 ] 109 m 125 }
46 . 62 > 78 N 94 ^ 110 n 126 ~
47 / 63 ? 79 O 95 _ 111 o
So if you wanted to shift “A” by three spaces, you would do the following:
Convert “A” to an ordinal (65).
Add 3 to 65, to get 68.
Convert the ordinal 68 back to a letter (“D”).
The chr() and ord() functions can convert between characters and ordinals.
The chr() and ord() Functions
The chr() function (pronounced “char”, short for “character”) takes an integer ordinal and returns
a single-character string. The ord() function (short for “ordinal”) takes a single-character string,
and returns the integer ordinal value. Try entering the following into the interactive shell:
>>> chr(65)
'A'
>>> ord('A')
65
>>> chr(65+8)
'I'
>>> chr(52)
'4'
>>> chr(ord('F'))
'F'
>>> ord(chr(68))
68
On the third line, chr(65+8) evaluates to chr(73). If you look at the ASCII table, you can see that 73
is the ordinal for the capital letter “I”.
On the fifth line, chr(ord('F')) evaluates to chr(70) which evaluates to 'F'. The ord() and chr() functions
are the opposite of each other.
CODE:
# CAESAR CIPHER
def encrypt(string, shift):
cipher = ''
for char in string:
if char == ' ':
cipher = cipher + char
elif char.isupper():
cipher = cipher + chr((ord(char) + shift - 65) % 26 + 65)
else:
cipher = cipher + chr((ord(char) + shift - 97) % 26 + 97)
return cipher
def decrypt(string, shift):
cipher = ''
for char in string:
if char == ' ':
cipher = cipher + char
elif char.isupper():
cipher = cipher + chr((ord(char) - shift - 65) % 26 + 65)
else:
cipher = cipher + chr((ord(char) - shift - 97) % 26 + 97)
return cipher
text = input("enter string: ")
s = int(input("enter shift number: "))
print("original string: ", text)
print("after encryption: ", encrypt(text, s))
CT=encrypt(text, s)
print("after decryption: ", decrypt(CT, s))
OUTPUT:
enter string: abc
enter shift number: 1
original string: abc
after encryption: bcd
after decryption: abc