Negarea condițiilor compuse cu not
În această lecție vei învăța cum să inversezi rezultatul unei condiții compuse folosind operatorul not și paranteze.
Ce face operatorul not?
Operatorul not inversează rezultatul unei condiții: True devine False, iar False devine True.
is_blocked = True print(not is_blocked)
False
Negarea unui grup de condiții
Pentru a nega o condiție compusă, grupăm verificările între paranteze și scriem not înaintea grupului.
if not (
candidate["city"] in blocked_cities
or candidate["level"] in blocked_levels
):
print(candidate["name"]) Candidatul este selectat doar dacă nu este adevărat că orașul sau nivelul său se află într-o listă blocată.
Cum funcționează?
city = "Cluj"
level = "Junior"
blocked_cities = ["Iasi", "Brasov"]
blocked_levels = ["Intern"]
result = not (
city in blocked_cities
or level in blocked_levels
)
print(result) True
Nici orașul, nici nivelul nu sunt blocate. Grupul dintre paranteze este fals, iar not îl transformă în adevărat.
Exemplu complet
candidates = [
{"name": "Ana", "city": "Bucuresti", "level": "Senior"},
{"name": "Mihai", "city": "Iasi", "level": "Junior"},
{"name": "Ioana", "city": "Cluj", "level": "Intern"},
{"name": "Radu", "city": "Cluj", "level": "Junior"}
]
blocked_cities = ["Iasi", "Brasov"]
blocked_levels = ["Intern"]
selected_candidates = []
for candidate in candidates:
if not (
candidate["city"] in blocked_cities
or candidate["level"] in blocked_levels
):
selected_candidates.append(candidate)
for candidate in selected_candidates:
print(candidate["name"], candidate["city"]) Ana Bucuresti Radu Cluj
Cum este evaluat fiecare candidat?
Ana este selectată deoarece Bucuresti nu este blocat, iar nivelul Senior nu este blocat.
Mihai nu este selectat deoarece Iasi se află în lista orașelor blocate.
Ioana nu este selectată deoarece nivelul Intern se află în lista nivelurilor blocate.
Radu este selectat deoarece nici orașul, nici nivelul său nu sunt blocate.
Model general
Sintaxăfiltered_items = []
for item in items:
if not (
first_condition
or second_condition
):
filtered_items.append(item) Exemplu cu produse
products = [
{"name": "Laptop", "brand": "Lenovo", "condition": "New"},
{"name": "Mouse", "brand": "Unknown", "condition": "New"},
{"name": "Monitor", "brand": "Samsung", "condition": "Used"}
]
blocked_brands = ["Unknown"]
blocked_conditions = ["Used"]
selected_products = []
for product in products:
if not (
product["brand"] in blocked_brands
or product["condition"] in blocked_conditions
):
selected_products.append(product)
for product in selected_products:
print(product["name"]) Laptop
Greșeli frecvente
Uiți parantezele
if not city in blocked_cities or level in blocked_levels:
În această formă, operatorul not se aplică doar primei verificări.
Pui not înaintea fiecărei valori
if not city in blocked_cities or not level in blocked_levels:
Această expresie folosește or între două negări și nu exprimă aceeași regulă.
Folosești and în interior
if not (city in blocked_cities and level in blocked_levels):
Această condiție respinge elementul doar atunci când ambele valori sunt blocate.
Rezumat
GrupeazăPune condițiile între paranteze.
NeagăScrie not înaintea grupului.
InverseazăRezultatul întregului grup este inversat.
Creează lista blocked_statuses cu valorile "Cancelled" și "Refunded". Creează lista blocked_countries cu valorile "Blocked" și "Restricted". Creează lista goală selected_orders.
Exercițiul, editorul și verificarea automată se deblochează cu Python Avansat.

Alex Cristian
Iulian Pahom
Catalina Ioana Dan