Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

The Influence of C in Python

00:00 When the Python language was originally being designed, the creators were very explicit that Python would retain a lot of compatibility with C and a lot of influence from C. In this lesson, I’ll take you through how that C influence on Python manifests itself in command line applications in Python. All right.

00:20 So, I have a C file here in my editor called main.c, and I’m just going to compile and run it so that you can see how it works. I think once you see how it works, you’ll understand the code as well.

00:33 I’m going to use gcc, which is the general Unix system’s C compiler, to compile this file main.c

00:41 into an executable called main. If you’re interested in the process of C compilation, I would encourage you to read a little bit more about it. Generally, for Linux and Mac terminals, gcc is the way to go, but you’ll have to use something called clang in Windows, or perhaps a Linux Subsystem for Windows, but there’s a lot of complexity there that I would encourage you just to read a little bit more about yourself.

01:03 So now, I have an executable called main that I can just run directly using the ./ notation. And as you can see, it prints out C Main, and then prints out that there’s 1 argument, and that that argument is the actual filename ./main.

01:18 It might be surprising that that’s considered an argument, but remember that really what’s happening here is main is the file being executed, so it’s an argument to the actual shell command that executes files.

01:31 So, that’s counted as an argument. But it will still be counted as an argument even if I pass in a few more arguments. As you can see now, there are now 5 arguments, including this original filename, as well as each whitespace-separated string that I pass in here on the command line. So now looking back at the code, as you can see, what this relies on is two parameters passed to the main() function, and these are passed in by the system itself.

01:59 Those parameters are the integer argc, for arg count, and that’s just the number of arguments, including that filename, and then a character pointer array or, in other words, a string array—because that’s how C handles strings—called *argv[].

02:16 I just print out "C Main\n" and then the number of arguments, and then I loop through all the number of arguments, printing out each argument with its index as I go along.

02:25 So, pretty simple code, and as I show you now the Python version of this code, you’ll see how it’s strongly influenced by this C code.

02:34 So, looking at this Python file what you can see is that there’s an argv, just like there was in C, except it’s now exposed through the system module, or sys module.

02:45 And as you can also probably notice, there’s no need anymore for an arg count, or an argc, because you can just use the length of sys.argv , because Python provides that to you natively. So, there’s definitely some syntactic sugar going on here.

03:00 For example, the enumerate() function that gives easy access to both the index and the actual value in a list. But the general structure of this code is the same, right?

03:10 I’m printing out "Python Main", printing out the arguments count, and then going through and looping through all the arguments and printing them all out.

03:18 One thing that I should note here is that this colon greater than 6 (:>6) notation that I’m using within this format string is just to generate the nice spacing that you see below in the terminal. It’s to pad the space for numbers so that if I were to have some larger numbers in there, it would still show with the same spacing for the arguments.

03:37 So, the philosophy underlying this is actually the same, and that’s by design because the Python designers were strongly influenced by C. So now, let’s see it run.

03:47 I’m just going to clear the terminal so that you have a little bit more space to see here.

03:52 I can use Python to run python main.py and as you can see, it prints out Python Main, still one argument—main.py is still treated as an argument, except maybe, in this case, it’s more obvious why it’s an argument, because it’s being passed as an argument to the python program.

04:08 And then, I can call this with A Few More Arguments, just for consistency’s sake.

04:15 And as you can see, 5 arguments passed, and all of them are now printed out to the command line. So, that’s generally how these command line interfaces work, and in later lessons, I’m going to get into much more detail on sys.argv, how to handle arguments passed in, and what to do with exception handling, for example.

04:33 But for the moment, I think this will suffice. In the next lesson, I’ll tell you a little bit more about how sys.argv actually works.

Become a Member to join the conversation.