Lecture code for 03/04/2025
This commit is contained in:
		
							parent
							
								
									019b5788d1
								
							
						
					
					
						commit
						4a5f06e9d1
					
				
					 16 changed files with 1308 additions and 0 deletions
				
			
		
										
											Binary file not shown.
										
									
								
							| 
						 | 
				
			
			@ -0,0 +1,24 @@
 | 
			
		|||
# CSE-160-D01_2024FA_Intro-to-for-Statements_0001.py
 | 
			
		||||
# Introduction to Python 'for' Statements
 | 
			
		||||
# Python 'for' statements implement "looping" in Python. In "looping" one uses 'for' statements 
 | 
			
		||||
# to execute a section of their code repeatedly (or iteratively) usually advancing some 
 | 
			
		||||
# control variable until some expression becomes false.
 | 
			
		||||
 | 
			
		||||
for i in [1, 2, 3, 4, 5]:
 | 
			
		||||
    print(f"i = {i}, i squared = {i**2}")
 | 
			
		||||
 | 
			
		||||
print("\n*******************************************\n")
 | 
			
		||||
 | 
			
		||||
myLstVar1 = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"]
 | 
			
		||||
print("The third color in the list is: ", myLstVar1[2])
 | 
			
		||||
 | 
			
		||||
print("\n*******************************************\n")
 | 
			
		||||
 | 
			
		||||
for eachColor in myLstVar1:
 | 
			
		||||
    print(eachColor)
 | 
			
		||||
 | 
			
		||||
print("\n*******************************************\n")
 | 
			
		||||
 | 
			
		||||
print("myLstVar1 contains ", len(myLstVar1), " items.")
 | 
			
		||||
for i in range(len(myLstVar1)):     # "range(n)" gives you series of numbers from 0 ... n-1
 | 
			
		||||
    print(i, myLstVar1[i])
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,159 @@
 | 
			
		|||
{
 | 
			
		||||
 "cells": [
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "markdown",
 | 
			
		||||
   "id": "847ab51a",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# Introduction to Python 'for' Statements"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "markdown",
 | 
			
		||||
   "id": "23b6fbf4",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "source": [
 | 
			
		||||
    "## Python 'for' statements implement \"looping\" in Python.  In \"looping\" one uses 'for' statements to execute a section of their code repeatedly (or iteratively) usually advancing some control variable until some expression becomes false."
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 1,
 | 
			
		||||
   "id": "e9a8883a",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "i = 1, i squared = 1\n",
 | 
			
		||||
      "i = 2, i squared = 4\n",
 | 
			
		||||
      "i = 3, i squared = 9\n",
 | 
			
		||||
      "i = 4, i squared = 16\n",
 | 
			
		||||
      "i = 5, i squared = 25\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "for i in [1, 2, 3, 4, 5]:\n",
 | 
			
		||||
    "    print(f\"i = {i}, i squared = {i**2}\")"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 2,
 | 
			
		||||
   "id": "ee7f5f12",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "The third color in the list is:  Yellow\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "myLstVar1 = [\"Red\", \"Orange\", \"Yellow\", \"Green\", \"Blue\", \"Indigo\", \"Violet\"]\n",
 | 
			
		||||
    "print(\"The third color in the list is: \", myLstVar1[2])"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 4,
 | 
			
		||||
   "id": "2667d8d8",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "Red\n",
 | 
			
		||||
      "Orange\n",
 | 
			
		||||
      "Yellow\n",
 | 
			
		||||
      "Green\n",
 | 
			
		||||
      "Blue\n",
 | 
			
		||||
      "Indigo\n",
 | 
			
		||||
      "Violet\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "for eachColor in myLstVar1:\n",
 | 
			
		||||
    "    print(eachColor)"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 4,
 | 
			
		||||
   "id": "dd6ef6a6",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "10\n",
 | 
			
		||||
      "12\n",
 | 
			
		||||
      "14\n",
 | 
			
		||||
      "16\n",
 | 
			
		||||
      "18\n",
 | 
			
		||||
      "20\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "for j in range(10,21,2):   # range(start, stop, step) returns start, start+step, start+2*step, ... while start + j*step < stop\n",
 | 
			
		||||
    "    print(j)"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 7,
 | 
			
		||||
   "id": "155e8b01",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "myLstVar1 contains  7  items.\n",
 | 
			
		||||
      "0 Red\n",
 | 
			
		||||
      "1 Orange\n",
 | 
			
		||||
      "2 Yellow\n",
 | 
			
		||||
      "3 Green\n",
 | 
			
		||||
      "4 Blue\n",
 | 
			
		||||
      "5 Indigo\n",
 | 
			
		||||
      "6 Violet\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "print(\"myLstVar1 contains \", len(myLstVar1), \" items.\")\n",
 | 
			
		||||
    "for i in range(len(myLstVar1)):     # \"range(n)\" gives you series of numbers from 0 ... n-1\n",
 | 
			
		||||
    "    print(i, myLstVar1[i])"
 | 
			
		||||
   ]
 | 
			
		||||
  }
 | 
			
		||||
 ],
 | 
			
		||||
 "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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										259
									
								
								bigos/lecture20250304/CSE-160-D01_2024FA_List-Backgrounder.ipynb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										259
									
								
								bigos/lecture20250304/CSE-160-D01_2024FA_List-Backgrounder.ipynb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,259 @@
 | 
			
		|||
{
 | 
			
		||||
 "cells": [
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 18,
 | 
			
		||||
   "id": "fb3af7ee",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "myList1 = []                    # an empty list\n",
 | 
			
		||||
    "myList2 = [16, 8, 4, 2, 1]      # a typical list\n",
 | 
			
		||||
    "myList3 = [\"Boston\", 23.5, \"Pantera\", \"Los Lobos\"]\n",
 | 
			
		||||
    "myList4 = [1, 10, 100, 1000, 10000, 100000, 1000000]\n",
 | 
			
		||||
    "myList5 = [5, 6, myList4, 8, 10]\n",
 | 
			
		||||
    "myList6 = [\"item0\", \"item1\", \"item2\", \"item3\", \"item4\"]"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 19,
 | 
			
		||||
   "id": "ffa22513",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "[] \n",
 | 
			
		||||
      " [16, 8, 4, 2, 1] \n",
 | 
			
		||||
      " ['Boston', 23.5, 'Pantera', 'Los Lobos'] \n",
 | 
			
		||||
      " [1, 10, 100, 1000, 10000, 100000, 1000000] \n",
 | 
			
		||||
      " [5, 6, [1, 10, 100, 1000, 10000, 100000, 1000000], 8, 10] \n",
 | 
			
		||||
      " ['item0', 'item1', 'item2', 'item3', 'item4']\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "print(myList1, \"\\n\", myList2, \"\\n\", myList3, \"\\n\", myList4, \"\\n\", myList5, \"\\n\", myList6)"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 20,
 | 
			
		||||
   "id": "f5c3ac8d",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "<class 'list'>\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "print(type(myList3))"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 21,
 | 
			
		||||
   "id": "43063b59",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "16 \n",
 | 
			
		||||
      " [10000] \n",
 | 
			
		||||
      " [1000, 10000, 100000, 1000000]\n",
 | 
			
		||||
      "['item2', 'item3', 'item4']\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "print(myList2[0], \"\\n\", myList4[4:5], \"\\n\", myList4[3:])\n",
 | 
			
		||||
    "print(myList6[-3:])"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 22,
 | 
			
		||||
   "id": "3802164a",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "Length of myList1 =  0\n",
 | 
			
		||||
      "Length of myList3 =  4\n",
 | 
			
		||||
      "Length of myList6 =  5\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "print(\"Length of myList1 = \", len(myList1))\n",
 | 
			
		||||
    "print(\"Length of myList3 = \", len(myList3))\n",
 | 
			
		||||
    "print(\"Length of myList6 = \", len(myList6))"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 23,
 | 
			
		||||
   "id": "3faf4519",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "1 \n",
 | 
			
		||||
      " 1000 \n",
 | 
			
		||||
      " item4\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "print(myList4[0], \"\\n\", myList4[3], \"\\n\", myList6[4])"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 24,
 | 
			
		||||
   "id": "269992d9",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "5   :   [16, 8, 4, 2, 1]\n",
 | 
			
		||||
      "6   :   [16, 8, 4, 2, 1, 0]\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "print(len(myList2), \"  :  \", myList2)\n",
 | 
			
		||||
    "myList2.append(0)\n",
 | 
			
		||||
    "print(len(myList2), \"  :  \", myList2)"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 27,
 | 
			
		||||
   "id": "25ea7ee9",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "4   :   ['Boston', 23.5, 'Pantera', 'Los Lobos']\n",
 | 
			
		||||
      "5   :   ['Boston', 23.5, 'Jeff Beck', 'Pantera', 'Los Lobos']\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "print(len(myList3), \"  :  \", myList3)\n",
 | 
			
		||||
    "myList3.insert(2, \"Jeff Beck\")\n",
 | 
			
		||||
    "print(len(myList3), \"  :  \", myList3)"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 28,
 | 
			
		||||
   "id": "144b2484",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "Other list operations include: extend(), remove(), pop(), & del()\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "print(\"Other list operations include: extend(), remove(), pop(), & del()\")"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 30,
 | 
			
		||||
   "id": "292d5cf1",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "[16, 8, 4, 2, 1, 0] myList2 as is\n",
 | 
			
		||||
      "[0, 1, 2, 4, 8, 16] myList2 sorted (normal order)\n",
 | 
			
		||||
      "[16, 8, 4, 2, 1, 0] myList2 sorted (reverse order)\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "print(myList2, \"myList2 as is\")\n",
 | 
			
		||||
    "myList2.sort()\n",
 | 
			
		||||
    "print(myList2, \"myList2 sorted (normal order)\")\n",
 | 
			
		||||
    "myList2.sort(reverse = True)\n",
 | 
			
		||||
    "print(myList2, \"myList2 sorted (reverse order)\")"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 33,
 | 
			
		||||
   "id": "5f4b1915",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "[5, 6, [1, 10, 100, 1000, 10000, 100000, 1000000], 8, 10]\n",
 | 
			
		||||
      "100000\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "print(myList5)\n",
 | 
			
		||||
    "print(myList5[2][5])"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": null,
 | 
			
		||||
   "id": "4ebaf20b",
 | 
			
		||||
   "metadata": {
 | 
			
		||||
    "collapsed": true
 | 
			
		||||
   },
 | 
			
		||||
   "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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,137 @@
 | 
			
		|||
{
 | 
			
		||||
 "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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										9
									
								
								bigos/lecture20250304/Conditionals_0001.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								bigos/lecture20250304/Conditionals_0001.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
# Conditionals_0001.py
 | 
			
		||||
#
 | 
			
		||||
boolTest1 = 63 > 127
 | 
			
		||||
boolTest2 = 63 < 127
 | 
			
		||||
print(boolTest1, "\n", boolTest2)
 | 
			
		||||
myNum = 100
 | 
			
		||||
boolTest3 = (myNum > 63) and (myNum < 127)
 | 
			
		||||
print(boolTest3)
 | 
			
		||||
print(not boolTest3)
 | 
			
		||||
							
								
								
									
										56
									
								
								bigos/lecture20250304/List-Intro_0001.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								bigos/lecture20250304/List-Intro_0001.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,56 @@
 | 
			
		|||
# List-Intro_0001.py
 | 
			
		||||
#
 | 
			
		||||
# A Python List is a type of variable which may contain
 | 
			
		||||
# zero or more values (could be numbers, strings, etc.)
 | 
			
		||||
# in an ordered manner.  Each value can be accessed using
 | 
			
		||||
# an index.  Here are some examples of python list variables:
 | 
			
		||||
#
 | 
			
		||||
myList1 = []                    # an empty list
 | 
			
		||||
myList2 = [16, 8, 4, 2, 1]      # a typical list
 | 
			
		||||
myList3 = ["Boston", 23.5, "Pantera", "Los Lobos"]
 | 
			
		||||
myList4 = [1, 10, 100, 1000, 10000, 100000, 1000000]
 | 
			
		||||
myList5 = [5, 6, myList4, 8, 10]
 | 
			
		||||
myList6 = ["item0", "item1", "item2", "item3", "item4"]
 | 
			
		||||
 | 
			
		||||
print(myList1, "\n", myList2, "\n", myList3, "\n", myList4, "\n", myList5, "\n", myList6)
 | 
			
		||||
 | 
			
		||||
# Finding the length of a list  len(list variable name):
 | 
			
		||||
print("Length of myList1 = ", len(myList1))
 | 
			
		||||
print("Length of myList3 = ", len(myList3))
 | 
			
		||||
print("Length of myList4 = ", len(myList4))
 | 
			
		||||
print("Length of myList6 = ", len(myList6))
 | 
			
		||||
 | 
			
		||||
# List indices begin at 0 and end with len(list variable name) - 1
 | 
			
		||||
print(myList4[0], "\n", myList4[3], "\n", myList6[4])
 | 
			
		||||
 | 
			
		||||
# Grabbing more than one list item is called slicing:
 | 
			
		||||
print(myList2[0], "\n", myList4[2:5], "\n", myList4[3:])
 | 
			
		||||
print(myList6[-3:])
 | 
			
		||||
 | 
			
		||||
print("Adding and removing list items ...")
 | 
			
		||||
# How does one add or remove items from a list?
 | 
			
		||||
#    append()   Adds an item to the end of the list:
 | 
			
		||||
print(len(myList2), "  :  ", myList2)
 | 
			
		||||
myList2.append(0)
 | 
			
		||||
print(len(myList2), "  :  ", myList2)
 | 
			
		||||
 | 
			
		||||
#    insert()   Adds an item at a specific index
 | 
			
		||||
print(len(myList3), "  :  ", myList3)
 | 
			
		||||
myList3.insert(2, "Jeff Beck")
 | 
			
		||||
print(len(myList3), "  :  ", myList3)
 | 
			
		||||
 | 
			
		||||
print("Other list operations include: extend(), remove(), pop(), & del()")
 | 
			
		||||
 | 
			
		||||
print("You can also sort lists")
 | 
			
		||||
# You can also sort lists:
 | 
			
		||||
print(myList2)
 | 
			
		||||
myList2.sort()
 | 
			
		||||
print(myList2)
 | 
			
		||||
myList2.sort(reverse = True)
 | 
			
		||||
print(myList2)
 | 
			
		||||
 | 
			
		||||
print("Indexing a list of lists ...")
 | 
			
		||||
# You can have multi-dimensional lists (i.e. list of lists):
 | 
			
		||||
print(myList5)
 | 
			
		||||
print(myList5[2])
 | 
			
		||||
print(myList5[2][5])
 | 
			
		||||
							
								
								
									
										52
									
								
								bigos/lecture20250304/Tuples-Intro_0001.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								bigos/lecture20250304/Tuples-Intro_0001.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,52 @@
 | 
			
		|||
# 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])
 | 
			
		||||
							
								
								
									
										160
									
								
								bigos/lecture20250304/f-String-Intro_0001.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										160
									
								
								bigos/lecture20250304/f-String-Intro_0001.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,160 @@
 | 
			
		|||
################################################################################################################
 | 
			
		||||
#
 | 
			
		||||
# A Brief Introduction to Python f-Strings
 | 
			
		||||
# f-Strings are used in Python to "pretty print" (i.e. format) printed results.
 | 
			
		||||
#
 | 
			
		||||
# Here's a typical Python print statement with very little print formatting:
 | 
			
		||||
myVar1 = 21.3
 | 
			
		||||
myVar2 = -5.7
 | 
			
		||||
myVar3 = myVar1 * myVar2
 | 
			
		||||
print(myVar3)
 | 
			
		||||
#    or    
 | 
			
		||||
print("The product of myVar1 and myVar2 is: ", myVar3)
 | 
			
		||||
 | 
			
		||||
print("################################################################################################################")
 | 
			
		||||
# f-Strings give us much more capability in formatting and therefore conveying the results of our programs.
 | 
			
		||||
# For a nice introduction to f-Strings check out:
 | 
			
		||||
#    http://cissandbox.bentley.edu/sandbox/wp-content/uploads/2022-02-10-Documentation-on-f-strings-Updated.pdf
 | 
			
		||||
#
 | 
			
		||||
import math                            # import Python's "math" module
 | 
			
		||||
import sys
 | 
			
		||||
variable = math.pi                     # set "variable" = the math module very accurate representation of pi.
 | 
			
		||||
 | 
			
		||||
print(f"Using Numeric {variable = }")  # The name of the variable you're printing goes in {} (curly braces).
 | 
			
		||||
                                       # We're using a 25 character wide field.
 | 
			
		||||
print(f"|{variable:25}|")              # Here we the vertical bar character known as "pipe" before and after.
 | 
			
		||||
print(f"|{variable:<25}|")             # Here we left   justify the printed value.
 | 
			
		||||
print(f"|{variable:>25}|")             # Here we right  justify the printed value.
 | 
			
		||||
print(f"|{variable:^25}|\n")           # Here we center justify the printed value.
 | 
			
		||||
 | 
			
		||||
variable = sys.version                 # Here we do the sames things with a string variable
 | 
			
		||||
variable = "Python " + variable[0:6]   # We form the string variable from the current version of Python.
 | 
			
		||||
print(f"Using String {variable = }") 
 | 
			
		||||
print(f"|{variable:25}|") 
 | 
			
		||||
print(f"|{variable:<25}|") 
 | 
			
		||||
print(f"|{variable:>25}|") 
 | 
			
		||||
print(f"|{variable:^25}|") 
 | 
			
		||||
 | 
			
		||||
print("################################################################################################################")
 | 
			
		||||
 | 
			
		||||
# Here we do the same but we provide a fill character instead of just spaces.
 | 
			
		||||
# Our variable, variable, is still equal to "Python 3.11.5"
 | 
			
		||||
print(f"Using String {variable = }") 
 | 
			
		||||
print(f"|{variable:=<25}|") 
 | 
			
		||||
print(f"|{variable:=>25}|") 
 | 
			
		||||
print(f"|{variable:=^25}|\n") 
 | 
			
		||||
variable = "Python 3.11.5" 
 | 
			
		||||
print(f"Using String {variable = }") 
 | 
			
		||||
print(f"|{variable:=<25}|") 
 | 
			
		||||
print(f"|{variable:=>25}|") 
 | 
			
		||||
print(f"|{variable:=^25}|") 
 | 
			
		||||
 | 
			
		||||
print("################################################################################################################")
 | 
			
		||||
 | 
			
		||||
# Here is a summary of how to format various data types using f-Strings:
 | 
			
		||||
# Type  Meaning
 | 
			
		||||
# ----  ------------------------------------------------------------------------------------------------------------
 | 
			
		||||
#   s   String format—this is the default type for strings
 | 
			
		||||
#   d   Decimal Integer. This uses a comma as the number separator character. 
 | 
			
		||||
#   n   Number. This is the same as d except that it uses the current locale setting to 
 | 
			
		||||
#       insert the appropriate number separator characters. 
 | 
			
		||||
#   e   Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent. 
 | 
			
		||||
#       The default precision is 6. 
 | 
			
		||||
#   f   Fixed-point notation. Displays the number as a fixed-point number. The default precision is 6.
 | 
			
		||||
#   %   Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign. 
 | 
			
		||||
 | 
			
		||||
variable = 10 
 | 
			
		||||
print(f"Using Numeric {variable = }") 
 | 
			
		||||
print(f"This prints without formatting {variable}") 
 | 
			
		||||
print(f"This prints with formatting {variable:d}") 
 | 
			
		||||
print(f"This prints also with formatting {variable:n}") 
 | 
			
		||||
print(f"This prints with spacing {variable:10d}\n") 
 | 
			
		||||
 | 
			
		||||
variable = math.pi 
 | 
			
		||||
print(f"Using Numeric {variable = }") 
 | 
			
		||||
print(f"This prints without formatting {variable}") 
 | 
			
		||||
print(f"This prints with formatting {variable:f}") 
 | 
			
		||||
print(f"This prints with spacing {variable:20f}") 
 | 
			
		||||
 | 
			
		||||
variable = 4 
 | 
			
		||||
print(f"Using Numeric {variable = }") 
 | 
			
		||||
print(f"This prints without formatting {variable}") 
 | 
			
		||||
print(f"This prints with percent formatting {variable:%}\n") 
 | 
			
		||||
variable = 403267890 
 | 
			
		||||
print(f"Using Numeric {variable = }") 
 | 
			
		||||
print(f"This prints with exponential formatting {variable:e}") 
 | 
			
		||||
 | 
			
		||||
print("################################################################################################################")
 | 
			
		||||
 | 
			
		||||
variable = 1200356.8796 
 | 
			
		||||
print(f"Using Numeric {variable = }") 
 | 
			
		||||
print(f"With two decimal places: {variable:.2f}") 
 | 
			
		||||
print(f"With four decimal places: {variable:.3f}") 
 | 
			
		||||
print(f"With two decimal places and a comma: {variable:,.2f}") 
 | 
			
		||||
print(f"With a forced plus sign: {variable:+.2f}\n") 
 | 
			
		||||
 | 
			
		||||
variable *= -1 
 | 
			
		||||
print(f"Using Numeric {variable = }") 
 | 
			
		||||
print(f"With two decimal places and a comma: {variable:,.2f}") 
 | 
			
		||||
 | 
			
		||||
print("################################################################################################################")
 | 
			
		||||
 | 
			
		||||
# Use of Tabs and Spacing Program 
 | 
			
		||||
# output is often required to be in tabular form. f-strings are very useful in formatting this kind of output. 
 | 
			
		||||
# The following lines produce a tidily aligned set of columns with integers and their squares and cubes, 
 | 
			
		||||
# using spaces to align the columns. 
 | 
			
		||||
# Notice the use of the field width to ensure that all the numbers are right-aligned: 
 | 
			
		||||
print(f'Number   Square       Cube') 
 | 
			
		||||
for x in range(1, 11): 
 | 
			
		||||
    print(f'{x:2d}       {x*x:3d}          {x*x*x:4d}') 
 | 
			
		||||
 | 
			
		||||
print("################################################################################################################")
 | 
			
		||||
 | 
			
		||||
# The tab character (\t) can also be used in an f-string to line up columns, 
 | 
			
		||||
# particularly when column headings are used: 
 | 
			
		||||
print(f'Number\t\t\tSquare\t\t\tCube') 
 | 
			
		||||
for x in range(1, 11):
 | 
			
		||||
    print(f'{x:2d}\t\t\t{x*x:3d}\t\t\t{x*x*x:4d}')
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
# Using either tabs or spacing is acceptable and may depend on which one you are more comfortable with. 
 | 
			
		||||
# Either way is acceptable in Python. 
 | 
			
		||||
# The following takes the previous program and converts x to a float() so that formatting of floating-point numbers 
 | 
			
		||||
# can be demonstrated: 
 | 
			
		||||
print(f'Number\t\tSquare\t\tCube') 
 | 
			
		||||
for x in range(1, 11):
 | 
			
		||||
    x = float(x)
 | 
			
		||||
    print(f'{x:5.2f}\t\t{x*x:6.2f}\t\t{x*x*x:8.2f}') 
 | 
			
		||||
 | 
			
		||||
print("################################################################################################################")
 | 
			
		||||
 | 
			
		||||
# This also demonstrates how the use of a value for width will enable the columns to line up. 
 | 
			
		||||
# The following program demonstrates the use of strings, decimals, and floats, as well as tabs 
 | 
			
		||||
# for a type of report that is often produced in a typical Python program. 
 | 
			
		||||
# Notice the use of the dollar sign ($) just before the variables that are to be displayed as prices. 
 | 
			
		||||
APPLES = .50 
 | 
			
		||||
BREAD = 1.50 
 | 
			
		||||
CHEESE = 2.25 
 | 
			
		||||
 | 
			
		||||
numApples = 3
 | 
			
		||||
numBread = 10 
 | 
			
		||||
numCheese = 6 
 | 
			
		||||
 | 
			
		||||
prcApples = numApples * APPLES 
 | 
			
		||||
prcBread = numBread* BREAD 
 | 
			
		||||
prcCheese = numCheese * CHEESE 
 | 
			
		||||
 | 
			
		||||
strApples = 'Apples' 
 | 
			
		||||
strBread = 'Rye Bread' 
 | 
			
		||||
strCheese = 'Cheese' 
 | 
			
		||||
 | 
			
		||||
total = prcBread + prcCheese + prcApples 
 | 
			
		||||
 | 
			
		||||
print(f'{"My Grocery List":^30s}') 
 | 
			
		||||
print(f'{"="*30}') 
 | 
			
		||||
print(f'{strApples:10s}{numApples:10d}\t${prcApples:>5.2f}') 
 | 
			
		||||
print(f'{strBread:10s}{numBread:10d}\t${prcBread:>5.2f}') 
 | 
			
		||||
print(f'{strCheese:10s}{numCheese:10d}\t${prcCheese:>5.2f}') 
 | 
			
		||||
print(f'{"Total:":>20s}\t${total:>5.2f}') 
 | 
			
		||||
 | 
			
		||||
print("################################################################################################################")
 | 
			
		||||
							
								
								
									
										414
									
								
								bigos/lecture20250304/f-Strings-Intro_0001.ipynb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										414
									
								
								bigos/lecture20250304/f-Strings-Intro_0001.ipynb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,414 @@
 | 
			
		|||
{
 | 
			
		||||
 "cells": [
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "markdown",
 | 
			
		||||
   "id": "de0911a3",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# A Brief Introduction to Python f-Strings"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "markdown",
 | 
			
		||||
   "id": "46d3d64d",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "source": [
 | 
			
		||||
    "## f-Strings are used in Python to \"pretty print\" (i.e. format) printed results."
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 2,
 | 
			
		||||
   "id": "d20dcd5f",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "-121.41000000000001\n",
 | 
			
		||||
      "The product of myVar1 and myVar2 is:  -121.41000000000001\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# Here's a typical Python print statement with very little print formatting:\n",
 | 
			
		||||
    "myVar1 = 21.3\n",
 | 
			
		||||
    "myVar2 = -5.7\n",
 | 
			
		||||
    "myVar3 = myVar1 * myVar2\n",
 | 
			
		||||
    "print(myVar3)\n",
 | 
			
		||||
    "#    or    \n",
 | 
			
		||||
    "print(\"The product of myVar1 and myVar2 is: \", myVar3)"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 3,
 | 
			
		||||
   "id": "564c0014",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "Using Numeric variable = 3.141592653589793\n",
 | 
			
		||||
      "|        3.141592653589793|\n",
 | 
			
		||||
      "|3.141592653589793        |\n",
 | 
			
		||||
      "|        3.141592653589793|\n",
 | 
			
		||||
      "|    3.141592653589793    |\n",
 | 
			
		||||
      "\n",
 | 
			
		||||
      "Using String variable = 'Python 3.11.5'\n",
 | 
			
		||||
      "|Python 3.11.5            |\n",
 | 
			
		||||
      "|Python 3.11.5            |\n",
 | 
			
		||||
      "|            Python 3.11.5|\n",
 | 
			
		||||
      "|      Python 3.11.5      |\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# f-Strings give us much more capability in formatting and therefore conveying the results of our programs.\n",
 | 
			
		||||
    "# For a nice introduction to f-Strings check out:\n",
 | 
			
		||||
    "#    http://cissandbox.bentley.edu/sandbox/wp-content/uploads/2022-02-10-Documentation-on-f-strings-Updated.pdf\n",
 | 
			
		||||
    "#\n",
 | 
			
		||||
    "import math                            # import Python's \"math\" module\n",
 | 
			
		||||
    "import sys                             # import Python's \"system\" module\n",
 | 
			
		||||
    "variable = math.pi                     # set \"variable\" = the math module very accurate representation of pi.\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "print(f\"Using Numeric {variable = }\")  # The name of the variable you're printing goes in {} (curly braces).\n",
 | 
			
		||||
    "                                       # We're using a 25 character wide field.\n",
 | 
			
		||||
    "print(f\"|{variable:25}|\")              # Here we use the vertical bar character known as \"pipe\" before and after.\n",
 | 
			
		||||
    "print(f\"|{variable:<25}|\")             # Here we left   justify the printed value.\n",
 | 
			
		||||
    "print(f\"|{variable:>25}|\")             # Here we right  justify the printed value.\n",
 | 
			
		||||
    "print(f\"|{variable:^25}|\\n\")           # Here we center justify the printed value.\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "variable = sys.version                 # Here we do the sames things with a string variable\n",
 | 
			
		||||
    "variable = \"Python \" + variable[0:6]\n",
 | 
			
		||||
    "print(f\"Using String {variable = }\") \n",
 | 
			
		||||
    "print(f\"|{variable:25}|\") \n",
 | 
			
		||||
    "print(f\"|{variable:<25}|\") \n",
 | 
			
		||||
    "print(f\"|{variable:>25}|\") \n",
 | 
			
		||||
    "print(f\"|{variable:^25}|\") "
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 13,
 | 
			
		||||
   "id": "9f9b8587",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "Using Numeric variable = 3.141592653589793\n",
 | 
			
		||||
      "|3.141592653589793========|\n",
 | 
			
		||||
      "|========3.141592653589793|\n",
 | 
			
		||||
      "|====3.141592653589793====|\n",
 | 
			
		||||
      "\n",
 | 
			
		||||
      "Using String variable = 'Python 3.11.5'\n",
 | 
			
		||||
      "|Python 3.11.5============|\n",
 | 
			
		||||
      "|============Python 3.11.5|\n",
 | 
			
		||||
      "|======Python 3.11.5======|\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# Here we do the same but we provide a fill character instead of just spaces.\n",
 | 
			
		||||
    "# Our variable, variable, is still equal to the current system Python version.\n",
 | 
			
		||||
    "variable = math.pi\n",
 | 
			
		||||
    "print(f\"Using Numeric {variable = }\") \n",
 | 
			
		||||
    "print(f\"|{variable:=<25}|\") \n",
 | 
			
		||||
    "print(f\"|{variable:=>25}|\") \n",
 | 
			
		||||
    "print(f\"|{variable:=^25}|\\n\") \n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "variable = sys.version                 # Here we do the sames things with a string variable\n",
 | 
			
		||||
    "variable = \"Python \" + variable[0:6]\n",
 | 
			
		||||
    "print(f\"Using String {variable = }\") \n",
 | 
			
		||||
    "print(f\"|{variable:=<25}|\") \n",
 | 
			
		||||
    "print(f\"|{variable:=>25}|\") \n",
 | 
			
		||||
    "print(f\"|{variable:=^25}|\") "
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 15,
 | 
			
		||||
   "id": "deee1957",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "Using Numeric variable = 10\n",
 | 
			
		||||
      "This prints without formatting 10\n",
 | 
			
		||||
      "This prints with formatting 10\n",
 | 
			
		||||
      "This prints also with formatting 10\n",
 | 
			
		||||
      "This prints with spacing         10\n",
 | 
			
		||||
      "\n",
 | 
			
		||||
      "Using Numeric variable = 3.141592653589793\n",
 | 
			
		||||
      "This prints without formatting 3.141592653589793\n",
 | 
			
		||||
      "This prints with formatting 3.141593\n",
 | 
			
		||||
      "This prints with spacing             3.141593\n",
 | 
			
		||||
      "\n",
 | 
			
		||||
      "Using Numeric variable = 4\n",
 | 
			
		||||
      "This prints without formatting 4\n",
 | 
			
		||||
      "This prints with percent formatting 400.000000%\n",
 | 
			
		||||
      "\n",
 | 
			
		||||
      "Using Numeric variable = 403267890\n",
 | 
			
		||||
      "This prints with exponential formatting 4.032679e+08\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# Here is a summary of how to format various data types using f-Strings:\n",
 | 
			
		||||
    "# Type  Meaning\n",
 | 
			
		||||
    "# ----  ------------------------------------------------------------------------------------------------------------\n",
 | 
			
		||||
    "#   s   String format—this is the default type for strings\n",
 | 
			
		||||
    "#   d   Decimal Integer. This uses a comma as the number separator character. \n",
 | 
			
		||||
    "#   n   Number. This is the same as d except that it uses the current locale setting to \n",
 | 
			
		||||
    "#       insert the appropriate number separator characters. \n",
 | 
			
		||||
    "#   e   Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent. \n",
 | 
			
		||||
    "#       The default precision is 6. \n",
 | 
			
		||||
    "#   f   Fixed-point notation. Displays the number as a fixed-point number. The default precision is 6.\n",
 | 
			
		||||
    "#   %   Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign. \n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "variable = 10 \n",
 | 
			
		||||
    "print(f\"Using Numeric {variable = }\") \n",
 | 
			
		||||
    "print(f\"This prints without formatting {variable}\") \n",
 | 
			
		||||
    "print(f\"This prints with formatting {variable:d}\") \n",
 | 
			
		||||
    "print(f\"This prints also with formatting {variable:n}\") \n",
 | 
			
		||||
    "print(f\"This prints with spacing {variable:10d}\\n\") \n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "variable = math.pi \n",
 | 
			
		||||
    "print(f\"Using Numeric {variable = }\") \n",
 | 
			
		||||
    "print(f\"This prints without formatting {variable}\") \n",
 | 
			
		||||
    "print(f\"This prints with formatting {variable:f}\") \n",
 | 
			
		||||
    "print(f\"This prints with spacing {variable:20f}\\n\") \n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "variable = 4 \n",
 | 
			
		||||
    "print(f\"Using Numeric {variable = }\") \n",
 | 
			
		||||
    "print(f\"This prints without formatting {variable}\") \n",
 | 
			
		||||
    "print(f\"This prints with percent formatting {variable:%}\\n\") \n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "variable = 403267890 \n",
 | 
			
		||||
    "print(f\"Using Numeric {variable = }\") \n",
 | 
			
		||||
    "print(f\"This prints with exponential formatting {variable:e}\") "
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 16,
 | 
			
		||||
   "id": "2a3efefd",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "Using Numeric variable = 1200356.8796\n",
 | 
			
		||||
      "With two decimal places: 1200356.88\n",
 | 
			
		||||
      "With four decimal places: 1200356.8796\n",
 | 
			
		||||
      "With two decimal places and a comma: 1,200,356.88\n",
 | 
			
		||||
      "With a forced plus sign: +1200356.88\n",
 | 
			
		||||
      "\n",
 | 
			
		||||
      "Using Numeric variable = -1200356.8796\n",
 | 
			
		||||
      "With two decimal places and a comma: -1,200,356.88\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "variable = 1200356.8796 \n",
 | 
			
		||||
    "print(f\"Using Numeric {variable = }\") \n",
 | 
			
		||||
    "print(f\"With two decimal places: {variable:.2f}\") \n",
 | 
			
		||||
    "print(f\"With four decimal places: {variable:.4f}\") \n",
 | 
			
		||||
    "print(f\"With two decimal places and a comma: {variable:,.2f}\") \n",
 | 
			
		||||
    "print(f\"With a forced plus sign: {variable:+.2f}\\n\") \n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "variable *= -1 \n",
 | 
			
		||||
    "print(f\"Using Numeric {variable = }\") \n",
 | 
			
		||||
    "print(f\"With two decimal places and a comma: {variable:,.2f}\") "
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 7,
 | 
			
		||||
   "id": "585ae52d",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "Number   Square       Cube\n",
 | 
			
		||||
      " 1         1             1\n",
 | 
			
		||||
      " 2         4             8\n",
 | 
			
		||||
      " 3         9            27\n",
 | 
			
		||||
      " 4        16            64\n",
 | 
			
		||||
      " 5        25           125\n",
 | 
			
		||||
      " 6        36           216\n",
 | 
			
		||||
      " 7        49           343\n",
 | 
			
		||||
      " 8        64           512\n",
 | 
			
		||||
      " 9        81           729\n",
 | 
			
		||||
      "10       100          1000\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# Use of Tabs and Spacing Program \n",
 | 
			
		||||
    "# output is often required to be in tabular form. f-strings are very useful in formatting this kind of output. \n",
 | 
			
		||||
    "# The following lines produce a tidily aligned set of columns with integers and their squares and cubes, \n",
 | 
			
		||||
    "# using spaces to align the columns. \n",
 | 
			
		||||
    "# Notice the use of the field width to ensure that all the numbers are right-aligned: \n",
 | 
			
		||||
    "print(f'Number   Square       Cube') \n",
 | 
			
		||||
    "for x in range(1, 11): \n",
 | 
			
		||||
    "    print(f'{x:2d}       {x*x:3d}          {x*x*x:4d}') "
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 8,
 | 
			
		||||
   "id": "28b43c9c",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "Number\t\t\tSquare\t\t\tCube\n",
 | 
			
		||||
      " 1\t\t\t  1\t\t\t   1\n",
 | 
			
		||||
      " 2\t\t\t  4\t\t\t   8\n",
 | 
			
		||||
      " 3\t\t\t  9\t\t\t  27\n",
 | 
			
		||||
      " 4\t\t\t 16\t\t\t  64\n",
 | 
			
		||||
      " 5\t\t\t 25\t\t\t 125\n",
 | 
			
		||||
      " 6\t\t\t 36\t\t\t 216\n",
 | 
			
		||||
      " 7\t\t\t 49\t\t\t 343\n",
 | 
			
		||||
      " 8\t\t\t 64\t\t\t 512\n",
 | 
			
		||||
      " 9\t\t\t 81\t\t\t 729\n",
 | 
			
		||||
      "10\t\t\t100\t\t\t1000\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# The tab character (\\t) can also be used in an f-string to line up columns, \n",
 | 
			
		||||
    "# particularly when column headings are used: \n",
 | 
			
		||||
    "print(f'Number\\t\\t\\tSquare\\t\\t\\tCube') \n",
 | 
			
		||||
    "for x in range(1, 11):\n",
 | 
			
		||||
    "    print(f'{x:2d}\\t\\t\\t{x*x:3d}\\t\\t\\t{x*x*x:4d}')"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 9,
 | 
			
		||||
   "id": "de6c45f8",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "Number\t\tSquare\t\tCube\n",
 | 
			
		||||
      " 1.00\t\t  1.00\t\t    1.00\n",
 | 
			
		||||
      " 2.00\t\t  4.00\t\t    8.00\n",
 | 
			
		||||
      " 3.00\t\t  9.00\t\t   27.00\n",
 | 
			
		||||
      " 4.00\t\t 16.00\t\t   64.00\n",
 | 
			
		||||
      " 5.00\t\t 25.00\t\t  125.00\n",
 | 
			
		||||
      " 6.00\t\t 36.00\t\t  216.00\n",
 | 
			
		||||
      " 7.00\t\t 49.00\t\t  343.00\n",
 | 
			
		||||
      " 8.00\t\t 64.00\t\t  512.00\n",
 | 
			
		||||
      " 9.00\t\t 81.00\t\t  729.00\n",
 | 
			
		||||
      "10.00\t\t100.00\t\t 1000.00\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# Using either tabs or spacing is acceptable and may depend on which one you are more comfortable with. \n",
 | 
			
		||||
    "# Either way is acceptable in Python. \n",
 | 
			
		||||
    "# The following takes the previous program and converts x to a float() so that formatting of floating-point numbers \n",
 | 
			
		||||
    "# can be demonstrated: \n",
 | 
			
		||||
    "print(f'Number\\t\\tSquare\\t\\tCube') \n",
 | 
			
		||||
    "for x in range(1, 11):\n",
 | 
			
		||||
    "    x = float(x)\n",
 | 
			
		||||
    "    print(f'{x:5.2f}\\t\\t{x*x:6.2f}\\t\\t{x*x*x:8.2f}') "
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 10,
 | 
			
		||||
   "id": "47fcbb2b",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "       My Grocery List        \n",
 | 
			
		||||
      "==============================\n",
 | 
			
		||||
      "Apples             3\t$ 1.50\n",
 | 
			
		||||
      "Rye Bread         10\t$15.00\n",
 | 
			
		||||
      "Cheese             6\t$13.50\n",
 | 
			
		||||
      "              Total:\t$30.00\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# This also demonstrates how the use of a value for width will enable the columns to line up. \n",
 | 
			
		||||
    "# The following program demonstrates the use of strings, decimals, and floats, as well as tabs \n",
 | 
			
		||||
    "# for a type of report that is often produced in a typical Python program. \n",
 | 
			
		||||
    "# Notice the use of the dollar sign ($) just before the variables that are to be displayed as prices. \n",
 | 
			
		||||
    "APPLES = .50 \n",
 | 
			
		||||
    "BREAD = 1.50 \n",
 | 
			
		||||
    "CHEESE = 2.25 \n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "numApples = 3\n",
 | 
			
		||||
    "numBread = 10 \n",
 | 
			
		||||
    "numCheese = 6 \n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "prcApples = numApples * APPLES \n",
 | 
			
		||||
    "prcBread = numBread* BREAD \n",
 | 
			
		||||
    "prcCheese = numCheese * CHEESE \n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "strApples = 'Apples' \n",
 | 
			
		||||
    "strBread = 'Rye Bread' \n",
 | 
			
		||||
    "strCheese = 'Cheese' \n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "total = prcBread + prcCheese + prcApples \n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "print(f'{\"My Grocery List\":^30s}') \n",
 | 
			
		||||
    "print(f'{\"=\"*30}') \n",
 | 
			
		||||
    "print(f'{strApples:10s}{numApples:10d}\\t${prcApples:>5.2f}') \n",
 | 
			
		||||
    "print(f'{strBread:10s}{numBread:10d}\\t${prcBread:>5.2f}') \n",
 | 
			
		||||
    "print(f'{strCheese:10s}{numCheese:10d}\\t${prcCheese:>5.2f}') \n",
 | 
			
		||||
    "print(f'{\"Total:\":>20s}\\t${total:>5.2f}') "
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": null,
 | 
			
		||||
   "id": "c85047aa",
 | 
			
		||||
   "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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								bigos/lecture20250304/homework04a.zip
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								bigos/lecture20250304/homework04a.zip
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								bigos/lecture20250304/homework04b.zip
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								bigos/lecture20250304/homework04b.zip
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								bigos/lecture20250304/homework04c.zip
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								bigos/lecture20250304/homework04c.zip
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										24
									
								
								bigos/lecture20250304/if-Statement_0001.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								bigos/lecture20250304/if-Statement_0001.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,24 @@
 | 
			
		|||
# if-Statement_0001.py
 | 
			
		||||
#
 | 
			
		||||
myNum1 = int(input("Please enter an integer: "))
 | 
			
		||||
if (myNum1 == 23):
 | 
			
		||||
    print("Yes, you entered 23!")
 | 
			
		||||
else:
 | 
			
		||||
    print("OK, that's not equal to 23.")
 | 
			
		||||
 | 
			
		||||
myNum2 = float(input("Please enter a number for square root: "))
 | 
			
		||||
if (myNum2 < 0.0):
 | 
			
		||||
    print("Sorry, I don't take square roots of negative numbers.")
 | 
			
		||||
else:
 | 
			
		||||
    print(myNum2**0.5)
 | 
			
		||||
 | 
			
		||||
if (myNum1 == 23):
 | 
			
		||||
    print("A")
 | 
			
		||||
elif (myNum1 > 0):
 | 
			
		||||
    print("The number is positive.")
 | 
			
		||||
elif (myNum1 == 0):
 | 
			
		||||
    print("The number is 0")
 | 
			
		||||
elif (myNum1 < 0):
 | 
			
		||||
    print("The number is negative.")
 | 
			
		||||
else:
 | 
			
		||||
    print("Ah, forget it.")
 | 
			
		||||
							
								
								
									
										7
									
								
								bigos/lecture20250304/while-Statement_0001.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								bigos/lecture20250304/while-Statement_0001.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,7 @@
 | 
			
		|||
# while-Statement_0001.py
 | 
			
		||||
#
 | 
			
		||||
myNum1 = 12
 | 
			
		||||
while (myNum1 >= 0):
 | 
			
		||||
    myNum1 = int(input("Please enter an integer: "))
 | 
			
		||||
    print("That's great!", myNum1)
 | 
			
		||||
    
 | 
			
		||||
							
								
								
									
										7
									
								
								bigos/lecture20250304/while-Statement_0002.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								bigos/lecture20250304/while-Statement_0002.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,7 @@
 | 
			
		|||
# while-Statement_0002.py
 | 
			
		||||
#
 | 
			
		||||
number = 1                              # initialize variable 'number'
 | 
			
		||||
while(number <= 10):                    # execute the indented statements below while x less than or equal to 10.
 | 
			
		||||
    print(number, " : ", number**2)     # print 'number' and number squared
 | 
			
		||||
    number = number + 1                 # replace number with number + 1
 | 
			
		||||
print("Done.")                          # print "Done." after dropping out of while loop.
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue