Here’s a simple Python program that takes an input value from the user and checks whether the number is even or odd:
# Take input from the user
number = int(input("Enter a number: "))
# Check if the number is even or odd
if number % 2 == 0:
print(f"{number} is an even number.")
else:
print(f"{number} is an odd number.")
How it works:
- The program takes an integer input from the user using the
input()
function. - The
int()
function converts the input string to an integer. - The program then checks if the number is divisible by 2 using the modulus operator
%
. - If the remainder is 0, the number is even; otherwise, it is odd.
The f
in the print
function refers to an f-string (formatted string literal) in Python. F-strings provide a way to embed expressions inside string literals, using curly braces {}
. They were introduced in Python 3.6 as a more readable and concise way to format strings.
Example:
name = "Alice"
age = 30
# Using an f-string
print(f"My name is {name} and I am {age} years old.")
Explanation:
- The
f
before the string indicates that it is an f-string. - Inside the f-string, you can directly include variables or expressions within curly braces
{}
. The content inside the curly braces is evaluated at runtime, and the result is embedded in the string.
In the example above:
{name}
is replaced by the value of thename
variable, which is"Alice"
.{age}
is replaced by the value of theage
variable, which is30
.
The output will be:
My name is Alice and I am 30 years old.
F-strings are preferred for their readability and efficiency compared to older string formatting methods like %
formatting or str.format()
.
You can run this code in any Python environment.
I like this web site so much, saved to my bookmarks. “Nostalgia isn’t what it used to be.” by Peter De Vries.