Syntax refers to the rules defining how Python code is written and interpreted. Just like grammar in human languages, proper syntax ensures your code is understood by the Python interpreter.
1. Understanding Python Syntax
Python follows a simple and readable syntax. Let’s explore its key components.
1.1 Quotation Marks (""
or ''
)
Quotation marks are used to define text, also known as strings. You can use either single ('
) or double ("
) quotes:
print("Hello, World!")
print('Python is fun!')
Both are valid, but consistency is recommended.
Example: Displaying a Reminder
print("Don't forget to complete your Python exercises!")
1.2 Parentheses ()
Parentheses are used for function calls, grouping expressions, and defining tuples.
print("This text is inside parentheses.")
They are also used when defining functions:
def greet(name):
print("Hello,", name)
greet("Ali")
Example: Creating a Tuple
fruits = ("apple", "banana", "cherry")
print(fruits)
1.3 Assignment Operator (=
)
The equals sign =
assigns values to variables.
name = "Ali"
age = 25
print(name, "is", age, "years old.")
⚠ Note: =
is for assignment, while ==
is used for comparison.
x = 5 # Assignment
print(x == 5) # Comparison, returns True
Example: Storing Product Prices
product = "Laptop"
price = 80000 # Price in PKR
print("The price of", product, "is", price, "PKR.")
2. Writing and Running Python Code
Python scripts are saved with a .py
extension and can be executed by running:
python script.py
Alternatively, you can use the Python interactive shell by typing python
or python3
in the terminal.
Example: Displaying a Greeting Message
print("Hello! Welcome to Python!")
3. Indentation in Python
Python uses indentation instead of curly braces {}
to define code blocks.
if True:
print("This is properly indented.")
Incorrect indentation will cause an IndentationError:
if True:
print("This will cause an error.") # IndentationError
Example: Checking Store Timings
hour = 10
if hour >= 9:
print("The store is open.")
4. Comments in Python
Comments help explain the code and are ignored by Python.
4.1 Single-Line Comment
# This prints a welcome message
print("Welcome!")
4.2 Multi-Line Comment
"""
This program calculates total expenses.
Useful for budget tracking.
"""
expense1 = 200
expense2 = 150
total = expense1 + expense2
print("Total expenses:", total)
Example: Documenting a To-Do List
# Task for the day
print("Complete Python tutorial.")
5. Printing Output in Python
5.1 Understanding the print()
Function
The print()
function is used to display output in Python. The word print
is a built-in function name that tells Python to display the provided content on the screen.
The text inside parentheses ()
is called an argument, which can be a string, number, or variable.
print("Hello, Python!")
5.2 Why Use Quotation Marks (""
or ''
)?
Quotation marks are used when printing text (strings). Without them, Python treats words as variable names.
Example:
print("Hello") # Works fine because "Hello" is a string
print(Hello) # Error because Hello is not defined as a variable
When Not to Use Quotation Marks
Numbers, variables, and mathematical expressions do not require quotes:
age = 25
print(age) # Prints 25 without quotes
Example: Displaying a Medicine Reminder
print("Reminder: Take your medicine at 8 PM.")
6. Taking User Input
The input()
function allows users to enter data.
name = input("What is your name? ")
print("Hello, " + name + "!")
⚠ Note: input()
returns a string. Convert it to a number if needed:
age = int(input("Enter your age: "))
print("You are", age, "years old.")
Example: Asking for Favorite Sport
sport = input("What is your favorite sport? ")
print("That's great! Playing", sport, "is fun!")
Understanding Python’s basic syntax—quotation marks, parentheses, indentation, and comments—is essential for writing clean and efficient code. The print()
function is a fundamental tool for displaying output, and knowing when to use quotes is crucial for avoiding errors. These fundamental concepts allow beginners to start coding in Python with confidence.