Course Overview | Previous Session | Next Session
Introduction
Up until now, we’ve been writing code directly in the Python interpreter. While this is great for quick tests, all our code is lost when we exit the interpreter. Today, we’re going to start using a text editor to save our code files. This will allow us to keep track of our progress and easily revisit or update our code whenever we need to.
Setting Up Your Workspace
Create a Code Directory:
- Open your file browser and navigate to your home directory.
- Right-click and create a new directory called “code”.
- This is where we will store all our Python files.
Creating a Python File:
- Open the new folder. With the mouse cursor inside the empty folder, right-click and select “Open Terminal Here.”
- Type the following command:
touch session04.py
- The
touch
command creates an empty file where we will write our code.
- The
Open the Python File:
- Double-click the session04.py file to open it in your text editor (e.g., Gnome Text for Gnome users, Kate or KWrite for KDE users).
Powerful Variables
Read this simple sentence:
Sam, my dog, loves to play outside every day. He’s growing fast and now weighs 10 pounds.
Today, we will learn how to make text more dynamic by replacing some words with variable names. This way, we can easily change these values later.
Coding Exercise
With the session04.py
file open in your text editor, type the following code:
day = "morning"
weight = 20
print(f"Sam, my dog, loves to play outside every {day}. He's growing fast and now weighs {weight} pounds.")
Explanation of the Code
- Variable Declarations:
day
is a variable that stores the string “morning
“weight
is a variable that stores the number20
- Formatted String:
- The
print()
function uses an f-string to dynamically insert the values ofday
andweight
into the text. f-strings
(formatted string literals) are a powerful feature in Python that allow you to embed expressions inside string literals, using {} braces. The values of these variables are directly inserted into the string where the placeholders are.
- The
Saving and Running Your Code
- Save the File:
- After typing the code, double-check for any mistakes and save the file.
- If prompted about overwriting the existing file, click OK.
- Running Your Code:
- You can now run your saved Python file using the terminal:
python3 session04.py
Don’t forget to press ENTER so that the terminal can display the output from the code. If you encounter an error (which is perfectly normal), backtrack to see where you went wrong.
Common Error Alert: If Python cannot find your file, ensure you’ve opened the terminal in the correct directory (inside the code folder).
Making Changes and Experimenting
To deepen your understanding of coding, try making changes to the variable values and run the code again. Notice how the story magically updates itself each time!
Feel free to save your revised scripts with new filenames (e.g., session04a.py
, session04b.py
) to track your revisions without losing earlier versions. This practice will help you find earlier code and copy-paste code between files.
Story Time: The Magic of f-Strings
Next week it will be one year since we adopted Sam and I am inviting my friends to celebrate this anniversary. Now, we use Python to create a fun invitation message. Let’s use f-strings to dynamically generate this message.
guest = “Alice”
party_time = “3 PM”
print(f”Hi {guest}, you’re invited to Sam’s birthday party at {party_time}! Don’t miss it!”)
Explanation:
- Variables:
guest
stores the name of the invited person.party_time
stores the time of the party.
- f-String Magic:
- The f-string in the
print()
function allows you to include the values ofguest
andparty_time
directly in your message. This makes it easy to update the message just by changing the variable values.
Quiz Time!
Which statement correctly describes the advantage of using a text editor to write Python code compared to using the Python interpreter?
- A) Using a text editor allows you to save and revisit your code, making it easier to track and modify your work compared to the Python interpreter.
- B) A text editor requires saving files with a .txt extension, which is essential for running Python code.
- C) Sticking to the Python interpreter is better because it doesn’t require saving files.
- D) A text editor is not suitable for Python files, as it is designed only for plain text.
Final Thoughts
Congratulations on progressing further into Python programming! By writing and saving your code, you’re setting yourself up for a more organized and effective learning experience. Remember, mistakes are part of the process, and each one helps you improve. Keep practicing, and enjoy the journey!