First Class (Python Section)!
It's great to be back and see all of our students (new and returning) for our fall course! In our Python section, we covered some basic but key concepts in Python, including variables, loops, conditionals and functions.
Here's a link to our Class 1 powerpoint if you need to ever refresh or reference:
Variables are like a container that can store some sort of data that you decide to put in it. The data can be a string ("hello"), an integer (5), a float (4.5), a boolean (true), or any other type of object that you might specify.
We assign variable by giving a variable name, an equals sign, and the data we want stored in the variable.
string_example = ("hello") int_example = (5) float_example = (4.5) boolean_example = (true) print (string_example + " students!")
String Concatenation: Remember, you can always print out your variable with a string using string concatenation. Use the plus sign to do this (ex. line 5 above).
Loops are a specific line of code that repeat over and over again until reaching a certain condition. For loops use the range() function to define when the code in the loop will execute. The range() function takes 3 parameters (a start value, an end value, and an increment/decrement value).
for x in range(6) print (x) for x in range (30, 0, -2) print (x)
The first for-loop in the above example will print out the integers starting with 0 until 6. Even though this range() function only has 1 parameter (the end parameter), its start parameter and increment parameter are set to 0 and +1 respectively.
In the second for-loop in the example, the loop will print out the even integers starting with 30 until 0. This range() function, unlike the first function, contains the start parameter, end parameter, and increment/decrement parameter.
While loops are another type of loop in Python, and they execute whenever their assigned condition is true.
while x<=10: x=x+1
A conditional statement ONLY executes when another statement is true.
Example: In the example below, x>5 is the condition, and if x is greater than 5, then the conditional executes and prints the phrase you ate too many cookies.
if (condition) do something if (x>5) print ("You ate too many cookies!")
We can also use the elif and else key-phrases with conditionals to help use evaluate other scenarios. The elif phrase will evaluate another condition, and the else phrase will execute if all other if and elif conditions are false. When one of the conditions is determined to be true, the other elif or else conditions will not be evaluated.
if (x>5) print ("You ate too many cookies!") elif (x=0) print ("You didn't eat any cookies!") else: print ("You didn't eat enough cookies!")
Functions are a group of code that you can use repeatedly to execute a specific purpose. This can help keep your code more organized and often times shorter.
Functions will take an input, and then they will use that input to produce an output.
Example: This find_rect_area() function takes an input of the width and the height in the form or parameters. It then uses that width and height to find and return the area (the output). When creating a function, you must always use the keyword def. Your parameters will always serve as your input. And remember, always make sure to specify a return statement (otherwise your function won't know what to return!)
def find_rect_area (width, height): area = width*height return area
In python, you can ask your user to input a piece of data for you, which you can then assign to a variable. This allows you to collect information from your user for a more interactive program!
Example: Use the input() function to collect the information from the user. Then, you can use that input as a piece of data to assign to a variable to use later.
name=input("What is your name?") print ("Hello " + name)
We ended the class with a coding challenge, where we asked the students to code for a program that generates a random number and figures out whether a user guesses that number.
We assigned the variable answer to a random integer (found with the randint() function in the random class that we imported). We then found the user's guess and assigned it to the variable number. We used a while loop that continued to execute so long as the number did not equal the answer. An if and elif conditional statement was used to guide the user to the correct number. Finally, the program printed out the random number when the user guessed correctly.
import random answer = random.randint(1, 99) print(answer) number = int(input("Enter an integer from 1 to 99: ") while number!=answer print (number) if number<answer: print("Your guess is too low.") number = int(input("Enter an integer from 1 t 99: ")) elif number>answer: print ("Your guess is too high.") number = int(input("Enter an integer from 1 to 99: ")) print ("You guessed the answer! It was " + str(answer))
Contact us with any questions:
Mana Vale: firstname.lastname@example.org
Rachael Kim: email@example.com
Kaitlyn Flowers: firstname.lastname@example.org
Celine Tan: email@example.com
Joey Dong: firstname.lastname@example.org
Molly Pate: email@example.com