Professional Documents
Culture Documents
ENGR 101.week 2 - HMA
ENGR 101.week 2 - HMA
Introduction to
Programming
Week 2
Values and Types
• Value e.g., a number, a letter, text, etc.
5, ‘a’, ‘hello world!’
• Values have types.
int 0, 10, 3, 7, 98, etc.
float 0.1, 4.2, etc.
str ‘a’, ‘ ’, ‘hello’, etc.
bool True, False
etc.
Values and Types
numbers. • _amount ✔
keywords. • class X
Operators and Operands
operand operator
Integer vs. Float Division
‘a’ + ‘b’ = ?
‘a’ * 3 = ?
Expressions
• An expression is a combination of values, variables, and
operators.
• A value by itself is an expression.
• A variable by itself is an expression.
3 + 5 # expression
5 # expression
print 42 # not an expression
amount = 7 # not an expression
Statements
NAME HEADER
BODY
def testing():
print “This is week 2.”
print “We are studying functions.”
STATEMENTS
Indentation
testing()
• Once defined, a function can be used inside other functions.
def repeat_function():
testing()
testing()
repeat_function()
Putting it all together
def testing():
print "This is week 2."
print "We are studying functions."
def repeat_function():
testing()
testing()
repeat_function()
Flow of execution
def print_twice(x):
print x
print x
print_twice(‘ENGR 211’)
Variables and Parameters
are Local
• first & last are arguments of the function student
• full is a local variable inside the function student
def student(first,last):
full = first + ‘ ’ + last
print_twice(full)
name1 = ‘red’
name2 = ‘kit’
student(name1, name2)
print full
Local Variables
def print_twice(x):
print x
print x
print full
Why Functions?
>>> int(’32’)
>>> int(‘hello’)
>>> int(‘2.39’)
>>> float(32)
>>> str(32.45)
Math functions
exercise
exercise
exercise
Some more Exercises
25% of 80 is 20.0
25% of 60 is 15.0
Some more Exercises
Write a function that takes four arguments x1, y1,
x2, y2 where each (x, y) pair refers to a point on
a 2D Cartesian plane and
prints the distance between the two
points(x1,y1)and(x2, y2).