2022年5月19日学习笔记-基础语法(2022年5月19日学习笔记-基础语法)

Python学习笔记-基础语法1、int()函数:,我来为大家讲解一下关于2022年5月19日学习笔记-基础语法?跟着小编一起来看一看吧!

2022年5月19日学习笔记-基础语法(2022年5月19日学习笔记-基础语法)

2022年5月19日学习笔记-基础语法

Python学习笔记-基础语法

第七章 用户输入和while循环
  • 一、input()函数
  • 二、while循环
  • 三、使用while循环来处理列表和字典
一、input()函数

1、int()函数:

  • input()函数输出为字符串,使用int()将之转换为数值

age = input("请输入你的年龄:") print(age >= 19) 输出结果为: 请输入你的年龄:11 Traceback (most recent call last): File "D:\Python_Work\Python学习\Python编程-从入门到实践\练习.py", line 2, in <module> print(age >= 19) TypeError: '>=' not supported between instances of 'str' and 'int'

2、求模求商

方法

描述

%

求模,取余数。可取2模以判断奇偶

//

求商

>>> 10 // 2 5 >>> 10 % 2 0

二、while循环

prompt = "\nTell me something, and I will repeat it back to you: " prompt = "\nEnter 'quit' to end the program. " "\n" message = '' while message != 'quit': message = input(prompt) if message != 'quit': print(message) 输出结果为: Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. 你好帅啊 你好帅啊 Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. quit

1、使用标志高效运行while循环

prompt = "\nTell me something, and I will repeat it back to you: " prompt = "\nEnter 'quit' to end the program. " "\n" active = True #标志 while active: message = input(prompt) if message == 'quit': active = False else: print(message) 输出结果: Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. 你好帅啊 你好帅啊 Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. quit

2、使用break退出循环

prompt = "\nTell me something, and I will repeat it back to you: " prompt = "\nEnter 'quit' to end the program. " "\n" while True: city = input(prompt) if city == 'quit': break 输出结果为: Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. 你好帅啊 Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. quit

  • 在任何Python循环中都可以使用break语句
  • 可使用break语句来退出遍历列表或字典的for循环

3、在循环中使用continue

  • continue:回到循环开头,根据条件测试结果决定是否继续执行循环
  • 它不像break语句那样不再执行余下的代码并推出整个循环

current_number = 0 while current_number < 10: current_number = 1 if current_number %2 == 0: continue #如果符合条件测试,continue让他回到while重新执行,直到不符合条件测试,打印current_number print(current_number) 输出结果为: 1 3 5 7 9

三、使用while循环来处理列表和字典
  • for循环中不应修改列表,否则将导致Python难以跟踪其中的元素
  • 要在遍历列表的同时对其进行修改,可使用while循环

1、在列表之间移动元素

unconfirmed_users = ['alice','brian','candace'] confirmed_users = [] while unconfirmed_users: #列表空则为False current_user = unconfirmed_users.pop() print("Verifying user: " current_user.title()) confirmed_users.append(current_user) print("\nThe following users have been confirmed: ") for confirmed_user in confirmed_users: print(confirmed_user) 输出结果为: Verifying user: Candace Verifying user: Brian Verifying user: Alice The following users have been confirmed: candace brian alice

2、删除包含特定值得所有列表元素

pets = ['dog','cat','dog','goldfish','cat','rabbit','cat'] print(pets) while 'cat' in pets: pets.remove('cat') print(pets) 输出结果为: ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat'] ['dog', 'dog', 'goldfish', 'rabbit']

3、使用用户输入来填充字典

responses = {} polling_active = True while polling_active: name = input("\nWhat is your name?\n ") response = input("Which mountain would you like to climb someday? \n") responses[name] = response repeat = input("Would you like to let another person respond? (yes/no) \n") if repeat == 'no': polling_active = False print("\n--- Poll Results ---") for name, response in responses.items(): print("\n" name.title() " would like to climb " response ".\n") 输出结果为: What is your name? 小明 Which mountain would you like to climb someday? 黄山 Would you like to let another person respond? (yes/no) yes What is your name? 小刚 Which mountain would you like to climb someday? 泰山 Would you like to let another person respond? (yes/no) no --- Poll Results --- 小明 would like to climb 黄山. 小刚 would like to climb 泰山.

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页