Programming with C ,Python 3.x ..

This blog is in continuation of my last blog . I have discussed this blog at  YouTube video in detail. Please SUBSCRIBE to YouTube channel Embedkari  for further discussion on Embedded Systems.

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%

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

In C, we use \ to extend macro definition to multiple lines

  • #define TEST()\

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

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)

You may try ipython  or simply use a text file e.g

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  #To execute file

One can use -i option to analyze the data after executed without terminating it. This is very useful for debugging purpose.

python -i  #Good for debugging purpose

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.

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)
>>> math.sqrt(16)

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 :

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.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.