Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple if-else statement in Python

I am writing a script that, if an array's elements are the subset of the main array, then it print PASS, otherwise, it print FAIL instead.

What should I add to my if-else statement below to make it works?

a = [1,2,3,4,5]
b = [1,2]
c = [1,9]

# The Passing Scenario 

if (i in a for i in b):
 print "PASS"
else:
 print "FAIL"

# The Failing Scenario

if (i in a for i in c):
 print "PASS"
else:
 print "FAIL"
like image 525
user3431399 Avatar asked Mar 19 '14 08:03

user3431399


People also ask

What is if-else statement in Python with example?

The elif clause in Python In case else is not specified, and all the statements are false , none of the blocks would be executed. Here's an example: if 51<5: print("False, statement skipped") elif 0<5: print("true, block executed") elif 0<3: print("true, but block will not execute") else: print("If all fails.")

What is simple if-else statement?

The if/else if statement allows you to create a chain of if statements. The if statements are evaluated in order until one of the if expressions is true or the end of the if/else if chain is reached. If the end of the if/else if chain is reached without a true expression, no code blocks are executed.

What is if-else statement with example?

Definition and Usage. The if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed. The if/else statement is a part of JavaScript's "Conditional" Statements, which are used to perform different actions based on different conditions.

What is short hand if-else in Python?

Shorthand If and If Else in Python Shorthand if and if else is nothing but a way to write the if statements in one line when we have only one statement to execute in the if block and the else block.


1 Answers

Use all.

# the passing scenario
if all(i in a for i in b):
    print 'PASS'
else:
    print 'FAIL'

# the failing scenario
if all(i in a for i in c):
    print 'PASS'
else:
    print 'FAIL'
like image 90
Jayanth Koushik Avatar answered Sep 28 '22 23:09

Jayanth Koushik