Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Conditional Expression | Python Ternary Operator
Conditional Statements in Python Clone

Veeg om het menu te tonen

book
Conditional Expression

At times, we may need to assign different values to a variable based on specific conditions. Achieving this efficiently can be done in just one line of code. Let's explore a few approaches to grasp this concept more effectively.

Example 1:

123456789
age = 23 status = '' if age >= 18: status = 'Adult' else: status = 'Not adult' print(status)
copy

Example 2:

12345
age = 15 status = 'Adult' if age >= 18 else 'Not Adult' print(status)
copy

Have you observed the distinction? The second method is not only more elegant but also more convenient, condensing it into just one line instead of four!

Conditional expressions, also known as Python's ternary operator, act as decision-making tools and adhere to the following syntax:

python

Let's practice!

Taak

Swipe to start coding

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 1
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?

Vraag AI

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

book
Conditional Expression

At times, we may need to assign different values to a variable based on specific conditions. Achieving this efficiently can be done in just one line of code. Let's explore a few approaches to grasp this concept more effectively.

Example 1:

123456789
age = 23 status = '' if age >= 18: status = 'Adult' else: status = 'Not adult' print(status)
copy

Example 2:

12345
age = 15 status = 'Adult' if age >= 18 else 'Not Adult' print(status)
copy

Have you observed the distinction? The second method is not only more elegant but also more convenient, condensing it into just one line instead of four!

Conditional expressions, also known as Python's ternary operator, act as decision-making tools and adhere to the following syntax:

python

Let's practice!

Taak

Swipe to start coding

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 1
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt