SlideShare a Scribd company logo
1 of 20
Welcome 
Avilay Parekh 
Anika Parekh 
@avilay
Datatypes cannot be mixed 
age = 30 
print(name + “is” + age + “years old”) 
Ka-Boom!!
Datatypes cannot be mixed 
age = 30 
print(name + “is” + str(age) + “years 
old”) 
Convert the number to a string using the str 
function
Datatypes cannot be mixed 
age = “30” 
print(name + “ is ” + age + “ years old ”) 
Or just declare the variable age as a string.
Datatypes cannot be mixed 
your_age = input(“Your age?”) 
my_age = 30 
older_by = my_age – your_age 
Ka-Boom!!
Datatypes cannot be mixed 
your_age = int(input(“Your age?”)) 
my_age = 30 
older_by = my_age – your_age 
input function always returns a string. Convert 
it to a number using the int (for integer) 
function.
Dragon Game - Choice 
import random 
::: 
good_cave = random.randint(1, 2) 
print(“Which cave will you go into? (1 or 2)”) 
chosen_cave = int(input())
Boolean 
Can only take on one of two values – 
True or False 
is_programming_fun = True 
is_cold = False 
print(is_cold)
Boolean 
Is my age same as yours? 
The == operator checks if the values are equal 
my_age = 30 
your_age = ? 
print(my_age == your_age)
Boolean 
my_age = 30 
your_age = ? 
print(my_age = your_age) 
Sneaky Bug!
Boolean 
Am I older than you? 
The > operator checks if LHS is greater than 
RHS 
my_age = 30 
your_age = ? 
am_i_older = my_age > your_age 
print(am_i_older)
Boolean 
money = 500 
phone_cost = 240 
tablet_cost = 200 
total_cost = phone_cost + tablet_cost 
can_afford_both = money > total_cost 
print(can_afford_both)
Conditionals 
if always checks a Boolean variable for True 
Has to be a Boolean 
variable 
if can_afford_both: 
message = “You can afford both.” 
else: 
message = “You cannot afford both.”
Conditionals 
your_age = int(input(“Your age?”)) 
my_age = 30 
older_by = -1 
if my_age > your_age: 
older_by = my_age – your_age 
elif my_age < your_age: 
older_by = your_age – my_age 
else: 
older_by = 0 
?
Dragon Game - Result 
import time 
::: 
print("You approach the cave..."); 
time.sleep(2) 
print("It is dark and spooky...") 
time.sleep(2) 
print("A large dragon jumps out in front of you! He opens his jaws 
and...") 
print() 
time.sleep(2) 
if chosen_cave == good_cave: 
print("Gives you his treasure") 
else: 
print("Gobbles you down in one bite!")
Loops 
ctr = 0 
while (ctr < 5): 
print(“This is line ” + ctr) 
ctr = ctr + 1
Loops 
while (True): 
print(“Stop this train, I wanna 
get off!”) 
print(“Oh! Thank goodness!”)
Dragon Game - play 
import random 
import time 
play_again = "yes" 
while (play_again == "yes"): 
print("you are in a land full of dragons…") 
:::: 
else: 
print("Gobbles you down in one bite!") 
play_again = input("Do you want to play again? (yes 
or no)")
Sing and Talk 
$ mpg123 /home/pi/viva.mp3 & 
$ espeak “hello there” 
>>> import os 
>>> os.system(‘mpg123 /home/pi/viva.mp3 &’) 
>>> os.system(‘espeak “hello there”’)
Conditionals 
Loops 
Making the Pi 
sing and talk 
Module 3

More Related Content

More from Avilay Parekh (9)

Backprop
BackpropBackprop
Backprop
 
Ai &amp; ml
Ai &amp; mlAi &amp; ml
Ai &amp; ml
 
Pupymeetup
PupymeetupPupymeetup
Pupymeetup
 
Day4
Day4Day4
Day4
 
Day2
Day2Day2
Day2
 
Git primer
Git primerGit primer
Git primer
 
Git undo
Git undoGit undo
Git undo
 
Ruby object graph
Ruby object graphRuby object graph
Ruby object graph
 
What is cloud computing
What is cloud computingWhat is cloud computing
What is cloud computing
 

Day3

  • 1. Welcome Avilay Parekh Anika Parekh @avilay
  • 2. Datatypes cannot be mixed age = 30 print(name + “is” + age + “years old”) Ka-Boom!!
  • 3. Datatypes cannot be mixed age = 30 print(name + “is” + str(age) + “years old”) Convert the number to a string using the str function
  • 4. Datatypes cannot be mixed age = “30” print(name + “ is ” + age + “ years old ”) Or just declare the variable age as a string.
  • 5. Datatypes cannot be mixed your_age = input(“Your age?”) my_age = 30 older_by = my_age – your_age Ka-Boom!!
  • 6. Datatypes cannot be mixed your_age = int(input(“Your age?”)) my_age = 30 older_by = my_age – your_age input function always returns a string. Convert it to a number using the int (for integer) function.
  • 7. Dragon Game - Choice import random ::: good_cave = random.randint(1, 2) print(“Which cave will you go into? (1 or 2)”) chosen_cave = int(input())
  • 8. Boolean Can only take on one of two values – True or False is_programming_fun = True is_cold = False print(is_cold)
  • 9. Boolean Is my age same as yours? The == operator checks if the values are equal my_age = 30 your_age = ? print(my_age == your_age)
  • 10. Boolean my_age = 30 your_age = ? print(my_age = your_age) Sneaky Bug!
  • 11. Boolean Am I older than you? The > operator checks if LHS is greater than RHS my_age = 30 your_age = ? am_i_older = my_age > your_age print(am_i_older)
  • 12. Boolean money = 500 phone_cost = 240 tablet_cost = 200 total_cost = phone_cost + tablet_cost can_afford_both = money > total_cost print(can_afford_both)
  • 13. Conditionals if always checks a Boolean variable for True Has to be a Boolean variable if can_afford_both: message = “You can afford both.” else: message = “You cannot afford both.”
  • 14. Conditionals your_age = int(input(“Your age?”)) my_age = 30 older_by = -1 if my_age > your_age: older_by = my_age – your_age elif my_age < your_age: older_by = your_age – my_age else: older_by = 0 ?
  • 15. Dragon Game - Result import time ::: print("You approach the cave..."); time.sleep(2) print("It is dark and spooky...") time.sleep(2) print("A large dragon jumps out in front of you! He opens his jaws and...") print() time.sleep(2) if chosen_cave == good_cave: print("Gives you his treasure") else: print("Gobbles you down in one bite!")
  • 16. Loops ctr = 0 while (ctr < 5): print(“This is line ” + ctr) ctr = ctr + 1
  • 17. Loops while (True): print(“Stop this train, I wanna get off!”) print(“Oh! Thank goodness!”)
  • 18. Dragon Game - play import random import time play_again = "yes" while (play_again == "yes"): print("you are in a land full of dragons…") :::: else: print("Gobbles you down in one bite!") play_again = input("Do you want to play again? (yes or no)")
  • 19. Sing and Talk $ mpg123 /home/pi/viva.mp3 & $ espeak “hello there” >>> import os >>> os.system(‘mpg123 /home/pi/viva.mp3 &’) >>> os.system(‘espeak “hello there”’)
  • 20. Conditionals Loops Making the Pi sing and talk Module 3

Editor's Notes

  1. Ask students the output of the print statement Ask the students to come up with more Boolean variables.
  2. Ask students the output of the print statement Ask the students to come up with more Boolean variables.
  3. Ask students the output of the print statement Explain the difference between = and ==
  4. Ask students the datatype of the am_i_older variable (is it a String, Number, or Boolean?)
  5. Note, can_afford_both is a Boolean variable that gets its value from an expression Change the value of tablet_cost to 260 to give the concept of the >= operator
  6. We can do a lot more inside if statements than simply print. Note that the if statement has an expression that evaluates to Boolean instead of a Boolean variable directly.
  7. We can do a lot more inside if statements than simply print. Note that the if statement has an expression that evaluates to Boolean instead of a Boolean variable directly.
  8. The last print statement will never print. This is an infinite loop because True will always be True.