Lecture 3 a Handout Datatypes
Lecture 3 a Handout Datatypes
In Python, data types define the kind of data a variable can store. They determine how the data is
stored, manipulated, and used in programs. Python is dynamically typed, meaning you don’t need to
specify the data type of a variable explicitly—it’s inferred from the value you assign.
1. Numeric Types
Used to represent numbers:
• int (Integer): Whole numbers, both positive and negative.
Example: x = 10
• float (Floating-Point): Numbers with decimals.
Example: y = 3.14
• complex: Numbers with a real and imaginary part.
Example: z = 2 + 3j
2. Sequence Types
Used to store collections of items:
• str (String): A sequence of characters enclosed in single, double, or triple quotes.
Example: name = "Python"
• list: A mutable, ordered collection of items.
Example: numbers = [1, 2, 3, 4]
• tuple: An immutable, ordered collection of items.
Example: coordinates = (10, 20)
3. Boolean Type
Represents truth values:
• bool: Can be either True or False.
Example: is_valid = True
4. Mapping Type
Used to store key-value pairs:
1
• dict (Dictionary): A mutable, unordered collection of key-value pairs.
Example: person = {"name": "Alice", "age": 30}
5. Set Types
Used to store unique, unordered items:
• set: A mutable collection of unique items.
Example: unique_numbers = {1, 2, 3}
• frozenset: An immutable version of a set.
Example: frozen = frozenset({1, 2, 3})
6. None Type
Represents the absence of a value:
• None: Used to indicate a null or undefined value.
Example: x = None
Type Conversion
You can convert between data types using built-in functions like int(), float(), str(), etc. For
example:
x = "42" # String
y = int(x) # Convert to integer