4. Variables and Data Types

4. Variables and Data Types

Python is dynamically typed, meaning you don't need to declare variable types explicitly.

  • Python variables are created when you assign a value. Common data types include: Variables are created when you first assign a value to them

  • Python supports several built-in data types:

  • Numeric types: int, float, complex

  • Sequence types: list, tuple, range

  • Text type: str

  • Mapping type: dict

  • Set types: set, frozenset

  • Boolean type: bool

# Example code:

# Integer
age = 25

# Float
height = 1.75
nHeight=int(height)
# String
name = "Alice"

# Boolean
is_student = True

# List
fruits = ["apple", "banana", "cherry"]

# Tuple
coordinates = (10, 20)

# Dictionary - key:value
person = {"name": "Bob", "age": 30}

# Set
unique_numbers = {1, 2, 3, 4, 5}

# Checking types
print(f'float2int conversion : {nHeight}')
print(f'type of age : {type(age)}')
print(f'type of height : {type(height)}')
print(f'type of name : {type(name)}')
print(f'type of unique_numbers : {type(unique_numbers)}')

Last updated