Lecture 04 Useful Functions & Comments

Joseph Haugh

University of New Mexico

Free Recall

  • Get out a sheet of paper or open a text editor
  • For 2 minutes write down whatever comes to mind about the last class
    • This could be topics you learned
    • Questions you had
    • Connections you made

Setting Up CS Circles

  • Sign up for a free account on CS Circles
  • This is mandatory
  • Next set jhaugh as your “guru”
    • Click “Edit my profile” in the top right menu
    • Set the guru field to jhaugh
  • I will now be able to see your progress and give you credit in Canvas

Setting Up OnlineGDB

  • In addition to CS Circles we will also be using a platform called OnlineGDB
  • This platform will facilitate in class exercises
  • Sign up here
  • Then join the course here

Exercise: Baker’s Math

  • Now you should be able to see an exercise called “Baker’s Math”
  • Complete this exercise now
  • You are welcome to discuss code with those at your table

Functions

  • For now think of functions as code someone else wrote that you can use
  • For example, the print() function or the input() function
  • There are many more that Python provides you

Function Arguments

  • Most functions take arguments
  • These arguments allow you to change the behavior of the function
  • For example,
    • When you give print a string argument it simply prints that string
    • When you give print a variable argument it looks at the value inside and prints that

Multiple Function Arguments

  • Many functions take more than 1 argument
  • For example, print may be called with more than 1 argument
  • Each argument is separated by a comma
  • print with multiple arguments prints each argument and separates each with a space in the output

CODE

x = 5
y = 6
print(x, y, "!")

Visualize

OUTPUT

5 6 !

More Useful Functions: Min and Max

  • The min function takes in any amount of numbers as arguments and returns the minimum
  • The max function takes in any amount of numbers as arguments and returns the maximum

CODE

x = 5
y = 50
z = 500
print(min(x, y, z))
print(max(x, y, z))
print(min(x, y, z, 1))
print(max(x, y, z, 1000))

Visualize

OUTPUT

5
500
1
1000

Common Errors

  • There a number of common errors that new programmers make when calling functions:

CODE

max()

MISTAKE

Not enough arguments

OUTPUT

TypeError: max expected 1 arguments, got 0

max(3, 5

Forgot closing paren )

SyntaxError: unexpected EOF while parsing (, line 1)

max(3, x)

Forgot to define variable x

NameError: name ‘x’ is not defined

Comments

  • Comments are invaluable tool for help other programmers understand your code
  • They are ignored by Python when running your code
  • You can start a comment with a hashtag/pound sign (#)
  • For example:
# This is a comment
# It is not read by the program
# It is only for humans to read
print("Hello") # A comment can be here too
# The next line of code will not run
# print("world")

Visualize

Escape Sequences

  • Sometimes you need to insert a special character into a string
  • This could be a newline, a tab, or even just a quote
  • To do this you need to use the backslash (\)
  • For example:
print("Hello\nWorld") # Prints Hello World on two separate lines
print("Hello\tWorld") # Prints Hello World with a tab between
print("\"Hello World\"") # Prints "Hello World"

Visualize

Chapter 2 & 3

Take the remaining time to complete all of the exercises in chapters 2 & 3