Practical programming

 a brief introduction

Why programming?

  • A structured formulation of ideas
    • Zero ambiguity!
  • Allows you to express your needs in order to GSD
    • Automation of tedious tasks
    • Large calculations
    • Fast
    • Multitasking
  • Abstract thinking
    • Symbolic logic
    • Lambda calculus
  • Why not ?

Python

  • A small, yet powerful programming language
    • Interpreted
    • Dynamically-typed
    • Cross-platform
  • Plenty of battle-hardened tools and libraries
    • REPL
    • IPython
    • pip
    • virtualenv
  • Abundant learning resources
    • Large developer community
    • Tons of free tutorials
    • Open source!

Fire up the REPL!


jl@jerluc-mws ~ $ ipython
Python 2.7.7rc1 (2.7:ece24bcd1a6f+, Jun 16 2014, 19:34:26) 
Type "copyright", "credits" or "license" for more information.

IPython 2.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:






Make mistakes!


In [1]: i have no idea what i'm doing!
  File "<ipython-input-1-5d2af8872bcc>", line 1
    i have no idea what i'm doing!
         ^
SyntaxError: invalid syntax



The REPL is a safe place.

Seriously, you're fine.


In [2]:  1 / 0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-2-b710d87c980c> in <module>()
----> 1 1 / 0

ZeroDivisionError: integer division or modulo by zero



    

Plus it's right, you know—all the time.

Doing the math


In [1]: 1 + 1
Out[1]: 2

In [2]: 0 - 1
Out[2]: -1

In [3]: 7 * 4
Out[3]: 28

In [4]: 81 / 9
Out[4]: 9
    
In [5]: 10 ** 2
Out[5]: 100

In [6]: (134 ** 6) * (25 % 5)
Out[6]: 0
    

Remembering things


In [1]: a = 1

In [2]: b = 12 / 6

In [3]: c = a + b

In [4]: a
Out[4]: 1

In [5]: b
Out[5]: 2

In [6]: c
Out[6]: 3

    
These are called variables and are expressed in the form:
variable_name = value_or_expression

Comparing things


In [1]: hours_of_sleep = 8

In [2]: hours_of_sleep > 7
Out[2]: True

In [3]: hours_of_sleep < 7
Out[3]: False

In [4]: hours_of_sleep >= 7
Out[4]: True

In [5]: hours_of_sleep <= 7
Out[5]: False

In [6]: hours_of_sleep != 7
Out[6]: True

In [7]: hours_of_sleep == 7
Out[7]: False

Maybe doing something


In [1]: hours_of_sleep = 8

In [2]: if hours_of_sleep > 7:
   ...:     print('Happy as can be')
   ...:
Happy as can be

This is called an if statement, of the form:
if some_condition:
    then_do_this_stuff

Maybe doing something else


In [1]: hours_of_sleep = 6

In [2]: if hours_of_sleep > 7:
   ...:     print('Happy as can be')
   ...: else:
   ...:     print('Coffee is imminent')
   ...:
Coffee is imminent

This is called an if-else statement, of the form:
if some_condition:
    then_do_this_stuff
else:
    do_this_stuff_instead

Lots of things


In [1]: days_of_week = ['S', 'M', 'T', 'W', 'Th', 'F', 'Sa']

In [2]: len(days_of_week)
Out[2]: 7

In [3]: days_of_week[0]
Out[3]: 'S'

In [4]: days_of_week[-1]
Out[4]: 'Sa'

In [5]: days_of_week + ['F']
Out[5]: ['S', 'M', 'T', 'W', 'Th', 'F', 'Sa', 'F']

This is called a list:
my_list = [item1, item2, ..., item_n]
fourth_thing = my_list[3]

One at a time


In [1]: days_of_week = ['S', 'M', 'T', 'W', 'Th', 'F', 'Sa']

In [2]: for day in days_of_week:
   ...:     print(day)
   ...:
S
M
T
W
Th
F
Sa
This is called a for loop, written as:
for thing in some_list_of_things:
    do_this_stuff

One at a time, maybe


In [1]: days_of_week = ['S', 'M', 'T', 'W', 'Th', 'F', 'Sa']

In [2]: for day in days_of_week:
   ...:     if day == 'M':
   ...:         print('Why oh why?!')
   ...:     else:
   ...:         print('TGINM!')
   ...:
TGINM!
Why oh why?!
TGINM!
TGINM!
TGINM!
TGINM!
TGINM!

Remembering what to do


In [1]: def enough_sleep(hours_of_sleep):
   ...:     if hours_of_sleep > 7:
   ...:         print('Happy as can be')
   ...:     else:
   ...:         print('Coffee is imminent')
   ...:
   
In [2]: enough_sleep(8)
Happy as can be

In [3]: enough_sleep(4)
Coffee is imminent
This is called a function, written as:
def function_name(arg1, arg2, ..., arg_n):
    do_this_stuff_with_args

Returning the favor


In [1]: def avg(a, b):
   ...:     sum = a + b
   ...:     average = sum / 2.0
   ...:     return average
   ...:

In [2]: avg(1, 2)
Out[2]: 1.5

In [3]: avg(1, 2) + avg(0, 1)
Out[3]: 2.0

In [4]: sum_of_averages = avg(1, 2) + avg(0, 1)

In [5]: sum_of_averages
Out[5]: 2.0
This is called a return value, written as:
def function_name(arg1, arg2, ..., arg_n):
    do_some_stuff
    return this_value

Shakespearean craft


my_hours_of_sleep = 10

def enough_sleep(hours_of_sleep):
    if hours_of_sleep > 7:
        return True
    else:
        return False
        
if enough_sleep(my_hours_of_sleep):
    print('Taking care of business')
else:
    1 / 0

jl@jerluc-mws ~ $ python sleepy.py
Taking care of business

HW: Odd couples




Write a program to sum the double of each odd number from 1-1000.

For example, the output for 1-5 would yield:
(1 * 2) + (3 * 2) + (5 * 2) = 18

HW (Hints)


Python has a special function for creating lists of numbers in a range:

In [1]: range(10)
Out[1]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

For checking if a number is odd or not, you can use the modulo operator:

In [1]: 10 % 2
Out[1]: 0

In [2]: 11 % 2
Out[2]: 1

Practical programming: A brief introduction

By Jeremy Lucas

Practical programming: A brief introduction

  • 350
Loading comments...

More from Jeremy Lucas