9 月 232020
 
#位置参数和关键词参数
#自定义函数,求梯形面积,上底,下底,高。
def trapezoid_area(base_up, base_down, height):
    return 1/2 * (base_up + base_down) * height

#先给变量赋值,再调用函数
base_up = 1
base_down = 2
height = 3

#比较
print(trapezoid_area(1, 2, 3))

#函数中的参数在传入之前已经有了具体值,因此这是位置参数传入而不是关键词参数传入
print(trapezoid_area(height, base_down, base_up))
#print(trapezoid_area(3, 2, 1))
# 1/2 * (3 + 2) * 1 即 2.5,而不是4.5 
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
4.5
2.5
PS C:\Users\harveymei> 
9 月 232020
 
#位置参数和关键词参数
#自定义函数,求梯形面积,上底,下底,高。
def trapezoid_area(base_up, base_down, height):
    return 1/2 * (base_up + base_down) * height

#位置参数
print('在函数调用中依次传入参数值的方式称为位置参数')
print('print(trapezoid_area(1,2,3))')
print(trapezoid_area(1,2,3))

#关键词参数
print('在函数调用中传输参数和赋予参数的值的方式称为关键词参数')
print('print(trapezoid_area(base_up=2, base_down=3, height=3))')
print(trapezoid_area(base_up=2, base_down=3, height=3))

#函数中引用参数时可以混合使用位置参数和关键词参数
#仅使用关键词参数时,参数顺序可以打乱
#混合使用参数时,位置参数必须在关键词参数之前

trapezoid_area(height=3, base_down=2, base_up=1) #正确
#trapezoid_area(height=3, base_down=2, 1) #错误
#SyntaxError: positional argument follows keyword argument
#trapezoid_area(base_up=1, base_down=2, 3) #错误
#SyntaxError: positional argument follows keyword argument
trapezoid_area(1, 2, height=3) #正确
trapezoid_area(1, height=3, base_down=2) #正确
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
在函数调用中依次传入参数值的方式称为位置参数
print(trapezoid_area(1,2,3))
4.5
在函数调用中传输参数和赋予参数的值的方式称为关键词参数
print(trapezoid_area(base_up=2, base_down=3, height=3))
7.5
PS C:\Users\harveymei>
9 月 222020
 
#Python算数运算符
a = 10
b = 15
# + 加
print('a + b =', a + b)
# - 减
print('a - b =', a - b)
# * 乘
print('a * b =', a * b)
# / 除
print('a / b =', a / b)
print('b / a =', b / a)
# % 取模,返回出除法的余数,余数为5
print('b % a =', b % a)
# ** 冥,返回x的y次方
print('a ** b =', a ** b)
# // 取整除,除法返回商的向下取整
print('b // a =', b // a)
print('16 // 7 =', 16 // 7)
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
a + b = 25
a - b = -5
a * b = 150
a / b = 0.6666666666666666
b / a = 1.5
b % a = 5
a ** b = 1000000000000000
b // a = 1
16 // 7 = 2
PS C:\Users\harveymei>
9 月 162020
 
#函数print()与关键字return
def fahrenheit_converter(C):
    fahrenheit = C * 9 / 5 + 32
#    return fahrenheit + 'F'
#禁用自定义函数的return返回,调用print()函数
    print(str(fahrenheit) + 'F')

#开始调用自定义函数
C2F = fahrenheit_converter(35)
print(C2F)
#输出的结果,第一行为指定的print()函数的打印输出
#第二行为调用fahrenheit_converter()函数的输出,函数中无return关键字,所以返回值为空
#Python中的return是可选的
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
95.0F
None
PS C:\Users\harveymei>
9 月 162020
 
PS C:\Users\harveymei> python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def fahrenheit_converter(C):
...     fahrenheit = C * 9 / 5 + 32
...     return str(fahrenheit) + 'F'
...
>>> fahrenheit_converter(30)
'86.0F'
>>> C = 40
>>> fahrenheit_converter(C)
'104.0F'
>>> exit()
PS C:\Users\harveymei> 

#自定义函数,摄氏度转换为华氏度
#Celsius 2 Fahrenheit
def fahrenheit_converter(C):
    fahrenheit = C * 9 / 5 + 32
    return str(fahrenheit) + 'F'
# 变量fahrenheit为float数值,与字符串F合并,需要使用str()函数转换
# 以下开始调用函数
# 摄氏温度35度,使用转换函数并赋值给变量C2F
C2F = fahrenheit_converter(35)
#打印C2F值
print(C2F)
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
95.0F
PS C:\Users\harveymei> 
不使用str()函数处理的float数值与字符串合并异常
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
Traceback (most recent call last):
  File "c:/Users/harveymei/hello.py", line 9, in <module>
    C2F = fahrenheit_converter(35)
  File "c:/Users/harveymei/hello.py", line 5, in fahrenheit_converter
    return fahrenheit + 'F'
TypeError: unsupported operand type(s) for +: 'float' and 'str'
PS C:\Users\harveymei>
9 月 152020
 

已使用函数的简单总结

print()
print是一个放入对象就能将结果打印的函数
input()
input是一个可以让用户输入信息的函数
len()
len是一个可以测量对象长度的函数
int()
int是一个可以将字符串类型的数字转换成整数类型的函数

Python 3.8内建函数列表

https://docs.python.org/3/library/functions.html
+---------------+-------------+--------------+--------------+----------------+
| abs()         | delattr()   | hash()       | memoryview() | set()          |
+---------------+-------------+--------------+--------------+----------------+
| all()         | dict()      | help()       | min()        | setattr()      |
+---------------+-------------+--------------+--------------+----------------+
| any()         | dir()       | hex()        | next()       | slice()        |
+---------------+-------------+--------------+--------------+----------------+
| ascii()       | divmod()    | id()         | object()     | sorted()       |
+---------------+-------------+--------------+--------------+----------------+
| bin()         | enumerate() | input()      | oct()        | staticmethod() |
+---------------+-------------+--------------+--------------+----------------+
| bool()        | eval()      | int()        | open()       | str()          |
+---------------+-------------+--------------+--------------+----------------+
| breakpoint()  | exec()      | isinstance() | ord()        | sum()          |
+---------------+-------------+--------------+--------------+----------------+
| bytearray()   | filter()    | issubclass() | pow()        | super()        |
+---------------+-------------+--------------+--------------+----------------+
| bytes()       | float()     | iter()       | print()      | tuple()        |
+---------------+-------------+--------------+--------------+----------------+
| callable()    | format()    | len()        | property()   | type()         |
+---------------+-------------+--------------+--------------+----------------+
| chr()         | frozenset() | list()       | range()      | vars()         |
+---------------+-------------+--------------+--------------+----------------+
| classmethod() | getattr()   | locals()     | repr()       | zip()          |
+---------------+-------------+--------------+--------------+----------------+
| compile()     | globals()   | map()        | reversed()   | __import__()   |
+---------------+-------------+--------------+--------------+----------------+
| complex()     | hasattr()   | max()        | round()      |                |
+---------------+-------------+--------------+--------------+----------------+
9 月 152020
 
#字符串格式化符,不同写法,效果相同
print('{} a word she can get what she {} for.'.format('With','came'))
#对字符串中的“空”使用.format()处理
print('{preposition} a word she can get what she {verb} for'.format(preposition = 'With',verb = 'came'))
#字符串中的“空”值可以是变量
print('{0} a word she can get what she {1} for.'.format('With','came'))
#字符穿中的“空”值按顺序填入
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
With a word she can get what she came for.
With a word she can get what she came for
With a word she can get what she came for.
PS C:\Users\harveymei>
#字符串格式化符
city = input("write down the name of city:")
#通过用户输出传入变量,使用变量值作为字符串中的填空值
url = "http://apistore.baidu.com/microservice/weather?citypinyin={}".format(city)
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
write down the name of city:
PS C:\Users\harveymei> 
9 月 152020
 
search = '168'
num_a = '1386-168-0006'
num_b = '1681-222-0006'

print(search + ' is at ' + str(num_a.find(search) + 1) + ' to ' + str(num_a.find(search) + len(search)) + ' of num_a ')
# 168 is at str(num_a.find(search) + 1) to str(num_a.find(search) + len(search)) of num_a
# 168 is at str(5 + 1) to str(5 + 3) of num_a
# 168 is at 6 to 8 of num_a
print(search + ' is at ' + str(num_b.find(search) + 1) + ' to ' + str(num_b.find(search) + len(search)) + ' of num_b ')
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
168 is at 6 to 8 of num_a 
168 is at 1 to 3 of num_b
PS C:\Users\harveymei>
9 月 152020
 
#字符串的方法method
phone_number = '138-1234-5678'
hiding_number = phone_number.replace(phone_number[4:8], '*' * 4)
#第3-7个字符,不含第7个字符,即1234,替换为*,连续4个
print(hiding_number)
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
138-****-5678
PS C:\Users\harveymei>
9 月 152020
 
#找到朋友中的魔鬼
word = 'friends'
find_the_evil_in_your_friends = word[0] + word[2:4] + word[-3:-1]
#f + ie + nd
print(find_the_evil_in_your_friends)
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
fiend
PS C:\Users\harveymei>