📜  关键字中的Python

📅  最后修改于: 2022-05-13 01:54:25.318000             🧑  作者: Mango

关键字中的Python

在编程中,关键字是语言的“保留字”,它向解释器传达特殊含义。它可以是命令或参数。关键字不能用作程序片段中的变量名。 Python语言还保留了一些传达特殊含义的关键字。在Python中,关键字区分大小写。

“在”关键字:

in 关键字有两个用途:

  • 检查列表、元组、范围、字符串等中是否存在值。
  • 在 for 循环中遍历一个序列。

句法:

# Using if statement
if ele in seq:
    statement(s)

# Using for statement
for ele in seq:
    statement(s)

示例 1:

Python3
# Python program to demonstrate
# in keyword
 
 
# Create a list
animals = ["dog", "lion", "cat"]
 
# Check if lion in list or not
if "lion" in animals:
    print("Yes")


Python3
# Python program to demonstrate
# in keyword
 
 
# Create a string
s = "GeeksforGeeks"
 
# Iterating through the string
for i in s:
     
    # if f occurs in the string
    # break the loop
    if i == 'f':
        break
     
    print(i)


输出:

Yes

示例 2:

Python3

# Python program to demonstrate
# in keyword
 
 
# Create a string
s = "GeeksforGeeks"
 
# Iterating through the string
for i in s:
     
    # if f occurs in the string
    # break the loop
    if i == 'f':
        break
     
    print(i)

输出:

G
e
e
k
s