Just pass regular Python code as the argument to the flag:
python -c 'print 1
print 2'
Import modules works, and blank lines are OK, too:
python -c '
import pprint
pprint.pprint(1)
'
When using this feature, just be mindful of shell quoting (and indentation), and keep in mind that if you're using this outside of a few shell scripts, you might be doing it wrong.
Answer from Thomas Orozco on Stack OverflowHow to use the -c flag in python - Stack Overflow
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?
Understanding what CPython actually IS has greatly enhanced my understanding of Python.
Can we use C code in Python? - Stack Overflow
Just pass regular Python code as the argument to the flag:
python -c 'print 1
print 2'
Import modules works, and blank lines are OK, too:
python -c '
import pprint
pprint.pprint(1)
'
When using this feature, just be mindful of shell quoting (and indentation), and keep in mind that if you're using this outside of a few shell scripts, you might be doing it wrong.
Easiest example
python -c "print 'example'"
It is useful whenever your program has a single line of code, for example, list comprehensions, etc.
Another example can be
python -c "a='example';print a"
As you can see, multiple statements are separated by ;
Any clarification is welcome!
First off, its perfectly understandable to not really care about language theory as a beginner. This stuff is not necessary to learn to code.
However, after recently doing some deep dives on what CPython really is and how it works, I have found the knowledge to be extremely enlightening. And it has really opened my eyes as to how Python is used, and why its used in the places it is.
For those who are unaware, allow me to share what I've learned.
So the key piece of information is that CPython is, at its core, a program written in C. Its purpose is to take Python code as input, then convert that Python into its own native instructions (written in C), and then execute them. And perhaps most importantly, it does this in a line-by-line manner. That just means it doesn't try to error check the entire program before running it. Potential errors just happen as it goes through each line of code, one by one.
However its also important to understand that Python is actually still semi-compiled into "bytecode", which is an intermediate stage between Python and full machine code. CPython converts your python scripts into bytecode files first, so what it actually runs is the bytecode files.
Now where it gets super interesting is that CPython is not the only "implementation" of Python (implementation means some kind of program, or system, that takes Python code as input and does something with it). More on that later.
On the subject of bytecode, it naturally leads to some other interesting questions, such as "Can I share the bytecode files?", to which the answer is no. That's one of the key aspects of CPython. The bytecode is "not platform agnostic". (I'm really sorry if that's not the correct term, I just learned all this stuff recently). That means the bytecode itself is compiled for your specific environment (the python version and dependencies). The reason for this is that its part of Python's design philosophy to be constantly improving the bytecode.
Once you understand that you can then comprehend what other implementations of Python do. PyPy for instance aims to make a Python running environment that works more like Java, where it performs "just-in-time" compilation to turn the bytecode into native machine code at runtime, and that's why it can make certain things run faster. Then you have the gamut of other ways Python can be used, such as:
-
Cython - aims to translate Python into C, which can then be compiled
-
Nuitka - aims to translate Python into C++, which is more versatile and less restrictive
-
Jython - this semi-compiles Python into Java bytecode that can be run in a Java virtual machine/runtime
-
IronPython - semi-compiles Python into C# bytecode, for running in .NET runtime
-
PyPy - A custom JIT-compiler that works in a manner philosophically similar to Java
-
MicroPython - a special version of python that's made for embedded systems and 'almost' bare-metal programming
Oh and then there's also the fact that if you want to use Python for scripting while working in other languages, its important to understand the difference between calling CPython directly, or using "embedded" CPython. For instance some game coders might opt to just call CPython as an external program. However some might opt to just build CPython directly into the game itself so that it does not need to. Different methods might be applicable to different uses.
Anyway all of this shit has been very entertaining for me so hopefully someone out there finds this interesting.
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.
You don't necessary need to extend Python (which is not trivial, btw), but can use foreign function interface such as ctypes.