DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 3 of 77
←PreviousPrevNextNext→

Python Syntax Fundamentals

Basic Syntax Rules

Python syntax is designed to be simple and readable. Code blocks are defined using indentation (whitespace) rather than braces or keywords.

  • **Case Sensitivity**: Python is case-sensitive (`myVar` ≠ `myvar`)
  • **Indentation**: Use 4 spaces per indentation level (PEP 8 style guide)
  • **Statements**: Typically one per line (no semicolon required)
  • **Multi-line Statements**: Continue lines with backslash `\` or by wrapping in parentheses, brackets, or braces
  • **Docstrings**: Use triple quotes `"""` for multi-line documentation

Identifiers and Keywords

Identifiers are names for variables, functions, and classes. They must follow these rules:

ℹ️ Note: Python reserves 35 keywords such as `if`, `while`, and `def`, which cannot be used as identifiers.
Valid ExamplesInvalid ExamplesReason
my_variablemy-variableHyphens are not allowed
_private3d_modelIdentifiers cannot start with a digit
MAX_SIZEforKeywords cannot be used as identifiers
x1x!Special characters (except underscore) are not allowed

Line Structure Example

Example
# Valid multi-line statement using parentheses
total = (item_one +
         item_two -
         item_three)

# Using backslash for explicit continuation
name = "John " + \
       "Doe"
Output
# No output - demonstrates syntax rules

Common Syntax Errors

  • **IndentationError**: Inconsistent indentation (mixing tabs and spaces)
  • **SyntaxError**: Missing colon `:` after control statements like if or for
  • **NameError**: Referring to a variable before assignment
  • **TypeError**: Performing operations on incompatible types
⚠️ Warning: Python scripts cannot run with syntax errors. They must be corrected before execution.
Test your knowledge: Python Syntax Fundamentals
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 3 of 77
←PreviousPrevNextNext→