This is in continuation of Programming in C-Python . It provides information about different Python implementations., built-in functions. Identifying Python version and how it finds modules
What are various Python Implementations ?
As we know there are various C implementations such as ANSI C,C95,C99 etc.
Similarly there are various Python implementations:
- CPython : Written in C. This is the original Python.
- Jython: Written in Java
- PyPy Written completely in Python. Supports advance features such as stackless support, JIT(Just In Time) compiler
- IronPython Written in Python for for .NET applications
Which Python is installed in my setup ?
>>> import platform
>>> platform.python_implementation()‘CPython’
Where is Python installation folder ?
>>> import sys
>>> sys.executable‘C:\\Users\\shreyash\\AppData\\Local\\Programs\\Python\\Python36\\python.exe’
One can find this in Windows cmd line with echo %PATH%
Comments
In C, Line comment start with // while in Python it is with #
Each line in C requires a well defined statement followed by termination (;) .
Each line in Python can have any assignment,expression,function or method without any termination symbol . It can have simple calculator kind of expression e.g. 1+2
Multiple Lines
In C, we use \ to extend macro definition to multiple lines
#define TEST()\ line1\ line2
In Python, we can use \ to make a multiple physical lines(CR LF) statement to a single logical statement.
>>> if rollno > 1 and rollno <20 \ ... and marks >50 :\ ... print(marks)
This is not required for statements written in parenthesis, square or curly brackets
Grouping of Statements
C & C++ uses braces { } to define a block of code or grouping statements .
Python uses leading white spaces or tabs in logical line to start the indentation and end with an empty logical line.
>>> count=10 >>> if count>100: ... print(count) ... >>> print(count) 10 >>>
Interactive Python
You may try ipython or simply use a text file e.g xyz.py
Interactive Python can be used for quick testing of some expression, but whatever command you enter will be lost after experiment. So you need to use a text editor such as notepad/vi to create .py file similar to .c
For ex: python test.py #To execute test.py file
One can use -i option to analyze the data after test.py executed without terminating it. This is very useful for debugging purpose.
python -i test.py #Good for debugging purpose
Module Integration
In C/C++ header file is used to access modules/functions in other source files/library. For ex : #include<stdio.h> in C for accessing printf()
In Python, import statement is used to access modules if it is not part of builtin functions. modules command in help> can provide list of available modules
What builtin functions are available ?
>>> import builtins >>> dir(builtins) ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', ::::::::::::::::::::::::::::::::::::
Using factorial and sqrt functions from math module.
C:\Users\shreyash>python Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import math >>> math.factorial(4) 24 >>> math.sqrt(16) 4.0 >>>
Functions inside module can be found using dir
>>>dir(math)
How Python finds modules ?
PYTHONPATH environment variable can also be used to define module search path. The path utilized to search modules can be found using
>>> import sys
>>> sys.path
References
- History of C language
- Python Documentation
- Python Documentation C:\Users\shreyash\AppData\Local\Programs\Python\Python36\Doc
I have discussed this at Programming C-Python video.
Thanks for reading till end. I am trying to improve usability of my site. Did you find this discussion helpful ? If so, Please subscribe to YouTube channel Embedkari as well for additional embedded related stuff.