Creating a python file

Hi guys!

This is part of a series of building a web application with python. Please note that this is a continuation of the previous part Setting up a Python Project.

Now to continue with our project, inside the directory where we activated the virtual environment, create a new file. Name it main.py. You can name it whatever you want aside from "main" but I'll stick to it because it's the file that we are going to use in the project.

Note

The environment is not yet needed in the following activities but you can leave it activated.

Create main.py

Fig. 1 - main.py file
Fig. 1 - main.py file

In python, we can just start giving statements to the file directly. No need for any code layout if we want. Let's try to print a text to the console window.

The Famouse Hello statement

hello program
hello program

In the main.py file:

1. In the file, type in a print statement.

Fig 2 - print statement
Fig 2 - print statement

2. Run the program.

On the terminal, type in the command:

python main.py

Press the return/Enter key and you should see the result on the console.

Fig. 3 - python run command
Fig. 3 - python run command

In Fig. 3, we can see the result of our program in python.

Congratulations!

You've run a python script!

The program prints out the "Hello, syncster.dev learner!"

We have only one line of code but it is already running. That's how easy to get started coding in python is!

A statement in python is a set of commands in a program that represents its own output or task.

Our statement is using a function called print. More on functions later. So what this function does is it accepts an argument and it prints it out to the screen. That's the basic use of this function.

During testing and debugging of our codes, we would most often use this function so you might get handy with it over time because we will see it again and again.

Summary

In this chapter, we've seen how to create and run a python program with the following steps:

  1. Create a file the ends in .py. In our program, we called it main.py.
  2. Write the python codes inside the file.
  3. Run the program/script using the command python <name_of_file>. In our case, it is python main.py.

This is just a short introduction in creating python file. See you on the next one!

Last Updated:
Contributors: alexiusacademia