Introduction to Python

Python is a general purpose , interpreted , interactive and object oriented programming language.

It supports both functional and structured approach as well as object oriented programming.

It is easy to learn, use and understand.

IDE available: IDLE,PYCHARM,SPYDER,PYDEV etc

LETS START LEARNING PYTHON

Steps for Installing an Python IDE

Select any python IDE of your choice and install the same.

Here we will see about installing PyCharm

  1. Download the PyCharm package from https://www.jetbrains.com/pycharm/

2)   Run the downloaded package and open the application as below 

3)  Choose the Create New Project and give a suitable name for the project of your choice.

4)  Choose Base Interpreter as Python3.8 (preferable version greater than 3) 

5)  In the left pane , you will be able to view the created project

6)  Let’s start with a basic program to better understand the python structure.

7) Go to File -> New and create a python file and give a suitable name.

8)  Python file will be created in .py format and will be placed under the project created.

9)  Lets write a simple code in python to do addition of two numbers based on users input.

10) To run the program , Go to Run -> Run the .py file 

Let’s understand the program :

In this program we are defining a Class called Sample which has two methods __getinput() and sPrint() methods

__getinput() is a private method and it is accessible only within the class.Private methods are declared by double underscore before the name __

sPrint() is a public method and can be accessed in all the classes of the package.

s1 is the object created for the class Sample

s1.sPrint() statement calls the methodPrint() 

sPrint() method in turn calls the private method __getinput()  to get the inputs from the user.

__getinput() returns the user inputs and sPrint() performs the addition of two numbers and prints the result.

Declaration, Assignment and Printing Data

For any new programmer, who is keen to learn coding, the initial step would be to strengthen the thinking process. Logical thinking is a key to be a successful programmer. Syntax will only change in programming. So all programming languages will have if else, for loop, while loop, switch case, classes, objects, references, constructor etc. It is the person’s logical think tank which plays a crucial role in writing an efficient program.

Rule1: Never think about writing an efficient fast effective code.

Rule2: Have confidence on what you write. It can go wrong. But make sure that you take the lesson learned from writing that wrong code.

Rule3: Always hold the Rule 2 in mind.

Rule4: Say you have written some ten lines of code. First step would be to write a program without syntax error

Rule5: Strengthen your thinking with those 10 lines of code. Ask yourself  ‘what I wrote’ ‘How I started’ ‘How I ended’. Repeat this Rule 5 till you are  +∞ percentage sure about the logic. This is actually an exercise. This should stimulate your brain to think, provided you have the necessary interest. Now let’s strengthen the thinking with “Declaration, Assignment and Printing Data” using ABAP program.

Be it any field, Physics, Chemistry, Maths input should come out as output in the respective form. Look at the below figure.,

Learn ABAP programming with these below steps.

  1. First have sample data written in a rough sheet. Define those data with data types. For example ‘New Born’ is a name of string type. Similarly, figure out the data type of each data.
  2. Declare meaningful variable names for the data with the data type in ABA editor.
  3. Input the data which is assigning the data to the variables defined.
  4. Process the data. For example, if age<40, then the generation type is young
  5. Print out the data processed.

Below figure shows the direct way of doing it in ABAP

The same can be achieved through structures and internal table. Declaration, Assignment and printing out data is similar through structures and internal table. But the data variables are declared inside the structure. Data is assigned through structures. Each line of record through structures is stored in internal table. Then it is printed out by looping through the internal table. Below figures explains about structure and Internal table.

Example:

Variable declaration

TYPES: BEGIN OF st_corona,

Person_id TYPE int4,

Age TYPE int4,

Gender TYPE char01,

First_name TYPE string,

Last_name TYPE string,

Address TYPE string,

Postalcode TYPE c length 10,

Infection_rate_city TYPE decfloat34,

END OF st_corona.

DATA lt_corona_table TYPE TABLE OF st_corona.

DATA ls_corona_struct TYPE st_corona.

DATA:  r_desc TYPE REF TO cl_abap_structdescr,

Wa_comp TYPE abap_compdescr.

DATA l_string TYPE string.

DATA stru_string TYPE string.

FIELD-SYMBOLS <fs_field> TYPE any.

Data Assignment

Ls_corona_struct-person_id = ‚1000‘.

Ls_corona-_struct-age = 31.

Ls_corona_struct-gender = ‚M‘.

Ls_corona_struct-first_name = ‚New‘.

Ls_corona_struct-last_name = ‚Born‘.

Ls_corona_struct-address = ‚new no.12 streetnew 2nd street‘.

Ls_corona_struct-postalcode = ‚600021‘.

Ls_corona_struct-infection_rate_city = ‚0.8‘.

APPEND ls_corona_struct TO lt_corona_table.

Printing Data

R_descr ?= cl_abap_typedescr=>describe_by_data( ls_corona_struct ).

WRITE: / ‚Corona details‘.

LOOP AT lt_corona_table INTO ls_corona_struct.

        LOOP AT r_descr->components INTO wa_comp.

                 WRITE: / wa_comp-name.

            ASSIGN COMPONENT wa_comp-name OF STRUCTURE  ls_corona_struct TO <fs_field>.

                  l_string = <fs_field>.

            CONDENSE l_string.

            WRITE l_string.

       ENDLOOP.

ENDLOOP.

WRITE: / ‚End of corona details‘.