Moving from C to Python

Matthew Andrews
6 min readOct 25, 2020
Photo by David Clode on Unsplash

In this post I’m going to share with you my thoughts on learning Python, from my position as someone more used to coding in C. I’ll talk about some of the differences between the two languages, and how those differences come about. If you’re about to start learning Python after C, or even the other way round, I hope this’ll give you a couple of things to watch out for.

The first programming language I really learnt was C at university. I even took an optional extra module in it. But after some years in the wilderness it was only about a year ago that I got to use it in anger when I started working with Arduinos. A task which C is more suited for than Python. Recently though, I started learning Python for Data Science, and this is what I found:

For-Loop Examples

To illustrate what I mean, here is an example from each language which prints a list of where different medals place. A minor task, but, looking at it, you can see some large differences in the code. Then there are the things which look not too dissimilar, but really are. And lastly a very minor change which hints at differences at the heart of the languages.

C:

#include <string.h>

char medals[3][7];
strcpy(medals[0], "Gold");
strcpy(medals[1], "Silver");
strcpy(medals[2], "Bronze");

int i;
for(i=0; i<=2; i++){
printf("Medal %d is %s \n", i+1, medals[i]);
}

Python:

medals = ["Gold", "Silver", "Bronze"]
for i, m in enumerate(medals):
print(f'Medal {i+1} is {m}')

What is a Word but a Collection of Letters?

I guess we should start at the top, at what certainly makes the biggest difference visually: creating the list of medals. Looking first to Python we see that we can declare the variable medals to be a list of strings by context. Strings because they’re in quotes (or apostrophes). List because they’re in square brackets.

However C doesn’t have a string data type. In C, strings are a list (or array) of characters, so a list of strings is an array of arrays of characters. (You’ll also notice that I’ve included the string.h library to help enter the words into the array.)

But Matthew, why did you set the length of the character arrays to 7 when the longest word has only 6 letters?
— I’m glad you asked. In C there is one thing more to a word than just a collection of letters: The null character: \0. C uses this to mark where the word ends. (To experiment I changed the length to 6 and the second line came out as: Medal 2 is SilverBronze)

Syn-taxing

The next big difference for how they look is the syntax. Of course the syntax is different between the 2 languages, but C has more of it. This makes C, to me, just feel a lot clunkier to code. At the end of every line you need a semi-colon. To group lines of code into a loop or function they need to be in braces.
Python code needs to perform the same syntactic functions but it does so through structure. Unless otherwise stated, the end of the line is the end of the line. A loop will begin with a colon but the code will be grouped by indent, which most editors put in automatically; And although C would still work without being indented, it still is for readability.

The For-Loop

Perhaps the biggest change I found when coming to Python was the for-loop. It’s just so alien. I was used to declaring the variable I was going to use to iterate (i, above) and having to set it all up: Initialise the variable to the start number (i=0); Enter the condition used to test whether to continue the loop (i<=2); and give it a function to update the variable(i++). I’d then use this changing number on whatever needed to be iterated through.

Python is completely different. Python has types with iterability coded into their very bones. To be iterable in Python the type, or class (a user-defined type, though these days the difference is negligible), must have an __iter__ function and a __next__ function. __iter__ returns an iterator for the object, and __next__ returns the next element. With these tools there’s no need to create an external variable to iterate over. The Python for-loop creates an iterator object for the medals list and tries to get the next element, assigning it to i. When it reaches the end and can’t get another element it raises a StopIteration exception which it handles by ending the loop.

In some cases you might want a number to climb with the iterations of the object. C would have this already for you, so I thought it only fair to show this case in my example. This is what Python’s enumerate function gives you. This leads me to:

Multiple Assignment

I noticed another difference inadvertently demonstrated: A slightly obscured example of multiple assignment. The enumerate function returns two values: the climbing integer and the corresponding element; and these are assigned to i and m -in a single line! The closest C gets to multiple assignment is assigning different variables to the same value.

Type Assignment:

Take a look at the for-loop in C, look at the i. The mere iterator of this humble loop here to do one job before we cast it aside- Even this i needs its type to be declared, as does every variable. Whereas in Python you don’t have to declare anything, you’re free to just assign anything anywhere. You want to assign something to a new variable? -Just do it. It’s a different type? -No problem.

So why is this? Well, in C you need to tell the computer what type the variable is, so it knows how much memory to assign to it. I.e. the line

int i

-sets aside a piece of memory, large enough for an integer, which we can refer to as i. A C variable is solid: you can go and find it, manipulate it; assign it to another variable (e.g. int j = i) and you end up with two separate chunks of memory with the same value.

Python works a bit differently. A python variable is more ephemeral. Sure the value exists in memory, in fact the type is stored with it, but the variable is just a name that gets attached to it. When you change the value of a variable, the new value is calculated, written to memory somewhere, and the variable is reassigned to the new section of memory.

There are times, though, where you need a variable with no value to have a type. For these occasions Python does have type declaration, but it’s subtler and done by syntax:

Dictionary = {}

Python Variables: You have the right to remain undeclared. But it may error your run if you do not declare anything whose method you later rely on in c̶o̶u̶r̶t̶, er… code.

Finally

Coding in Python seems sleek, nimble. I’ve found it to feel a lot more free than coding in C. Like that point in Autumn where I change from trainers to football boots: more of my energies go into getting me to where I want to be, not wallowing in words, or sliding on syntax. But this comes at a cost. Though easier to code, it takes more effort to run, and is slower than C (There’s a whole interpreted-vs-compiled thing going on which I shan’t get into here).

P.S.

However there is one thing quicker to code in C. In the C for-loop you’ll find i++, this raises the value of i by one. The equivalent in Python is i +=1 which isn’t nearly as nice. I suspect this arises from the difference in variable storage I mentioned when talking about type assignment.

--

--