Now, in our second week of class, we are continuing to refresh our brains on the basics of Python. We covered lists and how to use them, the variable type Boolean, Logic statements, if/elif/else statements, as well as while and for loops.
Read over the code given as examples, then, answer the questions marked with “???” in your own Python program.
To make a list in python you simply follow the structure:
<listName> = [<item1>, <item2>, ...]
Here is an example of a list of fruit strings we used in class.
list_fruit = ["apples", "oranges", "bananas", "kiwi", "mango"]
Lists can hold integers or even a mix of variable types. The list named
list_num_fruits represents what we “have” of each fruit in
list_mixed shows how a list can have multiple variable types inside. Now, you try. Make a list with 5 items in the room around you.
list_num_fruits = [4, 2, 7, 12, 1] list_mixed = [1, 12.4, "abc", "nope"] # Write a list with five items in the room around you items = ???
Next, to get the length, or number or items inside the list, you can write
len(listName). Below in an example of getting the length of
list_length = len(list_mixed)
List syntax allows us to access specific items based on the index. Remember: indexes are zero-based. They start at zero. For example, the code below references the 3rd item in
list_fruit, which is
"bananas". Try it yourself as well.
list_fruit # From list_mixed print out 12.4 list_mixed[???]
You can add new items (one at a time) to the end of the list like so:
list_fruit.append("peach") # Add to list_mixed 27 list_mixed.???
Boolean Variable Type
Boolean is a variable that can be one of two values,
x = True
The difference logic operators are:
> Greater than
>= Greater than or equals
< Less than
<= Less than or equals
Logic operators ask a question and Boolean is the answer. Take the statement
1 == 2. This asks “Is 1 equal to 2?”. We know the answer is
False, which is a Boolean. Logic operators are often used in if-statements.
If, If-Else, If-Elif-Else
count = 5 if count == 5: print("YES!")
The code above shows and example of a simple if-statement. Since the variable
count equals 5, the if-statement is
True and “YES!” will be printed.
count = 3 if count == 5: print("YES!") else: print("Nope!")
Next, we have an If-Else statement above. Notice the value of
count has changed. This means the if-statement is
False and the code under the else will run. In this case, “Nope!” will be printed.
count = 3 if count == 5: print("YES!") elif count == 3: print("Maybe?") else: print("Nope!")
Lastly, we have if-elif-else. Elif stands for “else if”. The snippet of code above will print “Maybe?” because the
elif count == 3 is
Try it yourself!
# If greater than 0, print "positive", if equal to 0, print "zero" # otherwise print "negative" eval_num = 12 # write if, elif, ... else statement ???
While loops mean that a block of code will be repeated until the given condition is
c = 0 while c < 5: print("Going... c =", c) c = c + 1
As an example, this while loop will run 5 times before stopping. This is because once c equals 5, c is no longer less than 5 and the looping is done. One very important note to make is that it is very easy to cause an infinite loop with while loops. If you were to run your code without the line
c = c + 1, you would have to force your program to stop running.
Try it yourself:
# Print out "strawberries" seven times using a while loop ???
Now, with Python, there are two types of for loops. There are regular for loops that use an integer variable to keep track of how many times to run. Then, we have for-each loops. For-each loops are used to loop through lists.
#Example of a regular for loop for i in range(7): print("strawberries") #Example of a for-each loop for fruit in list_fruit: print("A fruit is", fruit);
You may notice that a for loop helps print “strawberries” seven times with less code than a while loop. This is because we can use a variable, like
c, right inside the for-loop line and for-loops will increment the variable for you up to the number you specify inside the parenthesis after
Finally, the example for-each loop checks on each item or fruit in
list_fruit and prints it. For-each loops are very handy for checking each item of a list when you don’t need to access the item’s index.
Important Note: for loops count starting at zero just like the indexes of a list.
We hope this helps. See you in class!