Python Syntax

Python programming syntax refers to the set of rules that define how a Python program is written and interpreted. Here’s a simple overview of the key syntax elements in Python:

if 15 > 12:
  print("Fifteen is greater than twelve!")

Do It

Spaces – Indentation Error

In Python, indentation is used to define the structure of code blocks, such as those found in functions, loops, and conditional statements. Unlike many other programming languages that use braces {} to mark code blocks, Python uses whitespace. The number of spaces used for indentation is up to the programmer, but it must be consistent within the same block of code. While it is technically acceptable to use any number of spaces (as long as there is at least one), the Python community standard—outlined in PEP 8—is to use four spaces per indentation level. Inconsistent indentation within a block will result in an Indentation Error, which makes it essential to choose a spacing style and stick with it throughout the program. Using four spaces is widely recommended for readability, consistency, and compatibility with most Python development tools.

if 5 > 2:
 print("Five is greater than two!") 
        print("Five is greater than two!")

Do It

Variables

In Python, variables are used to store data values, and they are created by simply assigning a value to a name using the equals sign (=). Python is dynamically typed, so you do not need to declare the type of a variable before using it. For example, you can assign an integer with age = 25, a floating-point number with height = 5.9, or a string with name = "Alice". Boolean values are also common and can be assigned using is_student = True or has_passed = False. Python supports more complex data types as well, such as lists (fruits = ["apple", "banana", "cherry"]), tuples (coordinates = (10, 20)), and dictionaries (person = {"name": "John", "age": 30}). You can also assign a null value using None, as in result = None, to indicate the absence of a value. Python allows multiple variable assignments in one line, such as x, y, z = 1, 2, 3, and you can assign the same value to multiple variables at once with a = b = c = 0. Variable names in Python should be descriptive and follow the snake_case convention for readability.

x = 235
y = "Hello, This is a string !"

print(x)
print(y)

Do it

Comments

In Python, comments are used to explain the code and make it more readable for others, including your future self. Comments are not executed by the Python interpreter and are purely for documentation purposes. A single-line comment begins with the hash symbol (#). Everything after the # on that line is considered a comment and ignored during execution. For example, # This is a single-line comment is a valid comment. Python also allows multi-line comments using triple quotes (''' or """), which are often used as docstrings to describe functions, classes, or modules. While triple-quoted strings can act like multi-line comments, they are technically string literals that are not assigned to any variable. It’s a best practice to use comments to clarify complex sections of code, describe the purpose of functions, or explain why certain decisions were made in the logic. Clear and concise comments improve code maintainability and collaboration among developers.

#This is a comment.
print("Hello, World!")

Do it