10 月 102020
 
#for循环
for num in range(1,11):
    #range()函数,输出连续的整数序列,起始start,终止stop,不包含终止值
    #将range()函数的值,循环赋值给变量num
    #str()函数将整数转换为字符串,打印字符串,并拼接计算结果
    print(str(num) + '+ 1 =', num + 1)
	print(range(1,11))
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
1+ 1 = 2
2+ 1 = 3
3+ 1 = 4
4+ 1 = 5
5+ 1 = 6
6+ 1 = 7
7+ 1 = 8
8+ 1 = 9
9+ 1 = 10
10+ 1 = 11
PS C:\Users\harveymei> 
10 月 092020
 
#for循环
#用for循环打印出“Hello world”这段字符串中的每一个字符
#for关键词,every_letter可以容纳每一个元素的变量名称
for every_letter in 'Hello world':
    print(every_letter)
for number in '1 23 4 56 78 9':
    print(number)
PS D:\OneDrive\Python> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe d:/OneDrive/Python/hello.py
H
e
l
l
o

w
o
r
l
d
1

2
3

4

5
6

7
8

9
PS D:\OneDrive\Python> 
10 月 092020
 
#条件控制
#如果……条件是成立的,就做……,反之,就做……
#定义一个函数
def account_login():
    password = input('Password:') #将用户输入赋值给变量
    password_correct = password == '12345' #将密码布尔运算结果赋值给变量
    if password_correct: #如果条件成立
        print('Login success!')
    else: #反之,打印错误提示信息并再次调用函数
        print('Wrong password or invalid input!')
        account_login()
#调用函数        
account_login()
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
Password:54321
Wrong password or invalid input!
Password:12345
Login success!
PS C:\Users\harveymei>
10 月 092020
 
#条件控制
#如果……条件是成立的,就做……,反之,就做……
#定义一个函数
def account_login():
    password = input('Password:') #使用input获得用户输入字符串并存储在变量中
    if password == '12345': #条件
        print('Login success!')
    else: #反之条件
        print('Wrong password or invalid input!')
        account_login() #输出错误信息后继续调用函数供用户输入
#调用函数        
account_login()
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
Password:12345
Login success!
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
Password:54321
Wrong password or invalid input!
Password:12345
Login success!
PS C:\Users\harveymei>
10 月 092020
 
#布尔运算符and,or用于布尔值之间的运算
#not x:如果x是True则返回False,否则返回True
#x and y:如果x和y都是True则返回True,有一个是False则返回False
#x or y:如果x和y有一个是True则返回True,如果x和y都是False则返回False
print(1 < 3 and 2 < 5) #True
print(1 < 3 and 2 > 5) #False
print(1 < 3 or 2 > 5) #True
print(1 > 3 or 2 > 5) #False
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
True
False
True
False
PS C:\Users\harveymei>
10 月 092020
 
#赋值变量
the_Eddie = 'Eddie'
name = 'Eddie'
#打印比较结果
print(the_Eddie == name)
print(the_Eddie is name)
#在python中任何对象都可以判断其布尔值
#除了0、None和所有空的序列与集合布尔值为False外,其他都为True
print('bool(0)', bool(0))
print('bool([])', bool([]))
print('bool('')', bool(''))
print('bool(False)', bool(False))
print('bool(None)', bool(None))
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
True
True
bool(0) False
bool([]) False
bool() False
bool(False) False
bool(None) False
PS C:\Users\harveymei>
10 月 092020
 
#成员运算符Membership Operators(in)与身份运算符Indentify Operators(is)
#集合类型之列表
#字符串,浮点,整数,布尔类型,变量,甚至是另一个列表都可以存储在列表中
album1 = []
#创建一个非空列表
album2 = ['Black Star', 'David Bowie', 25, True]
#使用列表的append方法向列表中添加新的元素
album2.append('new song')
#使用列表索引打印列表中第一个和最后一个元素
print(album2[0], album2[-1])
#打印第二个和倒数第二个元素
#print(album2[1], album2[-2])
#测试字符串Black Star是否存在列表album2中
print('Black Star' in album2)
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
Black Star new song
True
PS C:\Users\harveymei> 
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>