Dictionaries, Booleans, None

Dictionaries (dict)

Use Cases: Use dictionaries when you need to store key-value pairs, where each key maps to a specific value. Dictionaries are efficient for looking up values based on keys.

Python: a dictionary has key value pairs, which is made up of keys (unique identifiers) and values (the data associated with each key). Keys must be immutable types (cannont be changed after created), while values can be any type.

# python
student = {
    "name": "John", # "name" is the key, "John" is the value
    "age": 21,
    "major": "Applied Mathematic"
}

print(student["name"]) # accessing a value
// javascript
let student = {
    name: "John",
    age: 21,
    major: "Applied Mathematics"
}

console.log(student.name); // accessing a value

Booleans (bool)

Use Cases: Use booleans to represent binary values, such as True or False. They are essential for making decisions and controlling the flow of your program using conditional statements.

  • Booleans can be used as is, as True or False.

  • Booleans can be used with logical operations. and: returns True if both operands are True, otherwise returns False. or: returns True if at least one operand is True, otherwise returns False. not: Inverts the Boolean value; True because False, and vice versa.

  • Comparison Operations: Booleans can be used for the result of comparisons: ==, !=, >, <

# python
is_student = True # Boolean value True
is_graduated = False # Boolean value False

# Using Boolean with logical operations
print(is_student and is_graduated) # False, because one of them is False
print(is_student or is_graduated) # True, because at least on is True
print(not is_student) # False, because is_student is True

age = 20
height = 175

# Comparison operations
is_adult = age >= 18 # True, because 20>=18
is_exact_height = height == 175 # True, because height is exactly 175

# printing the results
print("Is the person an adult?", is_adult)
print("Is the height exactly 175?", is_exact_height)
// javascript
// js operators: === (scrictly equal), !== (both are different), && (both must be true)
let isStudent = true;
let isGraduated = false: 

// Using Boolean with logical operations
console.log(isStudent && isGraduated); //false, because one of them is false
console.log(isStudent || isGraduated); //true, because at least one is true
console.log(!= isStudent); // false, because isStudent is true

let age = 20;
let height = 175;

// Comparison operations
let isAdult = age >= 18; //True, because 20>= 18
let isExactHeight = height === 175; /// True, because height is exactly 175

// printing the results 
console.log("Is the person an adult?", isAdult); // Output: true
console.log("Is the height exactly 175?", isExactHeight); //Output: true

None (NoneType)

Use Cases: Use None when you want to represent the absence of a value or indicate that a variable has no assigned value (null). It’s often used as a placeholder or default initial value.

# Default return value
def my_function():
    pass
print(my_function()) # output: None

# Placeholder
list = None
value = None

# Comparisons - None is only equal to itself
print(None == None) # True
print(None == 0) # False
// Default return value
function myFunction() {
    // no return value
}

console.log(myFunction());  // Output: undefined

// Placeholder
let list = null;
let value = null;

// Comparisons - null is only equal to itself and undefined
console.log(null === null);   // true
console.log(null === 0);      // false