r/AskAnythingPython • • 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

14 comments sorted by

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

1

u/Buttleston Sep 09 '23

C++ is actually also pretty good these days and much more expressive than when I did it for a living - which was prior to C++11 adoption. It's not a bad choice imo.

For LLM stuff or any machine learning the language kind of doesn't matter. In the end you're going to hand it all off to the GPU, probably. There are multiple libraries that provide abstractions that let you define models and then hand off the training and evaluation to GPUs. Keras is the one I've used but not really extensively. I don't know that I have any real advice for you there. There are some decent programs for machine learning - I took one a few years ago, via coursera. The company I was working for paid for it. I found it... uninteresting.

I actually specialized in machine learning for my masters degree but that was in the dawn of time, it was a very different subject then.