CS50P week0 学习笔记 - 更新完成

写在最前面,python 官方网站:

docs.python.org

week 0 的 problem set 都比较简单,这里就不把代码放出来了

week 0 Functions, Variables

Functions

python 里面有大量不同的函数,函数里有可以输入的参数,例如下面 hello.py 文件里:

print("hello, world")
# print 是函数,"hello, world" 则是这个函数的参数

Variables

变量是在程序中可以存储值的容器(A variable is just a container for a value within your own program)

修改 hello.py 程序:

name = input("What's your name? ") 
# 这个函数 input("What's your name? ") 返回的值赋值给 name,在这里 name 就是变量
print("hello, ")
print(name)

Comments

在程序中需要添加注释,让其他人能更快了解程序的功能

# 询问名字 单行注释
'''
多行注释 或者 双引号
'''
name = input("What's your name? ")
print("hello, ")
print(name)

Pseudocode

当不知道如何写代码时,可以先写伪代码,即仅将注释写出,然后再根据注释完成程序

# Ask the user for their name
name = input("What's your name? ")

# Print hello
print("hello,")

# Print the name inputted
print(name)

Further Improving Your First Python Program

  • 第一种修改方案

    # 询问名字
    name = input("What's your name? ")
    
    # 打印 hello 及输入的名字
    print("hello, " + name)
    

    上面用的 【+】是将左右两边的字符串连接起来,可以再进行添加

  • 第二种修改方案

    # 询问名字
    name = input("What's your name? ")
    
    # 打印 hello 及输入的名字
    print("hello," , name)
    

    这里使用【,】添加得是两个参数,第一个是【“hello,”】,第二个是【name】中间会自动加上一个空格

Strings and Paremeters

字符串在 python 里用 str 表示,是一串字符

通过查找官方文档,可以发现 print 函数里有一个 end 参数

https://docs.python.org/3/library/functions.html#print

print(*objects, sep=' ', end='\n', file=None, flush=False)
# end 参数只在函数末尾添加

所以可以修改代码如下:

# 询问名字
name = input("What's your name? ")

# 打印 hello 及输入的名字
# 参考 python 官方文档
print("hello, " , end="")
print(name)

问题:如果想在引号中再输入引号,该如何解决?

两种写法如下:

# 第一种写法
print("hello, 'friend'")

# 第二种写法
print("hello, \"friend\"")

Formatting Strings

上述 hello.py 还有一种更加优雅的写法:

# 询问名字
name = input("What's your name? ")

# 打印 hello 及输入的名字
print(f"hello, {name}")

More on Strings

参考文档:https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str

当用户输入并不是标准时,例如:名字前后有空格,该如何解决?

通过查询参考文档,发现有一个能删除字符串两边空格的函数【strip()】,代码修改如下:

# 询问名字
name = input("What's your name? ")

# 删除两边的空格
name = name.strip()

# 打印 hello 及输入的名字
print(f"hello, {name}")

image-20240127173325551

输入的名字又没有大写:

# 询问名字
name = input("What's your name? ")

# 删除两边的空格
name = name.strip()

# 大写首字母
name = name.capitalize()

# 打印 hello 及输入的名字
print(f"hello, {name}")

image-20240127173505050

但是若输入多个名字时,并不会将每个名字的开头字母大写

image-20240127173614250

# 询问名字
name = input("What's your name? ")

# 删除两边的空格
name = name.strip()

# 大写首字母
name = name.title()

# 打印 hello 及输入的名字
print(f"hello, {name}")

image-20240127173825466

更加简洁的写法:

# 询问名字,并进行格式修改
name = input("What's your name? ").strip().title()

# 打印 hello 及输入的名字
print(f"hello, {name}")

Int

建立一个新的 calculator.py 文件,里面的代码如下:

x = input("What's x ? ")
y = input("What's y ? ")

z = x + y

print(z)

运行结果如下:

image-20240127201437565

因为 【input()】 接收输入返回的数据是字符串,这里【+】是将两个字符串连接起来。

在 python 里,可以将一种数据类型转换成另一种数据类型:

x = input("What's x ? ")
y = input("What's y ? ")

z = int(x) + int(y)

print(z)

可以简化上述代码为:

x = int(input("What's x ? "))
y = int(input("What's y ? "))

print(x + y)

先执行括号里面的【input()】函数得到结果,再将结果传给外面的函数执行【int()】

这两种写法均可,各有各的优势,根据需要进行权衡即可

Float Basic

浮点值是带有小数点的实数,更改代码如下:

x = float(input("What's x ? "))
y = float(input("What's y ? "))

print(x + y)

image-20240130113250862

如果想对得出来的结果取到最近的整数,可以用到 round 函数:

image-20240130113437169

x = float(input("What's x ? "))
y = float(input("What's y ? "))

z = round(x + y)

print(z)

如果想格式化输出更大的数字,该如何解决?比如 不输出1000, 而要求输出 1,000。可以更改代码如下:

x = float(input("What's x ? "))
y = float(input("What's y ? "))

z = round(x + y)

print(f"{z:,}")

image-20240130114315249

More on Floats

在 python 中整数可以无限大,但对于浮点数来说却有精度的限制。对于小数的四舍五入,可以修改代码如下:

x = float(input("What's x ? "))
y = float(input("What's y ? "))

z = round(x / y, 2)

print(z)
    
# 另一种写法
x = float(input("What's x ? "))
y = float(input("What's y ? "))

z = x / y

print(f"{z:.2f}")

image-20240130115220487

Def

创建自己的函数:

def hello(to):
    print("hello,", to)

name = input("What's your name? ")
hello(name)
        
# 当调用 hello(name) 时,计算机将 name 传递给 hello 函数

image-20240130170607098

当函数的参数有默认值时,不传递参数就会使用默认值:

def hello(to="world"):
    print("hello,", to)

name = input("What's your name? ")
hello(name)
hello()

image-20240130170739118

但是,通常我们不会将自定义的函数放置在最开始的位置,一般先写的是主函数,在主函数中调用自定义的函数:

def main():
    name = input("What's your name? ")
    hello(name)
    hello()

def hello(to="world"):
    print("hello,", to)

image-20240130171120878

这样写还会有一个问题:当我们运行程序时,什么都不会发生。这时因为在这个函数中没有对 main 函数进行调用。将程序修改如下即可:

def main():
    name = input("What's your name? ")
    hello(name)
    hello()

def hello(to="world"):
    print("hello,", to)
        
main()

image-20240130171141952

Returning Values

函数是可以有返回值,例如先前使用的 input 函数,自定义函数同样如此。修改 calculator.py 文件:

def main():
    n = int(input("What's the number? "))
    print(f"n squared is {square(n)}")

# 求某个数的平方
def square(n):
    return n ** 2

main()

image-20240130171649124

  • 19
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值