Chapters One and Two Python Program/Notes¶
Overview¶
This example program demonstrates core Python language features:
Variables and dynamic typing
Classes and object instantiation
Attributes and instance methods
Conditional logic (
if/else)Loops
Introspection (
type(),id(),dir())The principle that everything in Python is an object
The goal is to show foundational understanding of Python’s syntax and object model.
Source Code¶
# Demonstrating that everything in Python is an object
# Basic variables (all are objects)
number = 42
text = "Hello, Python!"
decimal = 3.14
is_active = True
print("Type of number:", type(number))
print("Type of text:", type(text))
print("Type of decimal:", type(decimal))
print("Type of is_active:", type(is_active))
print("-" * 40)
# Defining a class (blueprint for objects)
class Person:
# Constructor method
def __init__(self, name, age):
self.name = name # attribute
self.age = age # attribute
# Instance method
def greet(self):
print(f"Hi, my name is {self.name}.")
def is_adult(self):
# Conditional logic
if self.age >= 18:
return True
else:
return False
def describe(self):
print(f"{self.name} is {self.age} years old.")
if self.is_adult():
print("Status: Adult")
else:
print("Status: Minor")
# Creating objects (instances)
person1 = Person("Alice", 30)
person2 = Person("Bob", 15)
# Accessing attributes and methods
person1.greet()
person1.describe()
print()
person2.greet()
person2.describe()
print("-" * 40)
# Everything is an object demonstration
items = [number, text, decimal, is_active, person1]
for item in items:
print(f"Value: {item}")
print(f"Type: {type(item)}")
print(f"ID (memory address): {id(item)}")
print(f"Has __class__ attribute? {'__class__' in dir(item)}")
print("-" * 20)
# Even functions are objects
def say_hello():
return "Hello!"
print("Function is object:", type(say_hello))
print("Function output:", say_hello())
print("Function attributes:", dir(say_hello)[:5])
Explanation of Key Concepts¶
1. Variables Are Objects¶
The variables number, text, decimal, and is_active are
instances of built-in classes:
intstrfloatbool
Using type() demonstrates that each variable is an object of a specific class.
2. Classes and Object Instantiation¶
The Person class defines a custom object type. It includes:
An initializer method
__init__Instance attributes (
self.name,self.age)Instance methods (
greet(),is_adult(),describe())
Creating person1 and person2 shows how objects are instantiated from a class.
3. Attributes and Methods¶
- Attributes:
self.nameandself.agestore object state.- Methods:
greet(),is_adult(), anddescribe()define object behavior.
This demonstrates encapsulation of state and behavior inside objects.
4. Conditional Logic¶
The is_adult() method uses an if / else statement:
if self.age >= 18:
return True
else:
return False
This shows decision-making based on object state.
5. Loops¶
The for loop iterates over a list of mixed objects:
for item in items:
This demonstrates iteration and polymorphism — different object types are handled uniformly.
6. Everything Is an Object¶
The program demonstrates that:
Integers are objects
Strings are objects
Booleans are objects
Custom class instances are objects
Functions are objects
The following confirms this:
type()shows each value has a classid()shows each object has a unique identitydir()reveals attributes and methodsThe function
say_hellois treated like any other object
This reflects Python’s unified object model: all values are objects.
Conclusion¶
This example provides a compact but complete demonstration of:
Python syntax fundamentals
Object-oriented programming principles
Runtime introspection
The object-based nature of the Python language
It serves as a clear foundational example for documentation, teaching, or introductory coursework.