Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to: Python and OpenSesame 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. 2 Intro to: Python and OpenSesame Python 3 From: XKCD comics Intro to: Python and OpenSesame Python Shell Start -> python -> Python GUI (IDLE) 4 Intro to: Python and OpenSesame 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] 5 Intro to: Python and OpenSesame 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’ 6 Intro to: Python and OpenSesame Variables (assign value to variable) • X = 1; Y = 2.0; Z1 = ‘Gevald’; Z2 = ‘avoi’ • • • • • • • 7 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 Variables (jumping from types) >> x = 2.0; y = 3 >> int(x) 2 >> str(x) ‘2.0’ >> float(y) 8 Intro to: Python and OpenSesame Variables (pointing) >> x = 1; y = 2 x 1 (memory location: 112125123) y 2 (memory location: 112125125) >> x = y; x is y? 9 x 1 (memory location: 112125123) y 2 (memory location: 112125125) Intro to: Python and OpenSesame Variables (pointing) - continue >> y = 1 x 1 (memory location: 112125123) y 2 (memory location: 112125125) >> x is y ? 10 Intro to: Python and OpenSesame Functions - BASICS String: “Hello World” INPUT 11 print(“Hello World”) BLACK BOX Intro to: Python and OpenSesame OUTPUT 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 12 Intro to: Python and OpenSesame Script! Super text files! Special language keywords are highlighted in different colors. Example 13 Intro to: Python and OpenSesame 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 14 Intro to: Python and OpenSesame 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 15 Intro to: Python and OpenSesame 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 Notice I’m using # (hash .mark) for comments. Python ignore everything after this symbol, but you shouldn’t! 16 Intro to: Python and OpenSesame 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. 17 Intro to: Python and OpenSesame Python Standard Library - Modules Math >> import math >> help(math) # lists all the available functions The dot stands for from, as in: use sin from math To use a function: >> math.sin() # The sine function >> math.factorial() # the factorial ( )עצרתfunction (also has some special variable values like math.pi) 18 Intro to: Python and OpenSesame Python Standard Library - Modules >> Import random >> random.random() if you only plan on using one function from a module: >> from random import random >> random() 19 Intro to: Python and OpenSesame 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) 20 Intro to: Python and OpenSesame 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”? 21 Intro to: Python and OpenSesame Objects – Tip of the iceberg! Objects are blueprints Roof Chimney Door Handle Address 22 Owner # Rooms Intro to: Python and OpenSesame Color Who lives there? 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’ 23 Intro to: Python and OpenSesame These Belong EXCLUSIVELY to the house1 instance of the house object Methods • Objects have functions – called Methods • Methods are functions that belong to the object type. Just an in object variable >> house.color Pink >> house.repaint(‘yellow’) >> house.color yellow 24 Intro to: Python and OpenSesame a method, notice the parenthesis. Methods We said everything in Python is an object. Int, float, str are objects too! st = ‘stringy‘ # Creating a string instance st.strip(‘y‘) # Using the strip method Ask for help! Use help(str) to learn all the str methods 25 Intro to: Python and OpenSesame Importing new objects! Similar to functions, we can import new objects from the Python Standard Library. We will now import the turtle library. 26 Intro to: Python and OpenSesame 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!) ** Some of the examples in this part were adopted from the Udacity Object Oriented Programming – Python course 27 Intro to: Python and OpenSesame Turtle – How it works Move Forward, 100 steps (0,0) 28 Intro to: Python and OpenSesame 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 objects 1. A screen object 2. A turtle object 29 Intro to: Python and OpenSesame 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 30 Intro to: Python and OpenSesame 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 ! 31 Intro to: Python and OpenSesame 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 32 Intro to: Python and OpenSesame Another type, Bool (Boolean) Use the type() function to on the input True • 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 [ >=, <= , != ] 33 Intro to: Python and OpenSesame Conditionals • Sets conditions to the program in the form: Condition if <this == True>: <do that> Indentation 34 Intro to: Python and OpenSesame Colon Conditionals • Example: Where is the <True> ? >> x = 5 >> y = 6 >> if x > y: >> print(‘x is bigger than y’) 35 Intro to: Python and OpenSesame Conditionals – what else? • We can create more advanced conditions: if 1 > 2: print(‘a new fact!’) else: print(‘The world is in order’) 36 Intro to: Python and OpenSesame Conditionals – what else? • And even more advanced conditions: if 1 > 2: print(‘1 > 2’) elif 2 > 1: print(‘2 > 1’) else: print(‘Chaos!’) 37 Intro to: Python and OpenSesame 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’ 38 Intro to: Python and OpenSesame 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 turtle 3. If y is bigger than x, draw a circle with turtle 4. 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. 39 Intro to: Python and OpenSesame