😨Basic Syntax

Python syntax refers to the rules and conventions that govern how Python code is written and interpreted. These rules determine how code should be structured, what characters can be used to represent different types of data, how statements and expressions should be organized, and so on.

Understanding and following Python syntax is essential for writing effective, efficient, and readable code. Properly structured code makes it easier to understand and debug, helps prevent errors and bugs, and can improve code performance.

Python syntax includes rules for naming variables, defining functions and classes, using indentation to denote code blocks, and using various keywords and operators to perform specific actions.

By following Python syntax guidelines and best practices, programmers can write efficient, well-structured code that is easy to understand and maintain.

First Python Program

To create a Python program, you can use any text editor such as Notepad, Notepad++, Sublime Text, or any Python IDE (Integrated Development Environment) like PyCharm, Spyder, or Jupyter Notebook.

Here's an example of a simple Python program that prints "Hello, World!" on the screen:

print("Hello, World!")

To run this program, save it with a .py extension, and then execute the following command in the terminal or command prompt:

python hello_world.py

This will run the Python interpreter and execute the code in the file. The output will be:

Hello, World!

This is a very basic example, but it demonstrates the basic structure of a Python program. The print() function is used to output text to the console, and the code is executed line by line, from top to bottom.

Example: Python Statements

s an example of Python statements:

# This is a comment

# Assigning values to variables
x = 5
y = 10

# Performing arithmetic operations
z = x + y
print(z)

# Using conditional statements
if z > 10:
    print("Z is greater than 10")
else:
    print("Z is less than or equal to 10")
    
# Defining a function
def greet(name):
    print("Hello, " + name + "!")

# Calling the function
greet("John")

In this example, we assign values to variables x and y, perform arithmetic operations to calculate z, use a conditional statement to determine whether z is greater than 10, define a function greet that takes a parameter name and prints a greeting message, and finally call the greet function with an argument "John".

Example: Multiple Statements in Single Line

In Python, it is possible to write multiple statements in a single line using a semicolon (;) to separate them. However, it is generally not recommended as it can make the code less readable.

print('Hello  \
     World!') # a multi-line statement
Hello World!
print('Hello') \
    print(' World!') # two statement in one logical line
SyntaxError: invalid syntax

As mentioned earlier, while it is possible to write multiple statements in a single line like this, it is generally not recommended as it can make the code harder to read and understand. It is better to write each statement on a separate line.

Indentation in Python

In Python, indentation is used to indicate a block of code. Unlike other programming languages that use braces or other symbols to define blocks of code, Python uses indentation to group statements into blocks. The amount of indentation must be consistent within a block, and typically four spaces are used for each level of indentation.

For example, in the following code snippet, the block of code inside the if statement is indented:

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

If the indentation is not correct, Python will raise an IndentationError. It is important to be consistent with indentation throughout the entire program.

Python Syntax Identifiers

In Python, an identifier is a name given to a variable, function, class, module, or any other object. It is used to uniquely identify an object in the program.

Some rules for naming identifiers in Python are:

  • The first character of the identifier must be a letter or an underscore (_).

  • The rest of the characters in the identifier can be letters, numbers, or underscores.

  • Identifiers are case sensitive.

  • Reserved words or keywords cannot be used as identifiers.

  • The length of the identifier is not restricted.

Here are some examples of valid and invalid identifiers in Python:

Valid Identifiers:

  • variable_name

  • _underscore

  • ClassName

  • function_name_2

Invalid Identifiers:

  • 2variable (cannot start with a number)

  • class (reserved keyword)

  • my-function (cannot contain hyphens)

  • if (reserved keyword)

Last updated