9 月 252020
 
>>> #多条件比较,先给变量赋值,再多条件比较
>>> middle = 5
>>> 1 < middle < 10
True
>>> #变量比较
>>> two = 1 + 1
>>> three = 1 + 3
>>> two < three
True
>>> #字符串比较
>>> 'Eddie Van Helen' == 'eddie van helen'
False
>>> #两个函数结果比较
>>> abs(-10) > len('length of this word')  
False
>>> #不同类型的对象不能使用“<”,“>”,“<=”,“>=”进行比较,但可以使用“==”,“!=”
>>> 42 > 'the answer'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'int' and 'str'
>>> 42 == 'the answer'
False
>>> 42 != 'the answer'
True
>>> 5.0 == 5
True
>>> 3.0 > 1
True
>>> # True = 1 与 False = 0
>>> True > False
True
>>> True + False > False + False
True
>>>
9 月 252020
 
>>> #布尔类型的数据只有两种,True和False
>>> #但凡能够产生一个布尔值的表达式称为布尔表达式
>>> 1 > 2
False
>>> 1 < 2 < 3
True
>>> 42 != '42'
True
>>> 'Name' == 'name'
False
>>> 'M' in 'Magic'
True
>>> number = 12
>>> number is 12
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
True
>>>
>>> #对于比较运算符,如果比较式成立则返回True,不成立则返回False
>>> #“==”左右两边等值
>>> #“!=”左右两边不相等
>>> #“>”左边大于右边
>>> #“<”左边小于右边
>>> #“<=”左边小于或等于右边
>>> #“>=”左边大于或等于右边
9 月 252020
 
#自定义函数,指定两个默认参数
def create_file(filename, content, censored_content = 'lame', changed_content = 'Awesome'):
    full_path = 'C:/Users/harveymei/Desktop/'
    full_name = full_path + filename + '.txt'
    file = open(full_name,'w')
    #在写入操作时对传入内容参数进行匹配替换
    #file.write(content)
    file.write(content.replace(censored_content, changed_content))
    file.close()
    print('Done')

#调用函数创建文件,传入文件名参数及内容参数
create_file('helloworld', 'Python is lame!')
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
Done
PS C:\Users\harveymei> type .\Desktop\helloworld.txt
Python is Awesome!     
PS C:\Users\harveymei> 
#自定义函数,敏感词删剪替换
#指定参数censored_word和changed_word默认值
def text_filter(word, censored_word = 'lame', changed_word = 'Awesome'):
    #替换word中匹配的censored_word为changed_word
    #返回replace()处理后的结果
    return word.replace(censored_word, changed_word)

#自定义函数,写文件
def create_file(filename, content):
    full_path = 'C:/Users/harveymei/Desktop/'
    full_name = full_path + filename + '.txt'
    file = open(full_name,'w')
    file.write(content)
    file.close()

#自定义函数,调用以上两个函数,分别替换关键词,写文件
def censored_create_file(filename, content):
    #调用敏感词删剪函数text_filter()先替换内容
    clean_content = text_filter(content)
    #调用生成文件函数create_file(),使用替换过的内容变量clean_content作为参数传入
    create_file(filename, clean_content)

#调用函数创建文件,传入文件名参数及内容参数
censored_create_file('helloworld', 'Python is lame!')
print('Done')
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
Done
PS C:\Users\harveymei> type .\Desktop\helloworld.txt
Python is Awesome!     
PS C:\Users\harveymei> 
9 月 252020
 
#自定义函数,敏感词删剪
#指定参数censored_word和changed_word默认值
def text_filter(word, censored_word = 'lame', changed_word = 'Awesome'):
    #替换word中匹配的censored_word为changed_word
    #返回replace()处理后的结果
    return word.replace(censored_word, changed_word)

#调用函数,只传入参数word,函数返回对参数word的替换操作后内容
print(text_filter('Python is lame!'))
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
Python is Awesome!
PS C:\Users\harveymei> 
9 月 252020
 
#定义一个函数,需要传入参数文件名称name和文件内容msg
def text_create(name, msg):
    #定义路径变量并赋值
    desktop_path = 'C://Users/harveymei/Desktop/'
    #定义文件完整路径,路径+文件名称+扩展名后缀
    full_path = desktop_path + name + '.txt'
    #open()函数,打开文件,传入参数为文件完整路径
    #写入模式,如果没有该文件就创建文件,如果有该文件就覆盖写入
    file = open(full_path, 'w')
    #写入传入的参数msg
    file.write(msg)
    #关闭文件
    file.close()
    print('Done')

#调用函数
#创建文件C://Users/harveymei/Desktop/hello.txt,内容为hello world
text_create('hello','hello world')
#创建文件C://Users/harveymei/Desktop/list.txt,内容为中加入换行符实现换行
text_create('list','123456\n654321')
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
Done
Done
PS C:\Users\harveymei> type .\Desktop\hello.txt
hello world
PS C:\Users\harveymei> type .\Desktop\list.txt
123456
654321
PS C:\Users\harveymei> 
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 月 232020
 
#自定义函数,a*a+b*b=c*c,开平方求c值
def Pythagorean_theorem(a,b):
    return 'The right triangle third side\'s length is {}'.format((a**2 + b**2)**(1/2))
#引号内的字符串出现符号时需要使用\转义
#使用.format()将格式化的运算值填入字符串中

#调用函数,传入参数并打印函数返回
print(Pythagorean_theorem(3,4))
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
The right triangle third side's length is 5.0
PS C:\Users\harveymei> 
9 月 232020
 
#6的2次方
print(6**2)
#6的4次方
print(6**4)
#36的2次方
print(36**2)
#开平方
#1296的1/2次方
print((6**4)**(1/2))
#1296的1/4次方
print((6**4)**(1/4))
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
36
1296
1296
36.0
6.0
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>