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": [
"# Python keywords\n",
"\n",
"#### Python's keywords are part of the syntax of the language's statements. They are all reserved, so you cannot use them as identifiers.\n",
"- All the keywords are ower-cased except for `None`, `True`, and `False`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import keyword\n",
"\n",
"print(f'{len(keyword.kwlist)} keywords:')\n",
"print()\n",
"print(keyword.kwlist)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.5 `if` Statement"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### At run time, the `if` statement executes a **suite** (group) of one or more statements if a condition is true. It skips the suite if the condition is false. Note the colon!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 85"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if grade >= 60:\n",
" print('Passed')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 50"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if grade >= 60:\n",
" print('Passed')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 90\n",
"\n",
"if grade >= 60:\n",
" print('Passed')\n",
" print('Good job!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grade = 50\n",
"\n",
"if grade >= 60:\n",
" print('Passed')\n",
" print('Good job!')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Suite Indentation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### We must indent the suite consistently after the `if` statement. Python programmers prefer to indent by 4 spaces. In a Juypyter notebook, pressing the tab key indents that amount."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if grade >= 60:\n",
"print('Passed') # statement is not indented properly"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if grade >= 60:\n",
" print('Passed')\n",
" print('Good job!') ## inconsistent indentation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `if` Statement Flowchart\n",
"#### Every Expression Can Be Treated as `True` or `False`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if 1:\n",
" print('Nonzero values are true, so this will print')\n",
" \n",
"if 'a string':\n",
" print('A nonempty string value is also true.')\n",
" \n",
"if True:\n",
" print('The value True is true, of course.')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if 0:\n",
" print('Zero is false, so this will not print')\n",
" \n",
"if '':\n",
" print('An empty string is false, so this also will not print.')\n",
" \n",
"if False:\n",
" print('The value False is false, so do not print this either.')"
]
},
{
"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
}