Data Types

Integer Type

  1. # integer
  2. age = 25
  3. type(age)
  4. #ans: <class 'int'>
  5. count = -10
  6. #ans: negative integer
  7. big_num = 1000000
  8. #ans: large integer

Float Type

  1. # float
  2. price = 19.99
  3. type(price)
  4. #ans: <class 'float'>
  5. pi = 3.14159
  6. #ans: float value
  7. negative = -5.5
  8. #ans: negative float

String Type

  1. # string
  2. message = "Hello"
  3. type(message)
  4. #ans: <class 'str'>
  5. name = 'Alice'
  6. #ans: single quotes
  7. text = """multi
  8. line"""
  9. #ans: multiline string

Boolean Type

  1. # boolean
  2. is_valid = True
  3. type(is_valid)
  4. #ans: <class 'bool'>
  5. is_empty = False
  6. #ans: False value

NoneType

  1. # None type
  2. x = None
  3. type(x)
  4. #ans: <class 'NoneType'>
  5. #ans: represents absence of value
  6. result = None
  7. #ans: no value yet

Checking Types

  1. # type() function
  2. x = 5
  3. type(x)
  4. #ans: <class 'int'>
  5. y = "hello"
  6. type(y)
  7. #ans: <class 'str'>
  8. z = 3.14
  9. type(z)
  10. #ans: <class 'float'>

Exercises - Part 1

  1. # what type is this?
  2. x = 5.0
  3. #ans: <class 'float'>
  4. # what type is this?
  5. y = "123"
  6. #ans: <class 'str'>
  7. # what type results?
  8. result = 10 / 2
  9. #ans: <class 'float'>
  10. # what type is this?
  11. z = True + False
  12. #ans: <class 'int'> (True=1, False=0)

Exercises - Part 2

  1. # what type?
  2. a = 5
  3. b = 2
  4. c = a / b
  5. #ans: <class 'float'>
  6. # what type?
  7. x = 10
  8. y = 3
  9. z = x // y
  10. #ans: <class 'int'>

Exercises - Part 3

  1. # tricky: what type?
  2. value = "5" + "3"
  3. #ans: <class 'str'> (result is "53")
  4. # what is the type?
  5. x = None
  6. #ans: <class 'NoneType'>
  7. # what type is this expression?
  8. result = 5 > 3
  9. #ans: <class 'bool'>

Exercises - Part 4

  1. # what happens?
  2. x = int(True)
  3. #ans: 1, type is <class 'int'>
  4. # mixing types?
  5. x = 5 + 2.5
  6. #ans: 7.5, type is <class 'float'>
  7. # edge case?
  8. x = bool([])
  9. #ans: False, type is <class 'bool'>

Exercises - Part 5

  1. # what type?
  2. x = 0
  3. #ans: <class 'int'>
  4. # empty string type?
  5. s = ""
  6. #ans: <class 'str'>
  7. # what type?
  8. x = float(5)
  9. #ans: 5.0, <class 'float'>

Google tag (gtag.js)