Home Articles Categories Series
Pythonise Just now

Creating and running Python files | Learning Python Pt. 2

Creating and running your first Python script in the terminal

Article Posted on by in Python
Julian Nash 2 years ago in Python

In this part of the "Learning Python" series, you'll learn how to create Python files and run them from the terminal. You'll be writing your very first Python script!

We'll be using Visual Studio Code to write our Python script and Cmder (the terminal emulator) to run it, but feel free to use your own tools.

Python files

Just like text files have the file extension .txt and HTML files have the extension .html, a Python file is characterised by the .py extension.

To create a Python file, simply save the filename as your_filename.py extension, being sure not leave any spaces in the filename.

You can call the filename whatever you like (with some exceptions which you'll learn about later in this series), however it's a good idea to give them a recognisable name, as opposed to something totally abstract like x.py.

We're going to create a new directory on the desktop using Cmder which we'll store all of our code in throughout this series.

We'll be creating sub directories within this folder for each module of the series too, so you'll be able to go back and look through the code for future reference.

Open up Cmder and navigate to your desktop (or wherever you'd like to create the directory)

We'll call the main directory learning_python


mkdir learning_python

Now move into the directory with the cd command:


cd learning_python

We'll create our first subdirectory called python_files using the mkdir command and move into it


mkdir python_files
cd python_files

And finally, let's create our first Python file! We're going to use the touch command and name out file main,py:


touch main.py

Let's write some Python code!

Writing Python code

Python code is written line by line in a .py file and is read from top to bottom.

We're not going to explain the code in detail (it's only a couple of lines) as you'll be learning about it in the next part of this series.

You can open up Visual Studio Code from Cmder with the following command:


code .

This command will open up VS Code in the current directory you're in. You could also do the same by right clicking on the learning_python directory and selection "Open with Code".

The first line of Python (Feel free to change the name to your own!):


my_name = "Julian"

I don't want you to be too concerned about the code here, but so you know what we've done:

  • We've created a variable called my_name and assigned it the value of "Julian" using the = operator
  • A variable is a placeholder to store a value or values, which we can then reuse over and over again
  • "Julian" is a string, which in programming terms is a string of characters wrapped in quotations or apostrophes

Let's add one more line of code below:


my_name = "Julian"


Let's quickly look at the second line:

  • We're using the print function and passing in the my_name variable in between the parenthesis ()
  • The print function in Python is used to print something out to the terminal. Whatever values we pass in will be printed for us to see

print is one of the built-in functions that comes with Python and you'll be learning all about functions soon!

Can you guess what might happen when we run this file? Let's find out.

Tip - Make sure you save your Python file. Use Ctrl + s in VS Code to save it

Running Python files

Running a Python file (or scripts as they're commonly referred to) is extremely simple.

To run a Python file form the terminal, simply use the python command followed by the name of your file!

For example (the opposing arrows <> show where you should replace the value for your own):


python <filename>.py

If you're not in the same directory, you can also provide the full path to your Python file, like so:


python /path/to/your/python/file/<filename>.py

We called our file main.py, so we'll run it with the following:


python main.py

You should see the following output in your terminal:



If you changed the value for my_name, you'll of course see that instead!

Let's spice up our script by printing out some more information.

Replace print(my_name) with the following:


print(f"My name is {my_name}!\n" * 10)

Again, don't worry too much about what's happening here! You'll learn all about strings and variables in the next part of the series.

We're using something called "string interpolation" to insert the my_name variable into another string, which we're then multiplying by 10 and passing it to the print function.

For now, go ahead and save the file and run it again form the terminal:


python main.py

You should see:


My name is Julian!
My name is Julian!
My name is Julian!
My name is Julian!
My name is Julian!
My name is Julian!
My name is Julian!
My name is Julian!
My name is Julian!
My name is Julian!

Now wasn't that fun! 馃槃

Wrapping up

This guide was to teach you how to write and run Python files and I hope you've learned how easy it is.

Key points to remember:

  • Python files must have the .py file extension
  • Don't use spaces in your filenames, instead, use underscores or dashes
  • Run a Python file from the terminal with the following:


python <name_of_your_file>.py

Next in the series - Variables and strings

Last modified 27 Feb 2019
Did you find this article useful?
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License