built-in functions (slides)

CSC 110 Python Functions

Built-in Functions

  • print()
  • round()
  • type()
  • input()
  • len()
  • int()
  • float()

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("Mickey", "Mouse") ) # Hello, Mickey Mouse!

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

Write a function

Write a Python function that does the following:

  1. Its name is greeting_again with no parameters
  2. It first prompts you to enter your first name
  3. It again prompts you to enter your last name
  4. It returns a string with the same greeting as greeting but replace with your first and last name
print( greeting_again() ) 

Write a function

def greeting(first, last):
    message = "Hello, " + first + " " + last + "!"
    return message

def greeting_again():
    first = input("Enter your first name:\n")
    last = input("Enter your last name:\n")
    message = "Hello, " + first + " " + last + "!"
    return message

def main():
    print(greeting("Mickey", "Mouse"))
    print(greeting_again())

main()

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("Mickey")
print(character_count)
6

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("hel", "lo", "world") ) # 10
print( count_characters("", "", "") ) # 0
print( count_characters(" ", " ", " ") ) # 3
print( count_characters("10", "2", "3") ) # 4

Write a function

def count_characters(a, b, c):
    return len(a) + len(b) + len(c)

def main():
    print( count_characters("hel", "lo", "world") ) # 10
    print( count_characters("", "", "") ) # 0
    print( count_characters(" ", " ", " ") ) # 3
    print( count_characters("10", "2", "3") ) # 4

main()
10
0
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)

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 calculate_year_born with no parameters. It prompts user to enter their age input().

It converts user’s age to integer and calculates (imperfectly) the year a person of age was born by subtracting age from 2025.

It returns an integer representing the approximate year person of age was born.

print( calculate_year_born() ) # user enters 60, function returns 1965

Write a function

def calculate_year_born():
    str_age = input("what's your age?\n")
    age = int(str_age)
    year_born = 2025 - age
    return year_born

def main():
    print(calculate_year_born())

main()