acm init() day 2
TRANSCRIPT
From last time• A little bit about Python
• How to use Koding.com
• Data types: numbers, string, and booleans
• Variables and naming conventions
• Printing
• String methods
• Math operators
• Comments
Brief topic: getting inputWe often want to get input from a user that we can use later.
Here is how to do that:
variable = input("Prompt: ")
Practice: Write a mad libs program
Here is an example of how to string together text:
Write your own mad libs with the people sitting around you!
I guarantee you will get an error message. Work with those around you to fix it!
Madlibs conclusionsJust using the keyword 'input' gives an error for strings.
!It will work for numbers, but strings need to be in quotes.
We can fix this!
We use 'raw_input' instead of 'input' when we expect a string. This is due to the version of Python we are using.
EqualWe assign variables like this:
What if we want to check if two variables are equal? Can we do this?
EqualWe cannot write x = y ! That would set x equal to the value of y.
If we want to compare two values we need to use '=='
This is correct syntax!
What type of variable is b? What is b's value?
Not equalThe way we denote not in Python is with an exclamation mark.
!Logically, not equal would be !=
b is still a boolean. What is its value?
Greater/less than...
• This is no different than regular math you have learned.
• Less than: <
• Greater than: >
... or equal to• Less than or equal to
and greater than or equal to are written by just adding an equals sign on to the end of the expression
• Less than or equal to: <=
• Greater than or equal to: >=
Boolean operators
• Remember the variable boolean that was either true or false? (Hint: it was just on the previous slide)
• Python has operators that can determine if a statement is true or false
• These operators are 'and', 'or', and 'not'
andThe symbol for 'and' is 'and'!
!An and will only result in a true value if the statements on both
sides of the and are true.
Let's look at some examples.
orThe symbol for 'or' is 'or'
!or evaluates to true if one or both statements on either side of
the or are true
Let's look at some examples.
notThe symbol for 'not' is 'not'
!not makes true values false and false values true
Let's look at some examples
Combining operators• We can combine and, or, and not to evaluate
statements.
• If the expression looks confusing, we can add parenthesis to clarify things. Expressions inside () will be evaluated first.
• Let's look at some examples.
Conditionals
• Programs need to make decisions based on different situations - user input, calculations, etc.
• Three types of control flow
• 'if', 'elif', 'else'
if to start
optional one or more elif
optional else
We'll go into more detail in the following slides
ifAn if statement looks at an expression then exectutes a block of code if the
expression is true. Otherwise, the block of code is skipped.
if statement:! do something!end
An if statement can stand alone. If the statement is not true, the 'do something' will be skipped.
elseAn else statement must always be paired with an if statement. If the if
statement is not true, the else statement will be executed.
if statement:! do something!else:! do something else
Note that if the if statement is not true the else statement will always be executed. In an if-else code block one of the two cases will be true.
elifelif must always be paired with an if statement and en else statement.
However, you can have as many elif statements as you want.
if statement:! do something!elif other statement:!! do something else!else:! do something else
Remember that you can have more than one elif statement, but only one if statement and one else statement. If and elif statements have arguments,
but else is at the bottom and is the fall-through case.