Labs ICT
โญ Pro Login

Data Types

Checking Types

Python has several built-in data types. Use type() to check what type a value is.

print(type("Hello"))
print(type(42))
print(type(3.14))
print(type(True))
print(type([1, 2, 3]))
print(type((1, 2)))
print(type({"name": "Alice"}))
print(type({1, 2, 3}))
Try it Yourself โ†’

Strings

Strings are sequences of characters enclosed in single or double quotes.

name = "Python"
greeting = 'Hello'
print(name)
print(greeting)
Try it Yourself โ†’

Integers

Integers are whole numbers, positive or negative, without a decimal point.

age = 25
population = 8_000_000_000
negative = -10
print(age)
print(population)
print(negative)
Try it Yourself โ†’

Floats

Floats represent numbers with decimal points. They're used when you need precision.

price = 19.99
pi = 3.14159
scientific = 1.5e10
print(price)
print(pi)
print(scientific)
Try it Yourself โ†’

Booleans

Booleans represent truth values: True or False. They're fundamental for decision-making in code.

is_active = True
is_done = False
print(is_active)
print(is_done)
Try it Yourself โ†’

Lists

Lists are ordered, mutable collections. They can hold items of different types and are defined with square brackets.

fruits = ["apple", "banana", "cherry"]
mixed = [1, "hello", True, 3.14]
print(fruits)
print(mixed)
Try it Yourself โ†’

Tuples

Tuples are ordered, immutable collections. Once created, they cannot be changed. Use parentheses.

coordinates = (10, 20)
colors = ("red", "green", "blue")
print(coordinates)
print(colors)
Try it Yourself โ†’

Dictionaries

Dictionaries store key-value pairs. They're perfect for structured data and are defined with curly braces.

person = {"name": "Alice", "age": 30, "city": "New York"}
print(person)
print(person["name"])
Try it Yourself โ†’

Sets

Sets are unordered collections of unique items. Duplicates are automatically removed. Use curly braces or set().

unique_numbers = {1, 2, 3, 3, 2, 1}
print(unique_numbers)
Try it Yourself โ†’

String Concatenation

Combine strings using the + operator.

first = "Hello"
second = "World"
message = first + " " + second
print(message)
Try it Yourself โ†’

List Operations

Lists support various operations like appending, inserting, and removing items.

numbers = [1, 2, 3]
numbers.append(4)
numbers.insert(0, 0)
numbers.remove(2)
print(numbers)
Try it Yourself โ†’

Tuple Unpacking

You can unpack a tuple's values into separate variables in one line.

point = (3, 7)
x, y = point
print(f"X: {x}, Y: {y}")
Try it Yourself โ†’

Dictionary Access

Access dictionary values using keys. Use .get() to avoid errors when a key doesn't exist.

capital = {"France": "Paris", "Japan": "Tokyo"}
print(capital["France"])
print(capital.get("Germany", "Unknown"))
Try it Yourself โ†’

Set Operations

Sets support mathematical operations like union, intersection, and difference.

a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a | b)
print(a & b)
print(a - b)
Try it Yourself โ†’

Type Conversion Between Types

You can convert between types using functions like int(), float(), str(), and list().

number = 42
text = str(number)
print(text)
print(type(text))
Try it Yourself โ†’

Checking Types

Use isinstance() to check if a value belongs to a specific type.

value = "Hello"
print(isinstance(value, str))
print(isinstance(42, int))
print(isinstance(3.14, float))
Try it Yourself โ†’

Mutable vs Immutable

Some types are mutable (lists, dicts, sets) and others are immutable (strings, tuples, integers). Mutable types can be changed after creation.

# List is mutable
my_list = [1, 2, 3]
my_list[0] = 99
print(my_list)

# String is immutable
my_string = "hello"
# my_string[0] = "H"  # This would cause an error
Try it Yourself โ†’

๐Ÿงช Quick Quiz

What does type(3.14) return?