homework5git/lists/range03.py

22 lines
642 B
Python

# This code is based on: https://www.w3schools.com/python/ref_func_range.asp
# Create a range from 2 to 10 with steps of two
x = range(2, 10, 2)
y = list(x)
# Note:
# What is the difference betweeen using range() and an explicit list?
# The main difference is that range calculates the value on the fly. This saves memory
# at the expense of processing speed. The list has each object stored in memory. This saves
# the processing time at the expense of memory usage.
print(f"x = {x}, type = {type(x)}, length = {len(x)}")
for item in x:
print(item)
print(f"y = {y}, type = {type(y)}, length = {len(y)}")
for item in y:
print(item)