Uszipcode: Best Module To Find Zip Codes in Python

In Python, we can build many day-to-day use applications. These applications can either be GUI-based applications based on Tkinter or PyQt5 or Web applications based on flask or Django. Nonetheless, due to the availability of tons of modules in python, users can implement a straightforward way of adding complex items in code. Uszipcode Python is one such module to fetch zipcodes of location.

Uszipcode Python is a famous module released on PyPi used to get the zipcode of a specific location. You can find the zip codes by using City, State, Coordinates, or zip code prefix. The module has already implemented a database containing all the zip codes and their city, state, and map borders.

In this post, we’ll go through the uszipcode module and ways to use them.

How To Install Uszipcode Python?

All the modules available on PyPi can be installed by using the pip install method. This method is only applicable if you have pip installed in your PATH. If you cannot run pip on your terminal, check out this guide.

Install Uszipcode Python

To install USzipcode Python, open your terminal or command shell and then type pip install uszipcode. Press enter and wait for it to complete. If you see an installation successful note, then this module is installed successfully. If not, then check for the reported errors and try to fix them.

How to Import and Use Uszipcode Python?

The main class in the USzipcode module is its SearchEngine. This engine is used to search through zip codes using different conditions. The following example demonstrates a basic import and uses the case of uszipcode –

Code

from uszipcode import SearchEngine
engine = SearchEngine()
zipcode = engine.by_zipcode(60007)
print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output

60007 Elk Grove Village 33820

Uszipcode Module Search Methods

There are several ways you can search for US zip codes in the uszipcode module. These searches are used depending on your use cases. For example, if your application has a population attribute, you can use population to get the city’s zip code. Following are methods which you can use to get zip codes –

  1. Zipcode
  2. City and State
  3. Latitude and Longitude
  4. City
  5. State
  6. Prefix
  7. Population
  8. Population Density
  9. Land Area in Sqmi
  10. Water Area in Sqmi
  11. Housing Units
  12. Occupied Housing Units
  13. Median Home Value
  14. Median Household Income

Note – All the following examples have search DEFAULT_LIMIT set to 5. As a result, a maximum of 5 results can be obtained in any query search. This limit can be changed according to your needs.

By Using Zipcode

If you already know the zip code of the place and need to find other information about the place, you can directly use the by_zipcode method. This method accepts an integer as a parameter.

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcode = engine.by_zipcode(60009)
print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

60009 Elk Grove Village None

By Using City and State

Mention the city name and state name as a parameter in the by_city_and_state method of the SearchEngine instance.

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcodes = engine.by_city_and_state(city="Los Angeles", state="california")
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

90001 Los Angeles 57110
90002 Los Angeles 51223
90003 Los Angeles 66266
90004 Los Angeles 62180
90005 Los Angeles 37681

By Using Latitude and Longitude

Gets all the zip codes that fall within the radius of the given circular region. by_coordinate method accepts two float values as latitude and longitude and radius as a parameter. Note that longitude fall in the western hemisphere for the US, so mention the negative sign for longitude in Uszipcode python.

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcodes = engine.by_coordinates(41.8781, -87.6298, radius=30)
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

60603 Chicago 493
60604 Chicago 570
60602 Chicago 1204
60606 Chicago 2308
60601 Chicago 11110

By Using City

Include the city name in the parameter for the by_city method.

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcodes = engine.by_city(city="Los Angeles")
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

90001 Los Angeles 57110
90002 Los Angeles 51223
90003 Los Angeles 66266
90004 Los Angeles 62180
90005 Los Angeles 37681

By Using State

Include the state name in the parameter for the by_state method.

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcodes = engine.by_state(state="ilinoy")
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

60002 Antioch 24299
60004 Arlington Heights 50582
60005 Arlington Heights 29308
60007 Elk Grove Village 33820
60008 Rolling Meadows 22717

By Using Prefix

You can find all the zip code that starts with your desired request in Uszipcode python. The following code finds all the zip codes that start with 600.

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcodes = engine.by_prefix("600")
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

60002 Antioch 24299
60004 Arlington Heights 50582
60005 Arlington Heights 29308
60007 Elk Grove Village 33820
60008 Rolling Meadows 22717

By Using Population

by_population method accepts a lower limit and upper limit for the search in Uszipcode python. By default, a lower limit of -1 and an upper limit of 2147483648 is set.

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcodes = engine.by_population(lower=10000, upper=60000)
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

94806 San Pablo 59861
76028 Burleson 59744
10027 New York 59707
91702 Azusa 59705
60016 Des Plaines 59690

By Using Population Density

Population density measures the population per square mile of the area. Unfortunately, you cannot change the units of this metric. You have to use the lower and the upper limit to apply this feature.

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcodes = engine.by_population_density(lower=100, upper=500)
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

62201 East Saint Louis 7547
70084 Reserve 7552
12019 Ballston Lake 14740
12804 Queensbury 26540
15546 Jenners 386

By Using Land Area in Sqmi

Fetches the zip codes of areas with lower and upper bounds of square mile area.

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcodes = engine.by_land_area_in_sqmi(lower=100, upper=5000)
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

99901 Ketchikan 13508
99737 Delta Junction 5011
99701 Fairbanks 19019
99615 Kodiak 12899
89445 Winnemucca 16114

By Using Water Area in Sqmi

Fetches the zip codes of areas with lower and upper bounds of square mile water area.

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcodes = engine.by_water_area_in_sqmi(lower=100, upper=5000)
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

99615 Kodiak 12899
99901 Ketchikan 13508
99664 Seward 4932
99686 Valdez 4005
70091 Venice 278

By Using Housing Units

Applies a filter of several housing units in a specific area in Uszipcode Python. You can mention both lower and upper units for the method.

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcodes = engine.by_housing_units(lower=100, upper=500)
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

34739 Kenansville 793
83848 Nordman 137
61232 Andalusia 1284
47529 Elnora 1042
50107 Grand Junction 1099

By Using Occupied Housing Units

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcodes = engine.by_occupied_housing_units(lower=100, upper=300)
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

80428 Clark 706
62343 Hull 712
48841 Henderson 826
63787 Zalma 719
57353 Iroquois 829

By Using Median Home Value

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcodes = engine.by_median_home_value(lower=1000000, upper=3000000)
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

85253 Paradise Valley 17047
90049 Los Angeles 35482
90077 Los Angeles 9377
90210 Beverly Hills 21741
90211 Beverly Hills 8434

By Using Median Household Income

Applies a filter over the median household income of a specific area in the Uszipcode python module.

Code:

from uszipcode import SearchEngine
engine = SearchEngine()
zipcodes = engine.by_median_household_income(lower=100000, upper=300000)
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.population)

Output:

81335 Yellow Jacket 131
21405 Annapolis 544
97028 Government Camp 217
77010 Houston 366
82063 Jelm 100

How to Sort Query Results in Uszipcode Python?

Apart from applying different filters in your search for US zip codes, you can also use the automated sorting feature to get the desired result. Both the ascending and descending options are available for the sort. By default, all the values are sorted according to ascending order of zip codes.

Moreover, you can apply to sort on different attributes. For example, you can find the top 5 richest cities in Illinois by using the following code –

Code:

from uszipcode import SearchEngine, Zipcode
engine = SearchEngine()
zipcodes = engine.query(state="Illinois", sort_by=Zipcode.median_household_income, ascending=False)
for zipcode in zipcodes:
    print(zipcode.zipcode, zipcode.major_city, zipcode.median_household_income)

Output:

60043 Kenilworth 227250
60184 Wayne 171667
60093 Winnetka 163719
60022 Glencoe 162460
60521 Hinsdale 158947

How to Restrict Number of Results in Uszipcode Python?

By default, the search results are restricted to 5 results at a time. If you want to change the number of results, you can change the returns parameter. This parameter takes an integer value.

Can You Use Multiple Filters in Uszipcode Python?

Yes, you can apply multiple filters at a time on a search query. These filters are applied on the based .query() method of the SearchEngine class. Make sure you pass the correct parameter values and their accepted values.

See Also

Final Words

Zip Codes are a good way of keeping track of regions. Uszipcode module in Python has provided a good opportunity to filter and get the results instantly. Moreover, there are other attributes like population, income, monthly rents, employment status, income, education, and many more. These attributes are helpful to get desired results in our Python applications.

Happy Coding!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments