Python to C++, A Data Scientist’s Journey to Learning a New Language — Conditions, Conditional Statements, and a Simple Calculator Project

Daniel Benson
Geek Culture
Published in
11 min readJun 19, 2021

Introduction

Welcome back, dear readers, to the next installment in the Python to C++ series. Last time we discussed the numbers and strings, particularly some of the methods we can use to work with each in the Python and C++ programming languages. In this article we will be looking at binary conditionals, their use within both programming languages, and we will take everything we have learned up to this point and create our very own simple calculator program in both languages! Here is a look at the overall code we will be using to learn about conditionals:

Python Raw Code

C++ Raw Code

Conditions

Conditions are a way to tell the computer that we want to check if some statement is true or false. These statements will always return a binary value of true or false. For example, given that I have thirteen apples I could ask you: “True or false, I have thirteen apples.” Likewise I could ask the computer to check the condition “does 13 equal 13?” or “does 13 equal 12”? To tell the computer that we want to check for a condition and receive a boolean value back we use a number of different symbols:

The Python language also has its own specific condition symbols for use in special cases, called identity operators. These operators are used to check if two variables, values, or objects are the same object:

Let’s take a look at some examples of these in code now!

Python:

# Boolean Values
print(True)
print(False)
# basic conditions
print(13 == 13)
print(13 == 12)
print(13 != 13)
print(13 != 12)
print(13 < 13)
print(13 < 14)
print(13 > 13)
print(13 > 12)
print(13 <= 13)
print(13 <= 12)
print(13 >= 13)
print(13 >= 14)
print(13 == 13)
print(13 == "Today")
print(13 != "Today")
# Python specific conditions
panda1 = "Greg"
panda2 = panda1.copy()
panda3 = panda1
panda4 = "Mike"
print(panda1 is panda2)
print(panda1 is panda3)
print(panda1 is not panda4)

Returns:

$ python3 binary_conditionals.py>> True
>> False
>> True
>> False
>> False
>> True
>> False
>> True
>> False
>> True
>> True
>> False
>> True
>> False
>> True
>> False
>> True
>> True
>> False
>> True

C++:

// boolean values
std::cout << true << std::endl;
std::cout << false << std::endl;
// basic condition statements
std::cout << (13 == 13) << std::endl;
std::cout << (13 == 12) << std::endl;
std::cout << (13 != 13) << std::endl;
std::cout << (13 != 12) << std::endl;
std::cout << (13 < 13) << std::endl;
std::cout << (13 < 14) << std::endl;
std::cout << (13 > 13) << std::endl;
std::cout << (13 > 12) << std::endl;
std::cout << (13 <= 13) << std::endl;
std::cout << (13 <= 12) << std::endl;
std::cout << (13 >= 13) << std::endl;
std::cout << (13 >= 14) << std::endl;
// std::cout << (13 == "Today") << std::endl;
// std::cout << (13 != "Today") << std::endl;

Returns:

$ g++ -o binary_conditionals binary_conditionals.cpp
$ ./binary_conditionals
>> 1
>> 0
>> 1
>> 0
>> 0
>> 1
>> 0
>> 1
>> 0
>> 1
>> 1
>> 0
>> 1
>> 0

Immediately a few differences between Python and C++ can be seen from the code above. First, while the Python language allows you to compare between string and number types, C++ will throw an error if you attempt to compare the two. Second, Python returns clear True and False boolean values while C++ returns 0s and 1s. While both are still considered to be boolean values, C++ codes boolean return value 0s as false and 1s as true.

Conditional Statements

Conditional statements are a way to check if certain conditions are met and base what code in the program is run next based on the boolean value returned. The most basic of these is the if…else statement, in which a condition is checked after the if statement. If this condition returns true the code within the if block is run. Otherwise the code within the else block is run instead. Again let’s assume I have 13 apples. I could say “If I have 13 apples I will give you 2 of them, otherwise I will give you 3.” Let’s see how this looks in code.

Python:

# Conditional Statements
a = True
b = False
if a == True:
print("Hello readers")
else:
print("a not true")
if b == True:
print("Hello again readers")
else:
print("b not true")

Returns:

$ python3 binary_conditionals.py>> Hello readers
>> b not true

C++:

// Conditional statementsbool a = true;
bool b = false;
if (a == true) {
std::cout << "Hello readers\n";
}
else {
std::cout << "a not true\n";
}
if (b == true) {
std::cout << "Hello again readers\n";
}
else {
std::cout << "b not true\n";
}

Returns:

$ g++ -o binary_conditionals binary_conditionals.cpp
$ ./binary_conditionals
>> Hello readers
>> b not true

If we have more than one condition we would like to check, we can use an if…else if…else statement. This statement will start by checking the condition in the if block. If that condition is not met it will move on and check the condition in the next block, the else if. If this condition is met it will run the code within this block then move out of the if…else if…else statement to whatever code comes next. If the condition is NOT met here it will move on either to the next else if block or the else block depending on how many conditions you want to check. In Python the else if statement is written as elif while in C++ it is written out in full as else if. Examples of this can be seen as follows.

Python:

c = 5if c > 10:
print("c is greater than 10")
elif c > 5:
print("c is greater than 5 but less than 10")
elif c > 0:
print("c is greater than 0 but less than 5")
else:
print("c is a negative number")
print("input any number you want")
d = int(input())
if d > 1000:
print("d is greater than 1000")
elif d > 500:
print("d is greater than 500 but less than 1000")
elif d > 100:
print("d is greater than 100 but less than 500")
elif d > 0:
print("d is greater than 0 but less than 100")
else:
print("d is a negative number")

Returns:

$ python3 binary_conditions.py>> c is greater than 0 but less than 5
>> input any number you want
>> 55
>> d is greater than 0 but less than 100

C++:

int c = 5;if (c > 10){
std::cout << "c is greater than 10\n";
}
else if (c > 5){
std::cout << "c is greater than 5 but less than 10\n";
}
else if (c > 0){
std::cout << "c is greater than 0 but less than 5\n";
}
else{
std::cout << "c is a negative number\n";
}
int d;std::cout << "Type in a whole number\n";
std::cin >> d;
if (d > 1000){
std::cout << "d is greater than 1000\n";
}
else if (d > 500){
std::cout << "d is greater than 500 but less than 1000\n";
}
else if (d > 100){
std::cout << "d is greater than 100 but less than 500\n";
}
else if (d > 0){
std::cout << "d is greater than 0 but less than 100\n";
}
else{
std::cout << "d is a negative number\n";
}

Returns:

$ g++ -o binary_conditionals binary_conditionals.cpp
$ ./binary_conditionals
>> c is greater than 0 but less than 5
>> Type in a whole number
>> 55
>> d is greater than 0 but less than 100

We can also have complex conditional statements in which more than one condition is checked before the decision to run the next block of code is made. An example of this would be, “If I have 13 apples and you don’t have any apples I will give you half of mine.” Some examples of code would look like.

Python:

# Complex conditional statementse = 200
f = 6
if e < 500 and e > 100:
print("e is between 100 and 500")
elif e < 100 and e > 0:
print("e is between 0 and 100")
elif e < 0:
print("e is negative")
if f > 100 or f < 0:
print("f is either greater than 100 or is a negative number")
elif f < 100 or f > 0:
print("f is either less than 100 or is a positive number")

Returns:

$ python3 binary_conditions.py>> e is between 100 and 500
>> f is either less than 100 or is a positive number

C++:

// Complex conditional statementsint e = 200;
int f = 6;
if (e < 500 && e > 100)
{
std::cout << "e is between 100 and 500\n";
}
else if ( e < 100 && e > 0)
{
std::cout << "e is between 0 and 100\n";
}
else if (e < 0)
{
std::cout << "e is negative\n";
}
if (f > 100 || f < 0)
{
std::cout << "f is either greater than 100 or is a negative number\n";
}
else if (f < 100 || f > 0)
{
std::cout << "f is either less than 100 or is a positive number\n";
}

Returns:

$ g++ -o binary_conditionals binary_conditionals.cpp
$ ./binary_conditionals
>> e is between 100 and 500
>> f is either less than 100 or is a positive number

Simple Calculator Project

Today we will be programming a calculator that can perform simple calculations including addition, subtraction, multiplication, and division on two given numbers. This calculator will be run through the command line and will ask the user to input two numbers and their desired operator. We will write this project in both Python and C++, tying together most of that we have learned in the series up to this point: output, user input, working with numbers, mathematical operations, and conditionals. Let’s get started!

The full raw code can be found here:

Raw Python Code

Raw C++ Code

We will begin by printing out a basic introduction for our program. This will welcome the user to the program and inform them of what the program is and how it works.

Python:

print("Welcome to the basic calculator!")
print("You will first choose two numbers")
print("Then you will tell me what calculation you")
print("wish to perform on those two numbers by")
print("typing in the symbol corresponding to that calculation")
print("For instance if you wanted to add the numbers 2 and 4 together")
print("you would give me the number 2 and 4")
print("then when prompted give me the symbol '+'.")
print("After a quick behind-the-scenes calculation")
print("I will give you the answer.")

Returns:

$ python3 simple_calculator.py>> Welcome to the basic calculator!
>> You will first choose two numbers
>> Then you will tell me what calculation you
>> wish to perform on those two numbers by
>> typing in the symbol corresponding to that calculation
>> For instance if you wanted to add the numbers 2 and 4 together
>> you would give me the number 2 and 4
>> then when prompted give me the symbol '+'.
>> After a quick behind-the-scenes calculation
>> I will give you the answer.

C++:

std::cout << "Welcome to the basic calculator!\n";
std::cout << "You will first choose two numbers,\n";
std::cout << "then you will tell me what calculation you\n";
std::cout << "wish to perform on those two numbers by\n";
std::cout << "typing in the symbol corresponding to that calculation.\n";
std::cout << "For instance if you wanted to add the numbers 2 and 4 together\n";
std::cout << "you would give me the numbers 2 and 4\n";
std::cout << "then when prompted give me the symbol '+'.\n";
std::cout << "After a quick behind-the-scenes calculation\n";
std::cout << "I will give you the answer.\n";

Returns:

$ g++ -o simple_calculator simple_calculator.cpp
$ ./simple_calculator
>> Welcome to the basic calculator!
>> You will first choose two numbers
>> Then you will tell me what calculation you
>> wish to perform on those two numbers by
>> typing in the symbol corresponding to that calculation
>> For instance if you wanted to add the numbers 2 and 4 together
>> you would give me the number 2 and 4
>> then when prompted give me the symbol '+'.
>> After a quick behind-the-scenes calculation
>> I will give you the answer.

Next we will gather the user’s desired input, including the two numbers they wish to use for calculation purposes and their desired calculation.

Python:

# Start by gathering user input
print("Input your first number here: ")
num_1 = int(input())
# Get user's second number
print("Input your second number here: ")
num_2 = int(input())
# Get user's desired calculation
print("Insert the symbol corresponding to your desired calculation")
print("using the symbols +, -, /, or *: ")
c = input()

Return:

$ python3 simple_calculator.py>> Input your first number here:
>> 4
>> Input your second number here:
>> 2
>> Insert the symbol corresponding to your desired calculation
>> using the symbols +, -, /, or *:
>> *

C++:

// Start by gathering user inputdouble a;
double b;
// Get user's numbersstd::cout << "Input your first number here: \n";
std::cin >> a;
std::cout << "Input your second number here: \n";
std::cin >> b;
// Get user's desired calculationstd::cout << "Insert the symbol corresponding to your desired calculation\n";
std::cout << "using the symbols +, -, /, or *: \n";
char c;
std::cin >> c;

Returns:

$ g++ -o simple_calculator simple_calculator.cpp
$ ./simple_calculator
>> Input your first number here:
>> 4
>> Input your second number here:
>> 2
>> Insert the symbol corresponding to your desired calculation
>> using the symbols +, -, /, or *:
>> *

Next we will use a series of if…else if…else statements to check the user’s desired calculation and perform said calculation.

Python:

# calculate the user's requestif c == '+':
calc = num_1 + num_2
elif c == '-':
calc = num_1 - num_2
elif c == '/':
calc = num_1 / num_2
elif c == '*':
calc = num_1 * num_2
else:
print("You entered an invalid character, please try again...")
quit()

C++:

// Calculate the user's requestdouble calc;
if (c == '+')
{
calc = a + b;
}
else if (c == '-'){
calc = a - b;
}
else if (c == '/'){
calc = a / b;
}
else if (c == '*'){
calc = a * b;
}
else{
std::cout << "You entered an invalid character, please try again...\n";
return 0;
}

Finally, we will end our program by outputting the calculation’s final result.

Python:

print(calc)

Returns:

16

C++:

std::cout << calc << std::endl;
return 0;

Returns:

$ g++ -o simple_calculator simple_calculator.cpp
$ ./simple_calculator
>> 16

…And easy as that, we have created a simple calculator program in Python and C++! Access to the raw Python code can be found here and the C++ code can be found here. Thank you again, dear reader, for joining me on this journey. If you are feeling lost on any of the above methods used feel free to check out my previous three articles in the Python to C++ series. I hope you enjoyed and I look forward to seeing you again in the next installment! As always, rep it up and happy coding.

--

--

Daniel Benson
Geek Culture

I am a Data Scientist and writer prone to excitement and passion. I look forward to a future I am able to focus those characteristics into work I love.