From 1c6f8f0a3e12a15889db7e048c7340927fd2fd6a Mon Sep 17 00:00:00 2001 From: govind Date: Mon, 14 Oct 2019 20:00:35 +0530 Subject: [PATCH] added alternate method in solution_02.py --- code/solutions/solution_02.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/code/solutions/solution_02.py b/code/solutions/solution_02.py index f330ad0..84606e1 100644 --- a/code/solutions/solution_02.py +++ b/code/solutions/solution_02.py @@ -1,12 +1,12 @@ # This is a possible solution for exercise_02.py # Explanation: In line 7 the variable state gets assigned a string -# based on the evaluation result of 'number % 2'. -# If the number is odd, then 'number % 2' is 1, hence True. -# If the number is even, then 'number % 2' is 0, hence False. -# 0 = False, all other numbers = True +# based on the evaluation result of 'number & 1'. +# If the number is odd, then 'number &1' is True. +# If the number is even, then 'number & 1' is False. +# ANDing with 1 is useful for large numbers number = int(input("Enter a number: ")) -state = "odd" if number % 2 else "even" +state = "odd" if number & 1 else "even" print(f"The number you entered is {state}.")