Execute Python Syntax
Execute Python Syntax
HTML CSS
Python Syntax
❮ Previous Next ❯
Python Variables
>>> print("Hello, World!")
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.
Where in other programming languages the indentation in code is for readability only,
the indentation in Python is very important.
Example
if 5 > 2:
print("Five is greater than two!")
Try it Yourself »
Example
Syntax Error:
if 5 > 2:
Try it Yourself »
Example
if 5 > 2:
if 5 > 2:
You have to use the same number of spaces in the same block of code, otherwise Python
will give you an error:
Example
Syntax Error:
if 5 > 2:
Try it Yourself »
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.
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:
Insert the missing part of the code below to output "Hello World".
("Hello World")
Submit Answer »
NEW
We just launched
W3Schools videos
Explore now
COLOR PICKER
Get certified
by completing
a course today!
school
w3 s
2
CE
02
TI 2
R
FI .
ED
Get started
CODE GAME
Play Game
Report Error
Forum
About
Shop
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
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
Top Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples
Web Courses
HTML Course
CSS Course
JavaScript Course
SQL Course
Python Course
PHP Course
jQuery Course
Java Course
C++ Course
C# Course
XML Course
Get Certified »
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant
full correctness of all content.
While using W3Schools, you agree to have read and accepted our terms of use,
cookie and privacy policy.