Skip to content

Data Types

Basic Data Types

Number Types

Python Number Types

python
# Python - Integer
age = 25
count = -10

# Python - Float
height = 1.75
pi = 3.14159

# Python - Complex Number
complex_num = 3 + 4j

JavaScript Number Type

javascript
// JavaScript - Only one number type
const age = 25;
const height = 1.75;
const count = -10;

String Type

Python String

python
# Python - String
name = "Alice"
message = 'Hello, World!'
multi_line = """
This is a
multi-line string
"""

# String methods
name.upper()      # Convert to uppercase
name.lower()      # Convert to lowercase
name.replace("A", "B")  # Replace
len(name)         # Length

JavaScript String

javascript
// JavaScript - String
const name = "Alice";
const message = "Hello, World!";
const multiLine = `
This is a
multi-line string
`;

// String methods
name.toUpperCase(); // Convert to uppercase
name.toLowerCase(); // Convert to lowercase
name.replace("A", "B"); // Replace
name.length; // Length

Boolean Type

python
# Python - Boolean
is_student = True
is_working = False

# Truthiness check
bool(0)      # False
bool("")     # False
bool([])     # False
bool(1)      # True
bool("Hello") # True
javascript
// JavaScript - Boolean
const isStudent = true;
const isWorking = false;

// Truthiness check
Boolean(0); // false
Boolean(""); // false
Boolean([]); // true (Note: empty array is truthy in JS)
Boolean(1); // true
Boolean("Hello"); // true

None vs null

python
# Python - None
value = None
if value is None:
    print("Value is None")
javascript
// JavaScript - null and undefined
const value = null;
const undefinedValue = undefined;

if (value === null) {
  console.log("Value is null");
}

Arithmetic Operations on Basic Data Types

Number Operations

python
# Python - Arithmetic operations
a, b = 10, 3

print(a + b)   # 13
print(a - b)   # 7
print(a * b)   # 30
print(a / b)   # 3.3333... (float division)
print(a // b)  # 3 (integer division)
print(a % b)   # 1 (modulo)
print(a ** b)  # 1000 (power)
javascript
// JavaScript - Arithmetic operations
const a = 10,
  b = 3;

console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.3333...
console.log(Math.floor(a / b)); // 3 (integer division)
console.log(a % b); // 1 (modulo)
console.log(a ** b); // 1000 (power)

String Operations

python
# Python - String operations
first = "Hello"
second = "World"

print(first + " " + second)  # String concatenation
print(first * 3)             # String repetition
print("Hello" in first)      # Membership check
javascript
// JavaScript - String operations
const first = "Hello";
const second = "World";

console.log(first + " " + second); // String concatenation
console.log(first.repeat(3)); // String repetition
console.log(first.includes("Hello")); // Membership check

Type Conversion

Python Type Conversion

python
# Python - Type conversion
# String to number
age_str = "25"
age = int(age_str)
height_str = "1.75"
height = float(height_str)

# Number to string
age_str = str(25)
height_str = str(1.75)

# Boolean conversion
bool(0)      # False
bool(1)      # True
bool("")     # False
bool("Hello") # True

JavaScript Type Conversion

javascript
// JavaScript - Type conversion
// String to number
const ageStr = "25";
const age = parseInt(ageStr);
const heightStr = "1.75";
const height = parseFloat(heightStr);

// Number to string
const ageStr2 = String(25);
const heightStr2 = (1.75).toString();

// Boolean conversion
Boolean(0); // false
Boolean(1); // true
Boolean(""); // false
Boolean("Hello"); // true

Lists and Tuples

Python List (similar to JavaScript Array)

python
# Python - List
fruits = ["apple", "banana", "orange"]
numbers = [1, 2, 3, 4, 5]

# Access elements
print(fruits[0])      # apple
print(fruits[-1])     # orange (last one)

# Modify elements
fruits[1] = "grape"

# Add elements
fruits.append("mango")
fruits.insert(1, "kiwi")

# Remove elements
fruits.remove("banana")
popped = fruits.pop()

# List methods
len(fruits)           # Length
fruits.sort()         # Sort
fruits.reverse()      # Reverse

JavaScript Array

javascript
// JavaScript - Array
const fruits = ["apple", "banana", "orange"];
const numbers = [1, 2, 3, 4, 5];

# Access elements
console.log(fruits[0]); // apple
console.log(fruits[fruits.length - 1]); // orange

# Modify elements
fruits[1] = "grape";

# Add elements
fruits.push("mango");
fruits.splice(1, 0, "kiwi");

# Remove elements
const index = fruits.indexOf("banana");
if (index > -1) {
  fruits.splice(index, 1);
}
const popped = fruits.pop();

# Array methods
fruits.length;
fruits.sort();
fruits.reverse();

Python Tuple (Immutable)

python
# Python - Tuple
point = (10, 20)
rgb = (255, 0, 0)

# Access elements
print(point[0]) # 10

# Tuples are immutable
# point[0] = 15  # This will raise a TypeError

Dictionaries and Objects

Python Dictionary

python
# Python - Dictionary
person = {
    "name": "Alice",
    "age": 25,
    "city": "New York"
}

# Access elements
print(person["name"])
print(person.get("age"))

# Modify elements
person["age"] = 26

# Add elements
person["country"] = "USA"

# Remove elements
del person["city"]
age = person.pop("age")

# Dictionary methods
person.keys()
person.values()
person.items()

JavaScript Object

javascript
# JavaScript - Object
const person = {
  name: "Alice",
  age: 25,
  city: "New York",
};

# Access elements
console.log(person.name);
console.log(person["age"]);

# Modify elements
person.age = 26;

# Add elements
person.country = "USA";

# Remove elements
delete person.city;

# Object methods
Object.keys(person);
Object.values(person);
Object.entries(person);

Sets

Python Set

python
# Python - Set
unique_numbers = {1, 2, 3, 2, 1}
print(unique_numbers)  # {1, 2, 3}

# Set operations
set1 = {1, 2, 3}
set2 = {3, 4, 5}

print(set1 | set2)  # Union
print(set1 & set2)  # Intersection
print(set1 - set2)  # Difference

JavaScript Set

javascript
# JavaScript - Set
const uniqueNumbers = new Set([1, 2, 3, 2, 1]);
console.log(uniqueNumbers); // Set { 1, 2, 3 }

# Set operations
const set1 = new Set([1, 2, 3]);
const set2 = new Set([3, 4, 5]);

const union = new Set([...set1, ...set2]);
const intersection = new Set([...set1].filter(x => set2.has(x)));
const difference = new Set([...set1].filter(x => !set2.has(x)));

Exercises

  1. Create variables for your name and age, and print a message.
  2. Create a list of your favorite fruits and add a new fruit to it.
  3. Create a dictionary representing a person and access their name.

Next Steps

Now that you have learned about Python's data types, you can move on to control flow.