🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Web & Software Development › PHP and Python Web Programming › Lesson 6
TEC-WEB46-03 · Lesson 6 · 14 clock hours · demo

Python Fundamentals: Syntax, Variables, Data Types, and Operators

Objective. Students write Python 3 scripts that use correct syntax, all primitive data types (int, float, str, bool, NoneType), and arithmetic, comparison, logical, and bitwise operators, aligning with PCEP exam objective PE1 (Basics of Computer Programming and Python)

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

Python Fundamentals: Syntax, Variables, Data Types, and Operators

Why Python Matters for This Program

Python is one of the most widely used programming languages in web development, automation, and data work. The PCEP (Certified Entry-Level Python Programmer) exam, issued by the Python Institute, validates that you understand Python 3 fundamentals. This lesson covers the core building blocks tested in PCEP exam objective PE1: basic computer programming concepts and Python essentials.

---

Python Syntax Basics

Python uses indentation to define blocks of code instead of curly braces. This is not optional — incorrect indentation causes an `IndentationError` and your script will not run. Use four spaces per level; do not mix tabs and spaces.

A Python script is a plain-text file saved with the `.py` extension. You can run it from the terminal with `python3 script.py`, or execute statements one at a time in the REPL (Read-Evaluate-Print Loop) by typing `python3` at the command line.

Comments begin with `#`. Python ignores everything after `#` on that line. Use comments to explain your intent.

```python

print("Hello, world!") # inline comment

```

---

Variables and Naming Rules

A variable is a named storage location for a value. In Python you create one with a simple assignment:

```python

temperature = 98.6

city = "Chicago"

is_online = True

```

Naming rules (these are enforced by the language):

Convention (not enforced, but expected in professional code): use snake_case for variable names (`miles_per_hour`, not `MilesPerHour`).

---

Primitive Data Types

Python has five primitive types you must know for PCEP:

| Type | Example | Notes |

|------|---------|-------|

| `int` | `42`, `-7` | Whole numbers, no size limit in Python 3 |

| `float` | `3.14`, `-0.5` | Decimal numbers, stored in IEEE 754 double precision |

| `str` | `"hello"`, `'world'` | Text, enclosed in single or double quotes |

| `bool` | `True`, `False` | Must be capitalized; subclass of `int` (`True == 1`, `False == 0`) |

| `NoneType` | `None` | Represents the absence of a value |

Use `type()` to check a value's type: `type(3.14)` returns `<class 'float'>`.

Type casting converts one type to another:

---

Operators

Arithmetic operators perform math:

| Operator | Meaning | Example | Result |

|----------|---------|---------|--------|

| `+` | Addition | `7 + 3` | `10` |

| `-` | Subtraction | `7 - 3` | `4` |

| `` | Multiplication | `7 3` | `21` |

| `/` | True division | `7 / 2` | `3.5` |

| `//` | Integer (floor) division | `7 // 2` | `3` |

| `%` | Modulo (remainder) | `7 % 2` | `1` |

| `` | Exponentiation | `2 8` | `256` |

Note: `` applied to a string repeats it: `"ab" 3` gives `"ababab"`.

Comparison operators return a `bool`:

`==`, `!=`, `<`, `>`, `<=`, `>=`

Logical operators combine boolean expressions:

Python uses short-circuit evaluation: with `and`, if the left side is False, the right side is never evaluated; with `or`, if the left side is True, the right side is skipped.

Bitwise operators work on integer bits:

`&` (AND), `|` (OR), `^` (XOR), `~` (NOT), `<<` (left shift), `>>` (right shift).

Operator precedence (highest to lowest, simplified): `**` → unary `~`, `+`, `-` → `*`, `/`, `//`, `%` → `+`, `-` → shifts → `&` → `^` → `|` → comparisons → `not` → `and` → `or`. When in doubt, use parentheses.

---

Formatted String Output (f-strings)

F-strings let you embed expressions inside string literals:

```python

name = "Alex"

score = 95

print(f"Student {name} scored {score} points.")

```

You can control decimal places: `f"{value:.2f}"` rounds to two decimal places.

---

Safety Note: Professional Coding Practices

While Python development does not involve physical hazards such as electrical work, professional responsibility still applies:

---

Connecting to the PCEP Exam

PECAP exam objective PE1 covers computer programming basics, Python's role as an interpreted language, literals, variables, operators, and basic I/O. Every concept in this lesson maps directly to PE1. When you sit the PCEP exam, expect questions that ask you to predict the output of short code snippets — exactly the skill you practice in today's REPL drill.

Key terms9

REPLRead-Evaluate-Print Loop; an interactive Python session where you type one expression at a time and see the result immediately.
intPython's integer data type; stores whole numbers of any size without a decimal point.
floatPython's floating-point data type; stores numbers with a decimal point using IEEE 754 double-precision format.
NoneTypeThe type of the special value None, which represents the intentional absence of any value.
Integer (floor) divisionThe // operator; divides two numbers and returns the largest whole number not greater than the true quotient (floors the result).
Short-circuit evaluationPython's behavior with and/or operators: evaluation stops as soon as the overall result is determined, skipping the remaining expression.
Type castingConverting a value from one data type to another using functions such as int(), float(), str(), or bool().
f-stringA formatted string literal (prefixed with f) that allows expressions inside curly braces to be evaluated and embedded directly in the string.
Operator precedenceThe set of rules that determines the order in which operators are evaluated in an expression; parentheses can override the default order.

Worked examples4

1. Convert 26.2 miles to kilometers. Use the formula: kilometers = miles × 1.60934. Display the result rounded to two decimal places using an f-string.
  1. Assign the input: miles = 26.2
  2. Apply the conversion formula: km = miles * 1.60934
  3. Calculate: 26.2 × 1.60934 = 42.164508
  4. Format the output: print(f"{miles} miles = {km:.2f} km")
  5. The :.2f format specifier rounds the displayed value to two decimal places, giving 42.16
Answer: 26.2 miles = 42.16 km
2. Convert 98.6 degrees Fahrenheit to Celsius. Formula: C = (F − 32) × 5 / 9. Show each step as a separate Python expression and predict the final value.
  1. Assign the input: fahrenheit = 98.6
  2. Subtract 32: 98.6 − 32 = 66.6
  3. Multiply by 5: 66.6 × 5 = 333.0
  4. Divide by 9: 333.0 / 9 = 37.0
  5. In Python: celsius = (fahrenheit - 32) * 5 / 9
  6. Note: use true division (/) not integer division (//), or the decimal will be lost
Answer: 37.0 (degrees Celsius)
3. Predict the output of this expression without running it: print(2 ** 3 + 10 // 3 - 7 % 4)
  1. Apply precedence: ** is highest among these operators, then // and %, then +/-
  2. 2 ** 3 = 8
  3. 10 // 3 = 3 (floor division: 10 ÷ 3 = 3.333…, floor = 3)
  4. 7 % 4 = 3 (remainder: 7 − 4 = 3)
  5. Combine left to right: 8 + 3 − 3 = 8
Answer: 8
4. What is the result of bool(0), bool(""), bool(None), and bool(42) in Python? Explain why.
  1. bool(0): zero is falsy in Python → False
  2. bool(""): an empty string has no characters and is falsy → False
  3. bool(None): None represents no value and is always falsy → False
  4. bool(42): any non-zero integer is truthy → True
  5. Rule: values that represent emptiness, zero, or absence convert to False; all other values convert to True
Answer: bool(0) → False, bool("") → False, bool(None) → False, bool(42) → True

The math in this lesson1

Learners code a miles-to-km conversion, 26.2 x 1.60934 = 42.16 km, rounded to two decimals.
N-Q.1
Brush up in the math halls at newSchoolWorks.com:
In class: PCEP-aligned drill: students work through 30 short Python expressions predicting output before running in the REPL (interactive interpreter), covering integer division, string repetition, boolean short-circuit evaluation, and operator precedence — PCEP objective PE1 · Instructor live-codes a unit-conversion script (miles to kilometers, Fahrenheit to Celsius) showing variable naming, type casting, and formatted string output (f-strings); students extend it to add two more conversions of their choice · Students complete a timed 15-question multiple-choice quiz drawn from PCEP sample questions on data types and operators; results are reviewed as a class with explanations for wrong answers
Materials: Python 3 official docs (docs.python.org), Python Institute PCEP exam syllabus, VS Code with Python extension, Python REPL, PCEP sample questions
Check: PCEP-style quiz score of 75% or higher (11/15); unit-conversion script runs without errors and produces correct output for all four conversions — both components must pass

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

PCEP PE1 – Operators and Expressions
1. What is the output of the following Python expression? print(7 // 2)
PCEP PE1 – Variables and Naming
2. Which of the following variable names is valid in Python 3?
PCEP PE1 – Data Types and Operators
3. What data type does Python assign to the value produced by 10 / 2?
PCEP PE1 – Logical Operators and Precedence
4. Evaluate the expression: True and False or not False
PCEP PE1 – String Literals
5. Which statement correctly creates a variable that holds the text: She said, "Hello!"
PCEP PE1 – Short-Circuit Evaluation
6. What is printed by the following code? x = 5 y = 0 print(y != 0 and x / y > 1)
PCEP PE1 – String Operators
7. What is the result of "py" * 3 in Python?
PCEP PE1 – Operators and Type Behavior
8. A student writes the conversion formula celsius = (fahrenheit - 32) * 5 // 9. Why might this produce an incorrect result for some inputs?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.