Python 1: Intro! Max Dougherty Andrew Schmitt

Similar documents
Lecture 3. Strings, Functions, & Modules

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

CS1 Lecture 3 Jan. 18, 2019

CS1 Lecture 3 Jan. 22, 2018

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

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Python for Non-programmers

Beyond Blocks: Python Session #1

Lecture 4. Defining Functions

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

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

Flow Control: Branches and loops

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

Python for C programmers

Java Bytecode (binary file)

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

CSCE 110: Programming I

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

Getting Started Values, Expressions, and Statements CS GMU

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Python: common syntax

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

Real Python: Python 3 Cheat Sheet

CONTROL AND ENVIRONMENTS 1

Control Structures 1 / 17

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

CS 111X - Fall Test 1 - KEY KEY KEY KEY KEY KEY KEY

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Unit E Step-by-Step: Programming with Python

Programming Fundamentals and Python

Downloaded from Chapter 2. Functions

CS 1301 Exam 1 Answers Fall 2009

The Big Python Guide

CSE : Python Programming

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

CS 1301 Exam 1 Fall 2009

C ONTROL AND H IGHER O RDER F UNCTIONS

Introduction to Python (All the Basic Stuff)

Lecture 3. Functions & Modules

Introduction to Python

Lecture 3. Functions & Modules

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

Reviewing all Topics this term

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

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Scripting Languages. Python basics

CS100 Spring 2012 Midterm 1 Practice

A Little Python Part 1. Introducing Programming with Python

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

MEIN 50010: Python Introduction

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

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

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

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Text Input and Conditionals

CSCE 110 Programming I

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

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

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

An Introduction to Python

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

Conditionals and Recursion. Python Part 4

Lecture 9: July 14, How to Think About Debugging

3. Conditional Execution

18.1. CS 102 Unit 18. Python. Mark Redekopp

Introduction to Python

3. Conditional Execution

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

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

Fundamentals of Programming (Python) Getting Started with Programming

Programming to Python

PREPARING FOR PRELIM 1

CS Advanced Unix Tools & Scripting

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

CSCE 110 Programming I Basics of Python: Lists, Tuples, and Functions

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

61A Lecture 2. Friday, August 28, 2015

VLC : Language Reference Manual

Computer Components. Software{ User Programs. Operating System. Hardware

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

The current topic: Python. Announcements. Python. Python

COMP 364: Functions II

Name Feb. 14, Closed Book/Closed Notes No electronic devices of any kind! 3. Do not look at anyone else s exam or let anyone else look at yours!

Python Intro GIS Week 1. Jake K. Carr

CMSC201 Computer Science I for Majors

Language Reference Manual

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

Programming for Engineers in Python. Recitation 1

Some material adapted from Upenn cmpe391 slides and other sources

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

Lists, loops and decisions

Advanced Python. Executive Summary, Session 1

CS1110 Lab 1 (Jan 27-28, 2015)

Transcription:

Python 1: Intro! Max Dougherty Andrew Schmitt

Computational Thinking Two factors of programming: The conceptual solution to a problem. Solution syntax in a programming language BJC tries to isolate and strengthen the first. Snap! helps us (more or less) remove the worry of syntax Our goal is not to teach Snap!, but instead to teach computational thinking

Computational Thinking is: using abstraction to generalize problems. logical analysis of data identification, implementation, and testing of possible solutions a.k.a The Iterative Design Process

Why learn Python? Python is learn and use Looks like pseudo-code! Quickly implement programs Widely used as a teaching tool Tons of online support Powerful and Fast, with hundreds of community supported code libraries. (Familiar?)

Python in the World Parts of Google web search are written in Python. Battlefield 2 used python for core functions. Walt Disney uses python for animation tools and scripts.

Getting Started: Opening Interpreter (on Mac OSX) Open the Command Line in Terminal The Command Line is a text based computer interface Type python3 and press return. Aside: We will be using python version 3.4 in this course.

Getting Started: Hello World The >>> at the bottom indicates that you are in the interpreter Type print( Hello World ) and press the return key. Putting quotes around Hello World turns it into a string. Print returns a string

From Snap! to Python: Variables >>> foo = 5 >>> (Notice how, unlike print, assigning a value to foo does not return anything)

From Snap! to Python: Variabless >>> foo = 5 >>> foo 5 >>>

From Snap! to Python: Operators >>> 6 >>> 2 >>> 12 >>> 2.5 2 + 4 5-3 3 * 4 5 / 2 Aside: >>> 5 // 2 2 Rounds down

From Snap! to Python: Operatorss >>> 2 < 4 True >>> 2 > 4 False >>> 3 == 3 True

From Snap! to Python: Operatorsss >>> (2 < 3) and (3 < 4) True >>> (5 == 4) or (7 > 6) True >>> not (3 == 3) True

From Snap! to Python: Operatorssss >>> 5 % 3 2 >>> Hello + World HelloWorld >>> len( fish ) 4 >>> world [0] w

New Python Function!: Substring Substring returns part of a string the : is used to separate the first and separate index If no number exists on either side of colon, the substring will extend as far as possible. >>> name = Alonzo >>> name[1:4] lon >>> name[2:] onzo >>> name[:3] Alo

From Snap! to Python: Conditionals if (i < 3): print( You ) if (grade == A ): print( CS Degree ) else: print( French Fries )

From Snap! to Python: Conditionalss The indentation of lines is super important! if (i < 3): print( You ) if (grade == A ): print( CS Degree ) else: print( French Fries ) The colon tells Python that any indented lines that follow are inside the if or if/else condition

From Snap! to Python: Conditionalsss if (cats == 0): print( I have allergies! ) elif (cats < 4): print( I love cats! ) else: print( OMG CAAAATS )

From Snap! to Python: Loops Python for requires a list of values instead of a start and end value. This list is more properly known as an iterable Range(1,5) returns the list [1,2,3,4] Notice how 5 is NOT in the list! sum = 0 for i in range(1,6): sum = sum + i print(sum)

From Snap! to Python: Loopss Python while is similar to repeat until. Difference: Repeat until ends on TRUE, while ends on FALSE Can both cause infinite loops if the index (bottles) is not updated. num = 99 while bottles > 0: print(num + bottles of beer ) bottles = bottles - 1 print( No more beer )

Snap! to Python: Calling Functions Calling a function requires a name func and the required comma separated arguments in parentheses (arg1, arg1) >>> func( foo, bar ) CS Rules! >>>

Snap! to Python: Defining Functions Instead of using a pop-up window like Snap!, python functions are defined in text. prefaced by the keyword, def def func(arg1, arg2): if (arg1 < arg2): return Arg! I m a Pirate else: return CS Rules! Uses same indentation system as loops and conditionals (colons and indentation)

Snap! to Python: Defining Functions Function Name Arguments def func(arg1, arg2): if (arg1 < arg2): return works exactly like Snap! report return Arg! I m a Pirate else: return CS Rules! Note: return does not require parentheses

Types in Python Functions in Python have Dynamic type Simply: A function can return any type of data! type is a Python function that returns the object s class Integer: >>> output = sum(2,3) >>> type(output) <class int > Floating point number: >>> output = sum(3.14, 2.718) >>> type(output) <class float > def sum(x, y): return x + y

Aside: Floating Point Numbers 3.1410 =???2 Doesn t look so pretty for a programmer. Computer Scientists decided to use an alternate representation for non-integer values Represent numbers to great accuracy Not responsible for knowing how floating point numbers work in computers

Types in Python Functions in Python have Dynamic type Simply: A function can return any type of data! type is a Python function that returns the object s class Integer: >>> output = sum(2,3) >>> type(output) <class int > def sum(x, y): return x + y Floating point number: String: >>> output = sum(3.14, 2.718) >>> output = sum( cat, dog ) >>> type(output) >>> type(output) <class float > <class str >

Snap! to Python: Importing Importing tools in Snap! gave us a more advanced set of functions Python also allows importing, but from a much, much larger library >>> import math >>>

Importing in Python >>> sin(0.5) # standard Python doesn t know what sin is Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name sin is not defined >>> import math >>> math.sin(0.5) 0.479425538604203 # adds tons of functions for math operations

Python Demo: Turtle Power! import turtle t = turtle.turtle() turtle.showturtle() turtle.pendown() recursive_tree(5, 40)

Thank You! Photo at xkcd.com/353