GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — mini-python-codes/check_even_odd.py at main · OlgaEle/mini-python-codes

Expand file tree

/

check_even_odd.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
99 lines (74 loc) · 3.29 KB
/

check_even_odd.py

Copy path

File metadata and controls

99 lines (74 loc) · 3.29 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 🔍 1st method using modulus (%) operator
number = int(input("Enter a number: "))
if number % 2 == 0:
print(f"{number} is even")
else:
print(f"{number} is odd")
# 🔍 2nd method using bitwise AND operator (&)
number = int(input("Enter a number:"))
if number & 1: # this uses the least significant bit (LSB) to determine odd/even
# If the least significant bit is 1, the number is odd
# If the least significant bit is 0, the number is even
# For example, 5 in binary is 101, and the LSB is 1, so it's odd
# I find this method exciting :)
print(f"{number} is odd")
else:
print(f"{number} is even")
# 🔍 3rd method using functions (reusable code)
def is_even(n):
return n % 2 == 0
def is_odd(n):
return n % 2 != 0
def check_number(n):
if is_even(n):
return f"{n} is even"
elif is_odd(n):
return f"{n} is odd"
number = int(input("Enter a number: "))
print(check_number(number))
# 🔍 4th method using a lambda function
check_even_odd = lambda n: f"{n} is even" if n % 2 == 0 else f"{n} is odd" # is good for quick checks
number = int(input("Enter "))
print(check_even_odd(number))
# 🔍 5th method using a list comprehension
def check_even_odd_list(numbers): # this method is useful for checking multiple numbers at once
return [f"{n} is even" if n %2 == 0 else f"{n} is odd" for n in numbers]
numbers = [int(x) for x in input("Enter numbers separated by space: ").split()] # the .split() method splits the input string into a list of strings (based on spaces by default)
print(check_even_odd_list(numbers))
# 🔍 6th method using a map function
def check_even_odd_map(numbers):
return list(map(lambda n: f"{n} is even" if n % 2 == 0 else f"{n} is odd", numbers)) # map applies the lambda function to each element in the numbers list
numbers = [int(x) for x in input ("Enter numbers separated by space: ").split()]
print(check_even_odd_map(numbers))
# 🔍 7th method using a filter function
def filter_even(numbers):
return list(filter(lambda n: n % 2 == 0, numbers)) #filter returns only the even numbers from the list
numbers = [int(x) for x in input("Enter numbers separated by spaces: ").split()]
print(filter_even(numbers))
# ⌛️ Compare methods by time taken
import time
def time_function(func, *args):
start_time = time.time()
result = func(*args)
end_time = time.time()
print(f"Time taken: {end_time - start_time:.8f} seconds")
def main():
number = int(input("Enter a number: "))
print("\nUsing modulus operator:")
time_function(lambda n: f"{n} is even" if n % 2 == 0 else f"{n} is odd", number)
print("\nUsing bitwise AND operator:")
time_function(lambda n: f"{n} is odd" if n & 1 else f"{n} is even", number)
print("\nUsing functions:")
time_function(check_number, number)
print("\nUsing lambda function:")
time_function(check_even_odd, number)
numbers = [int(x) for x in input("Enter numbers separated by space: ").split()]
print("\nUsing list comprehension:")
time_function(check_even_odd_list, numbers)
print("\nUsing map function:")
time_function(check_even_odd_map, numbers)
print("\nUsing filter function:")
time_function(filter_even, numbers)
if __name__ == "__main__":
main()
stop = input("Press Enter to exit...")
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.