Python
-
General Characteristics:
-
Indentation-Based: Uses indentation to define code blocks.
-
Whitespace Significant: Proper indentation is crucial.
-
Readable and Clean: Emphasizes readability.
-
-
Example:
def greet(name):
if name:
print(f"Hello, {name}!")
else:
print("Hello, World!")
greet("Alice")
JavaScript
-
General Characteristics:
-
Brace-Based: Uses curly braces
{}
to define code blocks. -
Semicolons Optional: Though not required, semicolons can be used to terminate statements.
-
Flexible Syntax: More flexibility with code structure.
-
-
Example:
function greet(name) {
if (name) {
console.log(`Hello, ${name}!`);
} else {
console.log('Hello, World!');
}
}
greet('Alice');
Python
-
Automatic Memory Management:
-
Garbage Collection: Uses reference counting and a cyclic garbage collector to manage memory.
-
Memory Allocation: Handled by Python’s memory manager, abstracting complexity from the developer.
-
-
Under the Hood:
-
Reference Counting: Keeps track of the number of references to each object.
-
Cycle Detection: Identifies and collects objects that reference each other, preventing memory leaks. JavaScript
-
-
Automatic Memory Management:
-
Garbage Collection: Primarily uses mark-and-sweep algorithms.
-
Memory Allocation: Managed by the JavaScript engine (e.g., V8), abstracting complexity from the developer.
-
-
Under the Hood:
-
Mark-and-Sweep: Periodically scans memory to identify and collect unused objects.
-
Generational Collection: Optimizes garbage collection by categorizing objects based on their lifespan.
-
Python
- Primitive Types:
- Integer:
int
- Integer:
age = 30
- Float:
float
pi = 3.14159
- String:
str
name = "Alice"
- Boolean:
bool
is_active = True
- Composite Types:
- List: Ordered, mutable collection
fruits = ["apple", "banana", "cherry"]
- Tuple: Ordered, immutable collection
coordinates = (10, 20)
- Dictionary: Key-value pairs
person = {"name": "Alice", "age": 30}
- Set: Unordered, unique elements
unique_numbers = {1, 2, 3}
JavaScript
- Primitive Types:
- Number: Represents both integers and floats
let age = 30;
let pi = 3.14159;
- String:
string
let name = 'Alice';
- Boolean:
boolean
let isActive = true;
- Undefined:
undefined
let data;
- Null:
null
let empty = null;
- Symbol: Unique and immutable
let sym = Symbol('unique');
- Composite Types:
- Object: Key-value pairs
let person = { name: 'Alice', age: 30 };
- Array: Ordered collection
let fruits = ['apple', 'banana', 'cherry'];
- Function: First-class objects
function greet(name) {
return `Hello, ${name}!`;
}
Python If Statement
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
Loops
- For Loop:
for i in range(5):
print(i)
- While Loop:
count = 0
while count < 5:
print(count)
count += 1
Functions
def add(a, b):
return a + b
result = add(3, 5)
print(result) # Output: 8
Lists
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
Objects (Dictionaries)
person = {"name": "Alice", "age": 30}
print(person["name"]) # Output: Alice
JavaScript If Statement
let x = 10;
if (x > 5) {
console.log('x is greater than 5');
} else if (x === 5) {
console.log('x is equal to 5');
} else {
console.log('x is less than 5');
}
Loops
- For Loop:
for (let i = 0; i < 5; i++) {
console.log(i);
}
- While Loop:
let count = 0;
while (count < 5) {
console.log(count);
count++;
}
Functions
function add(a, b) {
return a + b;
}
let result = add(3, 5);
console.log(result); // Output: 8
Arrays
let fruits = ['apple', 'banana', 'cherry'];
console.log(fruits[0]); // Output: apple
Objects
let person = { name: 'Alice', age: 30 };
console.log(person.name); // Output: Alice
Python
-
Float Representation:
-
Double Precision: Python’s
float
type uses double-precision (64-bit) IEEE 754 format. -
Storage: Consists of sign bit, exponent, and mantissa.
-
-
Example:
pi = 3.141592653589793
print(pi) # Output: 3.141592653589793
-
Memory Storage:
-
Internally stored as binary fractions.
-
Limited precision can lead to rounding errors.
-
print(0.1 + 0.2) # Output: 0.30000000000000004
JavaScript
-
Float Representation:
-
Double Precision: JavaScript’s
Number
type also uses double-precision (64-bit) IEEE 754 format. -
Storage: Similar structure with sign bit, exponent, and mantissa.
-
-
Example:
let pi = 3.141592653589793;
console.log(pi); // Output: 3.141592653589793
-
Memory Storage:
-
Internally stored as binary fractions.
-
Limited precision can lead to rounding errors.
-
console.log(0.1 + 0.2); // Output: 0.30000000000000004
Python
- Function Types:
- Standard Functions:
def greet(name):
return f"Hello, {name}!"
print(greet("Alice")) # Output: Hello, Alice!
- Lambda Functions (Anonymous Functions):
add = lambda a, b: a + b
print(add(3, 5)) # Output: 8
- Higher-Order Functions:
def apply_function(func, value):
return func(value)
def square(x):
return x * x
print(apply_function(square, 4)) # Output: 16
- Generator Functions:
def count_up_to(max):
count = 1
while count <= max:
yield count
count += 1
counter = count_up_to(5)
for num in counter:
print(num)
# Output: 1 2 3 4 5
JavaScript
- Function Types:
- Function Declarations:
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet('Alice')); // Output: Hello, Alice!
- Function Expressions:
const add = function (a, b) {
return a + b;
};
console.log(add(3, 5)); // Output: 8
- Arrow Functions:
const multiply = (a, b) => a * b;
console.log(multiply(3, 5)); // Output: 15
- Higher-Order Functions:
function applyFunction(func, value) {
return func(value);
}
function square(x) {
return x * x;
}
console.log(applyFunction(square, 4)); // Output: 16
- Generator Functions:
function* countUpTo(max) {
let count = 1;
while (count <= max) {
yield count;
count++;
}
}
const counter = countUpTo(5);
for (let num of counter) {
console.log(num);
}
// Output: 1 2 3 4 5
Python
- Class Definition and Inheritance:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.speak()) # Output: Buddy says Woof!
print(cat.speak()) # Output: Whiskers says Meow!
-
Key Points:
-
Inheritance: Use parentheses to inherit from a parent class.
-
Method Overriding: Subclasses can override methods of the parent class.
-
super()
Function: To call methods from the parent class.
-
-
Class Definition and Inheritance (ES6 Syntax):
class Animal {
constructor(name) {
this.name = name;
}
speak() {
// To be overridden by subclasses
}
}
class Dog extends Animal {
speak() {
return `${this.name} says Woof!`;
}
}
class Cat extends Animal {
speak() {
return `${this.name} says Meow!`;
}
}
const dog = new Dog('Buddy');
const cat = new Cat('Whiskers');
console.log(dog.speak()); // Output: Buddy says Woof!
console.log(cat.speak()); // Output: Whiskers says Meow!
-
Key Points:
-
Inheritance: Use
extends
keyword to inherit from a parent class. -
Method Overriding: Subclasses can override methods of the parent class.
-
super()
Function: To call the constructor or methods from the parent class.
-
Topic | Python | JavaScript |
---|---|---|
Syntax | Indentation-based, readableExample:python def greet(name): print(f"Hello, {name}!") |
Brace-based, flexibleExample:javascript function greet(name) { console.log( Hello, ${name}! );} |
Memory Management | Automatic with reference counting and garbage collector | Automatic with mark-and-sweep and generational collection |
Data Types | int, float, str, bool, list, tuple, dict, setExample:python age = 30 |
Number, String, Boolean, Undefined, Null, Symbol, Object, ArrayExample:javascript let age = 30; |
Basic Constructs | if, for, while, def, list, dictExample:python if x > 5: print(x) |
if, for, while, function, array, objectExample:javascript if (x > 5) { console.log(x); } |
Floats | Double-precision (float)Example:python pi = 3.14 |
Double-precision (Number)Example:javascript let pi = 3.14; |
Functions | Standard, lambda, higher-order, generatorsExample:python def add(a, b): return a + b |
Function declarations, expressions, arrow functions, higher-order, generatorsExample:javascript function add(a, b) { return a + b; } |
OOP & Inheritance | class, inheritance using parenthesesExample:python class Dog(Animal): def speak(self): return "Woof!" |
class, extends keywordExample:javascript class Dog extends Animal { speak() { return "Woof!"; } } |
This comparison covers the fundamental aspects of Python and JavaScript across various programming concepts. Make sure to practice writing and understanding code in both languages to solidify your understanding for the exam. Good luck!