Lession 2: Control Structure in Python by Github Copilot
To demonstrate control structures in Python, let's add some examples of `if-else` statements, `for` loops, and `while` loops to your existing code.
```python
num_str = "123"
num_int = int(num_str) # Converts string to integer
print(num_int) # Output: 123
# If-else statement
if num_int > 100:
print("The number is greater than 100")
else:
print("The number is 100 or less")
# For loop
for i in range(5):
print(f"Iteration {i}")
# While loop
count = 0
while count < 5:
print(f"Count is {count}")
count += 1
```
This code demonstrates:
1. An `if-else` statement to check if
num_int
is greater than 100.
2. A `for` loop to iterate 5 times.
3. A `while` loop to count from 0 to 4.
Comments
Post a Comment