2026fa_cse160/lecture20261110/Tuples-Intro_0001.py

52 lines
2.5 KiB
Python

# CSE-160-D01_2024FA_Intro-to-Python-Tuples
# -----------------------------------------
# Tuple-Intro_0001.py
#
# A tuple is type of Python variable that can hold an ordered collection
# of multiple values of possibly differing data type, possibly duplicated
# values and are used for information that should not be changed.
#
# We have talked about Python list variables. Tuples have some
# similarities to Python lists.
# lists and tuples similarities:
# both are ordered collections of multiple values
# both can contain multiple data types (e.g. integers, floats, strings, etc.)
# both can contain duplicate values
# length of both can be determined using the len() function
# lists and tuples differences:
# tuples are described using parentheses
# tuples are "immutable" (i.e. they can't be changed)
#
# What are tuples used for in Python?
# Do a Google (or other fine) search on the exact question in the line above.
#
#############################################################################################
#
# Example tuples:
# Latitude and Longitude of STCC Building 19:
STCC_Bldg19_Loc = (42.10998017567259, -72.58147247672)
# Atomic symbol, atomic number, and atomic weight of aluminum:
aluminumData = ("Al", 13, 26.982)
# A single item tuple is described like so:
avgDiameterOfEarthInMiles = (7926,)
#
lockCombination = ("right24", "left31", "right10")
#
print("STCC_Bldg19_Loc = ", STCC_Bldg19_Loc)
print("STCC_Bldg19_Loc type and length: ", type(STCC_Bldg19_Loc), len(STCC_Bldg19_Loc), "\n")
print("aluminumData = ", aluminumData)
print("aluminumData type and length: ", type(aluminumData), len(aluminumData), "\n")
print("avgDiameterOfEarthInMiles: ", avgDiameterOfEarthInMiles)
print("avgDiameterOfEarthInMiles type and length: ", type(avgDiameterOfEarthInMiles), len(avgDiameterOfEarthInMiles), "\n")
print("lockCombination = ", lockCombination)
print("lockCombination type and length: ", type(lockCombination), len(lockCombination))
print()
#
#############################################################################################
#
# Tuples are indexed just like list variables (yeah, they even used square brackets on the indices):
print("STCC Blg 19 Latitude = ", STCC_Bldg19_Loc[0])
print("STCC Blg 19 Longitude = ", STCC_Bldg19_Loc[1], "\n")
print("Atomic weight of ", aluminumData[0], " = ", aluminumData[2], "\n")
print("Average diameter of earth (in miles) is = ", avgDiameterOfEarthInMiles[0], "\n")
print("2nd lock combination move is: ", lockCombination[1])