Develop easily your first Python program today in 20′

Develop easily your first Python program today in 20'

Develop easily your first Python program, in 20′. I guess you are a bit intimidated, right? How could this be so fast? How could it be so easy?

Python is an easy programming language. It is easy to learn and worth it. It is the most popular language right now. In the following tutorial, you will learn how to develop your first program in Python. It will take you some minutes depending on the technical experience you have. Whether it takes 15′ or 45′ you gain knowledge and maybe the first step towards another life! So, sit back and develop easily your first Python program.

Introduction

Install Python

Download Python installer

The first thing you need to develop easily your first Python program is, you guessed it, Python. You need to download Python from the official website python.org.

Develop easily your first Python program today in 20'
Download installer 1

As seen in image 1, go to the second tab Downloads, your operating system (here Windows is highlighted), and then the last version of Python which at the current day is 3.8.5. It is important to choose version 3 instead of 2 since there have been major changes in the language.

Download 3.8.5

Run the installer

The downloaded file is a standard .exe Windows executable. You may choose the path or leave the default one and hit the Install Now button. An important note is to make sure you check the box “Add Python 3.8 to PATH”. In that case, Python will be added to the execution path and available in whichever folder you are. This means that you will not need to be in the folder where Python is installed to run the interpreter.

Install Pycharm

The second thing you need to develop easily your first Python program is a tool, an IDE as it is called. This is a tool to help you develop the program you want, organize the files, and run it. There is another way of running the program, through the command-line but in this tutorial, we will focus on the tool since it is easier to grasp what is happening.

The IDE we will use in this small course is Pycharm. You may download it here. I recommend you to download the community version on the right side of the page.

When it is downloaded, you may open it by typing Pycharm in the applications as any other application.

Coding time!

Now that you’ve got your environment set up, it is time to configure what is needed and develop a simple application.

Create a project

Before starting to develop easily your first Python program, you need to create a project. To do that you have to go to File->New Project as shown in the image below.

Create new project

Choose the path and the name of the new project and the interpreter you installed in the previous step (Python 3.8) and click create as shown below.

The project, MyAwesomeProject in that case is shown on the left side in the project workspace.

The new project

Create the file

Now that you have the project, you need to create a new file and write the code. You may do that by right-clicking on MyAwesomeProject -> New -> Python File as shown in the picture below.

Create new Python file

In the new window type Calculator as the name of the file as shown below

Calculator

A new empty file is created. Now you need to write the Python code which will do something. In this example, we will develop a very simple Calculator. The functional and technical details are described below.

Analysis

Functional description

  • Receive input from the user
  • Interact with the user through the run tool window (window displaying the output of our application)
  • Display the result in the run tool window

Technical description

  • Create a class Calculator
  • Create a variable named result
  • Create methods add, subtract, multiply, divide
  • Receive three different inputs from the user: Action, numberA, numberB
  • Display the result in the run tool window

Develop

Now it is time to write the code of the application. You may either copy-paste the code below or type it little by little. It is recommended to type it because you can understand better the purpose and remember it more easily afterward.

Along with the source code, comments are included to explain what the code does line by line.

"""__author__ = "Thanos Floros, strong-programmer.com"
"""


class Calculator:

    # the init function is used to initialize the class
    def __init__(self):
        # in the initialization we are also creating a variable named result and set its value to 0
        self.result = 0

    # print the result of the operations
    def print_result(self):
        print("Current result is {}".format(self.result))

    # In all the functions below we have three arguments, self (instance of the class)
    # the other two, numberA and numberB are the numbers which will be used for the operations
    def add(self, numberA, numberB):
        self.result = numberA + numberB

    def subtract(self, numberA, numberB):
        self.result = numberA - numberB

    def multiply(self, numberA, numberB):
        self.result = numberA * numberB

    def divide(self, numberA, numberB):
        if numberB != 0:
            self.result = numberA / numberB
        else:
            pass


# the way we run this application is "__main__" because it is the main program
if __name__ == '__main__':

    # create a new object my_calculator by calling the class
    my_calculator = Calculator()
    print("The cool calculator!")
    # this means that the program will run indefinitely
    while True:
        # here with the keyword input, we are asking the user to type something in the run tool.
        # it is the actual interaction with the user
        action = input("What should I do? [A]dd, [S]ubtract, "
                       "show [M]ultiply, or [D]ivide?").upper()
        # number should be an integer, that is why the keyword int is here
        numberA = int(input("Input first number for the calculation: "))
        numberB = int(input("Input second number for the calculation: "))
        if action not in "ASMD" or len(action) != 1:
            print("I don't know how to do that")
            continue
        # Depending on what the user has typed, a different action is executed - different function is called
        if action == 'A':
            my_calculator.add(numberA, numberB)
        elif action == 'S':
            my_calculator.subtract(numberA, numberB)
        elif action == 'M':
            my_calculator.multiply(numberA, numberB)
        elif action == 'D':
            my_calculator.divide(numberA, numberB)

        # show the result by calling the function print_result
        my_calculator.print_result()

This may look a bit intimidating in the beginning. However, it will be easier if you check the lines one by one carefully and read the explanation in the comments along with the explanation below.

class Calculator:

Python is an Object Oriented Programming language. Applications have objects which represent the world. Class, in Python, is a template, or else, a blueprint of an object. In our example, we are creating a class from which an object will be created so that we can do something with it.

Classes include functions that do something, an action. In our case they do the actual operations: add, subtract, multiply, divide. All these operations have three arguments. An argument is a value passed in the function. The first one, self is the instance of the class, which is a concrete concurrence of any object. We need to add it to the declaration of the function.

Run the Calculator application

There are several ways to run the application we have just coded. Here, let’s use the one where we click on the file. So, go to the Calculator.py file on the left of your screen and right-click like shown in the image below. You may also do it with the shortcut Ctrl+Shift+F10

This action will open the run tool in the bottom of the screen. It will ask you to type the operation and the two numbers we were talking about.

Now we can interact with the application and use it to add, subtract, multiply and divide, as a Calculator does. Here is an example that uses all the operations.

Conclusion

There you go, congratulations on following the article “Develop easily your first Python program” until the end. Well done for implementing and running your first Python application! It wasn’t as hard as you thought, right?

If you have any questions or comments you may add them below or contact me directly through the contact page.

If you liked the article “Develop easily your first Python program” and you would like to read more you may open the articles page here and also subscribe to have access to the newsletter.

Leave a Comment