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
 
>>> #布尔类型的数据只有两种,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
 
#自定义函数,敏感词删剪
#指定参数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>