SlideShare a Scribd company logo
ACM init()
!
Day 2: April 22, 2015
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 input
We often want to get input from a user that we can use later.
Here is how to do that:
variable = input("Prompt: ")
Input
Here is an example of getting the user's name:
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 conclusions
Just 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.
Comparison operators
We are going to look at comparison operators. There are 6 to
know:
Equal
We assign variables like this:
What if we want to check if two variables are equal? Can we do
this?
Equal
We 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 equal
The 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:
>=
Some bools
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'
and
The 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.
and examples
and examples
or
The 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.
or examples
or examples
not
The symbol for 'not' is 'not'
!
not makes true values false and false values true
Let's look at some examples
not example
not example
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.
True or false?
Answer
True or false?
Answer
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
if
An 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.
What will this print?
else
An 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.
What will this print?
elif
elif 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.
What will this print?
What will this print?
What we did today
• Input- Madlibs!
• Comparison operators
• Boolean operators
• Conditionals
Practice
At home, try to make a calculator.
!
Ask the user what operation they want to do. (+, *, etc)
Ask the user for the numbers they want to do the calculations
on.
!
Print the answer!

More Related Content

PDF
Init() day2
PDF
Little lessons learned from Swift
PPTX
Quotes grammar presentation
PPTX
JavaScripts internals #1
PPT
Last line effect
PDF
Kotlin generics
PDF
ACM init() Spring 2015 Day 1
PPTX
JavaScript Debugging Tips and Tricks
Init() day2
Little lessons learned from Swift
Quotes grammar presentation
JavaScripts internals #1
Last line effect
Kotlin generics
ACM init() Spring 2015 Day 1
JavaScript Debugging Tips and Tricks

Viewers also liked (20)

PDF
Intro to Hackathons (Winter 2015)
PPTX
Building a Reddit Clone from the Ground Up
PDF
Angora Gardens Car Show
PDF
Angora Gardens Classic Car Show
DOCX
Problema 4
PDF
Niaeproc 2011 combating waste in cattle examination and treatment
PPTX
Syarikat Aku Kaya (AK)
PPT
Paz en red
PDF
UCLA ACM Spring 2015 general meeting
PDF
ACM General Meeting - Spring 2014
PDF
An Introduction to Sensible Typography
PDF
UCLA Geek Week - Claim Your Domain
PPT
P1 induction comely park
Intro to Hackathons (Winter 2015)
Building a Reddit Clone from the Ground Up
Angora Gardens Car Show
Angora Gardens Classic Car Show
Problema 4
Niaeproc 2011 combating waste in cattle examination and treatment
Syarikat Aku Kaya (AK)
Paz en red
UCLA ACM Spring 2015 general meeting
ACM General Meeting - Spring 2014
An Introduction to Sensible Typography
UCLA Geek Week - Claim Your Domain
P1 induction comely park
Ad

Similar to ACM init() Day 2 (20)

PPT
Introduction to Python Lesson Three_041313.ppt
PDF
An SEO’s Intro to Web Dev PHP
DOCX
Python Keywords
PDF
powerpoint 1-19.pdf
PDF
Java ppt2
PDF
Learn Java Part 2
PDF
Java ppt2
PPTX
Lecture 07.pptx
PPTX
Brixton Library Technology Initiative Week1 Recap
PDF
Java ppt2
PPTX
Python if_else_loop_Control_Flow_Statement
PPTX
06.pptx
PPTX
Week 2.pptx
PPTX
python conditional statement.pptx
PPTX
conditional statement in python day 5.pptx
PPTX
module 2.pptx
PPTX
Dasar-Dasar Program Keahlian Program Perangakat Lunak dan Gim
PPTX
Spreadsheets for developers
PPTX
Data Science-2.pptx for engineering students
PPTX
Grade XI - Computer science Data Handling in Python
Introduction to Python Lesson Three_041313.ppt
An SEO’s Intro to Web Dev PHP
Python Keywords
powerpoint 1-19.pdf
Java ppt2
Learn Java Part 2
Java ppt2
Lecture 07.pptx
Brixton Library Technology Initiative Week1 Recap
Java ppt2
Python if_else_loop_Control_Flow_Statement
06.pptx
Week 2.pptx
python conditional statement.pptx
conditional statement in python day 5.pptx
module 2.pptx
Dasar-Dasar Program Keahlian Program Perangakat Lunak dan Gim
Spreadsheets for developers
Data Science-2.pptx for engineering students
Grade XI - Computer science Data Handling in Python
Ad

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PDF
RMMM.pdf make it easy to upload and study
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Cell Structure & Organelles in detailed.
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
GDM (1) (1).pptx small presentation for students
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
VCE English Exam - Section C Student Revision Booklet
RMMM.pdf make it easy to upload and study
Sports Quiz easy sports quiz sports quiz
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Cell Structure & Organelles in detailed.
Microbial disease of the cardiovascular and lymphatic systems
102 student loan defaulters named and shamed – Is someone you know on the list?
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPH.pptx obstetrics and gynecology in nursing
TR - Agricultural Crops Production NC III.pdf
Pharma ospi slides which help in ospi learning
Microbial diseases, their pathogenesis and prophylaxis
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Institutional Correction lecture only . . .
GDM (1) (1).pptx small presentation for students
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

ACM init() Day 2

  • 1. ACM init() ! Day 2: April 22, 2015
  • 2. 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
  • 3. Brief topic: getting input We often want to get input from a user that we can use later. Here is how to do that: variable = input("Prompt: ")
  • 4. Input Here is an example of getting the user's name:
  • 5. 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!
  • 6. Madlibs conclusions Just 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.
  • 7. Comparison operators We are going to look at comparison operators. There are 6 to know:
  • 8. Equal We assign variables like this: What if we want to check if two variables are equal? Can we do this?
  • 9. Equal We 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?
  • 10. Not equal The 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?
  • 11. Greater/less than... • This is no different than regular math you have learned. • Less than: < • Greater than: >
  • 12. ... 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: >=
  • 14. 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'
  • 15. and The 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.
  • 18. or The 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.
  • 21. not The symbol for 'not' is 'not' ! not makes true values false and false values true Let's look at some examples
  • 24. 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.
  • 29. Conditionals • Programs need to make decisions based on different situations - user input, calculations, etc. • Three types of control flow • 'if', 'elif', 'else'
  • 30. if to start optional one or more elif optional else We'll go into more detail in the following slides
  • 31. if An 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.
  • 32. What will this print?
  • 33. else An 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.
  • 34. What will this print?
  • 35. elif elif 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.
  • 36. What will this print?
  • 37. What will this print?
  • 38. What we did today • Input- Madlibs! • Comparison operators • Boolean operators • Conditionals
  • 39. Practice At home, try to make a calculator. ! Ask the user what operation they want to do. (+, *, etc) Ask the user for the numbers they want to do the calculations on. ! Print the answer!