Here are two opening exercise sets you can work on with a partner.
Brief explanation on some of the most common elements of Python:
# I am a comment!
Any line that that starts with the '#' is ignored by the computer. It's just for us humans :)
This is useful for explaining what your code is doing, or "turning off" some code for testing.
Python takes white space very seriously. You will notice how the lines after our "if" statements or loops are indented. Any indented lines are contained within the conditional or loop. To end your statement or loop, simply make the next line of code *not* indented
looping = True
while(looping):
#inside the loop
print("we are looping")
#outside the loop
print("no longer looping")
year = 2021
"Year" is a variable. It is a name we created that points to some piece of information (in this case the number '2021').
Let's do another:
year_founded = 1895
Since our variable is now a sort of "stand-in" for our number, we can do math with it:
age = year - year_founded
And now we have a third variable, "age" with a value of 126.
A list is a great way to keep track of large amounts of information! Here's how we make an empty list:
list = []
and here's how we add items to it:
list.append(laser) #this assumes we have a variable named laser
If we wanted to create a new list with some items already inside:
colors = ["orange", "violet", "goldenrod"]
and lastly, we get items from lists using their position in the list (starting with 0)
my_color = colors[0]
print(my_color) #this will print out "orange"
For more fun with lists, see "For Loops"
Conditionals are a great way to introduce some logic into your code. This allows you to run different steps based on some condition. Think of this like decision trees. To do this we use "if" statements like so:
condition = True
if(condition):
print("it's true!")
else:
print("it's false :(")
Our condition will always be logically "True" or "False". To make these logical statements we can use a number of different comparators: (<, >, <=, >=, ==). These can be used to compare numbers (and other things too!). Note that our "equals" comparator uses two equals signs rather than just one.
x = 12
if(x < 9):
print("it's less than nine")
elif(x <=20):
print("it's less than or equal to 20")
else:
print("greater than 20!")
Loops help us write code to be repeated – this way we don't have to write the same line of code over and over again! A "while" loop works similarly to an "if" statement, but instead of just asking once whether the condition is true, it keeps looping and until the condition becomes false. Here's what this might look like:
x = 3
while(x < 14):
print("x is still less than 14")
x = x + 1
each time the loop runs, our loop checks if x is less than 14, and adds 1 to x if it's still not there. In this example, our statement will print 11 times.
Another mechanism we have to loop code is the "for" loop. These are helpful when you want to run a piece of code for every item in a list. Here's how this might look:
colors = ["orange", "violet", "goldenrod"]
for c in colors:
print(c)
we can think of "c" as a variable that represents each item in the list as we go through it. If we want to loop a specific number of times, we can use "range" like this:
for n in range(6):
print(n)
Functions help keep your code really organized and readable once it gets longer! Functions are just blocks of code that are packaged together and given a name. You can create a function like this:
def my_function():
print("hi I'm a function!")
We have defined a function called "my_function", but our text won't print until we call the function. We can do that like this:
my_function()
Notice the parentheses( ). Most of the time you see those parentheses it means function! You may have noticed that "print" also uses them – "print" is also a function! We call the function using the parentheses and put our message inside. This message is called a parameter. You can add parameters to our function like this:
def my_function(message):
print("here's my message: ")
print(message
and then call our function:
my_function("look at this cool function!")
Python makes it easy to generate random numbers, but requires you to import the module. You can import the "random" module using:
import random
once it is imported, we can generate random numbers within a defined range. For example, to generate a random number between 5 and 11 we would use this:
random.randint(5,11)
You will be tackling one of these prompts with your group and presenting your results to the class. Feel free to consult the internet for help, but make sure you have a full understanding of your solution. If you finish early, ask "what is another way we can approach this problem?"
1) Write a program that creates a list of names, and prints the names in order.
Can you print them in reverse order?
2) Write a program that generates 10 random numbers between 0 and 100.
Can your program make a list of all the numbers that generated between 40 and 60?
3) Write a program that generates 10 random numbers between 0 and 100.
Have your program find and print out the highest number generated. Can you also print out the lowest?
4) Write a function that takes a number using a parameter (see "Functions" above) and multiplies it by 11, printing the result. How do you call the function?
For the brave of heart.
1) Given a list of numbers, write a function that sorts the numbers in decreasing order. You can use the following list to test with:
my_numbers = [9, 57, 18, 3, 29, 4, 7, 99, 62, 111]
2) Given a list of numbers, write a function that will pick a random number from the list. Make sure that each time you call the function it gives you a different number (no repeats) until you've exhausted the list. You can use the following list to test with:
my_numbers = [9, 57, 18, 3, 29, 4, 7, 99, 62, 111]
3) Given a list of words, write a function that finds only the members of the list that start with the letter 'm'. You can use the following list to test with:
my_words = ["laser", "toothpaste", "molasses", "marble", "pumpkin", "Lick-Wilmerding", "macchiato", "textile", "mischief"]
4) Given a word, write a program that counts the number of vowels in the word. You can use the following list of words to test with:
my_words = ["laser", "toothpaste", "molasses", "marble", "pumpkin", "Lick-Wilmerding", "macchiato", "textile", "mischief"]