写在最前面,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}")
输入的名字又没有大写:
# 询问名字
name = input("What's your name? ")
# 删除两边的空格
name = name.strip()
# 大写首字母
name = name.capitalize()
# 打印 hello 及输入的名字
print(f"hello, {name}")
但是若输入多个名字时,并不会将每个名字的开头字母大写
# 询问名字
name = input("What's your name? ")
# 删除两边的空格
name = name.strip()
# 大写首字母
name = name.title()
# 打印 hello 及输入的名字
print(f"hello, {name}")
更加简洁的写法:
# 询问名字,并进行格式修改
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)
运行结果如下:
因为 【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)
如果想对得出来的结果取到最近的整数,可以用到 round 函数:
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:,}")
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}")
Def
创建自己的函数:
def hello(to):
print("hello,", to)
name = input("What's your name? ")
hello(name)
# 当调用 hello(name) 时,计算机将 name 传递给 hello 函数
当函数的参数有默认值时,不传递参数就会使用默认值:
def hello(to="world"):
print("hello,", to)
name = input("What's your name? ")
hello(name)
hello()
但是,通常我们不会将自定义的函数放置在最开始的位置,一般先写的是主函数,在主函数中调用自定义的函数:
def main():
name = input("What's your name? ")
hello(name)
hello()
def hello(to="world"):
print("hello,", to)
这样写还会有一个问题:当我们运行程序时,什么都不会发生。这时因为在这个函数中没有对 main 函数进行调用。将程序修改如下即可:
def main():
name = input("What's your name? ")
hello(name)
hello()
def hello(to="world"):
print("hello,", to)
main()
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()