mardi 26 février 2019

multiple condition in if statement error in python [duplicate]

This question already has an answer here:

I didn't understand the if statement in python

x = "qqq @aaa test word anyword #bbb"
y = x.split()
for word in y:
    if '@' in word:
        print(word)

The output is "@aaa" but i want to add other condition,

x = "qqq @aaa test word anyword #bbb"
y = x.split()
for word in y:
    if '@' or '#' in word:
        print(word)

The output is all of list.

when I add parenthesis;

x = "qqq @aaa test word anyword #bbb"
y = x.split()
for word in y:
    if ('@' or '#') in word:
        print(word)

The output is "@aaa" ,again

Why? what is the logic of if statement? How can write more than one statement in if block?

Aucun commentaire:

Enregistrer un commentaire