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
 
#定义密码列表并赋值给变量
password_list = ['*#*#', '12345']
#定义函数
def account_login():
    password = input('Password:') #用户输入赋值变量
    password_correct = password == password_list[-1] #使用列表索引取值进行布尔运算并赋值变量
    password_reset = password == password_list[0]
    if password_correct: #条件成立打印信息
        print('Login success!')
    elif password_reset: #反之,password_reset条件成立(即输入*#*#)
        new_password = input('Enter a new password:') #用户输入赋值变量
        password_list.append(new_password) #列表追加数据
        print('Your password has changed successfully!')
        account_login() #继续调用函数
    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> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
Password:*#*#
Enter a new password:hahaha
Your password has changed successfully!
Password:hahaha
Login success!
PS C:\Users\harveymei>
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>