概述
python in关键字
关键字中的Python (Python in keyword)
in is a keyword (case-sensitive) in python, it is used to check whether an element exists in the given sequence like string, list, tuple, etc.
in是python 中的一个关键字(区分大小写),用于检查元素是否以给定的顺序存在,例如字符串,列表,元组等。
in keyword is also used with the for loop to iterate the sequence.
in关键字也与for循环一起使用以迭代序列。
Syntax of in keyword
in关键字的语法
if element in sequence:
statement(s)
for element in sequence:
statement(s)
Example:
例:
Input:
cities = ["New Delhi", "Mumbai", "Chennai", "Banglore"]
# condition
if "Mumbai" in cities:
print("Yes")
else:
print("No")
Output:
Yes
in关键字的Python示例 (Python examples of in keyword)
Example 1: Check whether an element exits in a list of not.
示例1:检查元素是否在列表中不存在。
# python code to demonstrate example of
# in keyword
# Check whether an element exits in a list of not
# create a list
cities = ["New Delhi", "Mumbai", "Chennai", "Banglore"]
# check if "Mumbai" is present in the list or not
if "Mumbai" in cities:
print("Yes "Mumbai" exists in the list")
else:
print("No "Mumbai" does not exist in the list")
# now check...
# "Gwalior" is present in the list or not
if "Gwalior" in cities:
print("Yes "Gwalior" exists in the list")
else:
print("No "Gwalior" does not exist in the list")
Output
输出量
Yes "Mumbai" exists in the list
No "Gwalior" does not exist in the list
Example 2: Take a string and print the characters one by one until a dot (".") is not found.
示例2:取一个字符串并逐个打印字符,直到找不到点(“。”)。
# python code to demonstrate example of
# in keyword
# Take a string and print the characters one by one
# until a dot ('.') is not found.
# string input
string = input("Enter a string: ")
# print characters until dot is not found
for ch in string:
if ch=='.':
break
print(ch)
Output
输出量
Enter a string: IncludeHelp.com
I
n
c
l
u
d
e
H
e
l
p
翻译自: https://www.includehelp.com/python/in-keyword-with-example.aspx
python in关键字
最后
以上就是机智彩虹为你收集整理的python in关键字_in关键字和Python示例的全部内容,希望文章能够帮你解决python in关键字_in关键字和Python示例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复