




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A set of lecture notes for a computer science course, cpsc 121, taught in the fall of 2012. The notes cover topics such as comments, white space, operators, and expressions in python programming. The instructor, s. Bowers, discusses the importance of using descriptive variable names, commenting code, and proper use of white space. The notes also include examples and rules for using operators and writing expressions.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Today ...
Homework
Reminder
Comments are special notes you can add to your programs
It’s always a good idea to comment your code
Regular comments start with a # in Python
percentage = (minute * 100) / 60
percentage = (minute * 100) / 60 # percentage of an hour
Comments are useful when they highlight non-obvious features v = 5 # assign 5 to v
v = 5 # velocity in meter/second
You should always try to choose descriptive variable and function names
Put at least one blank line between header and function definitions
Incorrect:
def f(x): print(x)
Correct:
def f(x): print(x)
Put a blank line between function definitions
Incorrect: def f(x): print(x) def g(x): print(x)
Correct:
def f(x): print(x)
def g(x): print(x)
Don’t put white space before or after function ()’s
Incorrect:
def f (x) : print(x)
Correct:
def f(x): print(x)
Put white space between arguments/parameters
Incorrect:
def f(x,y): print(x,y)
Correct:
def f(x, y): print(x, y)
Expressions are statements that always evaluate to (return) a value
Expressions can combine values, variables, and operators
Examples of expressions
What should the value of the last expression be?
Beware the Python interpreter! (common mistake)
>>> 1 + 1 2
An aside – What happens if we enter the following: >>> 12 = 3* SyntaxError: can’t assign to literal
Precendence rules define the order of operator evaluation
This spells PEMDAS