Concepts / Working with Strings, Integers, and Floats

Working with Strings, Integers, and Floats

The operation performed by the function determines which argument types are suitable. The function does not need to ask whether its argument is a string or a number before printing it.

  • Programming

One Function, Several Value Types

A function does not always require one fixed data type. Its compatibility depends on what the function does with its argument. The function print_twice demonstrates this idea: it prints its argument two times, so it can work with a string, an integer, or a floating-point number as long as the value can be printed.

Key point

The operation performed by the function determines which argument types are suitable. The function does not need to ask whether its argument is a string or a number before printing it.

print_twiceprints its argument twotimesSpamstringprintingcompatible operation17integer3.141592653589793floating-point numberargumentargumentargumentperforms
How can the same function accept a string, an integer, and a float?

Tracing Each Call

What do you think happens?

What happens when print_twice receives a string, then an integer, then a floating-point number?

  • Only the string call succeeds

  • All three calls print their argument twice

  • The function changes every argument into an integer

  • The function prints each argument only once

Reveal answer
Answer:

All three calls print their argument twice

The function performs the same operation in every call: it prints the value bound to arg, then prints arg again. Strings, integers, and floating-point numbers can all be printed.

print_twice('Spam')callargSpamSpam, Spamprinted twiceprint_twice(17)callarg1717, 17printed twiceprint_twice(math.pi)callarg3.1415926535897933.141592653589793printed twicebinds valueprints twicebinds valueprints twicebinds valueprints twice
What changes when the same function receives three different argument types?

Binding Before Printing

When print_twice('Spam') is called, the string 'Spam' is bound to the parameter arg. The function then uses arg in its body two times. First it prints the value represented by arg, and then it prints the same value again. Nothing in this process requires the function to test whether arg is a string.

The integer call follows the same execution pattern. In print_twice(17), the integer 17 is bound to arg. The function prints 17 and then prints 17 again. The function's behavior has not changed; only the value supplied to the parameter has changed.

The floating-point call works in the same way. In print_twice(math.pi), arg is bound to 3.141592653589793, and that value is printed two times. The print function knows how to convert these values to a string for display.

CallValue bound to argOperationResult
print_twice('Spam')'Spam'Print arg two times'Spam' appears two times
print_twice(17)17Print arg two times17 appears two times
print_twice(math.pi)3.141592653589793Print arg two times3.141592653589793 appears two times

The value changes between calls, but the function's operation remains the same.

Operation-Based Compatibility

Definition

A data type is compatible with a function when the function's operations can successfully be performed on values of that type.

String'Spam'Printingfunction operationSuccessful outputvalue printed twiceInteger17Float3.141592653589793can be printedcan be printedcan be printedsucceeds
Which part of the function determines whether strings, integers, and floats can be used?

To determine whether another type will work, inspect the function's operations rather than guessing from the parameter name. For print_twice, the relevant operation is printing. Test the value with that operation and check whether every operation in the function succeeds.

Checking for Failure

The fact that print_twice accepts strings, integers, and floats does not mean that every possible value must work. Its requirement is that the value be printable. The source explains that strings, integers, floats, booleans, and many other types can be printed, while a value that the print function cannot handle would cause the function to fail.

Argument valueCan be printedPrinted twiceoperations succeedFunction failsprinting cannot handle thevaluepass to functionyesno
Where does execution go when a value is passed to a function that prints it twice?

Common Reasoning Mistakes

  • Common mistake

    Assuming a function accepts only the type used in its first example.

    What it looks like

    Seeing print_twice('Spam') and concluding that print_twice accepts strings only.

    Why it fails

    The function's operation is printing, and the same operation also works with the integer 17 and the floating-point value 3.141592653589793.

    Do this instead

    Inspect what the function does with its argument and check whether that operation works with the proposed type.

  • Common mistake

    Expecting the function to behave differently because the argument type changes.

    What it looks like

    Predicting that print_twice(17) will perform a different sequence of operations from print_twice('Spam').

    Why it fails

    In both calls, the value is bound to arg and then printed two times.

    Do this instead

    Trace the binding and the function body separately. The value changes, but the operation sequence stays the same.

  • Common mistake

    Assuming every data type is automatically compatible.

    What it looks like

    Believing that any value can be passed to print_twice without checking whether printing can handle it.

    Why it fails

    The constraint is that the value must be printable. A value that the print function cannot handle would cause failure.

    Do this instead

    Test the type with the function's operations and verify that all of them succeed.

Practice the Trace

EASY

For each call, identify the value bound to arg, state the operation performed by print_twice, and predict what appears as output: print_twice('Hello'), print_twice(42), and print_twice(2.5). Then explain why the same reasoning applies to all three calls.

Hints
  • Start by identifying the argument value in each call.
  • The function performs the same operation twice for every call.
  • Ask whether the value can be printed.
Worked example

Tracing a New String Argument

Predict the behavior of print_twice('Hello').

  1. 1
    Bind

    The string 'Hello' is bound to the parameter arg.

  2. 2
    First operation

    The function prints the value represented by arg.

  3. 3
    Second operation

    The function prints arg again.

Result

The string 'Hello' is printed two times because it can be handled by the function's printing operation.

Key Takeaways

  1. 1
    A function can accept different data types when its operations are compatible with those types.
  2. 2
    print_twice works with strings, integers, and floats because each value can be printed.
  3. 3
    Changing the argument changes the value bound to the parameter, not the function's sequence of operations.
  4. 4
    To verify compatibility, test the argument type and check whether every operation in the function succeeds.

At a glance

  • Function compatibility comes from the operations performed on an argument.
  • print_twice prints strings, integers, and floating-point numbers two times.
  • Tracing a call means following the argument into the parameter and then through each operation.
  • A type is suitable only when every operation required by the function can handle its value.

More concepts starting with W