day2

20
Raspberry Pyrates Welcome Avilay Parekh Anika @avil ay

Upload: avilay-parekh

Post on 24-Jun-2015

331 views

Category:

Software


4 download

DESCRIPTION

Beginners introduction to Python and Raspberry Pi programming. More details on http://raspberrypyrates.wordpress.com

TRANSCRIPT

Page 1: Day2

Raspberry Pyrates

Welcome

Avilay ParekhAnika Parekh

@avilay

Page 2: Day2

Sequential Execution Variables Conditionals Loops

Module 2

Page 3: Day2

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

Page 4: Day2

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

Page 5: Day2

Turtle Program

Draw a rectangle, triangle, “s” shape

Students to draw a square

>>> import turtle>>> turtle.forward(0)

Page 6: Day2

Dragon Game

Demo the full game.

Source: http://inventwithpython.com/chapter6.html

Page 7: Day2

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()

Page 8: Day2

Variablesa = 10b = 5c = a + b

print(c)

Page 9: Day2

Variables

a10

b5

c15

a = 10b = 5c = a + b

Page 10: Day2

Variables

a20

b5

c15

a = 20b = 5c = a - b

Page 11: Day2

Variables

a = 20b = 5c = a – b

print(a) a = 100 print(a)

Page 12: Day2

Variables import random

a = 20b = 5c = a – b

print(a) a = random.randint(1, 10) print(a)

Page 13: Day2

Strings

a“hello”

b“world”

c“helloworld”

a = “hello”b = “world”c = a + b

Page 14: Day2

Strings

a“hello”

b“world”

c“hello world”

a = “hello”b = “world”c = a + “ ” + b

Page 15: Day2

Strings can be concatenated

name = “Avilay” profession = “programmer” print(name + “ is a ” + profession)

Page 16: Day2

Datatypes cannot be mixed age = 30 print(name + “is” + age + “years old”)

Ka-Boom!!

Page 17: Day2

Datatypes cannot be mixedage = 30print(name + “is” + str(age) + “years old”)

Convert the number to a string using the str function

Page 18: Day2

Datatypes cannot be mixedage = “30”print(name + “ is ” + age + “ years old ”)

Or just declare the variable age as a string.

Page 19: Day2

Datatypes cannot be mixed your_age = input(“Your age?”) my_age = 30 older_by = my_age – your_age

Ka-Boom!!

Page 20: Day2

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.