Hello World! An Invitation to Computer Science

Lecture Slides

Introduction to Programming in Python
Thursday April 3, 2008


Key ideas from last time


How are high-level programs executed?


What is Python?


Identifiers


Expressions


Statements


Accumulator pattern

def factorial():
    n = input("Enter value for n: ")
    product = 1
    i = 2
    while i <= n:
        product = product * i
        i = i + 1
    print "The factorial of", n, "is", product

Real computers are finite


Another example

def sumDigits():
    n = input("Enter value for n: ")
    sum = 0
    x = n
    while x > 0:
        sum = sum + (x % 10)
        x = x / 10
    print "Sum of digits in ", n, "is", sum