I want to invoke those C function or executables in python. Is that possible.

Yes, you can write C code that can be imported into Python as a module. Python calls these extension modules. You can invoke it from Python directly, an example from the documentation:

Python Code

import example
result = example.do_something()

C Code

static PyObject * example(PyObject *self)
{
    // do something
    return Py_BuildValue("i", result);
}

If I want the C code to be a library, which means I use it with #include and linkage of the *.o likely in python, how to do it or is that possible.

You build it as a shared library *.dll or *.so You can also investigate using distutils to distribute your module.

If I write the C code into executable, which means it becomes a command, can I invoke it in python directly?

If you write a *.exe then you are doing the opposite (invoking Python from C). The method you choose (exe vs shared library) depends on if you want a "C program with some Python" or a "Python program with some C".

Also, I heard that python code can be compiled, does that mean we can execute the code without the source file? Are the output files binary files? Does it improve performance?

Python reads *.py files and compiles to *.pyc bytecode files when you run it. The bytecode is then run in the Python virtual machine. This means "executing the same file is faster the second time as recompilation from source to bytecode can be avoided." (from the Python glossary) So if you haven't edited your *.py files, it will run the *.pyc. You can distribute *.pyc files without *.py files, however they are not encrypted and can be reverse-engineered.

Answer from Jacqui Gurto on Stack Overflow
🌐
Real Python
realpython.com › c-for-python-programmers
C for Python Programmers – Real Python
January 25, 2023 - Free Download: Get a sample chapter from CPython Internals: Your Guide to the Python 3 Interpreter showing you how to unlock the inner workings of the Python language, compile the Python interpreter from source code, and participate in the development of CPython. The preprocessor, as the name suggests, is run on your source files before the compiler runs. It has very limited abilities, but you can use them to great advantage in building C programs.
🌐
University of Toronto
cs.toronto.edu › ~patitsas › cs190 › c_for_python.html
C for Python Programmers
One major difference between C and Python is simply how you go about executing programs written in the two languages. With C programs, you usually use a compiler when you are ready to see a C program execute; by contrast, with Python, you typically use an interpreter.
Discussions

Can we use C code in Python? - Stack Overflow
I know that Python provides an API so you can call Python interpreter in C code, but what I want is the opposite. My program needs to use some C API, so the code must be written in C. But I also w... More on stackoverflow.com
🌐 stackoverflow.com
How are Python and C related? I've read that Python is 'made from C'. Does that mean that Python is just an abstraction of lots of large C functions?
Python runs on an interpreter* called CPython. The interpreter takes in Python code (,compiles it into an internal representation) and runs it. CPython, as the name suggests is written in C. So, when you run a Python script like "print(5+5)", CPython, the program you usually run with the python command, reads the line, lexes it into Tokens like print, (, 5, +, 5, ) then transforms it a couple more times, until it ends up with a bytecode representation, something like: LITERAL 5, LITERAL 5, ADD, PRINT, where LITERAL pushes the number 5 onto the "stack", ADD takes the two items highest on the stack, removes them from it, and pushes their sum back onto it and PRINT removes the top item from the stack and prints it. So it works like this: INSTRUCTION: {} STACK: [] INSTRUCTION: LITERAL STACK: [5] INSTRUCTION: LITERAL STACK: [5, 5] INSTRUCTION: ADD STACK: [10] INSTRUCTION: PRINT STACK: [] stdio: "10" It's not an abstraction in C per se, as in, a C compiler can be not involved in the life of a Python program. The interpreter is compiled on someone else's computer, you can download it, and it will work without a C compiler. CPython doesn't compile Python to C, so there never is a point where your python program is a C program. *- that's the most commonly used implementation anyway More on reddit.com
🌐 r/learnprogramming
25
70
August 29, 2022
Coming from C, how should I learn Python? - Stack Overflow
I've got a good grasp on C, my first programming language. I know a reasonable number of tricks and techniques and have written quite a few programs, mostly for scientific stuff. Now I'd like to branch out and understand OOP, and Python seems like a good direction to go. More on stackoverflow.com
🌐 stackoverflow.com
How hard is it going to be to learn C after python ?
It for sure is manageable and you will get used to it. Already knowing a programming language will help a lot (C was my fist programming language) the syntax of python was always a bit weird for me as im used to C so it could be similar for you wit the C syntax maybe. But i think it's quite intuitive once u got used to it. Also you are usually closer to the hardware when writing code but also not nearly as close as assembly for example so i think it will take a bit of practice but you will get used to it. More on reddit.com
🌐 r/C_Programming
23
8
August 2, 2020
🌐
GeeksforGeeks
geeksforgeeks.org › python › using-c-codes-in-python-set-1
Using C codes in Python | Set 1 - GeeksforGeeks
July 11, 2025 - As it is very evident that many of Python’s built-in libraries are written in C. So, to access C is a very important part of making Python talk to existing libraries. There is an extensive C programming API that Python provides but there are many different to deal with C.
Top answer
1 of 2
128

I want to invoke those C function or executables in python. Is that possible.

Yes, you can write C code that can be imported into Python as a module. Python calls these extension modules. You can invoke it from Python directly, an example from the documentation:

Python Code

import example
result = example.do_something()

C Code

static PyObject * example(PyObject *self)
{
    // do something
    return Py_BuildValue("i", result);
}

If I want the C code to be a library, which means I use it with #include and linkage of the *.o likely in python, how to do it or is that possible.

You build it as a shared library *.dll or *.so You can also investigate using distutils to distribute your module.

If I write the C code into executable, which means it becomes a command, can I invoke it in python directly?

If you write a *.exe then you are doing the opposite (invoking Python from C). The method you choose (exe vs shared library) depends on if you want a "C program with some Python" or a "Python program with some C".

Also, I heard that python code can be compiled, does that mean we can execute the code without the source file? Are the output files binary files? Does it improve performance?

Python reads *.py files and compiles to *.pyc bytecode files when you run it. The bytecode is then run in the Python virtual machine. This means "executing the same file is faster the second time as recompilation from source to bytecode can be avoided." (from the Python glossary) So if you haven't edited your *.py files, it will run the *.pyc. You can distribute *.pyc files without *.py files, however they are not encrypted and can be reverse-engineered.

2 of 2
16

You don't necessary need to extend Python (which is not trivial, btw), but can use foreign function interface such as ctypes.

Top answer
1 of 12
159
Python runs on an interpreter* called CPython. The interpreter takes in Python code (,compiles it into an internal representation) and runs it. CPython, as the name suggests is written in C. So, when you run a Python script like "print(5+5)", CPython, the program you usually run with the python command, reads the line, lexes it into Tokens like print, (, 5, +, 5, ) then transforms it a couple more times, until it ends up with a bytecode representation, something like: LITERAL 5, LITERAL 5, ADD, PRINT, where LITERAL pushes the number 5 onto the "stack", ADD takes the two items highest on the stack, removes them from it, and pushes their sum back onto it and PRINT removes the top item from the stack and prints it. So it works like this: INSTRUCTION: {} STACK: [] INSTRUCTION: LITERAL STACK: [5] INSTRUCTION: LITERAL STACK: [5, 5] INSTRUCTION: ADD STACK: [10] INSTRUCTION: PRINT STACK: [] stdio: "10" It's not an abstraction in C per se, as in, a C compiler can be not involved in the life of a Python program. The interpreter is compiled on someone else's computer, you can download it, and it will work without a C compiler. CPython doesn't compile Python to C, so there never is a point where your python program is a C program. *- that's the most commonly used implementation anyway
2 of 12
14
Python and C are not directly related. The official Python interpreter named CPython is written in C, but that has nothing to do with the Python language itself. Other people have written other Python interpreters in other languages.
🌐
Aimore Technologies
aimoretechnologies.com › software training institute in chennai › python insights › python vs c: differences, pros & cons decoded
Python vs C: Differences, Pros & Cons Decoded
November 4, 2025 - In contrast, C is a low-level and compiled language that offers fine-grained control over system resources. It is commonly used in systems programming, embedded systems, and performance-critical applications.
🌐
Python
docs.python.org › 3 › extending › extending.html
1. Extending Python with C or C++ — Python 3.14.3 documentation
Such extension modules can do two things that can’t be done directly in Python: they can implement new built-in object types, and they can call C library functions and system calls. To support extensions, the Python API (Application Programmers Interface) defines a set of functions, macros and variables that provide access to most aspects of the Python run-time system.
Find elsewhere
🌐
Hacettepe
web.cs.hacettepe.edu.tr › ~bbm101 › fall16 › lectures › w12-c-for-python-programmers.pdf pdf
C for Python Programmers BBM 101 - Introduction to Programming I
Python · • Created by Guido van Rossum in the late 1980s · • Allows programming in multiple paradigms: object- oriented, structured, functional · • Uses dynamic typing and garbage collection · Slide credit: Thomas J. Cortina · Building a simple program in C ·
🌐
DEV Community
dev.to › rapidnerd › using-python-and-c-together-d4e
Using Python and C together - DEV Community
August 8, 2018 - How to use Python and C in the same project. Tagged with python, c, tutorial.
🌐
Unstop
unstop.com › home › blog › 20+ differences between c and python explained with examples
20+ Differences Between C and Python Explained With Examples
August 30, 2024 - C is a compiled language that directly translates code into machine code for faster performance, while Python is an interpreted language that executes code line-by-line, which introduces overhead and generally results in slower performance.
🌐
InfoWorld
infoworld.com › home › software development › programming languages › python
PythoC: A new way to generate C code from Python | InfoWorld
December 19, 2025 - Generating C code with Python has typically involved libraries like Cython, which use type-annotated Python code to generate C extension modules for Python. A new project, PythoC, takes a different approach.
🌐
Rose-Hulman Institute of Technology
rose-hulman.edu › class › cs › csse120 › Resources › C › Python_vs_C.html
Python and C -- Comparisons and Contrasts
/* Simple C program */ #include <stdio.h> int main() { int year = 2007; printf("Hello World!\n"); /* Notice that C's printf does not automatically put in a newline */ printf("CSSE 120 changed a lot in %d.\n", year); return 0; }
🌐
DigitalOcean
digitalocean.com › community › tutorials › calling-c-functions-from-python
Calling C Functions from Python | DigitalOcean
August 3, 2022 - If you change the C program file, you will have to regenerate the shared library file. The python default implementation is written in C programming and it’s called CPython. So it’s not very uncommon to use C functions in a python program.
🌐
Medium
medium.com › @raihansltn › why-is-c-faster-than-python-os-level-perspective-04d6e485e3ae
Why is C Faster Than Python? OS-Level Perspective | by Raihan Sultan Pasha Basuki | Medium
March 15, 2025 - I classify some key differences between C and Python implementation ... Both programs count prime numbers up to 250.000 and print the result, the C version is significantly faster because it is compiled to machine code, yet the Python version is slower because it’s interpreted line by line.
🌐
Python
python.org
Welcome to Python.org
Whether you're new to programming or an experienced developer, it's easy to learn and use Python. ... Python source code and installers are available for download for all versions!
🌐
Wikipedia
en.wikipedia.org › wiki › CPython
CPython - Wikipedia
3 weeks ago - CPython is the reference implementation of the Python programming language. Written in C and Python, CPython is the default and most widely used implementation of the Python language.
Top answer
1 of 7
31

I knew C before I knew Python. No offence intended, but I don't think that your C knowledge is that big a deal. Unless you read very, very slowly, just set out to learn Python. It won't take that long to skim through the material you're familiar with, and it's not as if a Python tutorial aimed at C programmers will make you a better Python programmer - it might teach you things in a different order, is all, and raise some specific things that you would do in C but that you should not do in Python.

Strings in Python actually are somewhat different from strings in C, and they're used differently. I strongly recommend learning them "from scratch", rather than thinking about them in terms of their differences from C strings. For one thing, in Python 2 it's best not to use Python's "string" class to represent strings: there's a separate unicode string class and for practical Python apps (pretty much anything involving user data), you need that. (Python 3 fixes this, making the str class a unicode string). You need to establish a good working practice for unicode/byte data and decode/encode.

A common mistake when learning a second programming language, is to think "I know how to program, I just need to translate what I do in C into Python". No, you don't. While it's true that an algorithm can be basically the same in different languages, the natural way to do a particular thing can be completely different in different languages. You will write better Python code if you learn to use Python idiomatically, than if you try to write Python like a C programmer. Many of the "tricks" you know that make sense in C will be either pointless or counter-productive in Python. Conversely many things that you should do happily in a typical Python program, like allocating and freeing a lot of memory, are things that in C you've probably learned to think twice about. Partly because the typical C program has different restrictions from the typical Python program, and partly because you just have to write more code and think harder to get that kind of thing right in C than you do in Python.

If you're learning the language because you urgently need to program a system/platform which has Python but doesn't have C, then writing Python programs that work like C programs is a reasonable interim measure. But that probably doesn't apply to you, and even if it did it's not the ultimate goal.

One thing you might be interested to look at because of your C experience, is the Python/C API. Python is great for many things, but it doesn't result in the fastest possible computational core of scientific apps [neither does C, probably, but let's not go into FORTRAN for now ;-)]. So if you're aiming to continue with scientific programming through your move in Python, and your programs are typically memory-bus- and CPU-bound doing immense amounts of number-crunching (billions of ops), then you might like to know how to escape into C if you ever need to. Consider it a last resort, though.

You do need to understand Python reasonably well before the Python/C API makes much sense, though.

Oh yes, and if you want to understand OOP in general, remember later on to take a look at something like Java, Objective-C, C++, or D. Python isn't just an OO language, it's a dynamic OO language. You might not realise it from comparing just C with Python, but dynamic vs static types is a completely independent issue from the OOP-ness of Python. Python objects are like hashtables that allow you to attach new fields willy-nilly, but objects in many other OO languages store data in ways which are much more like a C struct.

2 of 7
15

I learned everything I know about Python from the official documentation: http://docs.python.org/

And it's free.