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>