1. Python Basic Syntax
Python is famous for being simple and readable. If Python were a person, it would be that friend who explains complex things in the simplest way possible. Let’s dive into the basics!
Printing to the Screen
Want to make Python talk? Easy!
print("Hello, World!")
Run this, and BOOM! Python greets you.
Variables & Data Types
Python is so chill, you don’t even need to declare variable types explicitly.
name = "Alice" # String
age = 25 # Integer
height = 5.7 # Float
is_human = True # Boolean
No need for int
, string
, or other scary declarations. Python just knows!
Indentation Matters!
Unlike other languages that love curly braces {}
(looking at you, Java and C++ 👀), Python uses indentation to define blocks of code.
if age > 18:
print("You are an adult.")
If you forget indentation, Python will politely yell at you.
2. Python Program Structure
A Python program generally follows this simple structure:
- Shebang (Optional) – Used in Linux/macOS for script execution.
- Imports – Bring in external functionalities.
- Functions & Classes – Reusable blocks of logic.
- Main Execution – Where the magic happens!
Example:
# 1. Shebang (for Linux/macOS)
#!/usr/bin/python3
# 2. Importing modules
import math
# 3. Function definition
def greet(name):
return f"Hello, {name}!"
# 4. Main execution
if __name__ == "__main__":
print(greet("Alice"))
That last part (if __name__ == "__main__"
) ensures the script only runs when executed directly—not when imported. A bit of Python magic!
3. Writing Comments
Comments are notes inside your code that Python ignores. Think of them as reminders for your future self (or anyone else who dares to read your code).
Single-Line Comments
Use #
to write a quick note:
# This is a comment
print("Python is awesome!") # This prints text
Multi-Line Comments
Use triple quotes ("""
or '''
) for longer explanations.
"""
This is a multi-line comment.
Python won’t execute this part,
but it helps in documentation.
"""
print("Learning Python is fun!")
Conclusion
Python’s syntax is simple, its structure is clean, and comments help keep your code understandable. Whether you're coding for fun, work, or world domination, Python makes it easy. Now go forth and write some beautiful code!
0 Comments