CODETRAILby SWOT & Study

PUBLIC LESSON PREVIEW / Python for AI

Set up Python and run your first file

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.

Choose one execution path

Python code needs an interpreter: a program that reads and executes Python instructions. You can use a local installation or a browser-based notebook. For the local path, open the official Python download page and choose a supported stable Python 3 release for your operating system. Follow its installer instructions, then open a terminal. On many macOS or Linux systems, python3 --version reports the installed version; on Windows, py --version often does. These are terminal commands, not Python statements. Use the command that actually works on your machine, and keep it consistent throughout the course. The examples require only the Python standard library, so there is no package installation or API key to configure. On a managed computer, follow your organization's installation rules rather than changing restrictions yourself.

Create and run a source file

Make a folder named codetrail-python and create a plain-text file named hello.py inside it. Use a code editor or the editor supplied with your Python installation; avoid a word processor that adds rich formatting. Copy the example exactly, save the file, and open a terminal in that folder. Run python3 hello.py, or py hello.py on a Windows setup using the launcher. If your interpreter command is python, substitute that command. The terminal should display two lines matching the expected output. A file ending in .py.txt may look correct in a file browser while being a different filename, so check the full extension if the command cannot find it. Saving before running is essential: the interpreter executes the saved file, not unsaved edits visible in the editor.

Use the browser alternative carefully

For a no-install route, visit the official Try Jupyter page and choose its browser-based JupyterLite option when available. Open a Python notebook, put the example in one code cell, and run the cell with the interface's Run control or Shift+Enter. A notebook mixes editable code cells with displayed results. The order in which cells are executed can differ from their visual order, which creates hidden state when variables were defined earlier. During this course, keep each worked example together and restart the kernel before a clean rerun when checking reproducibility. Browser demos may have temporary storage or environment limitations. Download your notebook or copy your code to a local file before closing the page, and do not use real private records in a public demonstration environment.

Debug the execution path first

When something fails, identify which layer failed before editing the program. Command not found means the terminal did not locate the interpreter. Cannot open file usually means the filename or current folder is wrong. A Python traceback means the interpreter started and encountered a problem in the code. Read the final error line for the exception name, then look at the cited file and line number. SyntaxError often indicates a missing quote, bracket, or colon; NameError often indicates an undefined or misspelled name. Change one thing, save, and rerun. Keep a short log of the observed message, your hypothesis, and the result. This habit is more useful than repeatedly pasting an entire program from scratch, because it teaches you to connect symptoms to causes. Before continuing, close and reopen your saved file or restart the notebook kernel, then run again. A repeatable second run confirms that you understand where the code and its required state live.

A two-line sanity check

Runnable Python 3, standard library only. The output is identical whether this is run as a saved file or as one fresh notebook cell.

course = "Python for AI"
print(course)
print(2 + 3)

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 ↗