{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"All the IPython Notebooks in **Python Introduction** lecture series by **[Dr. Milaan Parmar](https://www.linkedin.com/in/milaanparmar/)** are available @ **[GitHub](https://github.com/milaan9/01_Python_Introduction)**\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python Keywords and Identifiers\n",
"\n",
"In this class, you will learn about keywords (reserved words in Python) and identifiers (names given to variables, functions, etc.)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Python Keywords\n",
"\n",
"Keywords are the reserved words in Python.\n",
"\n",
"We cannot use a keyword as a **[variable](https://github.com/milaan9/01_Python_Introduction/blob/main/009_Python_Data_Types.ipynb)** name, **[function](https://github.com/milaan9/04_Python_Functions/blob/main/001_Python_Functions.ipynb)** name or any other identifier. They are used to define the syntax and structure of the Python language.\n",
"\n",
"In Python, keywords are **case sensitive**.\n",
"\n",
"There are **36** keywords in Python 3.9. This number can vary slightly over the course of time.\n",
"\n",
"All the keywords except **`True`**, **`False`** and **`None`** are in lowercase and they must be written as they are. The **[list of all the keywords](https://github.com/milaan9/01_Python_Introduction/blob/main/Python_Keywords_List.ipynb)** is given below.\n",
"\n",
"**Keywords in Python**\n",
"\n",
"| | | | |\n",
"|:----|:----|:----|:----|\n",
"| **`False`** | **`break`** | **`for`** | **`not`** |\n",
"| **`None`** | **`class`** | **`from`** | **`or`** |\n",
"| **`True`** | **`continue`** | **`global`** | **`pass`** |\n",
"| **`__peg_parser__`** |**`def`** | **`if`** | **`raise`** |\n",
"| **`and`** | **`del`** | **`import`** | **`return`** |\n",
"| **`as`** | **`elif`** | **`in`** | **`try`** |\n",
"| **`assert`** | **`else`** | **`is`** | **`while`** |\n",
"| **`async`** | **`except`** | **`lambda`** | **`with`** |\n",
"| **`await`** | **`finally`** | **`nonlocal`** | **`yield`** |\n",
"\n",
"You can see this list any time by typing help **`keywords`** to the Python interpreter. \n",
"\n",
"Trying to create a variable with the same name as any reserved word results in an **error**:\n",
"\n",
"```python\n",
">>>for = 6\n",
"\n",
"File \"\", line 1\n",
"for = 6 # It will give error becasue \"for\" is keyword and we cannot use as a variable name.\n",
" ^\n",
"SyntaxError: invalid syntax\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2021-06-22T14:51:40.668985Z",
"start_time": "2021-06-22T14:51:40.658242Z"
}
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (, line 1)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m for = 6 # It will give error becasue \"for\" is keyword and we cannot use as a variable name.\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"for = 6 # It will give error becasue \"for\" is keyword and we cannot use as a variable name."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2021-06-22T14:51:41.967978Z",
"start_time": "2021-06-22T14:51:41.945516Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"For = 6 # \"for\" is keyword but \"For\" is not keyword so we can use it as variable name\n",
"For"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. Python Identifiers\n",
"\n",
"An **identifier** is a name given to entities like **class, functions, variables, etc**. It helps to differentiate one entity from another."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Rules for writing identifiers\n",
"\n",
"1. **Identifiers** can be a combination of letters in lowercase **(a to z)** or uppercase **(A to Z)** or digits **(0 to 9)** or an underscore **`_`**. Names like **`myClass`**, **`var_1`** and **`print_this_to_screen`**, all are valid example. \n",
"\n",
"2. An identifier cannot start with a digit. **`1variable`** is invalid, but **`variable1`** is perfectly fine. \n",
"\n",
"3. Keywords cannot be used as identifiers\n",
"\n",
"```python\n",
">>>global = 3\n",
"\n",
"File \"\", line 1\n",
" global = 3 # because \"global\" is a keyword\n",
" ^\n",
"SyntaxError: invalid syntax\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2021-06-22T14:51:45.004986Z",
"start_time": "2021-06-22T14:51:44.995218Z"
}
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (, line 1)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m global = 3 # because \"global\" is a keyword\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"global = 3 # because \"global\" is a keyword"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. We cannot use special symbols like **!**, **@**, **#**, $, % , etc. in our identifier.\n",
"\n",
"```python\n",
">>>m@ = 3\n",
"\n",
"File \"\", line 1\n",
" m@ = 3\n",
" ^\n",
"SyntaxError: invalid syntax\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2021-06-22T14:51:48.063967Z",
"start_time": "2021-06-22T14:51:48.058109Z"
}
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (, line 1)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m m@ = 3\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"m@ = 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Things to Remember\n",
"\n",
"Python is a case-sensitive language. This means, **`Variable`** and **`variable`** are not the same.\n",
"\n",
"Always give the identifiers a name that makes sense. While **`c = 10`** is a valid name, writing **`count = 10`** would make more sense, and it would be easier to figure out what it represents when you look at your code after a long gap.\n",
"\n",
"Multiple words can be separated using an underscore, like **`this_is_a_long_variable`**."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2021-06-22T14:51:49.685264Z",
"start_time": "2021-06-22T14:51:49.666710Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"this_is_a_long_variable = 6+3\n",
"this_is_a_long_variable"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2021-06-22T14:51:50.580889Z",
"start_time": "2021-06-22T14:51:50.571121Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add_6_and_3 = 6+3\n",
"add_6_and_3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"gist": {
"data": {
"description": "01_Learn_Python4Data/01_Python_Introduction/005_Python_Keywords_and_Identifiers.ipynb",
"public": true
},
"id": ""
},
"hide_input": false,
"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.10.9"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}