Programming C ,Python 3.x

My first objective to provide working knowledge of Python with a simplified and a practical approach.  This is the reason , I am comparing it with well known programming language C.

Please install Python from this before proceeding further. I will refer to C as well for better understanding of Python. I had discussed about use of Eclipse IDE for practice in C .

Why version matters ?

The latest C standard has seen many changes since 1978. The history provides a summary about features of original K & R , ANSI C, C89 or C90 , C95, C99, C11 and C18 . If  you observe some unexpected result or error, verify Python version.  Similarly  take a note of Python version being discussed , If some feature discussed is missing  in your installation. Latest versions should have backward compatibility.

Same thing applies to Python as well. Python major version differences are described here. I am using python 3.6.6

C Keywords

C Keywords snapshot from WiKi

lang1

What is the size of int 2 bytes or 4 bytes ? 

Please refer to Type Layout of  AVR ABI  for various data type sizes to be used in C programming of AVR based devices. The C/C++ data type int is defined as 2 bytes. You may find data type detail of ARM architecture here . The C/C++ data type int is mapped to signed word of 4 bytes. It is generally 4 bytes in 32/64 bit systems.

  • Array defines  multiple data elements of same type:   Ex int marks[]
  • struct defines multiple data elements of different types:
  • In C++ , struct or  class can define  multiple data elements of different types along with function. class provides many other features such  as Inheritance, Polymorphism, Abstraction,Encapsulation etc. The instance of a class is called object and so C++ is called OOPS (Object Oriented Programming System).

Python Keywords

lang2

If you compare keyword in C and Python, one major change is absence of data types. That is the main reason for popularity of Python in non-programming background professionals. So data memory management is being taken care automatically by Interpreter.

Identifier: User defined names for variables,functions,structure etc

Both C and Python have rules to define valid identifier. A sequence of digits and letters can form a valid identifier provided it is not part of Keywords list. It can also has _ symbol. In C,  If  you may validate this during compilation. However in Python, there is no offline compilation so you can verify this in two parts :

  • Is the defined name a keyword ? If False then do second step
  • Is the defined name a valid Identifier ?

lang3

Variable : Storage with a symbolic name (Identifier)

In C, You must declare a variable before use.

  • int count ;

In Python, You must initialize a variable before use. Variables are actually objects here and data type is dynamic.

  • >>> myvar='temp'
    >>> type(myvar)
    <class 'str'>
    >>> myvar=21
    >>> type(myvar)
    <class 'int'>

C allocates a memory location to a variable but Python allocates unique ID to object for its lifetime. This ID is for internal purpose of Interpreter.

  • >>> temp=1
    >>> id(temp)
    1654680608
    >>> temp=2.5
    >>> id(temp)
    2073498030344
    >>> id(1)
    1654680608
    >>> id(2.5)
    2073498030368
    >>> type(temp)
    <class 'float'>

References :

I have discussed this at Getting started with Python .

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.

Embedkari  provides  100 mins videos on step-by-step Python uses with Cloud and Machine Learning. These videos are under low cost training and kept at Rs500 only.. Please check the detail  of Low Cost Training Option1.

Leave a Reply

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