# Print a string
print "Hello World!"
# Strings can have either single or double quotes
print 'Hello'
# Printing an integer
print 1
# This is a single-line comment
# Addition
2 + 2
# Subtraction
10 - 3
# Multiplication
6 * 7
# Integer division - throws away remainder
15 / 4
# Float division
15./4
# Adding two integers gives an integer
1 + 1
# Adding a float and an integer gives a float
1. + 1
# How to convert an integer to a float
float(1)
# Exponent
2**5
# Remainder
26 % 5
# Variable on the left, value on the right
x = 1
# The variable x now has the value 1
print x
x = 1
X = 2
print x, X
# Can use the variables instead of the values
y = 2
z = x + y
print z
# "print" adds a newline - usa a comma to stop this
print x, y, z
print x,
print y,
print z
a, b = 3, 4
print a, b
# Swap a and b
a, b = b, a
print a, b
# An ugly way to do the same thing
c = a
a = b
b = c
print a, b
quiz_max = 10
quiz_ave = 9
quiz_percent = (quiz_ave/quiz_max) * 100
print "Quiz average =", quiz_percent, "%"
import math
# Need to prefix variables and functions with "math"
math.pi
math.sin(1)
math.cos(0)
from math import pi
# Don't need to preface "pi" anymore
pi
from math import *
# All math variables and functions are now available
pi
sin(1.5)
print dir(math)
? degrees
degrees(pi)
degrees(2*pi)
Out[38]
Out[38] + Out[39]
True
False
# Test for equality
3 == 2
# Test for inequality
3 != 2
# Greater than
3 > 3
# Greater than or equals
3 >= 3
# Less than
2 < 1
# Less than or equals
2 <= 1
if 3 > 2:
print "3 is greater than 2"
if 3 < 2:
print "3 is less than 2"
else:
print "3 is not less than 2"
grade = 88
if grade >= 90:
print 'A'
elif grade >= 80:
print 'B'
elif grade >= 70:
print 'C'
else:
print 'F'
n = 10
while n > 0:
print n
n -= 1
print "Blast Off!"
# Define the function
def add(x, y):
return x + y
# Call the function
add(10, 20)
# Can also add a documentation string
def add(x, y):
"Add x and y, return result"
return x + y
# Our documentation string now becomes part of the help system
? add
# a += b means a = a + b
a = 10
b = 5
a += b
print a
# a -= b means a = a - b
a = 10
b = 2
a -= b
print a
# a*= b means a = a*b
a = 55
b = 2
a *= b
print a
# a /= b means a = a / b
a = 10
b = 2
a /= b
print a
# "not" turns True into False and False into True
not 10 == 10
# "and" is only True if all of the boolean expressions are True
6 < 5 and 2 < 3
# "or" is True if any of the boolean expressions are True
6 < 5 or 2 < 3
# The "not" is evaluated first, then the "or"
not 3 < 2 or 3 > 2
# Gets evaluated as: (not False) or True
# Expressions in parentheses are evaluated first
not (3 < 2 or 3 > 2)
# Gets evaluated as: not(False or True)
# We can chain together as many logical operators as we like
False or False or True or False
x = 2
1 < x < 3
# The code below does the same thing, but is more complicated
1 < x and x < 3
This is basically the same as PEMDAS: Parentheses, Exponents, Multiply/Divide, Add/Subtract
In Python, the order is:
Operations at the same level are evaluated from left to right.
# The exponent is performed first, before the addition
3 + 6**2
# Use brackets to force evaluation in a particular order
(3 + 6)**2
# Execute all the code in the file "report1.py"
%run report1.py