Learning

Python Syntax Fundamentals

Python Syntax Fundamentals

The print() Function

Python's primary output mechanism. Unlike many languages, it's a built-in function, not a statement.

python
# Basic output print('Hello, World!') print(42) print(3.14) # Multiple arguments (space-separated) print('Name:', 'John', 'Age:', 25) # Custom separator and end print('A', 'B', 'C', sep='-') # A-B-C print('Hello', end=' ') # No newline print('World') # Hello World # f-strings inside print name = 'Python' print(f'Learning {name}!') # Learning Python!

Comments

python
# This is a single-line comment ''' This is a multi-line comment (or docstring) ''' """ Another multi-line comment style """

Note: Multi-line strings technically create string objects. Python ignores them if not assigned to a variable, making them work as comments.

Indentation (The Core Rule)

Python uses whitespace indentation to define code blocks — NOT curly braces {}.

python
# ✅ Correct indentation (4 spaces — PEP 8 standard) if True: print('This is indented') if True: print('Nested indentation') # ❌ WRONG — will throw IndentationError if True: print('Missing indent!') # ❌ WRONG — mixed tabs and spaces if True: print('Tab here') print('Space here') # TabError!

Critical rules:

  • Use 4 spaces per level (PEP 8 standard)
  • NEVER mix tabs and spaces
  • All statements in a block must have the same indentation
  • First line of a block cannot be empty (unlike some languages)

Keywords

Python has 35+ reserved words that cannot be used as identifiers:

python
import keyword print(keyword.kwlist) # ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', # 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', # 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', # 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', # 'try', 'while', 'with', 'yield'] print(keyword.iskeyword('if')) # True print(keyword.iskeyword('hello')) # False

Python's Philosophy

python
import this # Prints 'The Zen of Python' — 19 guiding principles
Key Rules
  • •Python uses 4-space indentation to define blocks — never mix tabs and spaces
  • •print() is a function in Python 3 — use parentheses, always
  • •Multi-line comments are technically unassigned string literals — they still create string objects
  • •Keywords are case-sensitive: True/False/None are keywords, true/false/null are NOT
  • •PEP 8 is Python's style guide — follow it for readable, professional code
Your Task

Create a program that prints your name, age, and favorite language using print() with custom separators. Then print Python's keyword list count. Finally, write a properly indented if/else block that prints 'Pythonic!' if a variable is set to True.

EditorPython · JSX
PreviewUpdates on Run Tests
Loading preview…
Tests
Should use print() with sep parameter
Should import keyword module
Should use len() to count keywords
Should have if/else with proper indentation (4 spaces)
Should print 'Pythonic!'