r/AskAnythingPython • u/kitkatmafia • Sep 08 '23
if else in one line!
just wanted to share something cool I found, - you can write the if-else statement in one line!
so instead of
age =20
if age>=18:
print("You can drive")
else:
print("Sorry, you cant drive")
you can write them in one line - this is called a terinary conditional operator. Means you need three operands (The 2 print statements and the test condition)
print("You can drive") if age>=18 else print("Sorry, you cant drive")
The syntax is as follows
a if condition else b
1
Upvotes
View all comments
Show parent comment
1
u/kitkatmafia Sep 09 '23
I've been writing a bit of C to learn some low level kernal implementations and memory management - again very very baisc to understand the concepts. While I have been using C++ to write "Python" like codes. Im kinda all over the place. My goal is to eventually work on large language models - not just using some pre writtent code to fine tune - but want to do my own kind of optimization. Which path would you recommend if I choose to go this route - python nd Cpp or python and C? thanks for your insight