python基础练习,循环、列表、字典、数组
# encoding: utf-8 ''' @author: mark @file: 20200214.py @time: 2020/2/14 14:21 @software: PyCharm ''' ''' #初始学习python print("hello word") #打印hello word price = 10 #初始化变量price rating = 4.9 #初始化变量rating name = "mark" #初始化变量name is_published = False #初始化变量is_published print(price) #打印price ''' ''' #控制台输入使用 name = input('what is your name? ') #控制台输入值赋值给name favorite_color = input("what is your favorite color ? ") #控制台输入值赋值给favorite_color print("Hi " + name + "your facorite_color " + favorite_color) #打印Hi name your facorite_color favorite_color ''' ''' #控制台输入使用 birth_year = input("Birth year: ") #控制台输入值赋值给birth_year print(type(birth_year)) #打印控制台输入值的类型 age = 2020 - int(birth_year) #根据控制台输入的值计算年龄 print(type(age)) #打印年龄的类型 print(age) #打印年龄 ''' ''' #控制台输入使用,以及加减乘除运算 weight_lbs = input('Weight (lbs): ') #控制台输入值赋值给weight_lbs weight_kg = float(weight_lbs) * 0.45 #weight_lbs 转换为公斤 print(weight_kg) #打印体重kg weight_kg = input('Weight (kg): ') #控制台输入体重kg weight_lbs = float(weight_kg) / 0.45 #体重公斤转换为磅 print(weight_lbs) #打印体重 磅 print(10 / 3) #除法,带小数点 print(10 // 3) #取商 print(10 % 3) #取余 print(10 ** 3) #次方 x = 10 #初始化X x += 3 #X自加3 X = X + 3 print(x) #打印X x -= 3 #X自减3 X = X - 3 print(x) #打印# x = 10 + 3 ** 3 ** 2 # 同理 x = 3 ** (3 ** 2) print(x) ''' ''' #字符串的截取 course = "Python's Course for Beginners " #初始化字符串 print(course) #打印字符串 print(course[0]) #打印字符串第一个字符 print(course[0:3]) #打印字符串第一个到第四个字符 print(course[1:]) #打印从第二个字符开始的整个字符串 print(course[:5]) #打印开始的五个字符串 print(course[-2]) #打印倒数第二个字符 print(course[1:-1]) #打印第二个字符到倒数第二个字符 ''' ''' #f字符串使用,调用变量 first = 'John' #初始化变量first last = 'Smith' #初始化变量last message = first + ' [' + last + '] is a coder' #字符串拼接 print(message) #打印拼接的字符串 msg = f'{first} [{last}] is a coder' #使用f字符串进行拼接字符串 print(msg) #打印拼接的字符串 ''' ''' #字符串操作 course = 'Python for Beginners' #初始化字符串 print(course.find('Beginners')) #查找第一次出现Beginners时的脚标 print(course.replace('Beginners', 'Absoulte Beginners')) #Absoulte Beginners替换字符串中的Beginners print('Python' in course) #查看Python是否在字符串中 print(len(course)) #查看字符串的长度 print(course.upper()) #字符串全部大写 print(course.lower()) #字符串全部小写 ''' ''' #math计算 import math #引入math x = 2.9 #初始化X print(round(x)) #四舍五入 print(abs(-x)) #绝对值 print(math.ceil(x)) #向上取整 print(math.floor(x)) #向下取整 ''' ''' #if语句 is_hot = True #初始化is_hot is_cold = True #初始化is_cold if is_hot : #判断是否为真 print("It's a hot day") #打印It's a hot day print("Drink plenty of water") #打印Drink plenty of water elif is_cold: #判断是否为真 print(" It's a cold day") #打印It's a cold day print("Wear warm clothes") #打印Wear warm clothes else: #其他情况 print("It's a lovely day") #打印It's a lovely day print(" Enjoy your day") #打印Enjoy your day price = 1000000 #初始化price has_good_credit = True #初始化has_good_credit if has_good_credit: #判断has_good_credit是否为真 down_payment = 0.1 * price #运算 else: #其他情况 down_payment = 0.2 * price #运算 print(f"Down payment: ${down_payment}") #打印 Down payment: $down_payment ''' ''' #if语句以及逻辑连接 has_high_income = True #初始化has_high_income has_good_credit = True #初始化has_good_credit has_criminal_record = False #初始化has_criminal_record if has_good_credit and has_high_income: #has_good_credit和has_high_income是否同时为真 print("has_good_credit and has_high_income Eligible for loan") #打印has_good_credit and has_high_income Eligible for loan if has_good_credit or has_high_income: #has_good_credit和has_high_income是否有一个为真 print("has_good_credit or has_high_income Eligible for loan") #打印has_good_credit or has_high_income Eligible for loan if has_good_credit and not has_criminal_record: #has_good_credit为真且has_criminal_record为假 print("Eligible for loan") #打印Eligible for loan temperature = 30 #初始化temperature if temperature > 30: #temperature > 30是否为真 print("It's a hot day") #打印It's a hot day else: #其他 print("It's not a hot day") #打印It's not a hot day #名字长度判断 name = "John smith" #初始化name if len(name) < 3: #判断len(name) < 3是否为真 print("Name must be at least 3 characters.") #打印 elif len(name) > 50: #判断len(name) > 50是否为真 print("Name must be a maximum of 50 characters.") #打印 else: #其他情况 print("Name looks good!") #打印 #体重转换 weight = int(input('Weight: ')) #对控制台输入的值进行类型转换 unit = input('(L)bs or (K)g: ') #控制台输入值赋值给unit if unit.upper() == "L": #unit.upper() == "L"是否为真 converted = weight * 0.45 #运算 print(f"You are {converted} kilos") #打印You are converted kilos else: #其他情况 converted = weight / 0.45 #运算 print(f"You are {converted} kilos") #打印You are converted kilos ''' ''' #while循环语句 i = 1 #初始化i while i <= 5: #i <= 5是否为真 print('*' * i) #打印* i += 1 #自加 print("Done") #打印Done #猜数字 secret_number = 9 #初始化secret_number guess_count = 0 #初始化guess_count guess_limit = 3 #初始化guess_limit while guess_count < guess_limit: #guess_count < guess_limit是否为真 guess = int(input('Guess: ')) #控制台输入赋值给guess guess_count += 1 #自加 if guess == secret_number: #guess == secret_number是否为真 print('You win!') #打印You win! break #跳出循环 else: #其他 print('Sorry , you failed!') #打印Sorry , you failed! ''' ''' #while循环和if语句结合的案例 #开车,①quit为退出,help为帮助信息,start为开始,stop为结束,②且已经开启的不能再次开启 command = "" #初始化command用以判断是否跳出循环 started = False #初始化started,默认车是关闭状态,防止车被重复开启 while command.lower() != "quit": #判断是否可以跳出循环 command = input("> ").lower() #控制台输入值赋给command if command.lower() == "help": #判断是否需要帮助 print('start/stop/quit') #输出帮助信息 elif command.lower() == 'start': #判断是否为开车信息 if started: #车是否已经开启 print("car is already start") #打印car is already start else: #车没有开启 started = True #变更车的状态信息 print('Start the car') #打印Start the car elif command.lower() == 'stop': #判断是否为停车信息 if not started: #车是否已经停 print("car is already stopped") #打印car is already stopped else: #车没有停 started = False #变更车的状态信息 print('stop the car ') #打印stop the car elif command == "quit": #判断是否为退出信息 break #跳出循环 else: #其他情况 print("don't understand") # 打印don't understand ''' ''' #循环遍历 prices = [10, 20, 30] #初始化prices total = 0 #设定遍历的初始值 for price in prices: #遍历的条件 total += price #自加 print(f"Total:{total}") #输出遍历结果 ''' ''' #所有坐标 for x in range(4): #设置循环条件 for y in range(3): #设置二级循环条件 print(f'({x},{y})') #输出结果 numbers = [5, 3, 5, 3, 3] #设置初始numbers for x_count in numbers: #设置循环条件 # print('X' * x) output = '' #输出值初始设置 for count in range(x_count): #设置输出值循环条件 output += 'X' #输出特征设置 print(output) #打印输出 #数组列表 names = ['John', 'Bob', 'Mosh', 'Sarah'] #设置初始列表names print(names) #打印输出列表 print(names[0]) #打印第一个元素 print(names[2:]) #打印第三个到最后一个元素 print(names) #查询列表中的最大值 numbers = [3, 6, 2, 8, 4, 10] #设置初始列表numbers max = numbers[0] #设置最大值的初始值 for number in numbers: #设置循环条件 if number > max: #进行判断是否为最大值 max = number #赋值 print(max) #输出最大值 ''' ''' #数组操作 matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ] #初始化数组matrix print(matrix[0][2]) #打印输出数组中第一个元素中的第三个元素 for row in matrix: #设置循环列表中的第一层 for low in row: #设置循环列表中的第二层 print(low) #输出打印 ''' ''' #列表中的操作 numbers = [5, 2, 1, 5, 7, 4] #初始化列表 numbers print(numbers.sort()) #反向排序 numbers.reverse() #按照从大到小排列 print(numbers) #打印 numbers.append(14) #列表中加入新的值 print(numbers) #打印 numbers.insert(2, 100) #在列表的第三个位置加入100 print(numbers) #打印 numbers.remove(2) #去除列表中的2 print(numbers) #打印 numbers2 = numbers.copy() #赋值 print(numbers2) #打印 numbers.pop() #去除末尾的值 print(numbers) #打印 print(numbers.index(1)) #列表中的第二个值 print(numbers.count(5)) #列表中出现5的次数 numbers.clear() #清除列表 print(numbers) #打印 #清楚列表中的重复项 numbers = [5, 2, 3, 3, 2, 5, 23, 32] #初始化列表numbers for number in numbers: #设置循环条件 if numbers.count(number) != 1: # 查询是否在列表里唯一 numbers.remove(number) #移除重复的元素 # numbers.append(number) # print(numbers) #打印没有重复值的列表 uniques = [] #设置一个空列表 for number in numbers: #设置循环条件 if number not in uniques: #设置判断条件 uniques.append(number) #将符合条件的元素添加到空列表中 print(uniques) #打印新的列表 ''' ''' #字典 customer = { "name": "John Smith", "age": 30, "is_verified": True } #初始化字典customer customer["name"] = "Mark" #更改对应key的value print(customer["name"]) #打印对应key的value print(customer["is_verified"]) #打印对应key的value print(customer.get("name")) #打印对应key的value ''' ''' #字典应用,输入数字对应英文 digits_mapping = { "1": "One", "2": "Two", "3": "Three", "4": "Four", "5": "Five", "6": "Six", "7": "Seven", "8": "Eight", "9": "Nine", } #初始化字典digits_mapping phone = input("Phone: ") #控制台输入 output = '' #初始化输出 for ch in phone: #设置循环条件 output += digits_mapping.get(ch, "!") + " " #获取对应字典中的value print(output) #打印 '''