Scripting

So far, you've been practicing new Python concepts (input, output, conditionals) in interactive mode: typing one line at a time in the terminal. That's a great way to experiment and learn, but it has a limitation: once you close the terminal, all your work is gone.

Back in Lesson 1, we introduced Python scripts as a way to save and run your code. Now it's time to put that idea into practice in a more general context.

Why scripts are useful at this stage:

  • You can save and reuse programs without re-typing them each time. 
  • Scripts let you combine multiple steps (like input + conditionals) into one program.
  • You can start building more complex logic and test it over and over.

Recall in the previous lesson, we introduced you to the input()  function. 

With the  input function, we can ask the user to type in a value and store it in a variable:

  Terminal - Python
>>> s = input("Give a number: ")   
Give a number: 20   # user types a number here
>>> print(s)                       
20

For a moment, we ask you to consider logistics. Imagine you have written the program snippet above on your laptop, and are running it through your terminal. In order for another user (or anyone for that matter) to physically be able to type their favourite number as input to the program, they would have to be sitting right next to you. Essentially, we have arrived at a point where the interactive session has lost its utility.

The idea with scripting, the topic of this lesson, is that we want to get to a point where we are writing programs that can accept inputs from users who are running them on their own computers, and do not have to be sitting right next to us and sharing our laptop keyboards. We want to create something that will execute the Python commands elsewhere without typing them in interactively.

This something is, of course, an app.

You have completed 0% of the lesson
0%