Beyond Blocks: Python Session #1

Similar documents
CSCE 110 Programming I

Python Reference (The Right Way) Documentation

Modules and scoping rules

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

Flow Control: Branches and loops

Intro. Scheme Basics. scm> 5 5. scm>

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

contacts= { bill : , rich : , jane : } print contacts { jane : , bill : , rich : }

CS1 Lecture 3 Jan. 22, 2018

Variables, expressions and statements

CIS192 Python Programming. Robert Rand. August 27, 2015

CS1 Lecture 3 Jan. 18, 2019

Fundamentals of Programming (Python) Getting Started with Programming

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Python 1: Intro! Max Dougherty Andrew Schmitt

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

CNRS ANF PYTHON Objects everywhere

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Getting Started with Python

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

About Variables in Python F E B 1 1 T H

Python Intro GIS Week 1. Jake K. Carr

Advanced Python. Executive Summary, Session 1

Python for Non-programmers

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

Variable and Data Type I

Iterators & Generators

The float type and more on variables FEB 6 TH 2012

Introduction to Python (All the Basic Stuff)

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Jython. secondary. memory

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

Beyond programming. CS 199 Computer Science for Beginners Spring 2009 Lois Delcambre Week 5/12/2009 1

Python for C programmers

Introduction to Python, Cplex and Gurobi

Comp Exam 1 Overview.

Python as a First Programming Language Justin Stevens Giselle Serate Davidson Academy of Nevada. March 6th, 2016

CS1 Lecture 5 Jan. 26, 2018

Chapter 2 Writing Simple Programs

Python BASICS. Introduction to Python programming, basic concepts: formatting, naming conventions, variables, etc.

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

CS1 Lecture 5 Jan. 25, 2019

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

CIS192: Python Programming

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

CS 11 Haskell track: lecture 1

Artificial Intelligence A Primer for the Labs

The Pyth Language. Administrivia

ENGR 101 Engineering Design Workshop

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

Introduction to Python

Python 3000 and You. Guido van Rossum PyCon March 14, 2008

Overview of List Syntax

Programming with Python

Variable and Data Type I

CSCE 110 Programming I

CS Summer 2013

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Downloaded from Chapter 2. Functions

Java Bytecode (binary file)

PREPARING FOR PRELIM 1

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Python: common syntax

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Here n is a variable name. The value of that variable is 176.

Scripting Languages. Python basics

Table of Contents EVALUATION COPY

Python Tutorial. CSE 3461: Computer Networking

Python. Executive Summary

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Play with Python: An intro to Data Science

Teaching London Computing

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

Babu Madhav Institute of Information Technology, UTU 2015

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Outline. Simple types in Python Collections Processing collections Strings Tips. 1 On Python language. 2 How to use Python. 3 Syntax of Python

Basic Syntax - First Program 1

An Introduction to Python

Overloading. F21SC Industrial Programming: Python: Advanced Language Features. Overloading. Overloading arithmetic operations

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Python in 10 (50) minutes

PROGRAMMING FUNDAMENTALS

python 01 September 16, 2016

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

CS S-02 Python 1. Most python references use examples involving spam, parrots (deceased), silly walks, and the like

Part III Appendices 165

Data type built into Python. Dictionaries are sometimes found in other languages as associative memories or associative arrays.

Python for ArcGIS. Lab 1.

Python for Non-programmers

Transcription:

Beyond Blocks: Session #1 CS10 Spring 2013 Thursday, April 30, 2013 Michael Ball Beyond Blocks : : Session #1 by Michael Ball adapted from Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. 1 1

Goals Quick introduction to Not a tutorial or how to Hope is that you ll want to learn (more) Advantages over higher level languages Challenges of programming syntax It s really like writing BYOB on Paper! 2 2

Beyond Blocks: #1 Installation: Mac Check Open Terminal Type python3 and hit return Type print( hello world ) and hit return The result should be: 3 3

Beyond Blocks: #1 Installation: Windows Check Get to "print" something with these instructions: http://docs.python.org/faq/windows.html (You only have to get to the "Many people use the interactive mode as a convenient yet highly programmable calculator" paragraph) 4 4

Beyond Blocks: #1 Installation: More Information Computer Science Circles : Run at Home cemclinux1.math.uwaterloo.ca/~cscircles/ wordpress/run-at-home/ 5 5

Beyond Blocks: #1 Installation: Version Check Michael> python3 -V 3.3.4 We ll be talking about version 3.3.x here, although version 2.7.x (which is more common) works just as well! If curious, there s more version info at: http://docs.python.org/whatsnew/index.html 6 6

Beyond Blocks: #1 Why used text based programming? <demo> 7 7

8 8

Variables 9 9

Variables 10 10

Variables NOTE: Assignment doesn t evaluate to anything, so nothing is printed! 11 11

Variables 12 12

Variables print(var) NOTE: Printing is one of the big differences between 2 and 3. 3 requires () with print! For the sake of humanity, just use print()! :) 13 13

Operators 14 14

Types Everything in has an internal type Types are determined dynamically x = 1 x now has the type int : (short for integer ) We ll talk about this script (or function) later... 15 15

Types: bool bool is short for boolean bool s can have two values: True False 16 16

Types: bool bool is short for boolean bool s can have two values: >>> type(true) <class 'bool'> True False >>> True True >>> False False NOTE: Upper case is important! 17 17

Types: function type() This function returns the type that has assigned the identifier. >>> type(true) <class 'bool'> >>> type(1) <class 'int'> >>> type(1.0) <class 'float'> >>> type("hello, there!") <class 'str'> >>> type("1") <class 'str'> 18 18

Operators 19 19

Operators 20 20

Operators Note the double =s! = means assign, == means compare Very common source of bugs! 21 21

Operators = means assign, == means compare 22 22

Operators = means assign, == means compare 23 23

Operators 24 24

Sidebar: Division (integer vs. real/float) 2 3 >>> 5/6 0 >>> 5.0/6.0 0.8333333333333334 >>> 5.0//6.0 0.0 >>> 5/6 0.8333333333333334 >>> 5.0/6.0 0.8333333333333334 >>> 5.0//6.0 0.0 25

Sidebar: Division (integer vs. real/float) >>> 5/6 0.8333333333333334 >>> 5.0/6.0 0.8333333333333334 >>> 5.0//6.0 0.0 Force integer division 26

Sidebar: Exponent BYOB has e x and 10 x, but can do any base & exponent! >>> 2**8 256 >>> 2**10 1024 >>> 2**100 1267650600228229401496703205376 27

Conditionals 28 28

Conditionals 29

Conditionals Notice the colon and indentation syntax! 30

Conditionals Notice the colon and indentation syntax! 31

Conditionals 32 32

Conditionals 33

Loops 34 34

Loops 35

Loops Note the indentation (again)! 36

Loops 37 37

Loops 38

More Loops 39 39

Moar [sic] Loops There isn t really an exact equivalent of this in... We ll talk more about this in Session #2... 40

Functions: Calling Calling functions (the syntax) looks like this: Equivalent to creating & running a BYOB block: 41 41

Functions: Calling Calling functions (the syntax) looks like this: Equivalent to creating & running a BYOB block: 42

Functions: Calling Calling functions (the syntax) looks like this: Equivalent to creating & running a BYOB block: 43

Functions: Calling Calling functions (the syntax) looks like this: Equivalent to creating & running a BYOB block: 44

Functions : Defining Keyword: DEF 45 45

Functions : Defining Name of the function 46 46

Functions : Defining Arguments, or inputs to the function 47 47

Functions : Defining Indentation: the key to and scope.! In : Indentation matters!! 48 We ll talk about scope later... 48

Functions : Defining pass: s placeholder Skip this, and do nothing. 49 49

Functions : Defining pass: s placeholder Skip this, and do nothing. Functions must have a body! 49 49

Functions : Defining 50

Functions : Defining Hitting Return/Enter (on an empty line) closes (finishes) the definition. 51 51

Sidebar: Keywords Words reserved by and del from not while as elif global or with assert else if pass yield break except import class exec in raise continue finally is return def for lambda try List at: docs.python.org/reference/lexical_analysis.html 52 52

Built In Functions http://docs.python.org/3.3/library/functions.html abs() dict() help() min() setattr() all() dir() hex() next() slice() any() divmod() id() object() sorted() ascii() enumerate() input() oct() staticmethod() bin() eval() int() open() str() bool() exec() isinstance() ord() sum() bytearray() filter() issubclass() pow() super() bytes() float() iter() print() tuple() callable() format() len() property() type() chr() frozenset() list() range() vars() classmethod() getattr() locals() repr() zip() compile() globals() map() reversed() import () complex() hasattr() max() round() delattr() hash() memoryview() set() 53 53

USEFUL: Built In Functions abs() help() min() max() print() range() type() str() chr() ord() bool() float() int() map() filter() sum() open() iter() len() tuple() list() dict() set() http://docs.python.org/3.3/library/functions.html 54 54

Functions : Returning Values 55 55

Functions : Returning Values 56

Functions : Returning Values return and report are equivalent! 57

Functions : Recursion! 58 58

Functions : Recursion! 59 59

Functions : Recursion! Within Reason! 60

Beyond Blocks: #1 More Information.org: www.python.org Docs: www.python.org/doc/ Modules: docs.python.org/modindex.html CodeAcademy: codeacademy.com Online Tutor: http://www.pythontutor.com 61 61

Beyond Blocks: #1 More Information Computer Science Circles: cemclinux1.math.uwaterloo.ca/~cscircles/ wordpress/using-this-website/ Dive Into : diveintopython.org/toc/ Cal s Self-Paced Center: inst.eecs.berkeley.edu/~selfpace/class/cs9h/ How to Think Like a Computer Scientist ( Version) www.greenteapress.com/thinkpython/thinkcspy/html/ 62 62