The print function in Python is used very often to output something.
What Integers, Variables, Lists, Strings are
Variables -> A place where you can store data
List -> A type of data that allows you to store multiple things
List: list1 = [1, 3, 5, 2, 4]
How would you print Integers, Variables, Lists, and Strings?
Integers: print(______[write in integer, no “”])
Variables: print(_______[write in variable, no “”])
Strings: print(“______”[You can write in anything you want as long as it’s in quotes])
Lists: print(_______[Write the variable to store in the list]])
List1 = ["hi", "this", "is", "a", "list"]
→ Input is when the user gets to choose
print(“Which color do you like best? Pink or blue.”)
print(“What is your name?”)
print(“Hello, my name is ” + Name + “. And I like the color ” + Color + “!”)
→ Hello my name is Sumay. And I like the color blue!
As you can see input() is really cool! Let’s see if we can expand on it!
Food = [“Apple”, “Banana”, “Peach”, “Watermelon”, “Kiwi”]
0 & -5 1 & -4 2 & -3 3 & -2 4 & -1
Indexes: They point to a specific element in a list
Indexes in Python always start with 0
print(Food) —> Watermelon
print(Food[-4]) —> Banana
print(Food[-2]) —> Watermelon
Sometimes you want to print multiple things in one print statement. This is made possible by using a + sign in your print statements!
print("My favorite food is " + Word + ".")
Output → My favorite food is Carrots.
print(Numbers + Numbers2)
all_numbers = (Numbers + Numbers2)
print(“These are some of the numbers I know! ” + all_numbers)
Output->These are some of the numbers I know! [1,2,3,4,5,6]
len() it counts how many items are in a list.
Number = [1,2,3,4,5,6,”Hello”]
Food = [“Pizza”, “Cookie”, “Orange”]
Monkey = [“M”, ”o”, ”n”, ”k”, ”e”]
That adds to the list so it has the word “monkey”.
Output —> ['M', 'o', 'n', 'k', 'e', 'y']
Del() is used to delete things from a list.
Output —> [1,2,3,4,6,’hello’]
Output —> [1,2,3,4,’hello’]
Why is the difference between = and ==?
= means assign == means compare.
Elif is for when there is another if
print(“Your apple is red!”)
print(“Your apple is green!”)
print(“Your apple is yellow!”)
Casting is converting integers into string and vice-versa
How would you change 3 into a string.
Or a string into an integer.
Functions are quick ways of running big amounts of code.
*What you want the function to do*
The thing about a function is that you can do it multiple times!
You can also have customizable functions where you have to input values in the () in your function.
def persontemplate(firstname, lastname, age):
print("First Name: " + firstname)
print("Last Name: " + lastname)
persontemplate("Jeff", "PersonDude", "155")
persontemplate("Sally", "Hello", "68")
By adding (firstname, lastname, age) in the function you make it so that when you call the function, you have to add those values.
How to make things uppercase
13. How to make things lowercase
A For loop loops through a list.
If you want to change something in a list you can do this:
What if you want to change x into:
That would take a long time.
So we can make a For loop.
This for loop would add 1 to every element of the list.
For *variable to hold the current element of the list* in *list*():
Then it does whatever you put for the for loop to do and does it to every element in the list.
How to keep track of points
How would you keep track of points in python?
→ Unfortunately there is no built in function that keeps track of points. So we have to build it ourselves.
# First we make a variable called points.
# We start with 0 because we start out with no points
# Now, we need to have something to put points for.
# So, we are going to make a quiz!
# Let me show you an example so you understand.
print(“What is my favorite color?”)
# My favorite color is black.
What is my favorite color?
# Now you make an example!
# After it works, then we can make our quiz longer!
# There are three important things that will be used a lot in python
# Lets start with learning what they are and how to use them
# These three things are integers, variables, and strings
Well, lets first start out by saying what printing means
When you print something, you are just showing people data or information
This information can also include integers, or as we just learned, numbers
How you print something is pretty simple...
The data that you want to show will go inside these things --> ()
When printing an integer you don't have to anything except put the integer inside --> ()
So, if I wanted to print the integer 3, I would do this
Can you print three different integers for me?
# Print your integers here
Next, what is a variable?
A variable is a place where you store information
After you store the information in a variable you can also print the information inside the variable
An example of a variable is this:
number is basically the name of the variable
Inside of number, we put in information
This information is an integer
To print the integer inside the variable we do this
We do kind of the same thing, but instead of putting in an integer, we put in a variable
Since the variable has an integer inside it, python will print the integer
If we accidentally put in the wrong name for the variable when we are trying to print it python will give us an
And error is when something goes wrong in the code that we write and python can't do what we want it to do
So if I did this by accident
Python will give me an error and will say it was not able to print the integer
Can you make three variables for me?
# Make the variables here
Now can you print the variables?
# Print the variables here
Turtle is a really cool package you can import into your python project to make graphics and drawings with your code. The first thing you need to do is at the top of your code write
Before making the turtle do or draw anything, your have to write turtle. This might not seem like much but if you are going to write a lot of code you may want to make it shorter. You can do this by writing
ttl = turtle.Turtle()
you can make the ttl anything you want. For example:
bob = turtle.Turtle()
Making the turtle move and draw
To make the turtle draw the first thing you have to do is
. Then when you make the turtle move it will drag a line behind it.
The way you can make the turtle move is with four functions:
ttl.forward(100) # the (100) makes the turtle go forward 100 pixels
ttl.left(90) # the (90) makes the turtle turn 90 degrees to the left
ttl.backward(100) # the (100) makes the turtle go backward 100 pixels
ttl.right(90) # the (90) makes the turtle turn 90 degrees to the right
You can also make a circle by writing:
ttl.circle(50) # the (50) affects how pixels long the circle's radius is
- You are able to change the pen color that your turtle draws with by saying:
Keep in mind that you need to make sure that the package turtle has the color you are using. Here is a chart with all the colors you can use:
- This allows you to change the with that you draw in by writing:
- This function allows you to change the shape of the turtle that is drawing the lines:
Cryptography is a way to encode and decode messages with python. This is useful for when you are sending sensitive information in a way that is safe from hackers. Here are some ways you can encode messages you are sending:
If you inputted message as hi, then the output would be “ih”
The way this works is the “”.join bit makes it so that in between each character there is a “”, so nothing is put there, and the the (reversed(message)) part reverses the message. You can also put something in the “” to make it so that there is something placed between each character!
This would make it so that if message = “hello” the output would be:
Which is a lot less readable.
Changing Position In Alphabet
The next way you can crypt a string is by changing the position in the alphabet. For example you could make it so that if you put in a letter, it will move 2 letters in the alphabet. So if you put in “a” the output would be “c”. Here’s the code for you to do it!
message = input("message: ").lower()
# This allows you to put in the message
# The reason why I added .lower() is because when a letter is uppercase the encryption
alphabet = "abcdefghijklmnopqrstuvwxyz"
# This is so we can find the letter's position in
# the alphabet and then change it
encrypt = ""
# This is an empty variable that we can use to store the encrypted message
for i in message:
# This is a for loop that loops through every letter in the message
position = alphabet.find(i)
# This finds the letters position in the alphabet and
# then stores in the variable position
newposition = (position+5) %26
# This variable changes the letter so it is five
# letters forward in the alphabet and then stores the new letter in newposition
# the %26 makes it so that if the message is "z" then to encrypt it, it will start
# over and start with a again
encrypt += alphabet[newposition]
# Then it takes the new letter and then stores it
# in the encrypt variable. The for loop will make it so that every letter will be
# This prints the encrypted message
question = input("Would you like to decrypt? (Yes/No)")
if question == "Yes":
# If statement that checks to see whether you inputted Yes
decrypt = ""
# Variable that will store the decrypted message
# This is just another for loop that will do the same thing as the other one except
# it will decrypt the message instead by doing "- 5" instead of "+ 5"
position = alphabet.find(i)
newposition = position - 5
decrypt += alphabet[newposition]
# Prints decrypted message
# Else statement if you didn't type Yes
Matplotlib is an extension to python that allows you to create charts and map data! The first thing you have to do is download the extension by running in your terminal (not the Python Shell):
python3 -m pip install matplotlib
Then you should import it at the top of your code like this:
By saying “as plt” this makes it so that you can now use the package by calling plt instead of having to write out matplotlib.pyplot. Then you can use
to insert what the graph should chart by using a list of numbers. You can also use the functions
to label the axis and what they represent.
After you have created what should be graphed and displayed, end your code with
import matplotlib.pyplot as plt
To plot multiple lines, just call
import matplotlib.pyplot as plt
You can change the color of the lines by adding
color = ‘’
in the plt.plot function:
plt.plot([1, 2, 3, 4], color="green")
To add a title to your graph, you can call the function:
You can add a key to your graph by labeling the lines and then displaying what they mean in a legend like this:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], label = 'Line 1')
# The label informs people what the line
plt.plot([2, 4, 1, 3], label = 'Line 2')
# The legend displays what the lines are
There is an extension package called time that allows you to wait for a certain amount of seconds before continuing on to the next function. To start using it write
at the top of your code.
time.sleep(5) # The five represents how many seconds to wait
You can also put in a decimal if you would like to have your code wait less than a second. This package can be useful when you are trying to pint many things but want to give people a chance to read it all.
Pygame is a python extension that allows you to create games in python! The first thing you need to do is install the extension by writing
python3 -m pip install pygame
in your computers terminal (not your python shell!)
Then, after it has successfully downloaded write
at the top of your code.
which initializes all your imported modules.
The next thing you should do is create a landscape in which you can code your game. You can do this by writing this code:
display_width = 800
# This is a variable that holds the width of our game landscape
display_hieght = 600
# This is a variable that holds the hieght of our game landscape
# This creates a space for us
# to make our game and sets the width and hieght to the variable we created earlier
This code would output this:
The caption would replace the text “pygame window” with whatever you want to name your game. To set the caption, add this to your code:
pygame.display.set_caption("INSERT GAME NAME")