Lesson 1 of 5 12 min read
Python setup and syntax
Install Python, run code interactively and from a file, and learn Python's core syntax rules.
In this lesson you will
- Install Python 3 and run code in the interactive shell and from a .py file
- Use print, input and comments
- Follow Python's indentation and naming rules
Why Python?
Python reads almost like English, has a huge standard library and is the main language for data analysis, automation and artificial intelligence. Companies use it for web backends, scripting and machine learning.
Installing Python
- Download Python 3 (3.12 or newer) from python.org. On Windows, tick Add python.exe to PATH during installation.
- Open a terminal and check the version:
python --version # on macOS and Linux this may be python3 --version- Install a code editor such as Visual Studio Code with the Python extension.
Two ways to run Python
Interactive shell (REPL). Type python in a terminal. You'll see the >>> prompt, where each line runs immediately. It's ideal for experiments:
>>> 2 + 3
5
>>> "Dev" + "Crest"
'DevCrest'A repeating cycle: Read your line, then Evaluate it, then Print the result, then back to Read your line
Type exit() to leave.
Script file. Save code in a file such as hello.py:
# hello.py — my first Python program
name = input("What is your name? ")
print("Welcome to DevCrest,", name)
print("Python runs this file from top to bottom.")Run it with python hello.py. input shows a prompt and returns what the user types as text; print displays values separated by spaces.
Syntax essentials
- No semicolons or braces. Each statement usually sits on its own line.
- Indentation defines blocks. Lines inside an
if, loop or function are indented, conventionally by four spaces. Inconsistent indentation causes anIndentationError. - Comments start with
#and are ignored by Python. - Names use
snake_case, such astotal_marks. They are case-sensitive:Scoreandscoreare different.
marks = 72
if marks >= 40:
print("Passed") # indented: part of the if block
print("Done") # not indented: always runsReading errors
Python reports problems with a traceback. Read it from the bottom: the last line names the error type and message, and the lines above show where it happened.
Traceback (most recent call last):
File "hello.py", line 3, in <module>
print("Total:" + 5)
TypeError: can only concatenate str (not "int") to strThis one says you cannot join text and a number with +. Use print("Total:", 5) or convert with str(5).
Hands-on exercise
Write a script greeting.py that asks for the user's name and favourite programming language, then prints a sentence using both. Next, deliberately break the indentation of one line and run it again. Read the traceback from the bottom up and fix the error.
Knowledge check
3 questions • 70% to pass • Practice only, no certificate
Key concepts
- Run Python interactively with the REPL or save code in .py files and run them with python file.py.
- Indentation, not braces, defines blocks in Python.
- # starts a comment; names use snake_case and are case-sensitive.
- Read tracebacks from the bottom line upward to find the error type and location.
