Skip to Content
Course content

4: Writing and Running Your First Script

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Up until now, we've been playing in the REPL—that interactive shell where you type a line and Python answers back immediately. It's great for testing a quick idea, but it's a nightmare for actual work. If you make a typo on line four of a ten-line block, you're starting over from scratch. We need a way to save our thoughts.

Moving from the Shell to a File

I want to build something slightly useful: a simple tool to split a dinner bill between a few friends. I'll open my text editor and create a file called splitter.py. I'm using the .py extension because it tells both me and the computer, "Hey, this is Python code."

total_bill = 120.50
people = 4
tip_percentage = 0.15

total_with_tip = total_bill * (1 + tip_percentage)
per_person = total_with_tip / people

print(per_person)

I've saved the file. Now, instinctively, I might try to just type splitter.py into my terminal and hit enter, expecting it to run. But when I do that, I get a "command not found" or it just opens the file in a text editor again. Why? Because the operating system doesn't inherently know that a .py file is an executable program. It's just a text file until we tell the Python interpreter to read it.

Calling the Interpreter

To actually make this happen, I have to call Python first and then pass the file as an argument. I'll try this in the terminal:

python3 splitter.py

There it is. 34.64375. It works, but it's a bit boring. The script is "hard-coded," meaning if the bill changes to $150, I have to open the source code, change the number, and save it again. That's not a tool; that's just a calculator with extra steps. I want this to be interactive.

Making it Dynamic

I'll go back into splitter.py and replace those hard-coded numbers with input(). But wait—I remember from the last lesson that input() always returns a string. If I try to multiply a string by 1.15, Python is going to scream at me.

I'll adjust the code to wrap the inputs in float(), which converts the text into a decimal number:

total_bill = float(input("What was the total bill? "))
people = int(input("How many people are splitting? "))
tip_percentage = 0.15

total_with_tip = total_bill * (1 + tip_percentage)
per_person = total_with_tip / people

print(f"Each person owes: ${per_person:.2f}")

I added a little f-string at the end with :.2f because seeing 34.64375 is annoying when you're dealing with money; I only want two decimal places. Now, when I run python3 splitter.py again, the terminal actually waits for me. It feels like a real program now.

The "Where Did It Go?" Moment

One thing that trips people up when they first move to scripts is the execution speed. When you run a script, Python starts, executes every line from top to bottom, and then immediately exits. If you're running this from an IDE's "Run" button, the window might vanish the instant the program finishes, making it look like nothing happened.

If you find your output disappearing, you can add a dummy input("Press Enter to exit...") at the very bottom of your script. It forces Python to wait for you before closing the session. It's a crude fix, but it saves a lot of sanity when you're starting out.




📋 Practical Task

Build a "Kilometers to Miles" Converter Script

Create a Python script named converter.py that does the following:

  • Prompts the user to enter a distance in kilometers.
  • Converts that value to miles (multiply the kilometers by 0.621371).
  • Prints the result to the console in a clear sentence (e.g., "10 kilometers is 6.21 miles").
  • Ensures the final result is rounded to two decimal places.

Save the file and run it from your terminal using the python3 command. Verify that it handles different numeric inputs correctly.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.