Learning Management Platform for Written Tutorial Series.

Python 3 Hello World - With complete source code

Python 3 Hello World - With complete source code

Python Hello World

In this practical lesson, we will create a simple python application that displays a simple message in the console. we will cover the basic laws that we must abide by to successful please the python language.

Topics to be covered

We will cover the following topics in this lesson

  • Lesson Prerequisites
  • Basic anatomy of a python application
  • Displaying output in the console
  • Accepting input from users
  • Defining and calling functions
  • Python comments

Lesson Prerequisites

For you to successfully complete this tutorial, you will need to have the following applications on your development machine.

  • Python - on operating system like MacOS, this comes out of the box but its usually the lower version. On other operating systems like Windows, the python language does not come preinstalled. You can download python from the official website. For these tutorials, we will be working with version 3 of python.
  • Text-editor or IDE - personally I use Visual Studio Code because it is cross platform, it works and most importantly supports many languages like PHP, C#, Ruby and others. Given I code using a number of languages it only makes sense to choose it. PyCharm is a great IDE that is dedicated to Python development. You can check it out and try it for yourself. Visual Studio Code is free while PyCharm has both a commercial and community version which is free.
  • A decent terminal - this basically comes with the operating system and both Visual Studio Code and PyCharm come with built-in terminals so you don't have to switch between programs for basic issues. On Windows I usually use Git-bash terminal that comes with Git. The command prompt can work but personally I find it weird. Perhaps I will wrote a blog post about that.
  • Basic Knowledge of Programming - this is not really a prerequisite but its an added advantage.

Basic anatomy of a python application

One of the great things about python is that it is really easy to learn. All you need is a single file with a single line to code to create a very basic application that you can execute and pat yourself on the back.

A basic python application is made up of the following

  • A source code file that ends with the *.py extension
  • Valid python code
  • Python interpret to execute the code

A few points worth knowing

  • Python is a case sensitive language. This means print is not the same as PRINT. Calling PRINT when you mean to call the built-in function print results into an interpretation error like variable not defined because python will treat PRINT like it's a variable or something.
  • In other languages, indenting is usually done for code readability. Python treats indents in a religious manner. For example, the code block for a function is defined using indents. If you remove the indents under the function then the code will not compile
  • Everything in python is an object. We will explore this concept more as we proceed.
  • Unlike many modern languages, its not necessary to end statements with semicolons ; in python. However, you can use ; on the same line to terminate statements. E.g. str_var = 'John'; int_x = 1;

Displaying output in the console

Let's now see this in action. Create a folder probably on the desktop and call it Python tutorials

Create a new file on the newly created folder called hello.py

I have opened my folder in Visual Studio Code and it looks like the following

VS Code Python

As you can see from the above image, Visual studio code really makes it easy for us to create python applications and has the necessary tools that we need to navigate our files, write code and execute it, and view the results.

Let's now write our first python code

Add the code code to the file hello.py

print('Hello World')

HERE,

  • print is a built-in python method that prints out the parameter value that is passed it to the terminal. 'Hello World') is the parameter value that we have passed in to the method.

Executing the code

We basically have two ways of doing this in Visual Studio Code. We can run the following command in the terminal like so.

python hello.py

HERE,

  • python calls the python interpreter to execute the filename that is passed in as the argument. hello.py is our source code file that contain python scripts.

The above code outputs the following code in the terminal.

Hello World

 

Accepting input from users

Let's now advance our application a little bit. We will ask the user to enter their name and display a customized personal greeting.

Modify the code to the following

name = input('What is your name? ')

greeting = f'Hello {name}'

print (greeting)

HERE,

  • name = input('What is your name? ') calls the built-in function input that prompts the user to enter the username and assigns the entered value to the variable name.
  • greeting = f'Hello {name}' formats the string as specified by the f at the beginning. {name} is a placeholder that will be replaced with the value of the variable name in the console.
  • print (greeting) prints the formatted greeting in the console

Run the execute button in VS code.

the terminal will prompt you for your name like so.

VS Code Python

Defining and calling functions

In this section, we will look at how to define functions in python. Functions promote code reuse and that's one of the reasons why we love them. In this section, we will create a function that greets the user.

Modify the source code to the following.

def greet():
    name = input('What is your name? ')
    greeting = f'Hello {name}'
    print (greeting)

greet()

HERE,

  • def greet(): the keyword def: is used tp define a new function called greet the : at the end of the definition is needed. Note: the source code that belongs to the function is indented. This tells python that this block of code belongs to the function greet. To end the function code, all we have to do is remove the indentation.
  • greet() is a standard way of calling python code.

Save the changes and execute the code. You should be able to get the same results as the one that you got in the above section.

Python Comments

Comments are simply statements that are ignored by the python interpreter. Comments are usually added for documentation purposes. A comment is defined using the # symbol. Everything that follows after the # will be ignored by Python.

# Calling the execute function
greet()

Summary

Let's wrap up on what we have learnt in this lesson.

  • Python is a cross platform interpreted language
  • All python files must end with *.py file extension
  • You need a text-editor like Visual Studio Code or IDE like PyCharm to write python applications smoothly.
  • The python files are executed in the terminal and print out the results if necessary
  • print and input are some of the most commonly used built-in functions.
  • Functions in python are defined using the def keyword and the : must be added at the end of the function definition. Code under the function is indented to beneath the function definition which ends when the indentation ends.

What next?

In the next tutorial, we will introduce you to variables and learn what role they play in python applications. If you enjoyed this lesson then show us your appreciation by creating a free accounts.


...