Python Syntax – An Overview

In this tutorial, you will learn what Python Syntax looks like on a fundamental level. Python is a very popular programming language, and its popularity will keep rising in the future – so will the demand for Python developers.

Python Syntax Top Image

Table of Contents

ℹ️ This article has a corresponding Replit repository that you can copy and play around with. Create a free Replit account and click on the Fork Repl button below. This is the best way to practice programming!

Python Syntax Overview

Python uses whitespace or indentation for its code structure. If you have been coding in other languages, such as JavaScript before, you probably have seen that certain statements are ended with a semicolon: ;

In Python, everything is structured using whitespace. It is up to you if you want to use spaces or tabs (which is an endless discussion in and of itself, but it’s entirely up to personal preference). For ease of use, we prefer to use tabs.

Note: If you use spaces, according to the official PEP8 style guide, you should use four spaces for indentation. If you use tabs, just one tab is fine. You should never mix spaces and tabs. Always stick with one!

Let’s look at an actual Python code snippet next. It is not important to understand the code itself. Just look at the syntax:

# Defining a function
def our_function():
  print("Hello from our function")

# Calling our function
our_function()Code language: PHP (php)

As you can see, there are no semicolons ; to finish a line or block. It is all done with indentation. This not only allows us to write less code but also makes it more of a human-readable language that actually reads a little like English.

To summarize, some advantages of using this approach are:

  • Better readability
  • Clearly defined functions by using indentation
  • Indentation is required, which makes reading other developers’ code easier
  • Lower barrier-of-entry. It makes learning Python syntax easier compared to other languages.

Comments

Comments in programming languages are an essential tool to describe your code. Not only if you work together with other developers but also for yourself, if you have a project that you haven’t been working on for a longer period of time, you will forget parts (or all) of its functionality.

Comments help us to remember what we have written and with pointing out key elements of our code’s functionality. Writing comments is required in most programming-related jobs since you almost always collaborate with other developers on different projects.

Commenting in Python is straightforward. To write a comment, you simply put a hashtag # in front of your comment, this indicates a comment to the compiler, and it won’t run this piece of text when you run your code.

Here is an example of a comment in Python:

# This is a single line comment in Python. It won't run if you execute your code.Code language: Python (python)

String Literals

If you are declaring strings in Python, you have a couple of options:

  • Single quotes: '
  • Double quotes: "
  • Tripple single quotes: '''
  • Tripple double quotes: """

It is crucial that you always start and end a string by using the same type of quotes. For example, if you start your string with a double quote, you can’t end it with a single quote. Here is a code example:

single_line_string = 'This is a single line string using single quotes.'

another_single_line_string = "This is another single line string using double quotes."

multi_line_string = '''This is a multi line string 
using tripple single quotes.'''

another_multi_line_string = """This is a multi line string
using tripple double quotes."""

print(single_line_string)
print(another_single_line_string)
print(multi_line_string)
print(another_multi_line_string)Code language: Python (python)

Output:

This is a single line string using single quotes.
This is another single line string using double quotes.
This is a multi line string 
using tripple single quotes.
This is a multi line string
using tripple double quotes.Code language: plaintext (plaintext)

Using multi-line strings is very common in Python syntax.

Long Statements

In Python programming, it is sometimes required to use longer statements that need to be separated for better readability. We can utilize a backslash \ to achieve just that.

The example below shows how we can use a backslash \ to continue with a certain statement in another line:

if (1 < 2) and (2 > 1) and \
   (1 == 1):
    print("Multi-line statement is running!")Code language: Python (python)

Casing

While sticking to casing isn’t mandatory for your code to work, it is highly recommended to stick with the recommended casing standards for Python according to PEP8:

  • Variables, Functions, Methods, and Modules: Snake Case.
  • Classes: Pascal Case.
  • Constants: Capitalized Snake Case.

We highly recommend sticking to these guidelines, which help tremendously with making your code better readable, both for you and anyone who works with you.

Let’s take the string Python syntax and look at some examples in the code:

# camelCase: Capitalizing the first letter of each word (except the first one) and removing all spaces
pythonSyntax = 'camelCase'

# PascalCase: Capitalizing all first letters and removing all spaces.
PythonSyntax = "PascalCase"

# snake_case: Default snake case. All letters lowercase and spaces replaced by _
python_syntax = "snake_case"

# CAPITALIZED_SNAKE_CASE: Capitalized snake case. All letters uppercase and spaces replaced by _.
PYTHON_SYNTAX = "CAPITALIZED_SNAKE_CASE"Code language: Python (python)

Summary

The takeaway from this Python Syntax Overview is:

  • Python code is indented with spaces or tabs (stick with one!)
  • In Python, we use:
    • snake_case for Variables, Functions, Methods, and Modules.
    • PascalCase for Classes.
    • CAPITALIZED_SNAKE_CASE for Constants.
  • We can separate long statements with the use of a backslash \.
  • We can use different types of quotes for string literals.
  • We can use hashtags # for writing comments.

🐍 Learn Python Programming
🔨 Python Basics
👉 Python Syntax
👉 Python Variables
👉 Python print()
👉 Python input()
👉 Python Constants
👉 Python Comments
⚙️ Python Specifics
👉 Filter Lists in Python
👉 Replacing List Items in Python
👉 Create a GUI in Python
👉 Find out the Length of a String in Python
👉 Enum in Python
👉 Python Inline If/Else One-Line Statements
👉 Python xrange()
👉 Python List Slicing
🏋️ Python Exercises
👉 Python Exercise Collection

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap