Built-in Python Functions (class slides)

CSC 110 Python Functions

Announcements

Check your grade on D2l

You can request a regrade on Gradescope (DEMO)

Write a function

Write a Python function that does the following:

  1. Its name is greeting
  2. It takes two arguments, first_name and last_name
  3. It returns a string with a greeting using first_name and last_name
print( greeting("Adriana", "Picoral") ) # Hello, Adriana Picoral!

Reading the documentation

Access the Python 3.11 documentation and read the definitions for print(), round(), input(), len(), int(), float(), str().

With your table members, write a short definition for each of the built-in functions above on a white board.

len() function

  • The len() function can be used with many types – we will be using it with string for now
  • It returns the number of characters in a string
character_count = len("Adriana")
print(character_count)
7

Write a function

Write a Python function that does the following:

  1. Its name is count_characters
  2. It takes three string arguments, a, b and c
  3. It returns the total number of characters for all three strings
print( count_characters("Adri", "ana", "Picoral") ) # 14
print( count_characters("", "", "") ) # 0
print( count_characters(" ", " ", " ") ) # 3
print( count_characters("10", "2", "3") ) # 4

Quiz 03

current time

You have 10 minutes to complete the quiz.

Your formula in your function should be Python code (in other words, do not just copy the formula in the instructions, make it so it uses actual symbols for Python operators)

input() function

  • The input() function prompts the user to input text in the standard output
  • Whatever is inside the parentheses in input() will be written to the standard output (without a trailing newline, which you can add using \n).
  • The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that
  • input() always returns a string
input("What's your name?\n")

int() function

  • The int() function can be used to convert a string to an integer type
  • It only works if the string only contains digits
age = '35'
age_int = int(age)
print(type(age), type(age_int))
<class 'str'> <class 'int'>

float() function

  • The float() function can be used to convert a string to a float type
  • It only works if the string only contains digits and optionally a decimal point
age = '35'
age_float = float(age)
print(type(age), type(age_float))
<class 'str'> <class 'float'>

Write a function

Write a Python function that does the following:

  1. Its name is calculate_year_born, with no parameters
  2. It prompts user to enter their age input()
  3. It converts user’s age to integer (since input() always returns a string)
  4. It calculates (imperfectly) the year a person of age was born by subtracting age from 2024
  5. It returns an integer representing the approximate year person of age was born
print( calculate_year_born() ) # user enters 43, function returns 1981