Python syntax is simple and easy to read, making it one of the most beginner-friendly programming languages. Let’s explore the basics of Python syntax! π
1. Writing Python Code
β Python uses indentation instead of curly braces {}.
β No need to declare variable types.
β Code is executed line by line (interpreted language).
Example:
print("Hello, World!") # Output: Hello, World!
2. Python Indentation
Python uses indentation to define blocks of code. Incorrect indentation will cause an error.
β Correct Example:
if True:
print("This is indented correctly.") # Indentation is required
β Incorrect Example (will cause an error):
if True:
print("This will cause an error.") # No indentation
3. Python Comments
Comments are ignored by Python and are used for explanations.
β Single-line Comment:
# This is a comment
print("Hello, Python!") # This prints a message
β Multi-line Comment:
"""
This is a multi-line comment.
It spans multiple lines.
"""
print("Python is awesome!")
4. Python Variables
β Variables do not need explicit declaration.
β The type is assigned automatically.
Example:
name = "Alice" # String age = 25 # Integer height = 5.6 # Float is_student = True # Boolean print(name, age, height, is_student)
5. Python Data Types
| Data Type | Example |
|---|---|
| String | "Hello" |
| Integer | 10 |
| Float | 3.14 |
| Boolean | True / False |
| List | [1, 2, 3] |
| Tuple | (1, 2, 3) |
| Dictionary | {"name": "Alice", "age": 25} |
6. Python Input & Output
β Taking User Input:
name = input("Enter your name: ")
print("Welcome, " + name + "!")
β Printing Multiple Values:
print("Python", "is", "fun", sep=" - ") # Output: Python - is - fun
7. Python Case Sensitivity
Python is case-sensitive, meaning Name and name are different variables.
name = "Alice" Name = "Bob" print(name) # Output: Alice print(Name) # Output: Bob
8. Python Line Breaks & Multiple Statements
β Writing multiple statements on one line:
x = 10; y = 20; print(x + y)
β Breaking long lines using \ (backslash):
total = 10 + 20 + \
30 + 40
print(total) # Output: 100
9. Python Code Execution
Python code can be executed in:
- Python Shell (REPL) β
python - Python Script (
.pyfiles) βpython filename.py - IDEs (VS Code, PyCharm, etc.)