Python in competitive programming

@veeru_uppin can you name any in built function that python has but not c++?

1 Like

@kapil16garg can you name any in built function that python has but not c++?

1 Like

python has inbuilt function to generate permutations whereas c++ don’t.

I will strongly recommend you to have a combination of both. The reason behind is that now a days python is the most growing programming language and there are a lot of job openings for python. Python is very popular because of its power for implementing machine learning projects and it has also a rich web development frameworks.
So keep practicing using both languages c++ and python otherwise restricting to one will make you forget other.

there is next_permutation in c++

1 Like

Agreed with your peers.

When it comes to CP, python has very obvious shortcomings when compared to other languages(which already many people mentioned).So if you are aiming high on CP ladder, you need to switch to a faster and better language.
But if your priority is the tech/job interviews regardless of the language you know, you just need to have a good grasp on DS-ALGO .Any language is welcomed (at least in MNCs).

1 Like

thanks digon I was really not aware with next_permutation function in c++.

@suryapal I didn’t mean to compare these languages based on their functions, these languages have their own functionalities and use cases. The only differences being:

  1. Python is dynamically typed, where as C++ is statically typed, resulting in Python performing slower than C++!
  2. Python doesn’t need to have a “main” function, whereas for C++, it’s mandatory to have a “main” function.

If you want to do CP just for problem solving then you don’t need to switch to cpp, python can still be used to solve many problems (you can see it for yourself by checking all submission section under any problem).

If you are doing CP for ranks then its better to switch to cpp as many ppl are using it.

itertools ??

Functions that are not in c++

  1. count() function
    that counts the number of occurrence of the substring in a given string
  2. pow(a,b,mod) it can calculate a^p%m

Personally, I use both c++ and python in cp according to my convenience(mainly I use python for string related problems) and rest in c++.

1 Like

Here’s the interesting story behind Python. Every line in Python is dynamically typed. But what really happens when you run Python snippets?

python3 program.py

The small statements in the .py file will get Converted to around 400 or more lines of C. Then, the program is run in C fashion. That’s the reason why Python doesn’t allow Overloading of methods (Correct me if I am wrong). This version of Python is called CPython.

There is another version of Python called JPython or Jython, whose implementation is done in Java, probably supporting all functions of OOPS.

Now, time limits for Python are not the same as that of CPP or C. It’s 5X, most of the times. It means if CPP is given a time limit of 1 Second, Python gets 5 Seconds, if CPP gets 2 Seconds, Python gets 10 Seconds.

And most of the times, it’s not the language that is slow, it’s the user who writes the program inefficiently. Say you use input() and print() statements usually for handling input/output in Python. But there is a faster way to do this. stdin and stdout modules give Python the power to handle inputs/outputs faster. And there is one other module named IO (if I am not wrong), using which IO can be handled the fastest in Python.

I am not kinda against to CPP, but one of the International grand masters, Second Thread codes in Java. He kinda hates CPP :sweat_smile:.

Dynamically typed is not the only reason making it slower than CPP, as I mentioned above. Hardcore CP programmers will have best advantage using CPP.

1 Like

@suman_18733097 Oh! I didn’t know! Thanks for this info mate!

This above statement may be true for CodeChef, but in Codeforces, all languages have a fixed time limit. And yeah, its true that it all depends on your implementation.

It is true for all platforms because there is one grandmaster in code forces who only code in python.

But most of the time codeforces recommend using PyPy because it runs the python code in the same time as it would take to run C++ code.

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.