PUBLIC LESSON PREVIEW / Python & Data Structures
Run your first Python program
Read this sample without an account. Sign in for the full workspace, saved progress and checkpoints. This preview does not save activity or award credit.
What you’ll learn
- Run a short program and predict execution order
- Distinguish source code, output, and error messages
- Return a value from the provided solve function
The concept
A Python program is a sequence of instructions written as text. An interpreter reads those instructions and carries them out. You can run Python in an interactive shell, a saved .py file, or a browser-based editor. In a terminal, a common command is python3 hello.py; some installations use python instead. A browser course environment may provide a Run button. The important idea is the same: editing text changes the recipe, while running it performs the recipe. Think of execution as a bookmark moving down the program. In print("Ready"), the quoted text is a string value and print sends that value to the output area. Python does not print every value automatically in a script. The interactive shell sometimes displays expression results for convenience, which can make the two environments look different. Blank lines make code easier to read. A comment beginning with # explains intent and is not executed as an instruction. Walk through the example one statement at a time. Python first creates a function called solve. Creating a function does not run its body. The later expression solve("Mina") calls that function with a name. Inside the function, "Hello, " + name + "!" joins three pieces of text. return hands the result back to the caller. Finally, print displays that returned result. The function therefore produces a reusable value rather than depending on a screen. Course problems ask you to implement solve with a specific contract. Keep the requested function name and parameters. The test runner calls your function directly, so return the answer; do not replace return with print. A function that reaches its end without returning a value produces None, which is different from the expected string. Common early mistakes are missing quotation marks, mismatched parentheses, and inconsistent indentation. Python uses indentation to group a function's body. Use four spaces and keep related statements aligned. When an error appears, first read the last line for the error type, then inspect the indicated source line. Start with the smallest working program, change one thing, and run it again. This edit-run-observe cycle is the foundation of every later debugging technique.
Worked example
def solve(name):
return "Hello, " + name + "!"
print(solve("Mina"))
Try thinking it through
A welcome label
Implement solve(name). name is a string. Return "Welcome, " followed by name followed by "!". Preserve every character in name; an empty name is allowed. Do not print the answer.
Example input
name = "Ada"
Expected output
"Welcome, Ada!"
Hint 1
You need to produce one string value.
Hint 2
Use + to join the fixed text and the parameter.
Hint 3
Return "Welcome, " + name + "!".
Ready to try it yourself?
ChatGPT sign-in takes you to OpenAI and back to CodeTrail. It keeps your learning account separate from other learners.
Open the full lesson ↗