This software is iPython Notebook. From the command line, change to the directory where your Notebooks (.ipynb) are located and type
ipython notebook
A Notebook contains "cells". Edit a cell by double clicking on it.
Some of the cells, like this one, contains text. The text is formatted using a language called "Markdown." It's similar to what's used in Wikipedia. You can find out more by going to the Help menu above.
Other cells, like the one below, contain Python code and can be run.
Both types of cells are "run" using the "play" button, above.
In [1]:
print("Hello world")
In [ ]:
#Input the hours worked
#Input the hourly pay
#Calculate the gross pay as hours worked multiplied by pay rate
#Display the gross pay
Typically, a computer performs a three-step process
We'll talk about output first, as that's the most straightforward and we did this in tutorial last week.
print function displays output on the screen."Calling" a function causes all the lines of code inside it to run
Here is an example of a function. Don't worry about the details now. We'll be talking about them more in a couple of weeks.
In [2]:
def first_function():
print("\"Beware the Jabberwock, my son!")
print("The jaws that bite, the claws that catch!")
print("Beware the Jubjub bird, and shun")
print("The frumious Bandersnatch!\"")
first_function()
The print function outputs strings and variables. So let's look at those now.
I usually say just "string"
Recommended style for Python is double quotation marks
In [ ]:
"This is a string literal"
'This is also a string literal'
"""He took his vorpal sword in hand:
Long time the manxome foe he sought --
So rested he by the Tumtum tree,
And stood awhile in thought.
"""
In [ ]:
# Varible assignment
# Let age be equal to 25
# Let 25 be assigned to age
age = 25
# Comparison
# Is age equal to 25?
age == 25
# Evaluates to True or False
In [ ]:
"age" = 25
In [3]:
age = 25
print(age)
In [4]:
print(temperature)
In [ ]:
temperature = 74.5
print(tmperature)
In [5]:
# This program demonstrates a variable
room = 503
print('I am staying in room number')
print(room)
In [6]:
room = 503
# Using print () is proper Python 3 Syntax
# It's acceptable in Python 2
print('I am staying in room number', room)
# Usual way in Python 2 is without ()
# This is not legal in Python 3
print 'I am staying in room number', room
print('I am staying in room number ' + str(room))
# This gives a type error
# print('I am staying in room number ' + room)
print('I am staying in room number %s') % room
print('I am staying in room number'),
# In Python 3
# print('I am staying in room number', end="")
print(room)
In [ ]:
# This is a Python comment
# It's actually pretty similar to a hashtag
In general, comments should provide rationale
Should NOT repeat code
Variable name should reflect its use
Recommended style for Python is to use nouns separated by underscores
In [ ]:
# Are these variable names legal or illegal?
units_per_day
daysOfWeek
3dGraph
June1997
Mixture%3
In [7]:
name = raw_input('What is your name? ') # For Python 3, use input()
print(name)
In [ ]:
In [8]:
# A program that repeats a user-inputed string twice
jacob_said = raw_input("What did you say?")
jacob_said = jacob_said + " " + jacob_said
print(jacob_said)
In [ ]:
In [ ]:
#Input the hours worked
#Input the hourly pay
#Calculate the gross pay as hours worked multiplied by pay rate
#Display the gross pay
In [ ]: