C++ and Python are widely used programming languages with contrasting characteristics. While C++ emphasizes efficiency and low-level control, Python prioritizes simplicity and readability. This article explores their differences in comparing operators and fundamental aspects. By examining their operator functionality, we can gain insights into the unique features of each language and determine their appropriate usage. Let’s delve into the distinct characteristics of C++ and Python when it comes to comparing operators and exploring their basics.

  1. Variables
  2. If, else
  3. Cycles

Differences between C++ and Python: Comparing Operators and Basics

1. Variables

C++

To use a variable you need to announce it first to interact with it. To input a variable you need to use cin >>, and to deduce you need to use cout <<:

#include <iostream>

using namespace std;

int main()
{
    int a = 0; // annonce a variable
    cin >> a; // input the varible
    cout << a; // deduce the varible
    return 0;
}

Python

To use a variable you don’t need to announce it. You can input the value of the variable and announce it in one line using int(input()). To deduce you need print():

a = int(input()) # input the variable
print(a) # deduce the variable

And you can already see that python code is shorter than C++ code. This is a good part of python that it is much shorter and easier to learn.

2. If, else

Operators if and else are quite the same. They have same designations, like:

 If equals ==
 If more >
 If less <
 If equals or more =>
 If equals or less =<

But there are still some differences.

C++

In C++ conditions are in (conditions) and after operator if to start actions you need use {actions}:

#include <iostream>

using namespace std;

int main()
{
    int a;
    cin >> a;
    if(a == 1) // make a condition that a = 1
    { // actions started
        cout << "a = 1";
    } // actions ended
    return 0;
}

Else there is simple. You need to type else and use {actions} to do actions:

#include <iostream>

using namespace std;

int main()
{
    int a;
    cin >> a;
    if(a == 1)
    {
        cout << "a = 1";
    }
    else
    {
        cout << "a != 1";
    }
    return 0;

}

Python

There to make conditions you don’t need something like (), you just type condition. And to do actions conditions place “:” and after you need just to tab from the beggining:

a = int(input())
if a == 1: #make a condition that a = 1
    print("a = 1")

Else is similar to c++ but like python if you need place “:” and tab to do actions:

a = int(input())
if a == 1:
    print("a = 1")
else:
    print("a != 1")

Elif is a python operator. It makes operator:

else:
    ifconditions:
        actions

But you can type elif and conditions:

a = int(input())
if a == 1:
    print("a = 1")
elif a == 2:
    print("a = 2")

In c++ there is no elif. You need to type:

else
    {
        if(conditions)
        {
            
        }
    }

3. Cycles

While

C++

While is very similar to if because how there announce conditions and actions:

#include <iostream>

using namespace std;

int main()
{
    int a;
    cin >> a;
    while(a>0)
    {
        cout << a << endl; // endl start output from new line
        a--; // a-- is a = a - 1
    }
    return 0;
}

In this code we made that while a more than 0 we deduce the value of a. This will deduce a times value of until 1.

Python

Like c++ it is similar to if in python but like if or else in the end there is “:” and conditions without ():

a = int(input())
while a > 0:
    	print(a)
    	a = a - 1

There is also that if a is more than 0 we deduce a. Also you can see that we don’t need endl. It will deduce from the new line. But there is no –. So we need to use a = a – 1.