PEP8
You should use snake_case
for fuctionfunction names. You did so with ip_information
, why not with to_binary
as well?
Naming
The function name ip_information
does not convey what it precisely does. You might want to rename it.
Docstrings
Both of your functions are excellent candidates for docstrings to describe what they are supposed to be doing.
Use existing library functions
Since you did not tag reinventing-the-wheel, I suppose that you just want to get the job done. If so, your entire code can be replaced with the usage of the ipaddress
module from the standard library:
from ipaddress import IPv4Network
def print_network_information(ipv4network: IPv4Network) -> None:
"""Prints the network address, broadcast address and number
of addresses on the given IPv4 network.
"""
print('Network IP:', ipv4network.network_address)
print('Broadcast address:', ipv4network.broadcast_address)
print('Number of hosts:', ipv4network.num_addresses)
def main() -> None:
print_network_information(IPv4Network('192.168.0.0/255.255.255.0'))
if __name__ == '__main__':
main()
Note that the number of hosts differs from your version's number, since the network address and broadcast address are also valid host addresses within CIDR. If you want to compensate for this, just subtract 2 from the respective value.