Conditionals and Looping in Python

0
Python, like many programming languages including C, treats any non-zero integer as true and zero as false. Beyond integers, Python's conditional logic extends to other data types. For instance, strings and lists—or more generally, any sequence—also follow this truthy and falsy paradigm. Any non-empty sequence is treated as true, while empty sequences are considered false.

Conditionals in Python

The standard comparison operators in Python are analogous to those in C:
  • < (less than) 
  • > (greater than)
  • == (equal to)
  • <= (less than equal to) 
  • >= (greater than equal to)
  • != (not equal to)
These operators allow you to test relationships between variables and values.

```python
a = 10
b = 5

if a > b:
    print("a is greater than b")
```

Indentation for Grouping Statements

Python uses indentation to group statements, a feature that significantly enhances code readability. Each level of indentation corresponds to a deeper block in the code. Typically, a single level of indentation is set using four spaces or one tab. For example, in a `for` loop, the body of the loop is indented:

```python
for i in range(5):
    print(i)
```

In an interactive Python prompt, indentation must be done manually using tabs or spaces. When working with a text editor, many editors provide auto-indentation features, making it easier to maintain proper code structure. Moreover, when using compound statements interactively, they should be followed by a blank line to signal the end of the block, as the parser cannot infer the end of the block from indentation alone.

```python
while True: 
    print("Infinite loop")
    break  # Exit the loop
# Blank line here signals end of the loop block in an interactive mode
```

The `print()` Function

The `print()` function in Python writes the value of the argument(s) provided. It is versatile and can handle multiple arguments, including integers, floating points, and strings. Unlike simply writing an expression to standard output, `print()` provides formatted output, inserting spaces between arguments when necessary.

```python
print("Hello", "World", 42)
# Output will be: Hello World 42
```

Strings are printed without quotes, and a space is automatically inserted between items. The function also adds a newline character at the end of the output by default. However, this behavior can be modified using the `end` keyword argument to avoid the newline or to end the output with a different string.

```python
print("Hello", end=", ")
print("World")
# Output will be: Hello, World
```

Applying Conditions and Looping with Sequences

Python’s handling of sequences (like lists and strings) in conditionals and loops follows the same logic. For example, non-empty lists or strings evaluate to true:

```python
my_list = [1, 2, 3]
if my_list:
    print("List is non-empty")
else:
    print("List is empty")
```

This allows for succinct and readable code when testing the presence or absence of elements.
Tags

Post a Comment

0Comments
Post a Comment (0)