350 likes | 368 Views
Learn the basics of Python programming, including bug fixing, binary representation, loops, boolean logic, variables, and functions. Suitable for Compsci 101 students.
E N D
Compsci 101, Intro to Python Owen Astrachan Kristin Stephens-Martinez January 16, 2018 Compsci 101, Spring 2018, Python Intro
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 Compsci 201, Fall 2017, First Day
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 Compsci 101, Spring 2018, Python Intro
Who Has Taken 101 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 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 … Compsci 101, Spring 2018, Python Intro
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.938e+120 • Operators: +, -, *, /, ** • Operators: // and % • Demonstration in PyDev console of these Compsci 101, Spring 2018, Python Intro
Interactive Console • Window>Show View> Console • Expand menu and choose PyDev Console • Type interactive Python commands to demo 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 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 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? Compsci 101, Spring 2018, Python Intro
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 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 Compsci 101, Spring 2018, Python Intro
Subtleties • Variables on LHS and RHS • Value compared to Name • What happens here? • Value compared to Name • In expressions? Value x = 17 y = x + 12 x = 17 y = x + 12 x = “hello” y = x * 3 Compsci 101, Spring 2018, Python Intro
Functions in the Real World 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 Compsci 101, Spring 2018, Python Intro
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 Compsci 101, Spring 2018, Python Intro
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? Compsci 101, Spring 2018, Python Intro
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 Compsci 101, Spring 2018, Python Intro
WOTO 1 http://bit.ly/101spring18-jan16-1 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 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 Compsci 101, Spring 2018, Python Intro
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") Compsci 101, Spring 2018, Python Intro
Solving BMI APT • Navigate to APTs in class website and … 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 Compsci 101, Spring 2018, Python Intro
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! Compsci 101, Spring 2018, Python Intro
Names and Return 0 Test • Make sure errors aren’t in code, test first 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 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 defbmi(weight, height): return 703.07 * weight/(height*height) if bmi(170,72) < 18.5: print ("underweight") call replaced by return value, why use function? Compsci 101, Spring 2018, Python Intro
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! 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 Compsci 101, Spring 2018, Python Intro
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 Compsci 101, Spring 2018, Python Intro
Organization Matters https://www.youtube.com/watch?v=1ve57l3c19g 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 Compsci 101, Spring 2018, Python Intro
WOTO 2 http://bit.ly/101spring18-jan16-2 Compsci 101, Spring 2018, Python Intro