[Python] Built-in Data Types

Date:     Updated:

Categories:

Tags:

📋 Here are the notes summarizing what I learned from the course!


Built-in Data Types

1. None

Defines a Null value, or no value at all. Similar to the Null value in other programming languages.

It is not the same as 0, False, or an empty collection.

a = None
print(f'{a} -> {type(a)}')
# None is a built-in object that is available to the interpreter
'None' in dir(__builtins__)
# Create new instance of None object
b = None
print(f'{b} -> {type(b)}')

# None adopts singleton pattern; therefore, all None objects point to the same instance 
print(id(None)) 
print(id(b))

Explanation:

  • None is used to define a variable with no value.
  • None is a singleton object, meaning there is only one instance of None in a Python runtime.

2. Numbers

Python has several built-in types for numbers.

2.1 Integers

Whole numbers, positive or negative, without decimals.

a = 10
print(f'{a} -> {type(a)}')

2.2 Floating Point Numbers

Numbers with a decimal point or in exponential (scientific) notation.

b = 3.14
print(f'{b} -> {type(b)}')

2.3 Complex Numbers

Numbers with a real and an imaginary part.

c = 1 + 2j
print(f'{c} -> {type(c)}')

2.4 Booleans

Subtype of integers with two values: True and False.

d = True
print(f'{d} -> {type(d)}')

Explanation:

  • Integers are used for whole numbers.
  • Floating-point numbers represent real numbers.
  • Complex numbers are used for complex mathematics.
  • Booleans are used for truth values.

3. Sequences

Python has several sequence types: strings, tuples, and lists.

3.1 Strings

Ordered collection of characters.

s = "Hello, World!"
print(f'{s} -> {type(s)}')

3.2 Tuples

Ordered, immutable collection of items.

t = (1, 2, 3)
print(f'{t} -> {type(t)}')

3.3 Lists

Ordered, mutable collection of items.

l = [1, 2, 3]
print(f'{l} -> {type(l)}')

Explanation:

  • Strings are used for text.
  • Tuples are used for collections that should not change.
  • Lists are used for collections that can change.

4. Sets

Unordered collection of unique items.

set_example = {1, 2, 3, 4, 4}
print(f'{set_example} -> {type(set_example)}')

Explanation:

  • Sets are used to store unique items.

5. Dictionaries

Unordered collection of key-value pairs.

d = {"name": "John", "age": 30}
print(f'{d} -> {type(d)}')

Explanation:

  • Dictionaries are used to store data values in key-value pairs.




Back to Top

See other articles in Category Python

Leave a comment