Compsci 101 Intro to Python Owen Astrachan Kristin
- Slides: 35
Compsci 101, Intro to Python Owen Astrachan Kristin Stephens-Martinez January 16, 2018 1/16/2018 Compsci 101, Spring 2018, Python Intro 1
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 2
PFTD and toward PFTW • Names, types, and values in Python • Variables, Expressions, and more • How to run a Python program • Eclipse IDE, Python Tutor, Py. Dev Console • Functions in Python • Decomposition, parameters, calling • APTs • Purpose, practical consideration, finishing 1/16/2018 Compsci 101, Spring 2018, Python Intro 3
Who Has Taken 101 1/16/2018 Compsci 101, Spring 2018, Python Intro 4
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 5
Names, Types, and Values • What type is each of these files? • glob. pdf, blurp. mp 4, 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. mp 4 not the same as moo. mp 4 • Type of blurp. mp 4 is the same as moo. mp 4 • Name of stairwaytoheaven. mp 3 means … 1/16/2018 Compsci 101, Spring 2018, Python Intro 6
Numeric Python Building Blocks • Numbers are not everything! • Values and arithmetic expressions • Integer aka int: 0, 3, -2, 5, … • Float: 2. 5, 3. 6673, 1. 938 e+120 • Operators: +, -, *, /, ** • Operators: // and % • Demonstration in Py. Dev console of these 1/16/2018 Compsci 101, Spring 2018, Python Intro 7
Interactive Console • Window>Show View> Console • Expand menu and choose Py. Dev Console • Type interactive Python commands to demo 1/16/2018 Compsci 101, Spring 2018, Python Intro 8
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 % • Modulus or remainder 1/16/2018 Compsci 101, Spring 2018, Python Intro 9
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 Py. Dev console 1/16/2018 Compsci 101, Spring 2018, Python Intro 10
Types and Conversion • How do you convert a. mp 4 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 11
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 1/16/2018 Compsci 101, Spring 2018, Python Intro 12
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 1/16/2018 Compsci 101, Spring 2018, Python Intro 13
Subtleties • Variables on LHS and RHS x = 17 • Value compared to Name y = x + 12 • What happens here? x • Value compared to Name y x y • In expressions? Value 1/16/2018 Compsci 101, Spring 2018, Python Intro = = 17 x + 12 “hello” x * 3 14
Functions in the Real World 1/16/2018 Compsci 101, Spring 2018, Python Intro 15
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 16
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 17
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? 1/16/2018 Compsci 101, Spring 2018, Python Intro 18
In Python 3 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 None. Type 1/16/2018 Compsci 101, Spring 2018, Python Intro 19
WOTO 1 http: //bit. ly/101 spring 18 -jan 16 -1 1/16/2018 Compsci 101, Spring 2018, Python Intro 20
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 21
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 22
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 deffunction inch 2 centi(inches): return 2. 54*inches xh = inch 2 centi(72) def pluralize(word): return word + "es" pf = pluralize("fish") 1/16/2018 Compsci 101, Spring 2018, Python Intro 23
Solving BMI APT • Navigate to APTs in class website and … 1/16/2018 Compsci 101, Spring 2018, Python Intro 24
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 25
Solving an APT: Python + Eclipse • Create new Py. De. V project • File>New>Project. . Py. Dev or other • Specify Python 3 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 26
Names and Return 0 Test • Make sure errors aren’t in code, test first 1/16/2018 Compsci 101, Spring 2018, Python Intro 27
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 1/16/2018 Compsci 101, Spring 2018, Python Intro 28
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 call replaced by return value, why use function? def bmi(weight, height): return 703. 07 * weight/(height*height) if bmi(170, 72) < 18. 5: print ("underweight") 1/16/2018 Compsci 101, Spring 2018, Python Intro 29
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 30
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 Py. Dev Console • In Py. Dev console • Must write import BMI • Must call BMI. calculate(3, 2) for example 1/16/2018 Compsci 101, Spring 2018, Python Intro 31
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 32
Organization Matters https: //www. youtube. com/watch? v=1 ve 57 l 3 c 19 g 1/16/2018 Compsci 101, Spring 2018, Python Intro 33
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 34
WOTO 2 http: //bit. ly/101 spring 18 -jan 16 -2 1/16/2018 Compsci 101, Spring 2018, Python Intro 35
- Compsci 201 duke
- Duke compsci 101
- Owen astrachan
- Owen astrachan
- Compsci 101 duke
- Softeng 750
- Compsci 345
- Compsci 201
- Compsci 230
- Softeng 350
- Compsci 111 review
- Compsci 732
- Jim kurose lectures
- Compsci 111g
- Duke compsci 201
- Compsci 220
- Compsci 111
- Compsci 230 review
- Compsci 773
- Comp 335
- Compsci 345
- Compsci 762
- Compsci 345
- Compsci 345
- Compsci 230 duke
- Compsci 732
- Compsci 230 duke
- Dsmp kristin
- Python tutor ames
- Kristin rossum
- Kristin kersten
- Bensen fan md
- Kristin mmari
- Kristin riebe
- Kristin bentz
- Adhb hippo