Python multi-line if statement

Ariel 119 Published: 12/17/2024

Python multi-line if statement

I'm glad you asked!

In Python, a multi-line if statement is achieved by using the if keyword followed by the condition to be evaluated. The syntax for this type of statement is:

if condition:
code block 1

[and/or]

code block 2

...

The condition can be any valid Python expression, including comparisons, logical operations, and more. If the condition evaluates to True, then the indented code block(s) will execute.

Here's an example of a multi-line if statement:

x = 5

if x > 3:

print("X is greater than 3")

if x < 10:

print("And X is less than 10")

In this example, the first if statement checks if x is greater than 3. If it is, then the indented code block will execute, which prints "X is greater than 3". The second if statement checks if x is also less than 10. If both conditions are met, then the second print statement will execute.

Now, let's create a more complex example with multiple conditions and nested logic:

age = 25

income = 50000

if age >= 18: # adult

if income > 40000: # high-income earner

print("You're an adult with a high income!")

elif income <= 20000: # low-income earner

print("You're an adult on a tight budget")

else:

print("You're an adult with a moderate income")

if age < 18: # minor

if income > 30000: # rich parent

print("You're a minor from a wealthy family!")

elif income <= 15000: # struggling parents

print("Your parents are working hard to make ends meet")

else:

print("You're a minor with financially comfortable parents")

In this example, we have two if statements: one for adults and one for minors. The adult block has three conditions: high-income earner, low-income earner, and moderate income earner. If the age is less than 18 (minor), then there are also three conditions: rich parents, struggling parents, and financially comfortable parents.

When you run this code, it will print a statement based on the values of age and income. This example demonstrates how Python's multi-line if statements can be used to create complex decision-making logic with multiple conditions and nested blocks.

How do you make a multiline text in Python?

I'm happy to help you with that!

To create a multiline text in Python, there are several ways to achieve this. I'll cover three common methods: using triple quotes, newline characters, and the textwrap module.

Method 1: Using Triple Quotes

You can use triple quotes (

python 

multiline_text = """

This is the first line.

And this is the second line.

And so on...

"""

print(multiline_text)

When you print this variable, you'll get a multiline output:

This is the first line.

And this is the second line.

And so on...

**Method 2: Using Newline Characters**

Another way to create a multiline string is by using newline characters (n):

python

multiline_text = "This is the first line.nAnd this is the second line.nAnd so on...n"

print(multiline_text)

The output will be similar:

This is the first line.

And this is the second line.

And so on...

**Method 3: Using the `textwrap` Module**

If you need to format your multiline text, such as wrapping it within a certain width, you can use the textwrap module:

python

import textwrap

multiline_text = "This is a very long line that needs to be wrapped.n" +

"And this is another long line too.n" +

"And so on..."

wrapped_text = textwrap.fill(multiline_text, width=80)

print(wrapped_text)

The output will have the text wrapped within an 80-character width:

This is a very long line that needs to be wrapped.

And this is another long line too.

And so on...


In conclusion, Python provides various ways to create and format multiline text. You can choose the method that best suits your specific requirements.