Second Class Recap
Updated: 3 days ago
Whoever is running the program (the “user”) is asked to input some information when you use input()
You can store the information they input in a variable that you can reference later
Today we had our second class - with one new student!
Please check the powerpoint below to see our materials.
Variables in Python are "containers" that store data (data = things like string, integer, float, boolean, etc.)
Variable name should follow these rules:
They can't have spaces in them - so we use _ (underscore) to join words (e.g. student_score)
They can't be the same as Python keywords (e.g. try, if, pass, while)
They can't start with a number
It is a convention to make them lowercase
You assign a value to your variable using equal sign (=)
name = "Rachael" last_name = "Kim" grade = 12
You can print the value of your variable with some string using 'string concatenation.'
In Python, string concatenation is as simple as just using a '+' sign:
name = "Rachael" print("Your name is "+name)
2. User Input
You can ask the user of your program to input something (e.g. answer the question you ask).
In Python, you use:
input() # examples: name = input("What is your name?")
In the above example, by setting the variable 'name' equal to your input, you are saving the user's answer you get for the question you ask to the variable.
So you can later use this response for other purposes:
print("Nice to meet you, "+name)
You can also combine inputs with if/else conditional statements to do something like this:
name = input("What is your name?") confirmation = input("Is "+name+" your name?") if confirmation == "yes": print("Nice to meet you, "+name) else: name = input("Please re-enter your name:")
Here, you first ask the user for their name, and then check if the user entered a correct name.
If the user says "yes," then you print a string concatenated with the value of the variable 'name.' If the user says "no," you ask for their name again.
3. For loop
Loop is a programming structure that repeats some code over and over until a certain condition is met. For loop is one kind.
Following is a type of for loop we will be looking at this class:
for x in range(6): print(x)
Here we use 'range()' - range is a type of 'function.'
Function is a block of code that is given an input and outputs something.
range() takes three inputs (which we call parameters for functions), starting number, ending number, and increment - but starting number and increment are optional, just like how I only specified ending number in the example. The default starting number is 0, and the default increment is 1.
TRY1 : Print numbers from 1 to 100 (Note that the starting number is 1)
TRY2 : Print numbers from 50 to 10, with a decrement of 2 each time.
4. While loop
While loop is another kind of a loop in Python. While loop runs the statements within it while the condition is True, just like how we say in English:
While I am awake, my phone should send me notifications, but when I go to sleep, it should stop.
while x < 10: x += 1 # This is a shorter version of x = x + 1
You can combine while loop with other things we learnt, like user input:
answer = input("What is 500 + 234?") while (answer != 734): print("Try again!") answer = input("500 + 234?")
TRY 1 : Try the example above with your own question and answer
TRY 2 : Print numbers from 0 to 100 using WHILE LOOP, not FOR LOOP.
For those who have not signed up for our meeting yet:
You can still sign up!
We will provide make up meetings.
Please use the link at the top right corner to sign up.
Contact us for any questions:
Rachael Kim email@example.com
Mana Vale firstname.lastname@example.org
Minseo Kim email@example.com