introduction to: python and opensesame part i. python a high-level programming language it can do a...

39
Introduction to: Python and OpenSesame Part I

Upload: bathsheba-andrews

Post on 30-Dec-2015

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

Introduction to:Python and OpenSesame

Part I

Page 2: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

2

Python

• A high-level programming language• It can do a lot of things• We will use python in this course in the

context of in-line scripting for opensesame.• For that, you need to know how to write basic

scripts.

Intro to: Python and OpenSesame

Page 3: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

3

Python

Intro to: Python and OpenSesameFrom: XKCD comics

Page 4: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

4

Python Shell

Intro to: Python and OpenSesame

Start -> python -> Python GUI (IDLE)

Page 5: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

5

Python Shell

• An Interpreter:– You write a line of code– Python interprets it and executes it

• Try:– 1+1 [addition]– 5*2 [multiplication]– ‘hello’ [string]

Intro to: Python and OpenSesame

Page 6: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

6

Python Basic (Data) Types

• INT (integer) – 2,3,0,15,100000• FLOAT (floating point) – 2.0, 3.5, 0.12, 100.999• STR (string) – ‘Hello’, ”to you all”, ‘1+1’, “11”

• Use type(…) to check:– 2– 2.2– ‘2.2’

Intro to: Python and OpenSesame

Page 7: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

7

Variables (assign value to variable)

• X = 1; Y = 2.0; Z1 = ‘Gevald’; Z2 = ‘avoi’• Operations on variables? No problem:• X+Y [int+float = float]• X/Y [/ = devision, // = int devision]• Z1+X [int+str = error]• Z1 + Z2 [str+str = str]• Z1*3 [str*int = str (overloaded?!)]• Z2*Y [str*float = error]

Intro to: Python and OpenSesame

Page 8: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

8

Variables (jumping from types)

>> x = 2.0; y = 3>> int(x)2>> str(x)‘2.0’>> float(y)

Intro to: Python and OpenSesame

Page 9: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

9

Variables (pointing)

>> x = 1; y = 2

>> x = y; x is y?

Intro to: Python and OpenSesame

x 1( memory location: 112125123)

y 2( memory location: 112125125)

x 1( memory location: 112125123)

y 2( memory location: 112125125)

Page 10: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

10

Variables (pointing) - continue

>> y = 1

>> x is y ?

Intro to: Python and OpenSesame

x 1( memory location: 112125123)

y 2( memory location: 112125125)

Page 11: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

11

Functions - BASICS

Intro to: Python and OpenSesame

BLACK BOXINPUT OUTPUT

String:“Hello World” print(“Hello World”)

Hello World

Page 12: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

12

Script!

A text file (.py) with python instructions for the interpreter. No more one liners!

In Python Shell:File -> New File

In the new File:File -> Save As.. -> example.py

Intro to: Python and OpenSesame

Page 13: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

13

Script!

Super text files! Special language keywords are highlighted in different colors.

Intro to: Python and OpenSesame

Example

Page 14: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

14

Your first Python program

In a new file (Call it first.py)

1. Create a program that saves the phrase: ‘hello world’ in a variable called phrase.

2. Use the print function to print the content of phrase.

3. Execute your program

Intro to: Python and OpenSesame

Page 15: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

15

Back to functions

Recall: input -> blackbox -> output

Python comes with many built-in functions.

For more information, visit the official documentation:https://docs.python.org/2/library/functions.html

Intro to: Python and OpenSesame

Page 16: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

16

Back to functions

Examples:>> len() # returns the length of the input>> abs() # returns the absolute value>> round() # rounds to the nearest integer >> raw_input() # takes user input as str

Intro to: Python and OpenSesame

Notice I’m using # (hash .mark) for comments.

Python ignore everything after this symbol, but you

shouldn’t!

Page 17: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

17

Python Standard Library

But the built-in functions are limited, and Python has MUCH MORE to offer in the Python Standard Library (https://docs.python.org/2/library/index.html)

The python standard library has many modules (script files and folders) with many useful functions that are readily available – once imported.

Intro to: Python and OpenSesame

Page 18: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

18

Python Standard Library - Modules

Math>> import math>> help(math) # lists all the available functions

To use a function:>> math.sin() # The sine function>> math.factorial() # the factorial (עצרת) function

(also has some special variable values like math.pi)

Intro to: Python and OpenSesame

The dot stands for from, as in: use sin

from math

Page 19: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

19

Python Standard Library - Modules

>> Import random>> random.random()

if you only plan on using one function from a module:

>> from random import random>> random()

Intro to: Python and OpenSesame

Page 20: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

20

More Python libraries

Python has even more libraries available on the web which you could download and use.

We will not cover these in this class, but feel free to explore: numpy, matplotlib and more..(Talk to me later if you want to learn more about this)

Intro to: Python and OpenSesame

Page 21: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

21

Objects – Tip of the iceberg!

Traditionally, this part comes much later in the learning process. But, it is essential for OpenSesame scripts and I believe the tools which it provides will help learning the other stuff.

Everything in Python is an object. This is what makes Python an OOP [Object Oriented Programming] language.

What is an “OBJECT”?

Intro to: Python and OpenSesame

Page 22: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

22

Objects – Tip of the iceberg!

Objects are blueprints

Intro to: Python and OpenSesame

Roof

Door

Chimney

Handle

Address Owner #Rooms Color Who lives there?

Page 23: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

23

Objects – Tip of the iceberg!

We can create an instance of an object:

>> house1 = house()

We can add properties to it:

>> house1.address = ‘Ragar 19, Be’er Sheva’>> house1.price = 100000>> house1.roof_color = ‘pink’

Intro to: Python and OpenSesame

These Belong EXCLUSIVELY to the house1 instance of the house object

Page 24: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

24

Methods

• Objects have functions – called Methods• Methods are functions that belong to the object

type.

>> house.colorPink>> house.repaint(‘yellow’)>> house.coloryellow

Intro to: Python and OpenSesame

Just an in object variable

a method, notice the parenthesis.

Page 25: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

25

Methods

We said everything in Python is an object.Int, float, str are objects too!

st = ‘stringy‘ # Creating a string instancest.strip(‘y‘) # Using the strip method

Ask for help! Use help(str) to learn all the str methods

Intro to: Python and OpenSesame

Page 26: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

26

Importing new objects!

Similar to functions, we can import new objects from the Python Standard Library.

We will now import the turtle library.

Intro to: Python and OpenSesame

Page 27: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

27

Turtle

Turtle is based on an old children’s programming language called “Logo”.

we will use turtle throughout the course to visualize everything we learn!

(This approach will also help us with OpenSesame inline which uses objects all the time!)

Intro to: Python and OpenSesame

** Some of the examples in this part were adopted from the Udacity Object Oriented Programming – Python course

Page 28: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

28

Turtle – How it works

Intro to: Python and OpenSesame

(0,0)

Move Forward, 100 steps

Page 29: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

29

Turtle - setup

First, we need to import the turtle module which has all the code for the needed objects.

** note: to make sure turtle works well, write all the code in script files and NOT in the shell.

I still use >> to indicate that it’s a line of code.

>> import turtle

We will need two objects1. A screen object2. A turtle object

Intro to: Python and OpenSesame

Page 30: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

30

Turtle – Screen Object

>> import turtle>> window = turtle.Screen()

We created a new Screen instance called window Let’s use methods to change its appearance and behavior:

>> window.bgcolor(‘red’) # sets the background to red>> window.exitonclick() # sets the close window properties

Intro to: Python and OpenSesame

Page 31: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

31

Turtle – Turtle Object

>> yoni = turtle.Turtle()

What can yoni (turtle instance) do?

>> yoni.forward(100) # Go forward, 100 steps!>> yoni.right(10) # Turn right, 10o !

Intro to: Python and OpenSesame

Page 32: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

32

Time for a Python Program

In a new file (call it, square.py)Use the turtle object to draw a square!

Don’t forget to create a window instance and use the exitonclick() method in the end of the script

Intro to: Python and OpenSesame

Page 33: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

33

Another type, Bool (Boolean)

• Bool is a True/False type• It is the basis of conditioning• You get a Boolean value for these type of

statements:– 1000 > 999– ‘aaa’ == ‘bbb’– 1 in (1,2,3,4)– and more [ >=, <= , != ]

Intro to: Python and OpenSesame

Use the type() function to on the

input True

Page 34: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

34

Conditionals

• Sets conditions to the program in the form:

Intro to: Python and OpenSesame

if <this == True>:<do that>

Condition

Colon

Indentation

Page 35: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

35

Conditionals

• Example:

>> x = 5>> y = 6>> if x > y:>> print(‘x is bigger than y’)

Intro to: Python and OpenSesame

Where is the <True> ?

x > y is True!== True is embedded

Page 36: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

36

Conditionals – what else?

• We can create more advanced conditions:

if 1 > 2:print(‘a new fact!’)

else:print(‘The world is in order’)

Intro to: Python and OpenSesame

NOTE: Code blocks (after a colon) are indented. We can have multilevel Indentation

Page 37: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

37

Conditionals – what else?

• And even more advanced conditions:

if 1 > 2:print(‘1 > 2’)

elif 2 > 1:print(‘2 > 1’)

else:print(‘Chaos!’)

Intro to: Python and OpenSesame

POP QUIZ: What will be the output?

1. 1 > 22. 2 > 13. Chaos!4. True5. False6. Error

Page 38: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

38

Logical Operators (AND / OR)

x = 1; y = 2; z = 3

if x == 1 and y == 2:print ‘ok’

if y == 2 or z == 4:print ‘no worries’

Intro to: Python and OpenSesame

AND – True if:Both are true

OR –True if:1 is true, 2 is true, both are true

Page 39: Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in

39

Time for a Python Program

In a new file (call it, conditions.py)Write a program that:1. Sets x and y to be either 1 or 2 randomly.2. If x is bigger than y, draw a square with turtle3. If y is bigger than x, draw a circle with turtle4. If x and y are equal, draw a line with turtle

Hint: Use the choice() function from the random module, it takes as input a number sequence from which it draws a choice (1,2).Also, turtle has a draw circle function, find out what it is.

Intro to: Python and OpenSesame