San Jose State University Department of Applied Data Science
**DATA 200 Computational Programming for Data Analytics**
Spring 2024 Instructor: Ron Mak
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.7 `while` Statement"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### What is the first power of 3 that is **greater than or equal to 100**?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"product = 3\n",
"\n",
"product = 3*product\n",
"print(product)\n",
"\n",
"product = 3*product\n",
"print(product)\n",
"\n",
"product = 3*product\n",
"print(product)\n",
"\n",
"product = 3*product\n",
"print(product)\n",
"\n",
"product = 3*product\n",
"print(product)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Instead of writing a group of statements over and over again, we can use a `while` loop with a suite of those statements."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"product = 3\n",
"\n",
"while product <= 50:\n",
" product = 3*product\n",
" print(product)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"product"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### How does a `while` loop work? The computer first evaluates the condition. If the condition is true, the computer executes the suite. After each time it has completed executing the suite, the computer re-evaluates the condition. If the condition is still true, the computer executes the suite again. The computer continues to execute the loop **while the condition is true** each time it's re-evaluated. As soon as the condition evaluates to false, the computer leaves the loop.\n",
"\n",
"#### If the condition evaluates to false to begin with, the computer doesn't execute the suite at all."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **EXERCISE:** Write Python code that includes a `while` loop to compute the highest power of 3 that is **less than or equal to 1000**. Don't print the intermediate powers. Will the following work?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"product = 3\n",
"\n",
"while product < 1000:\n",
" product = 3*product\n",
" #print(product)\n",
" \n",
" \n",
"print(product)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"##########################################################################\n",
"# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n",
"# Pearson Education, Inc. All Rights Reserved. #\n",
"# #\n",
"# DISCLAIMER: The authors and publisher of this book have used their #\n",
"# best efforts in preparing the book. These efforts include the #\n",
"# development, research, and testing of the theories and programs #\n",
"# to determine their effectiveness. The authors and publisher make #\n",
"# no warranty of any kind, expressed or implied, with regard to these #\n",
"# programs or to the documentation contained in these books. The authors #\n",
"# and publisher shall not be liable in any event for incidental or #\n",
"# consequential damages in connection with, or arising out of, the #\n",
"# furnishing, performance, or use of these programs. #\n",
"##########################################################################\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Additional material (C) Copyright 2023 by Ronald Mak"
]
}
],
"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": 4
}