C to Python: A Beginner's Guide to Making the Transition

C to Python: A Beginner's Guide to Making the Transition


4 min read

The above joke sounds a lot funnier if you've made the transition from python to C/C++ or vice versa ๐Ÿ˜…. As for me, I did both, I transitioned from python to C to python; so trust me when I say I can relate.

What Does C Look Like?

Well, apart from the fact that C can be fun as long as you don't get segmentation faults, C is extremely fast, It runs on all operating systems and it provides you with so much power.

We all argue that although C has all these features that appeal to programmers, it is quite raw and low level, so low level that strings aren't a standard datatype.

Although C has a standard library that provides almost everything you'll need to create a basic or even sophisticated program, you'll find yourself writing most of the functionality you need.

Another thing that is worth mentioning is pointers, sigh. If there's something that's dreaded in C, it's pointers. Pointers are an integral part of C and even the so-called strings are basically pointers too. Pointers tend to get intense really fast and you start dealing with function pointers and then the whole world seems to be crashing down. Imagine trying to read this:

void (*(*fn[])())();

Nonetheless, C gives you more control over memory, and with C the possibilities are endless. C has been the father of so many other programming languages, including python.

What Does Python Look Like?

Python looks like a game, you fit a bunch of English words together with the right amount of indention and magic happens โœจ.

Python has been made with simplicity in mind and the learning curve is not so curved, so learning python would take a couple of weeks to a few months if you're coming from C/C++.

Python has been used to teach kids programming because of its simplicity and ease of use. Python also has a large community so it'll be easier to find help if you ever get stuck.

Python From A C Perspective

I don't think python should be compared to C considering the fact that python and C are connected in amazing ways and you can leverage both python's simplicity and C's speed by using CPython, Cython or shared libraries.

When I moved to C from python, I noticed C was very verbose, which meant you'd have to specify and create almost every bit of functionality you needed. I also noticed that I didn't have to indent (also it's advisable to do that) but there were semicolons everywhere. Also, I found that pointers expose the way memory works and this is what python tried so hard to hide from us.

When I moved back to python, I wondered where all the semicolons and pointers went ๐Ÿ˜‚๐Ÿ˜‚. In python you can do something in 3 lines which would take you up to 20 lines in C. Python provided almost everything you could ask for out of the box. Jeez, I was blown. Python was like C for toddlers. But, it seemed to come at a huge price, which we pay dearly in bytes.

So when you write C, you're conscious of how much memory your program is using, it's hardwired into us (or maybe softwired ๐Ÿ˜‰). I created an integer in python when I checked its size, I was beyond shocked...

>>> import sys
>>> number = 56
>>> sys.getsizeof(number)

Python is using 28 bytes to store something that'll take 4 bytes in C !!!

So that's the tradeoff for all the cool stuff that python offers. Also, you do not need to declare variables in python ๐Ÿ˜, sweet right?

How To Make The Switch

No matter what your background is, chances are, you'll work with different programming languages during your career, use these tips to guide yourself.

programming languages are just syntax

There are general concepts that appear in every programming language, focus on learning these concepts and you'll find it easy to adapt fast. Key concepts include but aren't restricted to

  • variables

  • loops

  • conditionals

  • data structures

  • functions

  • I/O

get a high-level overview of the new language

You need to know what the new programming language would look like so you can start conditioning your mind in time. You can do this by looking at some codes in the new language (e.g python), this would give you an idea of what the syntax looks like generally.

be optimistic

believe you'll love the new language and chances are, you will.

So, I still think Python and C are so cool that using them together would be so much fun. Tell me what you think, which would you rather go for?