day2
DESCRIPTION
Beginners introduction to Python and Raspberry Pi programming. More details on http://raspberrypyrates.wordpress.comTRANSCRIPT
Raspberry Pyrates
Welcome
Avilay ParekhAnika Parekh
@avilay
Sequential Execution Variables Conditionals Loops
Module 2
Game TimeRobots were not able to make decisions for themselves
Had to give them very precise instructions
Source: http://www.raspberrypi.org/learning/turing-test-lessons/Lesson-1/lesson-plan-1.md
Program Code
Execute statements in sequence
Control flowStatement that is currently executingStatement that is next
Source: http://www.raspberrypi.org/learning/turing-test-lessons/Lesson-1/lesson-plan-1.md
Turtle Program
Draw a rectangle, triangle, “s” shape
Students to draw a square
>>> import turtle>>> turtle.forward(0)
Dragon Game
Demo the full game.
Source: http://inventwithpython.com/chapter6.html
Dragon Game - Introprint(“You are in a land full of dragons.”)print(“In front of you, you see two caves.”)print(“In one cave, lives a good dragon ”)print(“who will share his treasure with you.”)print(“In the other lives a bad dragon ”)print(“who will eat you on sight!”)print()
Variablesa = 10b = 5c = a + b
print(c)
Variables
a10
b5
c15
a = 10b = 5c = a + b
Variables
a20
b5
c15
a = 20b = 5c = a - b
Variables
a = 20b = 5c = a – b
print(a) a = 100 print(a)
Variables import random
a = 20b = 5c = a – b
print(a) a = random.randint(1, 10) print(a)
Strings
a“hello”
b“world”
c“helloworld”
a = “hello”b = “world”c = a + b
Strings
a“hello”
b“world”
c“hello world”
a = “hello”b = “world”c = a + “ ” + b
Strings can be concatenated
name = “Avilay” profession = “programmer” print(name + “ is a ” + profession)
Datatypes cannot be mixed age = 30 print(name + “is” + age + “years old”)
Ka-Boom!!
Datatypes cannot be mixedage = 30print(name + “is” + str(age) + “years old”)
Convert the number to a string using the str function
Datatypes cannot be mixedage = “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.