Introduction to Python and Open Sesame I t

  • Slides: 39
Download presentation
Introduction to: Python and Open. Sesame I t Par

Introduction to: Python and Open. Sesame I t Par

Python • A high-level programming language • It can do a lot of things

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 Open. Sesame

Python 3 Intro to: Python and Open. Sesame From: XKCD comics

Python 3 Intro to: Python and Open. Sesame From: XKCD comics

Python Shell Start -> python -> Python GUI (IDLE) 4 Intro to: Python and

Python Shell Start -> python -> Python GUI (IDLE) 4 Intro to: Python and Open. Sesame

Python Shell • An Interpreter: – You write a line of code – Python

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 Open. Sesame

Python Basic (Data) Types • INT (integer) – 2, 3, 0, 15, 100000 •

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’ 6 Intro to: Python and Open. Sesame

Variables (assign value to variable) • X = 1; Y = 2. 0; Z

Variables (assign value to variable) • X = 1; Y = 2. 0; Z 1 = ‘Gevald’; Z 2 = ‘avoi’ • • 7 Operations on variables? No problem: X+Y [int+float = float] X/Y [/ = devision, // = int devision] Z 1+X [int+str = error] Z 1 + Z 2 [str+str = str] Z 1*3 [str*int = str (overloaded? !)] Z 2*Y [str*float = error] Intro to: Python and Open. Sesame

Variables (jumping from types) >> x = 2. 0; y = 3 >> int(x)

Variables (jumping from types) >> x = 2. 0; y = 3 >> int(x) 2 >> str(x) ‘ 2. 0’ >> float(y) 8 Intro to: Python and Open. Sesame

Variables (pointing) >> x = 1; y = 2 x 1 (memory location: 112125123)

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 Open. Sesame

Variables (pointing) - continue >> y = 1 x 1 (memory location: 112125123) y

Variables (pointing) - continue >> y = 1 x 1 (memory location: 112125123) y 2 (memory location: 112125125) >> x is y ? 10 Intro to: Python and Open. Sesame

Functions - BASICS String: “Hello World” INPUT 11 print(“Hello World”) BLACK BOX Intro to:

Functions - BASICS String: “Hello World” INPUT 11 print(“Hello World”) BLACK BOX Intro to: Python and Open. Sesame orld W Hello OUTPUT

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

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 Open. Sesame

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

Script! Super text files! Special language keywords are highlighted in different colors. Example 13 Intro to: Python and Open. Sesame

Your first Python program In a new file (Call it first. py) 1. Create

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 Open. Sesame

Back to functions Recall: input -> blackbox -> output Python comes with many built-in

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 Open. Sesame

Back to functions Examples: >> len() # returns the length of the input >>

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 Open. Sesame

Python Standard Library But the built-in functions are limited, and Python has MUCH MORE

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 Open. Sesame

Python Standard Library - Modules Math >> import math >> help(math) # lists all

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 Open. Sesame

Python Standard Library - Modules >> Import random >> random() if you only plan

Python Standard Library - Modules >> Import random >> random() if you only plan on using one function from a module: >> from random import random >> random() 19 Intro to: Python and Open. Sesame

More Python libraries Python has even more libraries available on the web which you

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 Open. Sesame

Objects – Tip of the iceberg! Traditionally, this part comes much later in the

Objects – Tip of the iceberg! Traditionally, this part comes much later in the learning process. But, it is essential for Open. Sesame 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 Open. Sesame

Objects – Tip of the iceberg! Objects are blueprints Roof Chimney Door Handle Address

Objects – Tip of the iceberg! Objects are blueprints Roof Chimney Door Handle Address 22 Owner # Rooms Intro to: Python and Open. Sesame Color Who lives there?

Objects – Tip of the iceberg! We can create an instance of an object:

Objects – Tip of the iceberg! We can create an instance of an object: >> house 1 = house() We can add properties to it: >> house 1. address = ‘Ragar 19, Be’er Sheva’ >> house 1. price = 100000 >> house 1. roof_color = ‘pink’ 23 Intro to: Python and Open. Sesame These Belong EXCLUSIVELY to the house 1 instance of the house object

Methods • Objects have functions – called Methods • Methods are functions that belong

Methods • Objects have functions – called Methods • Methods are functions that belong to the object type. >> house. color Pink >> house. repaint(‘yellow’) >> house. color yellow 24 Just an in object variable Intro to: Python and Open. Sesame a method, notice the parenthesis.

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

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 Open. Sesame

Importing new objects! Similar to functions, we can import new objects from the Python

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 Open. Sesame

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

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 Open. Sesame 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 Open. Sesame

Turtle – How it works Move Forward, 100 steps (0, 0) 28 Intro to:

Turtle – How it works Move Forward, 100 steps (0, 0) 28 Intro to: Python and Open. Sesame

Turtle - setup First, we need to import the turtle module which has all

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 Open. Sesame

Turtle – Screen Object >> import turtle >> window = turtle. Screen() We created

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 Open. Sesame

Turtle – Turtle Object >> yoni = turtle. Turtle() What can yoni (turtle instance)

Turtle – Turtle Object >> yoni = turtle. Turtle() What can yoni (turtle instance) do? >> yoni. forward(100) # Go forward, 100 steps! >> yoni. right(10) # Turn right, 10 o ! 31 Intro to: Python and Open. Sesame

Time for a Python Program In a new file (call it, square. py) Use

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 Open. Sesame

Another type, Bool (Boolean) Use the type() function to on the input True •

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 Open. Sesame

Conditionals • Sets conditions to the program in the form: Condition if <this ==

Conditionals • Sets conditions to the program in the form: Condition if <this == True>: <do that> Indentation 34 Intro to: Python and Open. Sesame Colon

Conditionals • Example: Where is the <True> ? >> x = 5 x >

Conditionals • Example: Where is the <True> ? >> x = 5 x > y is T rue! == True >> y = 6 is embedd ed >> if x > y: >> print(‘x is bigger than y’) 35 Intro to: Python and Open. Sesame

Conditionals – what else? • We can create more advanced conditions: NOTE: Code bloc

Conditionals – what else? • We can create more advanced conditions: NOTE: Code bloc ks (after a colon) are inden ted. We c an have multileve l Indentat ion if 1 > 2: print(‘a new fact!’) else: print(‘The world is in order’) 36 Intro to: Python and Open. Sesame

Conditionals – what else? • And even more advanced conditions: if 1 > 2:

Conditionals – what else? • And even more advanced conditions: if 1 > 2: print(‘ 1 > 2’) elif 2 > 1: print(‘ 2 > 1’) else: print(‘Chaos!’) 37 POP QUIZ: What will be the ou tput? 1. 1 > 2 2. 2 > 1 3. Chaos! 4. True 5. False 6. Error Intro to: Python and Open. Sesame

Logical Operators (AND / OR) x = 1; y = 2; z = 3

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 AND – True if: Both are true OR – True if: 1 is true, 2 is true, both are Intro to: Python and Open. Sesame true

Time for a Python Program In a new file (call it, conditions. py) Write

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 Open. Sesame