Python <-> Dart Code Snippets Comparison
Comparison
- Iteration: Both Python and Dart provide straightforward ways to iterate over elements and their indexes.
- Data Structures: Dart uses lists and maps similar to Python's lists and dictionaries.
- Arithmetic Manipulations: Basic arithmetic operations are similar, but Dart requires a loop for exponentiation.
- Encapsulation: Dart uses classes with private fields (indicated by
_
) similar to Python. - Inheritance and Polymorphism: Dart supports these concepts similarly to Python, using classes and method overriding.
Iteration
Iterating Over Elements
nums = [1, 2, 3, 4, 5]
for x in nums:
print(x)
void main() {
nums = [1, 2, 3, 4, 5]
for (var x in nums) {
print(x);
}
}
Iterating Over Elements with Indexes
nums = [1, 2, 3, 4, 5]
for i, x in enumerate(nums):
print(f"index: {i}, value: {x}")
void main() {
var nums = [1, 2, 3, 4, 5];
for (var i=0; i<nums.length; i++) {
print('index: $i, value: ${nums[i]}');
}
}
Data Structures
Lists and Vectors
nums = [1, 2, 3, 4, 5]
nums.append(6)
void main() {
var nums = [1, 2, 3, 4, 5];
nums.add(6);
}
Dictionaries and HashMaps
ages = {'Alice': 30, 'Bob': 25}
ages['Charlie'] = 35
void main() {
var ages = {"Alice": 30, "Bob": 25};
ages["Charlie"] = 35;
}
Arithmetic Manipulations
a = 10
b = 5
sum = a + b
difference = a - b
product = a * b
quotient = a / b
void main() {
var a = 10;
var b = 5;
var sum = a + b;
var difference = a - b;
var product = a * b;
var quotient = a / b;
}
remainder = a % b
power = a ** b
void main() {
var a = 10;
var b = 5;
var remainder = a % b;
var power = a
for (var i=1; i<b; i++) {
power *=a;
}
}
Encapsulation
Python (Class-Based)
class Rectangle:
def __init__(self, width, height):
self._width = width # Private attribute
self._height = height
def area(self):
return self._width * self._height
rect = Rectangle(10, 20)
print(rect.area())
Dart (Structs and Methods)
class Rectangle {
final double _width;
final double _height;
Rectangle(this._width, this._height);
double area() {
return _width * _height;
}
}
void main() {
var rect = Rectangle(10, 20);
print(rect.area());
}
Inheritance
Python (Class-Based)
Python supports inheritance, allowing you to create subclasses that inherit attributes and methods from a parent class
class Shape:
def area(self):
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
Dart (Class-Based)
Dart supports inheritance similar to Python
class Shape {
double area() => 0;
}
class Rectangle extends Shape {
final double width;
final double height;
Rectangle(this.width, this.height);
@override
double area() {
return width * height;
}
}
Polymorphism
Python (Class-Based)
Polymorphism in Python is achieved through inheritance and method overriding
class Shape:
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
shapes = [Circle(5), Rectangle(10, 20)]
for shape in shapes:
print(shape.area())
Dart (Class-Based)
Dart achieves polymorphism through inheritance and method overriding
abstract class Shape {
double area();
}
class Circle extends Shape {
final double radius;
Circle(this.radius);
@override
double area() {
return 3.14 * radius * radius;
}
}
void main() {
var shapes = [Circle(5), Rectangle(10, 20)];
for (var shape in shapes) {
print(shape.area());
}
}