Python Conditional Expressions using the Ternary Operator - Python Code Example - APPFICIAL

Опубликовано: 16 Сентябрь 2021
на канале: Appficial
4,485
48

A conditional expression, also called the ternary operator, lets you execute an if else statement in one line of code

if condition:
var= expr1
else:
var = expr2

AFTER

var = expr1 if (condition) else expr2

You can see that we were able to do the entire if-else in one line of code, and not even have to worry about indenations or using the colon after the if or else part. One nice benefit is that you can stick an entire conditional expression inside a print function, which you cannot do if you wrote it all out the full way.

Ex: print(expr1 if (condition) else expr2)

However, it is considered good practice to not use the conditional expression, since many programmers are confused by this and may not understand your code.

Subscribe to Appficial for more programming videos coming soon. Also, don't forget to click LIKE and comment on the video if it helped you out!


На этой странице сайта вы можете посмотреть видео онлайн Python Conditional Expressions using the Ternary Operator - Python Code Example - APPFICIAL длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Appficial 16 Сентябрь 2021, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 4,485 раз и оно понравилось 48 зрителям. Приятного просмотра!