Python - Basic Syntax

Python - Basic Syntax

Greetings, fellow Python enthusiasts! Welcome to our ongoing series on mastering Python programming. If you're just joining us, we recommend checking out our previous article, "Python - Overview". In that article, we covered the history of Python, the reasons to learn it, its characteristics, and its advantages and disadvantages compared to other languages. It's a valuable read to gain a broader understanding of Python's foundation and its place in the programming world.

In this article, we will dive deeper into Python's basic syntax—the building blocks that lay the groundwork for writing Python code effectively. We will cover essential topics such as the first Python program, identifiers, reserved words, lines and indentation, multiline statements, quotations, and comments. So, let's jump right in!

1) First Python Program:

To begin our Python journey, let's write our first Python program. Open any text editor and enter the following code:

print("Hello, World!")

Save the file with a .py extension, such as first_program.py. Now, open the command prompt or terminal, navigate to the file's directory, and run the program using the command:

python first_program.py

Congratulations! You have executed your first Python program, which prints the famous "Hello, World!" message.

2) Python Identifiers:

In Python, identifiers are names used to identify variables, functions, classes, modules, or other objects. Here are the rules for naming identifiers:

  • They must start with a letter (a-z, A-Z) or an underscore (_).

  • The rest of the identifier can contain letters, digits (0-9), and underscores.

  • Python is case-sensitive, so myVar and myvar are different identifiers.

  • Avoid using reserved words as identifiers (discussed in the next section).

Examples of valid identifiers: my_variable, _count, totalSum123

3) Python Reserved Words:

Python has a set of reserved words that have predefined meanings and cannot be used as identifiers. Some common reserved words include if, else, while, for, def, class, and import. It's essential to avoid using these reserved words as identifiers to prevent conflicts and syntax errors in your code.

4) Python Lines and Indentation:

Python uses indentation to define the structure and hierarchy of the code. It follows a strict rule of using the same level of indentation within a block of code. Typically, four spaces or one tab character is used for indentation.

For example, in a conditional statement like an if statement, the indented code block is executed only if the condition is true. Consider the following example:

if x > 5:
    print("x is greater than 5")

Notice how the indented code block follows the colon (:) after the if statement.

5) Python Multiline Statements:

In Python, statements are typically written on a single line. However, when a statement is too long to fit on one line, you can use a backslash (\) as a line continuation character. Another way is to enclose the statement within parentheses, brackets, or braces.

For example:

total = number1 + \
        number2 + \
        number3

or

total = (number1 +
         number2 +
         number3)

Both approaches ensure that the statement is treated as a single line.

6) Quotations in Python:

Python allows the use of single quotes (') or double quotes (") to denote strings. You can choose either style based on your preference. However, it is essential to be consistent throughout your codebase.

Example:

name1 = 'John'
name2 = "Doe"

Both name1 and name2 store string values.

7) Comments in Python:

Comments in Python are essential for code documentation and readability.

You can add comments to your code using the # symbol. Anything written after the # symbol is considered a comment and is not executed by the interpreter.

Example:

# This is a comment explaining the code below
x = 10  # Assigning a value to variable x

Comments provide insights into the code's functionality and make it easier for others (including your future self) to understand the code.

Conclusion:

Understanding the basic syntax of Python is a crucial step toward becoming a proficient programmer. In this article, we explored the first Python program, identifiers, reserved words, lines and indentation, multiline statements, quotations, and comments. By grasping these fundamental concepts, you have laid a strong foundation to dive deeper into Python's vast capabilities.

Remember, practice and experimentation are key to mastering Python syntax. So, start writing code, explore various programming constructs, and embrace the power and elegance of Python.

Happy coding with Python!

Did you find this article valuable?

Support Coding Insights by becoming a sponsor. Any amount is appreciated!