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.6 `if`…`else` and `if`…`elif`…`else` Statements\n",
"\n",
"#### The `if`…`else` statement executes one suite or another, depending on whether a condition is `True` or `False`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 85\n",
"\n",
"if grade >= 60: \n",
" print('Passed!')\n",
" print('Great job!')\n",
"else:\n",
" print('Failed!')\n",
" print('Too bad.')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 57\n",
"\n",
"if grade >= 60: \n",
" print('Passed!')\n",
" print('Great job!')\n",
"else:\n",
" print('Failed!')\n",
" print('Too bad.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Conditional Expressions\n",
"\n",
"#### Suppose you want to store a different value in a variable like `result` based on the value of a condition."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 87\n",
"\n",
"if grade >= 60:\n",
" result = 'Passed'\n",
"else:\n",
" result = 'Failed'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"result"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### A more consise way to write this is with a **conditional expression**."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"result = ('Passed' if grade >= 60 else 'Failed')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"result"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The parentheses aren't required, but they make it clearer what you're assigning to `result`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"result = 'Passed' if grade >= 60 else 'Failed'\n",
"\n",
"result"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### We can simply display the value of a conditional expression."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 50\n",
"\n",
"'Passed' if grade >= 60 else 'Failed'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **Yikes!** The following is possible, but maybe it's too hard (for us humans) to read? At least we didn't write it as one long line."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 75\n",
"\n",
"letter = 'A' if grade >= 90 \\\n",
" else 'B' if grade >= 80 \\\n",
" else 'C' if grade >= 70 \\\n",
" else 'D' if grade >= 60 \\\n",
" else 'F'\n",
"\n",
"letter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Multiple Statements in a Suite"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 49\n",
"\n",
"if grade >= 60:\n",
" print('Passed')\n",
"else:\n",
" print('Failed')\n",
" print('You must take this course again')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 100\n",
"\n",
"if grade >= 60:\n",
" print('Passed')\n",
"else:\n",
" print('Failed')\n",
" print('You must take this course again.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `if`…`elif`…`else` Statement\n",
"\n",
"#### You can test several conditions in succession."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 67\n",
"\n",
"print('Your grade is: ', end='')\n",
"\n",
"if grade >= 90:\n",
" print('A')\n",
" print('Excellent!')\n",
"elif grade >= 80:\n",
" print('B')\n",
"elif grade >= 70:\n",
" print('C')\n",
"elif grade >= 60:\n",
" print('D')\n",
" print('Barely passing!')\n",
"else:\n",
" print('F')\n",
" print('You must take this course again.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Notice that we used the `end` **keyword argument** at the end of the first call to `print()`. The default for `print()` is to end each line with a line feed character. Adding `end=''` changes the line ending to an empty character, and therefore there won't be a line feed after printing the string `'Your grade is '`.\n",
"\n",
"#### **NOTE:** `end=''` is a keyword argument. It does **not** mean that `end` is a keyword (i.e., it is **not** reserved)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Be careful to indent your statements properly!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 99\n",
"\n",
"print('Your grade is: ', end='')\n",
"\n",
"if grade >= 90:\n",
" print('A')\n",
" print('Excellent!')\n",
"elif grade >= 80:\n",
" print('B')\n",
"elif grade >= 70:\n",
" print('C')\n",
"elif grade >= 60:\n",
" print('D')\n",
" print('Barely passing!')\n",
"else:\n",
" print('F')\n",
"print('You must take this course again')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Just like the `if` statement, the `else` is optional. But leaving it off may result in a logic error. What happens if the value of `grade` is below 60?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 50\n",
"\n",
"print('Your grade is: ', end='')\n",
"\n",
"if grade >= 90:\n",
" print('A')\n",
"elif grade >= 80:\n",
" print('B')\n",
"elif grade >= 70:\n",
" print('C')\n",
"elif grade >= 60:\n",
" print('D')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **EXERCISE:** Why is this wrong?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if grade >= 90:\n",
" print('A')\n",
"else:\n",
" print('Not A or B')\n",
"elif grade >= 80:\n",
" print('B')"
]
},
{
"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\n",
"# Richter Scale problem based on Python for Everyone by Cay Horstmann and Rance Necaise"
]
}
],
"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
}