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内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复