Python Syntax: Basic Principles

python syntax basics
10 November 2024

Python is one of the most popular and widely-used programming languages because of its simple and understandable syntax, making it a top choice for many programmers, especially newcomers. The syntax of Python is designed in such a way that both reading and writing code becomes easy. In the following, we will explore several core concepts in Python syntax to gain a better understanding of this language.

The first point about Python is that it uses indentation to define code blocks. Unlike certain other languages that use brackets, indentation in Python helps you organize your code and makes it look visually neat and coherent.

Moreover, Python is a dynamic language. This means that you do not need to declare variable types, and this task is automatically handled by Python. You just need to assign a value to a variable, and Python will identify its type accordingly.

There are more than 40 keywords in Python that may vary based on different versions, and this number may change. These keywords are reserved words that cannot be used as variable names. Some of these reserved keywords include 'if', 'while', 'for', 'else', and 'import'.

Another important point is the use of the hash symbol (#) for writing comments in the code. Anything that comes after the hash symbol will be ignored by Python and will only serve as explanations for human readers.

In summary, remember that Python does not require specific indentation markers for its commands, except for certain special cases; this feature makes the code simpler and easier to understand.

Python Code Example


# Defining a variable and printing its value
name = "Ali"
print("Hello, " + name)

# Using the if statement
age = 20
if age > 18:
    print("You are an adult.")

# For loop
for i in range(3):
    print(i)

Code Explanation

# Defining a variable and printing its value
This line defines a variable named name with a value of "Ali", and then uses the print statement to output a concatenated string including "Hello, " and the value of name.
# Using the if statement
The variable age has a value of 20. The if statement checks whether age is greater than 18. If so, the message "You are an adult." will be printed.
# For loop
This loop will execute three times, printing the value of the variable i from 0 to 2.

FAQ

?

How can I define a variable in Python?

?

What is used to define code blocks in Python?

?

How can I write a comment in Python?

?

Do I need to specify variable types in Python?