Python入门 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 the hello_returning function, and you can call it using the variable function_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.

相关推荐

  1. 6.824-Lab 1: MapReduce

    2024-07-09 23:12:04       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 23:12:04       50 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 23:12:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 23:12:04       43 阅读
  4. Python语言-面向对象

    2024-07-09 23:12:04       54 阅读

热门阅读

  1. 深入解析Spring Boot的application.yml配置文件

    2024-07-09 23:12:04       23 阅读
  2. Docker技术简介

    2024-07-09 23:12:04       19 阅读
  3. Qt 的 qmake的语法简介与例子介绍

    2024-07-09 23:12:04       22 阅读
  4. C#用链表和数组分别实现堆栈

    2024-07-09 23:12:04       21 阅读
  5. Go bytes包

    2024-07-09 23:12:04       24 阅读
  6. C#面 :ASP.Net Core中有哪些异常处理的方案?

    2024-07-09 23:12:04       21 阅读
  7. Redis

    2024-07-09 23:12:04       22 阅读