Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Python Syntax

The document provides an overview of Python syntax, emphasizing the importance of indentation in defining code blocks, which differs from other programming languages. It explains how to execute Python code both in the command line and through .py files, and introduces the concept of variables and comments in Python. Additionally, it highlights common syntax errors related to indentation and offers examples for clarity.

Uploaded by

nshansundar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python Syntax

The document provides an overview of Python syntax, emphasizing the importance of indentation in defining code blocks, which differs from other programming languages. It explains how to execute Python code both in the command line and through .py files, and introduces the concept of variables and comments in Python. Additionally, it highlights common syntax errors related to indentation and offers examples for clarity.

Uploaded by

nshansundar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

3/28/25, 4:13 PM Python Syntax

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

Python Syntax
❮ Previous Next ❯

Execute Python Syntax On this page

As we learned in the previous page, Python syntax can


Execute Python Syntax
be executed by writing directly in the Command Line:
Python Indentation

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:

C:\Users\Your Name>python myfile.py

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

Example Get your own Python Server

if 5 > 2:
print("Five is greater than two!")

Try it Yourself »

Python will give you an error if you skip the indentation:

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 »

Python has no command for declaring a variable.


https://www.w3schools.com/python/python_syntax.asp 3/9
3/28/25, 4:13 PM Python Syntax

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 »

Video: Python Syntax

https://www.w3schools.com/python/python_syntax.asp 4/9
3/28/25, 4:13 PM Python Syntax

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

❮ Previous Next ❯

Track your progress - it's free! Sign Up Log in

ADVERTISEMENT

https://www.w3schools.com/python/python_syntax.asp 5/9
3/28/25, 4:13 PM Python Syntax

 Tutorials  Exercises  Services   Sign Up Log in

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

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

ADVERTISEMENT

 

 PLUS SPACES

GET CERTIFIED FOR TEACHERS

FOR BUSINESS CONTACT US

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

 Tutorials  jQuery Tutorial


Exercises 
Top References
Services   Sign Up Log in

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

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


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.

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

You might also like