Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
4: Writing and Running Your First Script
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.
There are no comments for now.