Software Engineer
@ ISI Technology
UX Development Lead
@ BOLT System
This course has NO hard requirements. We do however recommend the following:
print "Hello, World!"class Hello {
public static void main (String[] args) {
System.out.println ("Hello, world.");
}
}Built with Python
Interpreted
Compiled
$ python
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 1 + 1
2$ python firstprogram.py
2# firstprogram.py
print 1 + 1
Types of errors (or "bugs")
Errors in computer programs are referred to as "bugs"
The process of tracking them down and fixing them is known as "debugging"
Syntax refers to the structure of a program and the rules about that structure.
For example, in English, a sentence must begin with a capital letter and end with a period.
this sentence contains a syntax error.
So does this one
The second type of error is a runtime error, so called because the error does not appear until you run the program.
These errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened.
The third type of error is the semantic error.
If there is a semantic error in your program, it will run successfully, in the sense that the computer will not generate any error messages, but it will not do the right thing. It will do something else. Specifically, it will do what you told it to do.
$ python
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>>>> print "Hello world!"
Hello World!message = "How are you?"
x = 5
pi = 3.14159Incorrect assignment
# don't do this
5 = xAssignment Statement
>>> type(message)
<type 'str'>
>>> type(n)
<type 'int'>
>>> type(pi)
<type 'float'>Variable types
>>> 76trombones = "big parade"
SyntaxError: invalid syntax
>>> more$ = 1000000
SyntaxError: invalid syntax
>>> class = "Computer Science 101"
SyntaxError: invalid syntaxReserved Keywords
Choose meaningful names for your variables. Choose clarity over brevity.
Which makes more sense?
x = "Bob"
or
name = "Bob"
number_of_students = 5
or
num = 5
Please feel free to work together on any of these exercises!
A statement is an instruction that the Python interpreter can execute.
We've seen two kinds of statements so far: print and assignment
print 1
x = 2
print xproduces the output:
1
2An expression is a combination of values, variables, and operators. If you type an expression on the command line, the interpreter evaluates it and displays the result:
>>> 1 + 1
2>>> x = 1 + 3
>>> x
4The evaluation of an expression produces a value, which is why expressions can appear on the right hand side of assignment statements. A value all by itself is a simple expression, and so is a variable.
In general, you cannot perform mathematical operations on strings, even if the strings look like numbers.
# The following are NOT VALID
message = "Hello"
message - 1
"Hello" / 123
message * "Hello"
"15" * 2Interestingly, the "+" operator does work with strings although it doesn't do what you might expect
fruit = "banana"
baked_good = " nut bread"
print fruit + baked_goodClick here to:
In the context of programming, a function is a named sequence of statements that performs a desired operation.
This operation is specified in a function definition
def NAME( LIST OF PARAMETERS ):
STATEMENTSdef say_hello():
print "Hello"
say_hello()Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
The first statement of a function can be an optional statement - the documentation string of the function or docstring.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.
def new_line():
printprint "First Line."
new_line()
print "Second Line."# output
First Line.
Second Line.print "First Line."
new_line()
new_line()
new_line()
print "Second Line."def three_lines():
new_line()
new_line()
new_line()
# since this line is not indented
# python knows it's not part of the
# function definition
print "First Line."
three_lines()
print "Second Line."
def new_line():
print
def three_lines():
new_line()
new_line()
new_line()
print "First Line."
three_lines()
print "Second Line."1
2
9
3
5
7
4
6
8
>>> abs(5)
5
>>> abs(-5)
5
>>> pow(2, 3)
8
>>> pow(7, 4)
2401
>>> max(7, 11)
11
>>> max(4, 1, 17, 2, 12)
17
>>> max(3 * 11, 5**3, 512 - 9, 1024**0)
503# example.py
# user-defined function with a parameter
def print_twice(param):
print param, param>>> from example import print_twice
>>> print_twice('Spam')
Spam Spam
>>> print_twice(5)
5 5
>>> print_twice(3.14159)
3.14159 3.14159
# We can also enter expressions for arguements
>>> print_twice('Spam' * 4)
SpamSpamSpamSpam SpamSpamSpamSpam
>>> print_twice(abs(-7))
7 7
>>> print_twice(max(3, 1, abs(-11), 7))
11 11