Skip to content

02-01: Basic Syntax

Python syntax is the set of rules that define how Python code is written and interpreted.


Used to display output on the screen.

Example:

print("Hello, World!")

or,

print('Hello, World!')

Explanation:

  • print() is a built-in function

  • Text inside quotes is called a string

  • can be used both single or double quote

👉 Output:

Hello, World!

input() Function

The input() function is used to take input from the user.

Example

name = input("Enter your name: ")
print(name)

👉 Sample run:

Enter your name: Imran
Imran

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

age = input("Enter your age: ")
print(type(age))

👉 Sample run:

Enter your age: 25
<class 'str'>

👉 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

# This is a comment
print("Hello")

👉 Output:

Hello

Inline comment:

print("Hello")  # This prints Hello

Indentation (VERY IMPORTANT)

Python uses indentation (spaces) to define blocks of code.

Example:

age = 18

if age >= 18:
    print("Adult")

👉 Output:

Adult

Explanation:

  • The indented line belongs to the if block

  • Without indentation → ❌ Error

❌ Wrong:

if age >= 18:
print("Adult")
IndentationError: expected an indented block after 'if' statement

Case Sensitivity

Python is case-sensitive.

Example:

name = "Imran"
Name = "Jahid"

print(name)
print(Name)

👉 Output:

Imran
Jahid

👉 These are treated as different variables


Statements & Lines

Each line is usually one statement.

Example:

x = 10
y = 20
print(x + y)

👉 Output:

30

Multiple Statements in One Line

Multiple statements in single line can be written ; (but not recommended).

x = 10; y = 20; print(x + y)

👉 Output:

30

👉 Better to use separate lines for readability


Line Continuation

Used to break long lines into multiple lines.

Using backslash:

total = 10 + 20 + 30 + \
        40 + 50

Using parentheses (recommended):

total = (10 + 20 + 30 +
         40 + 50)

Strings and Quotes

Strings can be written using both double and single quote:

"Hello"
'Hello'

Multi-line string is written within triple quotes:

text = """This is
a multi-line
string"""

or,

text = '''This is
a multi-line
string'''

Basic Output Formatting

🔹 1. Comma Formatting (Basic Print)

name = "Imran"
print("Name:", name)

👉 Output:

Name: Imran
  • "Name:" → string (fixed text)
  • name → variable
  • , → separates items in print()

🔹 2. f-String Formatting (Modern Way)

age = 25
print(f"My age is {age}")

👉 Output:

My age is 25
  • 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