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.)