Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

ENGR 101

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

type(‘Hello World’) str


type(17) int
type(3.2) float
type(’17’) str
Variables

• A name that refers to a value.


message = ‘hello’
n = 17
pi = 3.1415926535897931
• use print to display the value of a variable.
• use type to check the type of a variable.
Variable names and
keywords
• amount  ✔

• Variable names can be • amount1  ✔


arbitrarily long. • amount_1  ✔

• Names contain both letters and • food_amount  ✔

numbers. • _amount  ✔

• Underscore (_) character can • _1amount  ✔

appear in a name. • Amount  ✔

• Variable names cannot start • 1amount X

with a number. • amount.of.food X

• Don’t use Python reserved • food-amount X

keywords. • class X
Operators and Operands

• Operators are special symbols that represent


computations like addition and multiplication.
• The values that the operator is applied to are called
operands.
• +,-,/,*,**
a+b

operand operator
Integer vs. Float Division

• When both operands are integers, / is a floor division.


3/5=? 5/3=? 6/3=? 6/4=?
• If either operands is a floating-point number, the result is
a float.
3 / 5.0 = ? 5 / 3.0 = ? 6.0 / 3.5 = ? 6 / 4.0 = ?
Order of Operations

• When multiple operators appears in an expression, the


order of evaluation depends on the rules of precedence.
4*5–1/3/2*9+1=?
• P E M D A S: parentheses, exponentiation,
multiplication & division, addition & subtraction.
 Same precedence: LEFT  RIGHT
String Operations

‘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

• Statement is a unit of code that Python interpreter can


execute.
• Anything that makes a line or multiple lines of Python
code.
• Each expression is a statement as well.
3+5 # statement and expression
print 42 # statement
a=7 # statement
Comments
# this is a comment.
print a # this is another comment.
# here is a multi-line comment
"""
comment
comment
Comment
"""
Functions

• When you define a function, you specify the name and


the sequence of statements.
• You can “call” the function by name.
Adding new functions
• The header has to end with a colon

NAME HEADER
BODY
def testing():
print “This is week 2.”
print “We are studying functions.”

STATEMENTS
Indentation

• No indentation is an end of the function


Adding new functions

• Defining a function creates a variable with the same name.


def testing():
print “This is week 2.”
print “We are studying functions.”

testing()
• Once defined, a function can be used inside other functions.
def repeat_function():
testing()
testing()
repeat_function()
Putting it all together

# function definitions have to come before use

def testing():
print "This is week 2."
print "We are studying functions."

def repeat_function():
testing()
testing()

repeat_function()
Flow of execution

• Execution starts from top, and proceeds to bottom in


order.
• Function definitions do not alter the flow of execution.
However, a function is not executed unless called
explicitly.
• A function call is like a detour in the flow of execution.
Parameters and Arguments

• Functions can have arguments.


e.g. Built in functions:
 math.sin(0.25) -sin function with 1 argument
 math.pow (2,3) -pow function with 2 arguments
• Inside the function, these arguments are assigned to
variables called parameters.
Parameters and Arguments
x is an argument to the function print_twice

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?

• Make a program smaller by eliminating repetitive code.


 write once, debug, and reuse many times
 if you make a change, you only have to make it in
one place.
• Dividing a long program into functions allows you
 to debug the parts one at a time and then assemble
them into a working whole.
Type conversion

>>> int(’32’)
>>> int(‘hello’)
>>> int(‘2.39’)
>>> float(32)
>>> str(32.45)
Math functions

>>> import math

• in order to access one of the functions defined in the


math module, you need to use the dot notation.
>>> math.sqrt(4)
Composition

• Wherever you can put a value, you can put an


arbitrary expression.

>>> x = math.sin(degrees/360.0 * 2 * math.pi)

EXCEPT: The left hand side of an assignment!


In-class Assignment
+-----+-----+
| | |
| | |
| | |
| | |
+-----+-----+
• Exercise 5 – Part 1 (Chapter 3)
| | |
| | |
| | |
| | |
+-----+-----+
Some more Exercises
Write a function that takes a string and an
integer as input (string, y). The function should
print the string y times,and each time the string
should be written in a new line.

e.g: print_str('exercise',3) should return below:

exercise
exercise
exercise
Some more Exercises

Write a function called percentage that takes


the percentage for two given numbers.

e.g: percentage(80,60,25) should return:

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).

You are encouraged to use functions from the math


module.

e.g. cartes(3,4,5,6) should give 2.828

You might also like