杰瑞科技汇

python if not in or

Of course! This is a very common point of confusion for Python developers. Let's break down if not in and the or operator, and then combine them.

python if not in or-图1
(图片来源网络,侵删)

The in and not in Operators

These are membership operators. They are used to check if a value exists (or does not exist) within a sequence (like a list, tuple, string, or set).

in

Checks if a value is present in a sequence. It returns True if it is, and False otherwise.

# Example with a list
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)  # Output: True
print("grape" in fruits)   # Output: False
# Example with a string
message = "Hello, world!"
print("world" in message)  # Output: True
print("goodbye" in message) # Output: False

not in

This is the direct opposite. It checks if a value is not present in a sequence. It returns True if it's absent, and False if it's present.

# Example with a list
fruits = ["apple", "banana", "cherry"]
print("grape" not in fruits)  # Output: True
print("banana" not in fruits) # Output: False
# Example with a string
message = "Hello, world!"
print("goodbye" not in message) # Output: True

The or Operator

The or operator is a logical operator. It's used to combine two or more boolean expressions. The result of an or expression is True if at least one of the expressions is True.

python if not in or-图2
(图片来源网络,侵删)
Expression 1 Expression 2 Expression1 or Expression2
True True True
True False True
False True True
False False False

A common use case is to provide a default value if a variable might be "falsy" (like None, an empty string , or an empty list []).

name = None
# If 'name' is falsy (like None or ""), use "Guest"
greeting = name or "Guest"
print(greeting)  # Output: Guest
name = "Alice"
# If 'name' is truthy (like "Alice"), use its value
greeting = name or "Guest"
print(greeting)  # Output: Alice

Combining Them: if not in or

Now let's put it all together. The expression if not in or is used when you want to execute a block of code if one of two conditions is met:

  1. A value is not in a list (using not in).
  2. OR another condition is true (using or).

The General Structure

if (value not in my_list) or (some_other_condition):
    # code to run if the value is NOT in the list,
    # OR if the other condition is true,
    # OR if both are true.

Practical Example

Let's say you're creating a program to check user permissions. A user can access a file if:

  • Their name is not on the banned_users list.
  • OR they are an admin.
banned_users = ["Charlie", "Diana", "Eve"]
user_role = "editor"
user_name = "Frank"
# Let's break down the condition:
# Condition 1: user_name not in banned_users
#   "Frank" not in ["Charlie", "Diana", "Eve"]  -> This is True
#
# Condition 2: user_role == "admin"
#   "editor" == "admin" -> This is False
#
# The full condition is: True or False
# In Python, True or False evaluates to True.
if (user_name not in banned_users) or (user_role == "admin"):
    print(f"Access granted for {user_name}.")
else:
    print(f"Access denied for {user_name}.")
# --- Output ---
# Access granted for Frank.

Let's try another example where the second condition is true.

python if not in or-图3
(图片来源网络,侵删)
banned_users = ["Charlie", "Diana", "Eve"]
user_role = "admin"  # Changed role to admin
user_name = "Diana"  # A banned user
# Let's break down the condition:
# Condition 1: user_name not in banned_users
#   "Diana" not in ["Charlie", "Diana", "Eve"] -> This is False
#
# Condition 2: user_role == "admin"
#   "admin" == "admin" -> This is True
#
# The full condition is: False or True
# In Python, False or True evaluates to True.
if (user_name not in banned_users) or (user_role == "admin"):
    print(f"Access granted for {user_name}.")
else:
    print(f"Access denied for {user_name}.")
# --- Output ---
# Access granted for Diana.

As you can see, even though Diana is banned, she was granted access because she is an admin, and the or condition was met.

Summary

Operator(s) Purpose Example Result
in Check for membership (is the value present?) "a" in ["a", "b", "c"] True
not in Check for non-membership (is the value absent?) "d" not in ["a", "b", "c"] True
or Combine conditions (is at least one condition true?) False or True True
if not in or Execute code if a value is absent OR if a separate condition is true. if ("x" not in [1,2,3]) or (5 > 10): True
分享:
扫描分享到社交APP
上一篇
下一篇