There exist
different ways to create a Python file and to run it in your computer. The most
complicated ones involve installing a text editor that allows you to create a
file written in Python language and save it as a Python file. This way, you
will be able to open it as a program but edit it later if something goes wrong.
Also it may be necessary to install some kind of shell in which your program
will be running after you have done the file.
But there
is an easier way to get through this process; through IDLE.
IDLE is one
of the programs you accepted to be installed during the “Terms and Conditions”
amd the complements while installing Python 3. You probably didn’t notice, but
this program works as an interactive console, a text editor, a shell, and it’s
able to run your Python files.
In this
post I will show you step by step how to do every of these things.
To find
this program, you can do it by the easy way which is just typing its name on
the search bar for Windows. It looks like a Python file because it has the
Python logo on the right just as the files created in Python.
Once you
select this option, this window will be opened:
This is the
Python Interactive Shell. It allows you to run small pieces of code such as it
is shown in the following example:
Later on,
this can be used for trying new functions and experiment with the results. You
can even use it as a calculator (as Ken Bauer recommends):
DO NOT
CONFUSE THIS WITH CREATING A PYTHON FILE. THE CODE WRITTEN IN HERE CANNOT BE
SAVED AND RUN LATER. – THIS IS NOT
ACTUAL CODE, JUST PIECES OF CODE.
For
actually creating a Python file you need to follow the next steps:
On the “File”
section located in the menu, there is an option which reads “New File”. You
need to select it.
This will
open the following window:
This one
just looks like an empty space ready to be used for writing code on it. For the
purpose of showing how this works, we will write an easy piece of code that
most programmers know:
This
programs asks the user’s name and saves it as a variable called “x”. Later, the
program shows the text “Hello, “ followed by the name you wrote when you were
asked.
For saving
it we just select the same option we did before from the menu on the upper
side; which is “File”. And the we select the option “Save As”.
After this
you can name your file however you want. The only condition is that the name
has to end with “.py”, which means this file is a Python file and not just a
.txt, .jpg, .png, etc file.
Now that we
have our Python file we just need to run it so we can see if it works. For
these we select the option “Run” from the menu shown on the upper side of the
window. After doing this we have two different options which are: Running the
program from the Python Shell, or running the program from the Run Module. If
we select the second option this is what will happen:
Now we know
how to create, save and run a python file, but we still need to know how to
edit it. Let´s suppose that I have already closed IDLE, and let’s say we now
want to say “Goodbye” to the same person. For this we open IDLE and select the
option “File” followed by the option “Open…”.
Now we look
for our file and select it. Once it opened you can edit it however you want.
Once you
are done remember to save the changes.
Now we can
run this file and the output will be different from before.
No hay comentarios:
Publicar un comentario