Lab1
1 celsius_to_fahrenheit.py
running result:
1.1
run python -i celsius_to_fahrenheit.py
- there’s no error message
1.2
run celsius_to_fahrenheit(100)
- the result is 212.0
- it meets the expectation
1.3
write more function calls of the celsius_to_fahrenheit
function with different arguments
- different arguments are tried as you can see in the screenshot above
- every result matches the expectation
2 division.py
running result:
2.1
- run
division(1,2)
: the result is 0.5 - run
division(2,1)
: the result is 2.0
the results are different
2.2
It tells me that the order in which the arguments are passed to the function matters.
In this given division function, the first argument is assigned to the dividend parameter, and the second argument is assigned to the divisor parameter.
Swapping the arguments changes the values assigned to dividend and divisor, which in turn affects the result of the division operation. This demonstrates that the order of the arguments influences the behavior and outcome of the function.
3 circle_property.py
running result:
3.1
2 values are being returned by this function
4 hello_returning.py
running result:
4.1
As shown in the screenshot above:
This function can’t be called with different parameters like previous functions that have been done in this lab. (e.g. run hello_returning(1)
→ \rightarrow → TypeError: hello_returning() takes 0 positional arguments but 1 was given)
But there still exists other way to call this function, for example:
function_reference = hello_returning
function_reference() # Output: 'hello'
- In this case,
function_reference
becomes a reference to thehello_returning
function, and you can call it using the variablefunction_reference
followed by parentheses.
5 hello_printing.py
running result:
5.1
- the result of function call of
hello_printing
: hello - the result of function call of
hello_returning
: ‘hello’
the results are different
Because:
The return statement is used to send a value back from a function to the caller. When you call the hello_returning()
function, it returns the string 'hello'
. So the result is a string with quotation marks to indicate that it is a string type.
On the other hand, the print statement is used to display output directly to the console. When you call the hello_printing()
function, it uses the print statement to directly print the string "hello"
to the console without returning a value. Therefore, the printed output does not include quotation marks.