Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compsci 101, Intro to Python

Similar presentations


Presentation on theme: "Compsci 101, Intro to Python"— Presentation transcript:

1 Compsci 101, Intro to Python
Owen Astrachan Kristin Stephens-Martinez January 16, 2018 1/16/2018 Compsci 101, Spring 2018, Python Intro

2 B is for … Bug What you will always have and need to fix Bits
Zeros and Ones that are our C,G,A,T Break An easy way out of a loop Boolean Type that's true or false 8/30/17 Compsci 201, Fall 2017, First Day

3 Compsci 101, Spring 2018, Python Intro
PFTD and toward PFTW Names, types, and values in Python Variables, Expressions, and more How to run a Python program Eclipse IDE, Python Tutor, PyDev Console Functions in Python Decomposition, parameters, calling APTs Purpose, practical consideration, finishing 1/16/2018 Compsci 101, Spring 2018, Python Intro

4 Compsci 101, Spring 2018, Python Intro
Who Has Taken 101 Lex butler, director of player personnel, Duke Football Mary McKee, Senior IT manager, Duke OIT Marshall Plumlee, NBA G League Andrew Van Kirk, Episcopal Rector and former Software Engineer Beth Trushkowsy, Assistant Prof. Harvey Mudd Tom Clifton, Entrepreneur Rachel Zurer, Editorial Director 1/16/2018 Compsci 101, Spring 2018, Python Intro

5 Compsci 101, Spring 2018, Python Intro
Exploring Language Do you first learn how to say something useful or do you learn the rules of grammar Duolingo: El hombre bebe How do you say the man thinks? The woman? We'll do some bottom up exploration so we can solve problems Variables, names, types, values 1/16/2018 Compsci 101, Spring 2018, Python Intro

6 Compsci 101, Spring 2018, Python Intro
Names, Types, and Values What type is each of these files? glob.pdf, blurp.mp4, egal.jpg, zoo.wav Does the name/suffix define the type or is it the bits in the file that defines the type? Value of blurp.mp4 not the same as moo.mp4 Type of blurp.mp4 is the same as moo.mp4 Name of stairwaytoheaven.mp3 means … 1/16/2018 Compsci 101, Spring 2018, Python Intro

7 Numeric Python Building Blocks
Numbers are not everything! Values and arithmetic expressions Integer aka int: 0, 3, -2, 5, … Float: 2.5, , 1.938e+120 Operators: +, -, *, /, ** Operators: // and % Demonstration in PyDev console of these In console go through arithmetic and show precedence and order of operations 3 + 5 * 2 3*2 + 5 3**25 3**250 3**2500 3.0**2500 Import math and then math.pi Math.sin(math.pi) and math.sin(0) 3.55 Use division for 3/2 and also 3//2 and also 3 % 2 and more 1/16/2018 Compsci 101, Spring 2018, Python Intro

8 Compsci 101, Spring 2018, Python Intro
Interactive Console Window>Show View> Console Expand menu and choose PyDev Console Type interactive Python commands to demo In console go through arithmetic and show precedence and order of operations 3 + 5 * 2 3*2 + 5 3**25 3**250 3**2500 3.0**2500 Import math and then math.pi Math.sin(math.pi) and math.sin(0) 3.55 Use division for 3/2 and also 3//2 and also 3 % 2 and more 1/16/2018 Compsci 101, Spring 2018, Python Intro

9 Compsci 101, Spring 2018, Python Intro
Summary of Numbers Integers are arbitrarily large in Python 3 In Python 2 there is int and long Float values do not have infinite precision We'll use these when we need decimal values Be attentive to parentheses and precedence Understand / and // and % Modulus or remainder 1/16/2018 Compsci 101, Spring 2018, Python Intro

10 Compsci 101, Spring 2018, Python Intro
Python Strings A string is a sequence of characters String literals use single or double quotes "hello" and 'world' are both strings Operators we'll use: + and [:] Concatenation and Slicing Adding and taking apart? Today just adding Demo in PyDev console We’ll do ”hello” + “world” and wonder about the space, how do we get that? Add explicitly Concatenate x + y + z using literals, we don’t have variables yet 1/16/2018 Compsci 101, Spring 2018, Python Intro

11 Compsci 101, Spring 2018, Python Intro
Types and Conversion How do you convert a .mp4 to a .mov? Change the bits from one format to another Can we add a string and an integer? What does 5 + "cow" mean? What does 5 * "cow" mean? Why? 1/16/2018 Compsci 101, Spring 2018, Python Intro

12 Variables and Functions
We use variables to store values so we can use them and re-use them in expressions Name associated with storage Assign value to a variable How to read: x = 5, y = "hello" Why say 'gets' or 'is assigned' and not 'equals’ We’ll use ‘equals’ later to mean equality PyDev demo briefly 1/16/2018 Compsci 101, Spring 2018, Python Intro

13 Compsci 101, Spring 2018, Python Intro
Anatomy of a variable Variables in Python have a type, changeable Initially x = 5, change to x = “hello” Use the type(..) function to determine type, but documentation/comments perhaps better Variables are names/labels, references to an object stored elsewhere (basically) My address is “202 Longwood Drive” That’s the name/label, my house is elsewhere For x = “hello”, the string is elsewhere Not dwelling on this, but we’ll come back to what a variable is. How does X = 23 Y = x + 5 Work? In an expression, variable’s value is used. In assignment? Lable is applied to an object whose value may change/be different 1/16/2018 Compsci 101, Spring 2018, Python Intro

14 Compsci 101, Spring 2018, Python Intro
Subtleties Variables on LHS and RHS Value compared to Name What happens here? In expressions? Value x = 17 y = x + 12 x = 17 y = x + 12 x = “hello” y = x * 3 Names are addresses. Like “202 longwood drive”. Like “ The content is elsewhere, the variable stores a reference, e.g., map location or IP address 1/16/2018 Compsci 101, Spring 2018, Python Intro

15 Functions in the Real World
Perhaps familiar and we ground in context of input and output: we’ve seen these before 1/16/2018 Compsci 101, Spring 2018, Python Intro

16 Compsci 101, Spring 2018, Python Intro
Anatomy of a Function Functions may have an input and always produce an output: math.sqrt(25) Input is a parameter Output is the return value In Python name followed by parentheses Named abstraction (over code in Python) Use the name and understand contract of input and corresponding output 1/16/2018 Compsci 101, Spring 2018, Python Intro

17 Functions in the Real World Redux
Type domain name into browser window.. Input is a domain name Output is connection/HTML from a specific IP address Wordcount Input is a document, output is # words/chars 1/16/2018 Compsci 101, Spring 2018, Python Intro

18 Simple Python Functions
How many digits in 3**2500? How many characters in "hello world" len is a Python function Input is a sequence, for now a string Output is an integer, length of sequence type is a function as are int, float, str What if the input isn't part of the domain? Error if we do float(“zebra”) compare to float(“3.1415”) What if we do math.sqrt(-1) 1/16/2018 Compsci 101, Spring 2018, Python Intro

19 In Python3 print is a function
Functions have parentheses Arguments are provided in parentheses We can print(3+5) or print("hello") or … What is returned by print? When there is no return value... None is returned, it has no representation It's type is NoneType Example of y = print("hello"), type(y) 1/16/2018 Compsci 101, Spring 2018, Python Intro

20 Compsci 101, Spring 2018, Python Intro
WOTO 1 Go over answers to these questions 1/16/2018 Compsci 101, Spring 2018, Python Intro

21 Compsci 101, Spring 2018, Python Intro
Barbara Liskov Turing award in 2008 Programming Languages Object-Oriented Liskov Substitution Principle It's much better to go for the thing that's exciting. But the question of how you know what's worth working on and what's not separates someone who's going to be really good at research and someone who's not. There's no prescription. It comes from your own intuition and judgment 1/16/2018 Compsci 101, Spring 2018, Python Intro

22 Compsci 101, Spring 2018, Python Intro
APTs in 101 and 201 Algorithm Problem-solving and Testing Algorithm that’s Automatically Tested In use at Duke since 2003, more than a million Given a problem statement Read, think, read, think, plan … Write a function to solve the problem Test the code, Submit the code 1/16/2018 Compsci 101, Spring 2018, Python Intro

23 APT: Write a Python Function
We def(ine) functions in Python Use indentation to create body of the function Calling function is different than writing function def inch2centi(inches): return 2.54*inches xh = inch2centi(72) def pluralize(word): return word + "es" pf = pluralize("fish") 1/16/2018 Compsci 101, Spring 2018, Python Intro

24 Compsci 101, Spring 2018, Python Intro
Solving BMI APT Navigate to APTs in class website and … 1/16/2018 Compsci 101, Spring 2018, Python Intro

25 Compsci 101, Spring 2018, Python Intro
Reading an APT Solve examples with "paper and pencil" Be able to solve another instance, different parameters/inputs Explain to someone else (yourself) what you're doing 1/16/2018 Compsci 101, Spring 2018, Python Intro

26 Solving an APT: Python + Eclipse
Create new PyDeV project File>New>Project .. PyDev or other Specify Python3 as needed Create new Python Module This is .py file, name it properly!! Create function within module Name it properly! 1/16/2018 Compsci 101, Spring 2018, Python Intro

27 Compsci 101, Spring 2018, Python Intro
Names and Return 0 Test Make sure errors aren’t in code, test first 1/16/2018 Compsci 101, Spring 2018, Python Intro

28 Compsci 101, Spring 2018, Python Intro
Use Online Testing What if things don’t go well? Round trip to testing server How to step through code or print results Test locally too See next slide for local testing Kind of straightforward, things to review after looking at the BMI.calculate definition and how to call it. Luckily it's all about types. This could lead to changing weight and height in BMI.calculate to w and h, change body, still get all-green even though parameters aren't right. But what about when we swap w and h? Still all green? Swap weight and height order, still all green? 1/16/2018 Compsci 101, Spring 2018, Python Intro

29 Compsci 101, Spring 2018, Python Intro
BMI dissected What is formula? How to use and re-use? Functions allow code to be re-used Square root, len, BMI.calculate How do we validate/test our code? APT testing harness def bmi(weight, height): return * weight/(height*height) if bmi(170,72) < 18.5: print ("underweight") call replaced by return value, why use function? 1/16/2018 Compsci 101, Spring 2018, Python Intro

30 Anatomy of Return Statement
Execution is one line/statement at a time After one statement, next statement Calling a function transfers control to function When finished? Control transferred back Return value replaces function call x = math.sqrt(25) if bmi(170.2) < 18.5: print("underweight") None returned by default! 1/16/2018 Compsci 101, Spring 2018, Python Intro

31 Compsci 101, Spring 2018, Python Intro
Testing BMI.calculate The function calculate is in Module BMI Wrote the function, how to call it? You can test if you provide main! Alternatively, import into PyDev Console In PyDev console Must write import BMI Must call BMI.calculate(3,2) for example Must 1/16/2018 Compsci 101, Spring 2018, Python Intro

32 APT Testing and Submission
You wrote the code, how is it tested? Submit .py module with function to server Server tests and checks by calling your function The APT testing framework calls your code! Don’t call us, we’ll call you: Hollywood principle Test, Submit, Reflect Make sure you do all three! See web pages 1/16/2018 Compsci 101, Spring 2018, Python Intro

33 Compsci 101, Spring 2018, Python Intro
Organization Matters This is an all time favorite of mine, may not be something you want to do? Apparently Susan still uses it too I think when Robert de Niro looks at Cybil Shepard in the clip it's clear that he's infatuated with her. It's a long time ago. We are still infatuated with teaching and coding and we hope they will be. We also want to be organizized with building programs. 1/16/2018 Compsci 101, Spring 2018, Python Intro

34 Compsci 101, Spring 2018, Python Intro
BMI on "real" data Preview of what's coming How do we process data from class? Read a file that's line-oriented Extract data from each line Clean/process the data Use the BMI.calculate function Make decisions based on return value Open, Loop, Convert, Index, Select 1/16/2018 Compsci 101, Spring 2018, Python Intro

35 Compsci 101, Spring 2018, Python Intro
WOTO 2 Maybe this is all correct, I’m out of gas 1/16/2018 Compsci 101, Spring 2018, Python Intro


Download ppt "Compsci 101, Intro to Python"

Similar presentations


Ads by Google