Python Syntax

Key Features of Python Syntax

Indentation Instead of Braces:
Python uses indentation to define blocks of code, unlike many programming languages that use curly braces { }.

Example:

# Using indentation for a block of code
if True:
print("This is indented correctly!")

Incorrect Example:

if True:
print("This will cause an error!") # Missing indentation

Case Sensitivity:
Python is case-sensitive, which means Variable and variable are treated as different identifiers.

Example:

name = "Alice"
Name = "Bob"
print(name) # Outputs: Alice
print(Name) # Outputs: Bob

No Semicolons:
In Python, you don’t need to end statements with semicolons ( ; ). Python automatically understands the end of a statement based on line breaks.

Example:

print("Hello, World!")  # No semicolon needed

However, you can use a semicolon to separate multiple statements on the same line, though it’s not recommended for readability.

print("Hello"); print("World")  # Works, but not recommended

Comments in Python:
Comments are used to explain code and are ignored by the Python interpreter.

  • Single-Line Comment: Use #.

# This is a single-line comment
print("Comments are ignored by the interpreter.")
  • Multi-Line Comments: Use triple quotes.
"""
This is a multi-line comment.
It can span multiple lines.
"""
print("Multi-line comments are useful for documentation.")

Writing Variables and Data Types

In Python, variables don’t require explicit declaration. You simply assign a value to a variable, and Python determines its type dynamically.

Example:

# Assigning values to variables
x = 10 # Integer
y = 3.14 # Float
name = "John" # String

print(x, y, name)

Output:

10 3.14 John

Input and Output Syntax

Output Using print():
The print() function displays output to the screen.

print("Hello, Python!")  # Outputs: Hello, Python!

Taking User Input:
Use the input() function to take input from the user.

user_name = input("Enter your name: ")
print("Welcome, " + user_name + "!")

Example Output:

Enter your name: Alice  
Welcome, Alice!

Python Statements

Single-Line Statement:
A single action written in one line.

print("This is a single-line statement.")

Multi-Line Statement:
Use a backslash (\) to continue a statement on the next line.

result = 10 + 20 + 30 + \
40 + 50
print(result)

Output:

150

Python Indentation Rules

  • Indent Consistently: Python recommends using four spaces per indentation level.
  • Avoid Mixing Tabs and Spaces: Mixing tabs and spaces for indentation will raise an error.

Example:

def greet():
print("Hello!") # Proper indentation

greet()

Python Syntax with Conditional Statements

Python uses if, elif, and else for conditional logic.

Example:

age = 18

if age < 18:
print("You are a minor.")
elif age == 18:
print("You just became an adult!")
else:
print("You are an adult.")

Output:

You just became an adult!

Loops in Python Syntax

For Loop:

for i in range(5):
print(i)

Output:

0
1
2
3
4

While Loop:

count = 0
while count < 3:
print(count)
count += 1

Output:

0  
1
2

Leave a Comment