First Python Program | Python Hello World program

First Python program:

What Is a Program?

A program is simply a sequence of instructions written in Python to solve a specific problem. In practice, programs are used to solve real-world problems efficiently.

Hello world Program:

The basic milestone for every programmer is writing their first python program that prints the phrase “Hello, World” on the screen.

print("Hello, World")

In the above code, print () is a function. A function is a piece of code that performs a specific task and can be executed using its name. It can take an input. The code above calls the print() function and passes text “Hello, World” as input.

The parentheses () tell Python to execute the print() function. They also enclose all the content that is provided as input to the function.
The quotation marks (” “) indicate that “Hello, World” is text and not something else.

You can use either single quotes ' ' or double quotes " ". Most people prefer single quotes, but double quotes are useful when your text contains an apostrophe (like in English).

Let us write a program to add, subtract, multiply and divide two numbers. In python, the arithmetic operators for addition, subtraction, multiplication, and division are +, -, *, and / respectively.

addition = 1+1
print (addition) 
subtraction = 2-1
print(subtraction)
multiplication = 4*2
print(multiplication)
devision = 17/5
print(devision)

In this code above, in the first two lines, python evaluates the expression 1+1, then saves the result in variable addition. Finally, it prints the result which is 2 in this case. In the subsequent lines, python evaluates all the expressions such as 2-1, 4*2 and 17/5 and save them in variables subtraction, multiplication and division.

Important Note:

In Python 3, the / operator performs true division, returning a floating-point number. If you want to perform integer division, you must use the // operator. These changes were introduced in Python 3 compared to previous versions.

If you are using an older version, keep in mind that:

  • In older versions, the / operator performed integer division by default when both operands were integers.
  • If at least one of the operands was a floating-point number, the division result was also a floating-point number.

Fortunately, this old behavior was removed in Python 3, as it often led to hard-to-detect bugs in some cases.

Additional Components of Programs

Let us look at some basic concepts that exist on programs of all programming languages:

  • Input: Getting data from the outside world (e.g., files, keyboard, microphone, GPS).
  • Output: Displaying results on a screen, saving them to a file, or sending them to another device.
  • Computation: Performing mathematical operations.
  • Sequential Execution: Running instructions one after another in order.
  • Conditional Execution: Executing certain instructions only if specific conditions are met.
  • Repetition (Loops): Repeating a set of instructions multiple times, often with slight changes.
  • Reusability: Writing a set of instructions once and using them again when needed.

That is almost everything a program involves. Every program you will ever write, no matter how complex, is built from these basic instructions, combined in ways that eventually become simple enough to be executed.

At first glance, these ideas may seem simple—like walking step by step. But the real power of programming lies in combining these elements effectively to solve real problems. We will read about these in next articles.

In the next article, we will read about Types of errors which occur in python and how to resolve them.

No Comments

Leave a Reply