Introduction to Python Basics
Python is summarized as a reminder.
Python is a powerful and easy-to-read programming language that’s perfect for beginners. In this article, we’ll explore the fundamentals of Python syntax: variables, data types, and operators. If you’re coming from Mojo or just starting out, this guide is for you.
Variable Declaration
In Python, you can declare a variable simply by assigning a value using the =
operator. Python is dynamically typed, so you don’t need to declare the type explicitly:
x = 10
name = "Alice"
pi = 3.14
Basic Data Types
int
: Integer (e.g., 1, 42)float
: Floating-point number (e.g., 3.14, 0.5)str
: String (e.g., “hello”)bool
: Boolean (True
/False
)
You can check a variable’s type using the type()
function:
print(type(x)) #
print(type(name)) #
Type Hints (Optional)
While Python is dynamically typed, you can optionally add type hints (especially useful for large codebases):
age: int = 25
pi: float = 3.14
name: str = "Alice"
Arithmetic Operators
Python supports all common arithmetic operators:
Operator | Description | Example |
---|---|---|
+ | Addition | 2 + 3 |
- | Subtraction | 5 - 2 |
* | Multiplication | 4 * 3 |
/ | Division | 10 / 2 |
// | Floor Division | 7 // 2 |
% | Modulo | 9 % 4 |
** | Exponentiation | 2 ** 3 |
Comparison with Mojo
If you’ve read our Mojo basics article, you’ll notice the syntax is very similar. The key difference is that Python doesn’t require explicit type declarations by default, while Mojo encourages them for performance.
Summary
- Python uses simple
=
assignments for variables - No need to declare types, but type hints are available
- Arithmetic operators are intuitive and similar to Mojo
Next time, we’ll dive into Python functions — with and without type hints.
The Python functions are described below.
Comment