Python to C++, A Data Scientist’s Journey to Learning a New Language –Working with Numbers and Strings

Daniel Benson
Geek Culture
Published in
13 min readJun 16, 2021

Introduction

Welcome back, reader, to the next installment in the Python to C++ series. In our last adventure we learned about output methods, data types, variables, and gathering user input in both C++ and Python. In this volume we will build on our knowledge of working with numbers and strings in our two languages of interest. We will touch upon the use of numbers with mathematical operators as well as accessing individual characters or groups of characters within a string.

The raw code used in the following snippets can be found in the repository here.

Working with Numbers

In Python numbers are used simply by typing in the number you want to use for integer data types and including a decimal for double data types. This can be seen in code through the following simple printout of the number 13:

# Simple number printout
print(13)

Using the terminal:

$ python3 working_with_numbers.py>> 13

This same method is used in C++ as seen in the following code:

// # Simple number printout
std::cout << 13 << std::endl;

leads to the output:

$ g++ -o working_with_numbers working_with_numbers.cpp
$ ./working_with_numbers
>> 13

Just as in math numbers in Python and C++ can be manipulated through the use of mathematical operations; the computer is told to perform these operations through specific operator symbols. These operations include: addition, subtraction, multiplication, and division.

Addition: In both Python and C++ the operator that indicates to the computer to perform addition is the “+” symbol. Placing this symbol between two numbers will tell the computer to perform addition on the two given numbers; due to the additive property which number is placed in front of the operator and which number is placed behind the operator does not change the final answer. An example can be seen in the following code:

print("1 + 1: ", 1 + 1)
print("1.5 + 1.5: ", 1.5 + 1.5)
print("1.5 + 1: ", 1.5 + 1)
a = 1
b = 1.5
print("a (1) + a (1): ", a + a)
print("a (1) + b (1.5): ", a + b)

returns:

$ python3 working_with_numbers.py>> 1 + 1:  2
>> 1.5 + 1.5: 3.0
>> 1.5 + 1: 2.5
>> a (1) + a (1): 2
>> a (1) + b (1.5): 2.5

And for C++:

std::cout << "1 + 1: " << 1 + 1 << std::endl;
std::cout << "1.5 + 1.5: " << 1.5 + 1.5 << std::endl;
std::cout << "1.5 + 1: " << 1.5 + 1 << std::endl;
int a = 1;
double b = 1.5;
std::cout << "a (1) + a (1): " << a + a << std::endl;
std::cout << "a (1) + b (1.5): " << a + b << std::endl;

returns:

$ g++ -o working_with_numbers working_with_numbers.cpp
$ ./working_with_numbers
>> 1 + 1: 2
>> 1.5 + 1.5: 3
>> 1.5 + 1: 2.5
>> a (1) + a (1): 2
>> a (1) + b (1.5): 2.5

Subtraction: The universal operator indicating the computer to perform subtraction is the “-“ symbol. Placing this symbol between two numbers will tell the computer to subtract the second number from the first number; here number placement matters. The following code gives an example:

print("1 - 1: ", 1 - 1)
print("1.5 - 1: ", 1.5 - 1)
print("a (1) - a (1): ", a - a)
print("b (1.5) - a (1): ", b - a)

returns:

$ python3 working_with_numbers.py>> 1 - 1:  0
>> 1.5 - 1: 0.5
>> a (1) - a (1): 0
>> b (1.5) - a (1): 0.5

And in C++:

std::cout << "1 - 1: " << 1 - 1 << std::endl;
std::cout << "1.5 - 1: " << 1.5 - 1 << std::endl;
std::cout << "a (1) - a (1): " << a - a << std::endl;
std::cout << "b (1) - a (1): " << b - a << std::endl;

returns:

$ g++ -o working_with_numbers working_with_numbers.cpp
$ ./working_with_numbers
>> 1 - 1: 0
>> 1.5 - 1: 0.5
>> a (1) - a (1): 0
>> b (1) - a (1): 0.5

Multiplication: To tell the computer to multiply two numbers together the operator used is “*”; this is the case for both Python and C++. Like addition, multiplication has a multiplicative property which keeps the final answer the same regardless of which number is placed in front of the operator and which is placed behind the operator. An example of the multiplication operation in code is as follows:

print("5 * 10: ", 5 * 10)print("2.5 * 5.5: ", 2.5 * 5.5)
print("2.5 * 10: ", 2.5 * 10)
a = 5
b = 10
c = 2.5
print("a (5) * b (10): ", a * b)
print("c (2.5) * c (2.5): ", c * c)

returns:

$ python3 working_with_numbers.py>> 5 * 10:  50
>> 2.5 * 5.5: 13.75
>> 2.5 * 10: 25.0
>> a (5) * b (10): 50
>> c (2.5) * c (2.5): 6.25

And in C++:

std::cout << "5 * 10: " << 5 * 10 << std::endl;
std::cout << "2.5 * 5.5: " << 2.5 * 5.5 << std::endl;
std::cout << "2.5 * 10: " << 2.5 * 10 << std::endl;
int c = 5;
int d = 10;
double e = 2.5;
std::cout << "c (5) * d (10): " << c * d << std::endl;
std::cout << "e (2.5) * e (2.5): " << e * e << std::endl;

returns:

$ g++ -o working_with_numbers working_with_numbers.cpp
$ ./working_with_numbers
>> 5 * 10: 50
>> 2.5 * 5.5: 13.75
>> 2.5 * 10: 25
>> c (5) * d (10): 50
>> e (2.5) * e (2.5): 6.25

Division: The division operation is where things begin to get a bit more tricky. Division, in essence, is an operation that works through fractional numbers. As such we will often times find ourselves dealing with double or floating-point numbers and because a computer is extremely picky about how accurate it needs its instructions to be, asking for a whole-number quotient will require a different method than asking for a double or floating-point quotient.

In Python we use different operator symbols to indicate whether or not we want a whole number or floating-point number answer. To return a floating-point number we use the “/“ symbol nested between the two numbers we wish to find the quotient for; like subtraction, division DOES depend on placement of the numbers. A good example of this can be seen in the following code:

print("10 / 2: ", 10/2)
print("14 / 3: ", 14/3)
print("12.4 / 2.2: ", 12.4/2.2)
a = 120
b = 13
c = 45
print("a (120) / b (13): ", a/b)
print("c (45) / b (13): ", c/b)
print(" a (120) / c (45): ", a/c)

returns:

$ python3 working_with_numbers.py>> 10 / 2:  5.0
>> 14 / 3: 4.666666666666667
>> 12.4 / 2.2: 5.636363636363636
>> a (120) / b (13): 9.23076923076923
>> c (45) / b (13): 3.4615384615384617
>> a (120) / c (45): 2.6666666666666665

If we want to find a whole-number quotient, or in other words perform division on two numbers and receive a whole number answer, we use the “\\” symbol between the two numbers. The computer will perform a division calculation, throw out whatever decimal is received, and return the whole number without any rounding. We can see this in the following code:

print("10 // 2: ", 10//2)
print("14 // 3: ", 14//3)
print("12.4 // 2.2: ", 12.4//2.2)
print("a (120) // b (13): ", a//b)
print("c (45) // b (13): ", c//b)
print("a (120) // c (45): ", a//c)

returns:

$ python3 working_with_numbers.py>> 10 // 2:  5
>> 14 // 3: 4
>> 12.4 // 2.2: 5.0
>> a (120) // b (13): 9
>> c (45) // b (13): 3
>> a (120) // c (45): 2

In C++ we use differing data types to tell the computer whether or not it should return a whole number or a floating-point number as the final quotient. The same operator, the “/“ symbol, is used for both division cases. To tell the computer we want a floating-point return we must ensure that at least one of the numbers in front of or behind the operator is of the floating-point or double data type. C++ will then automatically return a quotient matching that data type. This can be seen in the following code:

std::cout << "10.0 / 2: " << 10.0/2 << std::endl;
std::cout << "14 / 3.0: " << 14/3.0 << std::endl;
std::cout << "12.4 / 2.2: " << 12.4/2.2 << std::endl;
double f = 120;
double g = 13;
double h = 45;
std::cout << "f (120.0) / g (13.0): " << f/g << std::endl;
std::cout << "h (45.0) / g (13.0): " << h/g << std::endl;
std::cout << "f (120.0) / h (45.0): " << f/h << std::endl;

returns:

$ g++ -o working_with_numbers working_with_numbers.cpp
$ ./working_with_numbers
>> 10.0 / 2: 5
>> 14 / 3.0: 4.66667
>> 12.4 / 2.2: 5.63636
>> f (120.0) / g (13.0): 9.23077
>> h (45.0) / g (13.0): 3.46154
>> f (120.0) / h (45.0): 2.66667

To tell the computer we want the quotient returned as a whole number we simply ensure that both numbers in front of and behind the division operator are of the integer data type. The computer will perform its division operation, throw out any remainder, and return whatever whole number remains with no rounding. An example of this can be seen in the following code:

std::cout << "10 / 2: " <<  10/2 << std::endl;
std::cout << "14 / 3: " << 14/3 << std::endl;
int i = 120;
int j = 13;
int k = 45;
std::cout << "i (120) / j (13): " << i/j << std::endl;
std::cout << "k (45) / j (13): " << k/j << std::endl;
std::cout << "i (120) / k (45): " << i/k << std::endl;

returns:

$ g++ -o working_with_numbers working_with_numbers.cpp
$ ./working_with_numbers
>> 10 / 2: 5
>> 14 / 3: 4
>> i (120) / j (13): 9
>> k (45) / j (13): 3
>> i (120) / k (45): 2

Modulo: Modulo is a special operation in computer science. It is similar to the division operation except that its return is actually the remainder of the operation’s quotient. For example if we are dividing 3 by 2 we know that our quotient will be 1 with a remainder of 1. Modulo tells the computer to return only that remainder and throw everything else out. In both Python and C++ the operator that tells the computer to perform the modulo is indicated by the “%” symbol between the two numbers we wish to perform the operation on. An example of this can be seen in the following code:

print("10 % 2: ", 10%2)
print("14 % 3: ", 14%3)
print("12.4 % 2.2: ", 12.4%2.2)
print("a (120) % b (13): ", a%b)
print("c(45) % b (13): ", c%b)
print("a (120) % c (45): ", a%c)

returns:

$ python3 working_with_numbers.py>> 10 % 2:  0
>> 14 % 3: 2
>> 12.4 % 2.2: 1.3999999999999995
>> a (120) % b (13): 3
>> c(45) % b (13): 6
>> a (120) % c (45): 30

And in C++:

std::cout << "10 % 2: " << 10%2 << std::endl;
std::cout << "14 % 3: " << 14%3 << std::endl;
// std::cout << "10 % 2: " << 12.4%2.2 << std::endl; cannot use modulo operand with floating point values in c++, only integers
std::cout << "i (120) % j (13): " << i%j << std::endl;
std::cout << "k (45) % j (13): " << k%j << std::endl;
std::cout << "i (120) % k (45): " << i%k << std::endl;

Returns:

$ g++ -o working_with_numbers working_with_numbers.cpp
$ ./working_with_numbers
>> 10 % 2: 0
>> 14 % 3: 2
>> i (120) % j (13): 3
>> k (45) % j (13): 6
>> i (120) % k (45): 30

Notice that while in Python we can use floating-point numbers to find modulo, we cannot use floating-points to find the modulo in C++.

Even/Odd Example: A great use for the modulo operation is when we wish to find whether or not a number is even or odd. The importance of this will be made more evident when we touch upon conditional statements later on, but for the time being know that there are many instances in which it is important for the programmer to determine whether or not a specific number or variable is even or odd. Using modulo we can place the number in question in front of the “%” operator and the number 2 behind the operator and see what the remainder (our return value) is. If the return value is 0 that means the number in question is even; if the return value is 1 that means the number in question is odd. Examples of this are given in the following code:

print("110 % 2: ", 110%2)
print("111 % 2: ", 111%2)
a = 2
print("8765 % a (2): ", 8765%a)
print("86312 % a (2): ", 86312%a)

Returns:

$ python3 working_with_numbers.py>> 110 % 2:  0
>> 111 % 2: 1
>> 8765 % a (2): 1
>> 86312 % a (2): 0

And C++:

std::cout << "110 % 2: " << 110%2 << std::endl;
std::cout << "111 % 2: " << 111%2 << std::endl;
int l = 2;
std::cout << "8765 % l (2): " << 8765%l << std::endl;
std::cout << "86312 % l (2): " << 86312%l << std::endl

Returns:

$ g++ -o working_with_numbers working_with_numbers.cpp
$ ./working_with_numbers
>> 110 % 2: 0
>> 111 % 2: 1
>> 8765 % l (2): 1
>> 86312 % l (2): 0

This, of course, is a method one can use to determine divisors; is our number in question divisible by 3? What about 4? 5? The importance of this, again, will become more clear when we learn the use of conditionals.

Working with Strings

Strings are made up of characters grouped together:

print("This string has 36 characters in it.")
print("String length: ", len("This string has 36 characters in it."))

Returns:

python3 working_with_strings.py>> This string has 36 characters in it.
>> String length: 36

In Python and C++ we can append strings together using the ‘+’ operator, seen in the following code:

a = "Hello"
b = " readers!"
print(a + b)

returns:

$ python3 working_with_strings.py>> Hello readers!

and in C++:

std::string a = "Hello";
std::string b = " readers!";
std::cout << a + b << std::endl;

returns:

$ g++ -o working_with_strings working_with_strings.cpp
$ ./working_with_strings
>> Hello readers!

Each character within a string belongs to a specific position within that string. These positions are called indices (singular: index). Each position, or index, is indicated by a unique number. The starting position, or index, in any string is 0, then is incremented by 1 for each position thereafter. For example, the string “Hello” has an ‘H’ at index 0 and an ‘o’ at index 4.

We can access specific characters within a string if we know their index position! For example, index 0 of the string “Hello” is ‘H’:

print(a[0])

returns:

$ python3 working_with_strings.py>> H

In C++:

std::cout << a[0] << std::endl;

returns:

$ g++ -o working_with_strings working_with_strings.cpp
$ ./working_with_strings
>> H

We can access the ‘o’ in “Hello” by calling index 4 of that string:

print(a[4])

returns:

$ python3 working_with_strings.py>> o

In C++:

std::cout << a[4] << std::endl;

returns:

$ g++ -o working_with_strings working_with_strings.cpp
$ ./working_with_strings
>> o

A specific group of characters within a string can also be accessed. This is done differently in Python and C++, however. In Python, if we have the string “ readers!” we can access the character grouping “read” by calling the indices 1–4 (notice that at index 0 we have a space, which still counts as a character taking up that index):

print(b[1:5])

returns:

$ python3 working_with_strings.py>> read

Notice that the first index called, 1, is inclusive while the second index called, 5, is NOT inclusive. This means that the characters from index 1 up to but not including index 5 will be accessed.

We can even do this from the end of the string, even if we don’t know the length of the string in question. This is done using -1 as the index, which can always be used to indicate the last index of a string:

print(b[-1])

returns:

$ python3 working_with_strings.py>> !

Using negative indices allows us to access specific characters starting from the end of the string. In this case each decrement of the index moves us one character closer to the beginning of the string. For the string “ readers!” index -1 gives us ‘!’ index -2 gives us ’s’ and index -3 gives us ‘r’:

print(b[-1])
print(b[-2])
print(b[-3])

returns:

$ python3 working_with_strings.py>> !
>> s
>> r

We can also access specific character groupings using negative indices. For the string “ readers!”:

print(b[-5:-1])
print(b[-5:])

return:

$ python3 working_with_strings.py>> ders 
>> ders!

Notice that omitting the second number is equivalent to saying “I want all characters from the first indicated index until the end of the string.” Likewise, leaving out the first index number is equivalent to saying “ I want all characters from the start of the string until but not including the index at the indicated number given. Leaving both the first and second numbers out (including only the ‘:’) just means that we want all characters from beginning to end of the string:

print(a[1:])
print(a[:2])
print(b[:])

returns:

$ python3 working_with_strings.py>> ello
>> He
>> readers!

Accessing specific groups of characters within a string in C++ is done a bit differently. In C++ the string class, called into the program using the #include <string> at the top of the program, builds an array from the characters passed to a given variable. For example, the variable a initialized with the string “Hello” is an array behind the scenes, of characters [‘H’], [‘e’], [‘l’], [‘l’], [‘o’]. To access multiple characters for use from a string we create a new variable to which we will initialize the grouped characters:

std::string c(b, 1, 4); // where c is the new variable, b is the variable we are accessing, 1 is the starting index and 4 is how many indices we are including
std::cout << c << std::endl;
std::cout << c.length() << std::endl;

returns:

$ g++ -o working_with_strings working_with_strings.cpp
$ ./working_with_strings
>> read
>> 4

We can also access the last character of a string in C++ through some creativity. We can use the length of the string that we want to access as the starting index. Remember to subtract the length by one to access the correct index (the length of a string will always be 1 longer than the number of accessible indices):

std::string d(b, b.length()-1, 1);
std::cout << d << std::endl;
std::cout << d.length() << std::endl;
std::string e(b, b.length()-2, 1);
std::cout << e << std::endl;
std::cout << e.length() << std::endl;
std::string f(b, b.length()-3, 1);
std::cout << f << std::endl;
std::cout << f.length() << std::endl;

returns:

$ g++ -o working_with_strings working_with_strings.cpp
$ ./working_with_strings
>> !
>> 1
>> s
>> 1
>> r
>> 1

Using more creativity we can access the last n characters of a string by incrementing each passed number in our group method call by one. The following code gives a more clear example of this:

std::string e(b, b.length()-2, 2);
std::cout << e << std::endl;
std::cout << e.length() << std::endl;
std::string f(b, b.length()-3, 3);
std::cout << f << std::endl;
std::cout << f.length() << std::endl;
std::string g(b, b.length()-4, 4);
std::cout << g << std::endl;
std::cout << g.length() << std::endl;

returns:

$ g++ -o working_with_strings working_with_strings.cpp
$ ./working_with_strings
>> s!
>> 2
>> rs!
>> 3
>> ers!
>> 4

Thank you for joining me again, dear readers, in this installment of Python to C++! I hope you learned plenty and that I will see you again next time when we discuss conditional statements in Python and C++ and create our very own basic calculator using both of these languages. 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.