Python Syntax

Python Syntax

Python syntax is actually a set of rules that define how code is written and interpreted by both the runtime system and human readers. Syntax defines the structure and format of code, including the use of keywords, operators, punctuation marks, and other elements.

However, it is important to note that while Python syntax can be written and executed directly in the command line, it is more commonly written in a code editor or integrated development environment (IDE) and then executed from there.

>>> print("Hello, World!")
Hello, World!

Alternatively, on the server, create a Python file with the.py file extension and run it from the command line.

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

Python Indentation

The spaces at the beginning of a code line are referred to as indentation. Whereas indentation in other programming languages is just for readability, an indentation in Python is critical. Python uses indentation to indicate a code block.

  • For example:
if 5 > 2:
  print("ten is greater than five!")
  • If you skip the indentation, Python will give you the following error:
  • For Example:
Syntax Error:
if 5 > 2:
print("ten is greater than five!")
  • As a programmer, you decide how many spaces to use, the most number being four, but there must be at least one.
  • For Example:
if 10 > 5:
 print("ten is greater than five!") 
if 10 > 5:
        print("ten is greater than five!")
  • Python will give an error if you don’t use the same number of spaces in the same block of code.
  • For Example:
Syntax Error:
if 10 > 5:
 print("ten is greater than five!")
        print("ten is greater than five!")

Python Variables

In Python, variables are created dynamically as soon as they are assigned a value. When you assign a value to a variable, Python creates that variable in memory and assigns a value to it. For example, consider the following code:

x = 10
  • In this code, we’re assigning the value 10 to the variable x. This creates the variable x in memory and assigns the value 10 to it.
  • You can also change the value of a variable by assigning it a new value. For example:
x = 10
x = 20
  • In this code, we’re first assigning the value 10 to the variable x, and then we’re re-assigning the value 20 to the same variable x. This changes the value of the x variable from 10 to 20.
  • It is worth noting that since Python is dynamically typed, variables in Python can store values of different types at different times. For example:
x = 10
x = "hello"

In this code, we’re first assigning the integer value 10 to the variable x, and then we’re re-assigning the string value “hello” to the same variable x. This is perfectly valid in Python because variables in Python are not statically typed.

Comments

In Python, you can use the # symbol to start a comment, and anything after the # symbol on that line will be ignored by the Python interpreter. This is commonly used for in-code documentation, to explain what a particular piece of code does, or to provide context for other developers who may be reading the code.

For example, here’s some Python code with comments:

# This is a comment. It explains what the following code does.
x = 10  # This is also a comment. It sets the value of x to 10.
y = x + 5  # This line adds 5 to x and assigns the result to y.

In this code, the first comment explains what the code does. And the other comments provide additional information about specific lines of code. These comments are ignored by the Python interpreter when the code is executed. But they can be very helpful for other developers who are reading and trying to understand the code.

Scroll to Top