Python Syntax
Python Syntax
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Python Syntax
❮ Previous Next ❯
Python Variables
>>> print("Hello, World!")
Hello, World! Python Comments
Exercises
Or by creating a python file on the server, using the .py file extension, and running it in
the Command Line:
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
https://www.w3schools.com/python/python_syntax.asp 1/9
3/28/25, 4:13 PM Python Syntax
Where in other programming languages the indentation in code is for readability only,
Tutorials in
the indentation Exercises
Python Services
important.
is very Sign Up Log in
HTML
Python CSS JAVASCRIPT
uses indentation SQL a block
to indicate PYTHON JAVA
of code. PHP HOW TO W3.CSS C
if 5 > 2:
print("Five is greater than two!")
Try it Yourself »
Example
Syntax Error:
if 5 > 2:
print("Five is greater than two!")
Try it Yourself »
The number of spaces is up to you as a programmer, the most common use is four, but it
has to be at least one.
Example
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
Try it Yourself »
https://www.w3schools.com/python/python_syntax.asp 2/9
3/28/25, 4:13 PM Python Syntax
You have to use the same number of spaces in the same block of code, otherwise Python
will giveTutorials
you an error:Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Example
Syntax Error:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Try it Yourself »
ADVERTISEMENT
Python Variables
In Python, variables are created when you assign a value to it:
Example
Variables in Python:
x = 5
y = "Hello, World!"
Try it Yourself »
You will learn more about variables in the Python Variables chapter.
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Comments
Python has commenting capability for the purpose of in-code documentation.
Comments start with a #, and Python will render the rest of the line as a comment:
Example
Comments in Python:
#This is a comment.
print("Hello, World!")
Try it Yourself »
?
Exercise
True or False: Indentation in Python is for readability only.
True
False
Submit Answer »
https://www.w3schools.com/python/python_syntax.asp 4/9
3/28/25, 4:13 PM Python Syntax
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
❮ Previous Next ❯
ADVERTISEMENT
https://www.w3schools.com/python/python_syntax.asp 5/9
3/28/25, 4:13 PM Python Syntax
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
https://www.w3schools.com/python/python_syntax.asp 6/9
3/28/25, 4:13 PM Python Syntax
COLOR PICKER
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
ADVERTISEMENT
ADVERTISEMENT
https://www.w3schools.com/python/python_syntax.asp 7/9
3/28/25, 4:13 PM Python Syntax
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
ADVERTISEMENT
PLUS SPACES
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
https://www.w3schools.com/python/python_syntax.asp 8/9
3/28/25, 4:13 PM Python Syntax
C++ Tutorial
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.
https://www.w3schools.com/python/python_syntax.asp 9/9