Python in competitive programming

Python is the only programming language I have learned and I use it to solve questions in competitive programming. But my peers advise me to switch to C++ stating that python is useless in cp. So, is it necessary that I switch, or should stick to my comfort zone, or maybe have a combination of both?

2 Likes

C++ is good for cp because of its speed python is very slow you may get TLE for some problems its better to switch to c++ or c .

2 Likes

yes bro, actually I was also doing it for 3 months in python, but i faced problems in Time limit and in string too. I would suggest to switch and Learn C++ and mainly STL library ,and you will feel that you have next level powers in you. It will be little bit difficult to learn for python coder, But you will conquer any contest after this, because there are many resources of C++ in CP, and all 5 star coders also use CPP only ,infact world no.1 of codechef also.

2 Likes

I am not saying that people do not use python for problem-solving but according to today’s situation, I think C++ is good for problem-solving
Reason:

  1. Python has the inbuilt function and using the inbuilt functions are not allowed in the interview.
  2. Almost all good youtube channels and also CP-algorithm use C++ for problem-solving or teach any concept so let’s suppose if you are learning any topic in C++, so then after you have to think code in python again this would be your time waste.
  3. Number of peoples who use C++ much greater than python so any can help you easily in your C++ code.
2 Likes

Cpp or Java, most of the tech companies in their interview expect you to write code in java or cpp. Java is also a good option.

Before I begin my answer, I would like to take a moment to admit that I too used to use Python for CP, but later, as I saw the shortcomings of Python in time and efficiency, I started using C++.

Some points that’ll make you shift to C++:

  1. Python is slower than C++. You may get TLE often.
  2. Python has a lot of built in functions, but coding interviews the use of those functions will be prohibited.
  3. If you observe the solutions of many CP programmers, either here, or in YouTube videos, they are written in C++, because it’s fast and efficient.
  4. C++ has STL (Standard Template Library), and it is acceptable to use it in coding interviews!

@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.