2025SP_cse160/bigos/lecture20250304/CSE-160-D01_2024FA_Tuple-Ba...

138 lines
4.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "ae280d81",
"metadata": {},
"outputs": [],
"source": [
"# CSE-160-D01_2024FA_Tuple-Backgrounder\n",
"#\n",
"# A tuple is type of Python variable that can holdan ordered collection\n",
"# of mutiple values of possibly differing data type, possibly duplicated\n",
"# values and are used for information that should not be changed.\n",
"#\n",
"# We have talked about Python list variables. Tuples have some\n",
"# similarities to Python lists.\n",
"# lists and tuples similarities:\n",
"# both are ordered collections of multiple values\n",
"# both can contain multiple data types (e.g. integers, floats, strings, etc.)\n",
"# both can contain duplicate values\n",
"# length of both can be determined using the len() function\n",
"# lists and tuples differences:\n",
"# tuples are described using parentheses\n",
"# tuples are \"immutable\" (i.e. they can't be changed)\n",
"#\n",
"# What are tuples used for in Python?\n",
"# Do a Google (or other fine search) on the exact question in the line above."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "987287d1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"STCC_Bldg19_Loc = (42.10998017567259, -72.58147247672)\n",
"STCC_Bldg19_Loc type and length: <class 'tuple'> 2 \n",
"\n",
"aluminumData = ('Al', 13, 26.982)\n",
"aluminumData type and length: <class 'tuple'> 3 \n",
"\n",
"avgDiameterOfEarthInMiles: (7926,)\n",
"avgDiameterOfEarthInMiles type and length: <class 'tuple'> 1 \n",
"\n",
"lockCombination = ('right24', 'left31', 'right10')\n",
"lockCombination type and length: <class 'tuple'> 3\n"
]
}
],
"source": [
"# Example tuples:\n",
"# Latitude and Longitude of STCC Building 19:\n",
"STCC_Bldg19_Loc = (42.10998017567259, -72.58147247672)\n",
"# Atomic symbol, atomic number, and atomic weight of aluminum:\n",
"aluminumData = (\"Al\", 13, 26.982)\n",
"# A single tuple is described like so:\n",
"avgDiameterOfEarthInMiles = (7926,)\n",
"#\n",
"lockCombination = (\"right24\", \"left31\", \"right10\")\n",
"#\n",
"print(\"STCC_Bldg19_Loc = \", STCC_Bldg19_Loc)\n",
"print(\"STCC_Bldg19_Loc type and length: \", type(STCC_Bldg19_Loc), len(STCC_Bldg19_Loc), \"\\n\")\n",
"print(\"aluminumData = \", aluminumData)\n",
"print(\"aluminumData type and length: \", type(aluminumData), len(aluminumData), \"\\n\")\n",
"print(\"avgDiameterOfEarthInMiles: \", avgDiameterOfEarthInMiles)\n",
"print(\"avgDiameterOfEarthInMiles type and length: \", type(avgDiameterOfEarthInMiles), len(avgDiameterOfEarthInMiles), \"\\n\")\n",
"print(\"lockCombination = \", lockCombination)\n",
"print(\"lockCombination type and length: \", type(lockCombination), len(lockCombination))\n",
"print()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "cc74b5b1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"STCC Blg 19 Latitude = 42.10998017567259\n",
"STCC Blg 19 Longitude = -72.58147247672 \n",
"\n",
"Atomic weight of Al = 26.982 \n",
"\n",
"Average diameter of earth (in miles) is = 7926 \n",
"\n",
"2nd lock combination move is: left31\n"
]
}
],
"source": [
"# Tuples are indexed just like list variables (yeah, they even used square brackets on the indices):\n",
"print(\"STCC Blg 19 Latitude = \", STCC_Bldg19_Loc[0])\n",
"print(\"STCC Blg 19 Longitude = \", STCC_Bldg19_Loc[1], \"\\n\")\n",
"print(\"Atomic weight of \", aluminumData[0], \" = \", aluminumData[2], \"\\n\")\n",
"print(\"Average diameter of earth (in miles) is = \", avgDiameterOfEarthInMiles[0], \"\\n\")\n",
"print(\"2nd lock combination move is: \", lockCombination[1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a966078e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}