Running a Python File

Now it's time to seriously write some code. You may have noticed that when we were typing code directly into the Python prompt, after you hit enter, you can't go back and change any lines. Can you imagine writing larger programs like this? Any time you wanted to back and change something, you'd have to retype everything. "There has to be a better way."

And there is! We can create a Python file, where we can write and edit as many lines of code as we want. Then, we can run the whole file through Python at once. This way, if we need to go back and edit something, we can just change specific lines in the file, without retyping anything.

To be able to do this, we need a program to edit the Python files with. Document editors such as Microsoft Word or Pages won't work for us-- they're not designed to edit plain text like we'll be doing. The most simple and popular text editors are Visual Studio Code and Sublime Text . The lab computers in Sutardja Dai Hall 200 have VS Code and Sublime Text installed. The CS10 course staff prefers Visual Studio Code (VS Code) and may be able to provide greater support for help on using VS Code.

Open a suitable text editor, and create a new file. Title it lab1.py, and save it in the PythonLab1 folder that we made earlier.

You've created your first Python file! Use the text editor to edit the new file so that it contains the following line:


print("Hello, World!")
        

Don't forget to save the file before you try to run it. Otherwise, when you run your program, you'll see the output of whatever you saved last.

Now, let's run the file with Python. Head back to your terminal, and use the commands you learned earlier to navigate to the PythonLab1 directory we made earlier. Once you are there, use the ls command to verify that lab1.py is there!


Alonzos-MacBook:PythonLab1 alonzo$ ls
lab1.py
        

To run your file with Python, you'll use the same Python command from earlier, but with an extra argument: your file's name. So, your command will either be python3 lab1.py or python lab1.py. Type the command and hit enter.

Congrats! You should see the words Hello, World! printed to your terminal. You've just run your file with Python.

Interactive Mode

Did you notice that after the command python3 lab1.py, the prompt was back to something like:

Alonzos-MacBook:PythonLab1 alonzo$?

This means that even though we opened Python to run our program, Python exited when it was done. Sometimes, we'd like to stay in Python after we run a file!

To stay in Python after running a file, use the same command, but with another argument -i, like this: python3 -i lab1.py. Before you run your file with this new option, add the following line to lab1.py using the text editor:


print("Hello, World!")
a = 5
        

Don't forget to save. Now, return to the terminal and run the command python3 -i lab1.py.

Notice how "Hello, World!" is still printed to the terminal, but we are left with a Python prompt, >>>! Get ready for this: type a and hit enter.

We get 5. Python remembers everything from the file that it ran when we opened it! Imagine, we could define all sorts of useful variables and functions in a file, then play around with them in Python.

Now let's write some Python functions >>>