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.

Slides:



Advertisements
Similar presentations
EasyGUI “Probably the Easiest GUI in the world”. Assumptions (Teachers’ Notes) This resources sets out an introduction to using easyGUI and Python
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
Intro to Python Welcome to the Wonderful world of GIS programing!
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
The If/Else Statement, Boolean Flags, and Menus Page 180
Functions and abstraction Michael Ernst UW CSE 190p Summer 2012.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
Recitation 1 Programming for Engineers in Python.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Python Programming Fundamentals
An Introduction to Textual Programming
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Python – May 11 Briefing Course overview Introduction to the language Lab.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Variables, Expressions and Statements
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #001 (January 17, 2015)
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Introduction to: Python and OpenSesame FOR PROS. OpenSesame In OpenSesame you can add Python in-line codes which enables complex experiment. We will go.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Input, Output and Variables GCSE Computer Science – Python.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Fundamentals of Programming I Overview of Programming
Whatcha doin'? Aims: To start using Python. To understand loops.
A basic tutorial for fundamental concepts
Python Let’s get started!.
Introduction to Python
Introduction to Programming
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Engineering Innovation Center
Introduction to Python
Introduction to Programming
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
T. Jumana Abu Shmais – AOU - Riyadh
COMPUTER PROGRAMMING PYTHON
Introduction to Programming
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 03
Introduction to Programming
Class code for pythonroom.com cchsp2cs
Using Modules.
Presentation transcript:

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 the context of in-line scripting for opensesame. For that, you need to know how to write basic scripts. Intro to: Python and OpenSesame2

Python Intro to: Python and OpenSesame3 From: XKCD comics

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

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 OpenSesame5

Python Basic (Data) Types INT (integer) – 2,3,0,15, FLOAT (floating point) – 2.0, 3.5, 0.12, STR (string) – ‘Hello’, ”to you all”, ‘1+1’, “11” Use type(…) to check: –2–2 – 2.2 – ‘2.2’ Intro to: Python and OpenSesame6

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 OpenSesame7

Variables (jumping from types) >> x = 2.0; y = 3 >> int(x) 2 >> str(x) ‘2.0’ >> float(y) Intro to: Python and OpenSesame8

Variables (pointing) >> x = 1; y = 2 >> x = y; x is y? Intro to: Python and OpenSesame9 x1 (memory location: ) y2 (memory location: ) x1 (memory location: ) y2 (memory location: )

Variables (pointing) - continue >> y = 1 >> x is y ? Intro to: Python and OpenSesame10 x1 (memory location: ) y2 (memory location: )

Functions - BASICS Intro to: Python and OpenSesame11 BLACK BOX INPUT OUTPUT String: “Hello World” print(“Hello World”) Hello World

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 OpenSesame12

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

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 OpenSesame14

Back to functions Recall: input -> blackbox -> output Python comes with many built-in functions. For more information, visit the official documentation: Intro to: Python and OpenSesame15

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 OpenSesame16 Notice I’m using # (hash.mark) for comments. Python ignore everything after this symbol, but you shouldn’t!

Python Standard Library But the built-in functions are limited, and Python has MUCH MORE to offer in the Python Standard Library ( 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 OpenSesame17

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 OpenSesame18 The dot stands for from, as in: use sin from math

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 OpenSesame19

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 OpenSesame20

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 OpenSesame21

Objects – Tip of the iceberg! Objects are blueprints Intro to: Python and OpenSesame22 Roof Door Chimney Handle Address Owner # RoomsColor 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 = >> house1.roof_color = ‘pink’ Intro to: Python and OpenSesame23 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. >> house.color Pink >> house.repaint(‘yellow’) >> house.color yellow Intro to: Python and OpenSesame24 Just an in object variable 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 Intro to: Python and OpenSesame25

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 OpenSesame26

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 OpenSesame27 ** Some of the examples in this part were adopted from the Udacity Object Oriented Programming – Python course

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

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 Intro to: Python and OpenSesame29

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 OpenSesame30

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 ! Intro to: Python and OpenSesame31

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 OpenSesame32

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 OpenSesame33 Use the type() function to on the input True

Conditionals Sets conditions to the program in the form: Intro to: Python and OpenSesame34 if : Condition Colon Indentation

Conditionals Example: >> x = 5 >> y = 6 >> if x > y: >> print(‘x is bigger than y’) Intro to: Python and OpenSesame35 Where is the ? x > y is True! == True is embedded

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 OpenSesame36 NOTE: Code blocks (after a colon) are indented. We can have multilevel Indentation

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 OpenSesame37 POP QUIZ: What will be the output? 1.1 > > 1 3.Chaos! 4.True 5.False 6.Error

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 OpenSesame38 AND – True if: Both are true OR – True if: 1 is true, 2 is true, both are true

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. Intro to: Python and OpenSesame39