02-01: Basic Syntax¶
Python syntax is the set of rules that define how Python code is written and interpreted.
print() Function¶
Used to display output on the screen.
Example:¶
or,
Explanation:
-
print() is a built-in function
-
Text inside quotes is called a string
-
can be used both single or double quote
👉 Output:
input() Function¶
The input() function is used to take input from the user.
Example¶
👉 Sample run:
Explanation:
-
input() waits for user input
-
The text inside " " is a prompt
-
The entered value is stored in the variable
⚠️ Important: input() Always Returns String
👉 Sample run:
👉 Even a number is entered, it will be stored as a string
Comments¶
Comments are used to explain code. They are ignored by Python.
Single-line comment: # is used for comment
Example¶
👉 Output:
Inline comment:
Indentation (VERY IMPORTANT)¶
Python uses indentation (spaces) to define blocks of code.
Example:¶
👉 Output:
Explanation:
-
The indented line belongs to the if block
-
Without indentation → ❌ Error
❌ Wrong:¶
Case Sensitivity¶
Python is case-sensitive.
Example:¶
👉 Output:
👉 These are treated as different variables
Statements & Lines¶
Each line is usually one statement.
Example:¶
👉 Output:
Multiple Statements in One Line¶
Multiple statements in single line can be written ; (but not recommended).
👉 Output:
👉 Better to use separate lines for readability
Line Continuation¶
Used to break long lines into multiple lines.
Using backslash:
Using parentheses (recommended):
Strings and Quotes¶
Strings can be written using both double and single quote:
Multi-line string is written within triple quotes:
or,
Basic Output Formatting¶
🔹 1. Comma Formatting (Basic Print)¶
👉 Output:
- "Name:" → string (fixed text)
- name → variable
- , → separates items in print()
🔹 2. f-String Formatting (Modern Way)¶
👉 Output:
- f"" → formatted string
- "My age is " → string
-
{age} → variable inserted inside string
-
👉 Python replaces {age} with its value
---¶
Exercises: 02-01: Exercises — Basic Syntax
⬅️ Previous: 01-05: Writing and Executing Python Code in VS Code ➡️ Next: 02-02: Variables and Data Types