Python in competitive programming

Oh ok fine mate!

Ya, its true that python is slower than other languages (as python is interpreter language ). According to me you should not leave it totally in some type of problems you can use the other language in which you can get TLE. You can also learn java or c++ and according to me java language would be better because it will be useful in other things also like android development and web technologies. In interview java language is preferred as more good.

You could try using Python. You should know that most competitions that do allow python actually set a different time limit for solutions submitted in the python language. However, C++ has one of the biggest communities and most solutions are also either given in C++ or Java (python too, but I would say not as much).

Also, if you write bad code, then no matter what language you use, you will be getting wrong solutions or TLE errors.

All in all, C++ is usually the preferred language for competitive programmers, but using Python doesn’t make you a bad sport programmer, writing shitty code does. [Speaking from personal experience]

I would like to answer this question because Python has been my main language for a very long time.

Python is decent for CP. But it depends on few things:

1. Judge Generosity
Codechef is generous and gives 4x or 5x time limit for Python. Other websites such as Codeforces will give you the same time limit for all languages. (you can say there is Pypy, but it’s not the same time)

2. Your goals?
If you want to be a 5 star coder here, you can easily do that in Python, but as you higher up, it will start to get harder. The easy problems usually have relaxed constraints. The hard problems often rely on optimizations, and advanced algorithms, which can be hard to work on without community support. As such if you only want to prepare for interviews, it should be totally fine and you can implement most standard algorithms easily. But it will be hard to make everything on your own when there is so much support for C++ users, and you will be at a disadvantage.

3. Hey but we get 5x time right?
Actually, i have seen pieces of code that take 100x time in Python compared to c++. Someone will tell you that you can always optimize your code using tricks, but it’s just extra effort to spend on your focus on that instead of just solving the problem. I experienced this first hand at last month long challenge where I barely got a 22/100 point AC on a problem subcase in Python, but my solution ran almost in 0 seconds in c++. Now I am also learning c++ and I get TLE’s very rarely.

6 Likes

Really a good answer!!! But will you please help me with this— Is it possible in C++?

i was in a same situation as you back then.
i used python for enough time and answer is -> it depends.

python: easy to write syntax ( problem logic will be same irrespective of language)
but python is not designed for CP.
mainly due to following :

  • yes, slow execution time but most platform’s have increased time accordingly. which is sufficient. if you get TLE probably you should improve your problem logic , you will never face much issue with this.

  • deep recursion , if you want to be 5 star coder you can manage it with python.
    but when problem involves recursion with n > 10^4 - 10^5. even sometimes 10 ^ 3 , you cannot execute it with python cause which has a recursion limit = 1000 and yes there are methods increase this recursion limit which creates some other issues like memory and go on…
    most problems in contests have n > 10^5,
    Now imagine to solve problems without recursion like DP, graphs , trees and many algorithms rely on recursion. these you have to implement it iteratively if you can implement it no problem.
    but in interviews though they say any language is fine, python is not their preferred language. python has humongous scope in data science but less in software field.

C++: most preferred language for CP and it occupies around 90% even more ,has a very large community . if the aim to become best in CP then C++ is your language.

java: from development perspective everything (i mean so many) things runs on JAVA , even interviewer preferred language is java and good for projects.

finally you must master one language after that always better to know 2 languages.