DS2000 (Spring 2019, NCH) :: Lecture 1a

0. Administrivia

  1. Welcome to DS2000! But ... what is DS2000?
    • Tight connection with DS2001 (this week = computer setup + first program!)
  2. Resources
  3. Policies
    • Grading: separate between lecture/practicum
    • Quizzes start next week: pre-class via Blackboard each week (do the reading!), in-class via Blackboard in practicum every other week
    • Misc: makeup policy, cheating
  4. Who am I (https://derbinsky.info)
    • I get on a plane later today for London and will be @ NCH for about 1 week :)

1. What is Programming??

  • Solving problems using a computer
    • What is a computer good for?
    • What are the issues that arise when using computation?
  • Computers are very fast at executing a small set of very precise operations
    • Examples: simple mathematical operations, store/retrieve information in limited ways
  • To solve problems we need to...
    1. Think of step-by-step directions to solve the problem ("algorithm")
    2. Express those steps as code in a "programming language" (we will use Python in this class)
      • Since this isn't a "natural" language, nor way of thinking, we aim to produce code that is readable and document the code (with "comments") to make our problem-solving clear to others (and ourselves!)
  • How do you get good at programming/succeed in this class?
    • Just like learning any new skill: practice coding every day!

2. Why Python??

  1. Python is a high-level language

  2. Python is used in a LOT of real-world settings

    • Computer/mobile/web, sys ops, robotics, analytics/machine learning, ...
    • Has great resources, tools, libraries

Take-away: Python will give you super-powers to make your life better :)

3. Process of Writing a Program

  1. Think! (don't touch the keyboard)
    • Basic tools you'll learn in this class...
      • input: get data
      • math/logic: crunch numbers/text/etc
      • repetition: do something over and over
      • conditional execution: do something if a situation holds
      • output: provide feedback
  2. Write code ("source code")
    • Interpretter good for tiny programs; otherwise in a file (save often!)
  3. Execute
    • Did it work??? (how do you know?)
  4. Document (with "comments")
    • Start each file with your name, class, assignment
    • Document each logical unit of code (more later), as well as steps that are not intuitive

What Could Go Wrong?

Consider a simple situation: some random person (let's call him "Nate") is coming to London and wants to get £100, write a program to compute how many US dollars Nate is going to need to bring

  1. Think!

    • Conversion rate (assume 1 USD = £0.8)
      • $ usd \times 0.8 = gbp = 100 $
      • $ usd = 100 \div 0.8 $
    • Fees (assume 0)
    • Set your own expectations: will the number be bigger/smaller?
    • How do we build confidence/test our program?
  2. NOW write code

In [2]:
# Attempt 1: Syntax Error
print(100 ÷ 0.8)
  File "<ipython-input-2-68472370efca>", line 2
    print(100 ÷ 0.8)
              ^
SyntaxError: invalid character in identifier
In [3]:
# Attempt 2: Runtime Error
print(100 / 0 + .8)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-3-14f2874fed0a> in <module>
      1 # Attempt 2: Runtime Error
----> 2 print(100 / 0 + .8)

ZeroDivisionError: division by zero
In [4]:
# Attempt 3: Semantic Error
print(100 + 0.8)
100.8
In [5]:
# Attempt 4
print(100 / 0.8)
125.0