Title here
Summary here
Based on the provided input, we extract the target language and the code example to perform the translation. Here is the translation:
Switch statements express conditionals across many branches.
import time
def main():
# Here’s a basic switch using if-elif-else in Python.
i = 2
print(f"Write {i} as ", end="")
if i == 1:
print("one")
elif i == 2:
print("two")
elif i == 3:
print("three")
# You can use multiple expressions in the same if statement.
# We use the else case in this example as well.
today = time.gmtime().tm_wday
if today == 5 or today == 6:
print("It's the weekend")
else:
print("It's a weekday")
# if-else logic
t = time.localtime().tm_hour
if t < 12:
print("It's before noon")
else:
print("It's after noon")
# Type switch using isinstance
def what_am_i(i):
if isinstance(i, bool):
print("I'm a bool")
elif isinstance(i, int):
print("I'm an int")
else:
print(f"Don't know type {type(i).__name__}")
what_am_i(True)
what_am_i(1)
what_am_i("hey")
if __name__ == "__main__":
main()
To run the program, simply execute the Python script.
$ python switch_example.py
Write 2 as two
It's a weekday
It's after noon
I'm a bool
I'm an int
Don't know type str
Now that we can run and build basic Python programs, let’s learn more about the language.
Next example: Arrays