Python Basics: The Most Versatile Language
Python is a high-level, general-purpose programming language known for its readability and simplicity. It allows you to work quickly and integrate systems effectively.
It is the dominant language in Data Science, Artificial Intelligence (AI), and Backend Web Development.
1. Key Features
- Readability: Its syntax resembles English, making it easy to learn and maintain.
- Interpreted: Code is executed line by line, allowing for quick prototyping and debugging.
- Vast Ecosystem: Includes powerful libraries for AI (PyTorch), Data (Pandas), and Web (FastAPI).
2. Basic Syntax
Variables and Printing
Python is dynamically typed; you don't need to declare types explicitly.
user_name = "Alice" # String
user_level = 10 # Integer
# Using f-strings for formatting (Recommended)
print(f"User: {user_name}, Level: {user_level}")
Conditionals (If / Else)
Python uses indentation (whitespace) to define code blocks, instead of curly braces {}.
temperature = 20
if temperature > 25:
print("It's hot.")
elif temperature > 15:
print("It's nice.")
else:
print("It's cold.")
Loops (For / While)
# Loop from 0 to 4
for i in range(5):
print(f"Number: {i}")
# Iterate over a list
colors = ["Red", "Green", "Blue"]
for color in colors:
print(color)
3. Functions
Defined using the def keyword.
def greet(name):
return f"Hello, {name}!"
message = greet("Bob")
print(message) # Output: Hello, Bob!
4. Importing Libraries
Python's strength lies in its extensive standard library and third-party packages.
import math
import datetime
print(math.pi) # 3.14159...
print(datetime.date.today()) # Prints today's date