San Jose State University Department of Applied Data Science
**DATA 200 Computational Programming for Data Analytics**
Spring 2023 Instructor: Ron Mak
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.13 Built-In Function `range`: A Deeper Look"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### You can specify a starting value in a call to the `range()` function. Recall that the default starting value is 0, and the ending value is not included in the values."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for number in range(5, 10):\n",
" print(number, end=' ')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### You can also specify an increment, known as the **step value**."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for number in range(0, 10, 2):\n",
" print(number, end=' ')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The step value can be negative!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for number in range(10, 0, -2):\n",
" print(number, end=' ')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **EXERCISE:** What happens if you try to print the items in `range(10,` `0,` `2)`? "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **EXERCISE:** Use a `for` statement and `range()`, and `print()` to display on one line the sequence of values 99 88 77 66 55 44 33 22 11 0, each separated by one space."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nested `for` Loops\n",
"\n",
"#### You can have a `for` loop inside of a `for` loop. The inner loop is \"nested\" in the outer loop -- it is part of the outer loop's suite. **Each time** the outer loop executes its suite, the inner loop executes its suites for **all values** of its sequence."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(3):\n",
" print(f'i = {i}')\n",
" \n",
" for j in range(2, 10, 2):\n",
" print(f' j = {j}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### You can also nest `while` loops, or any combination of `while` and `for` loops, to any depth."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **EXERCISE:** What does the following code print?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for row in range(10):\n",
" for column in range(10):\n",
" print('<' if row%2 == 1 else '>', end='')\n",
" print()"
]
},
{
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}