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": [
"# 2.2 Variables and Assignment Statements"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### We can use IPython’s interactive mode, and we can also use a Jupyter notebook."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"45 + 72"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **Identifiers** are names that you use in a Python program. **Variables** are identifiers that represent values at runtime (when the code is executing).\n",
"\n",
"#### An **assignment statement** sets the value of a variable at runtime."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 7"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Adding Variable Values and Viewing the Result"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x + y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Calculations in Assignment Statements"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"total = x + y"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"total"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **EXERCISE:** Calculate the sum of `10.8`, `12.2` and `0.2`, store it in the variable `total`, then display `total`’s value."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"total = 10.8 + 12.2 + 0.2\n",
"total"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Python Style"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The **_Python Style Guide_** recommends using a space around the equal sign."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variable Names"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### A Python identifier may consist of letters, digits, and underscore ( _ ) characters, but it cannot begin with a digit. Identifiers are case sensitive: `count` and `Count` are two different identifiers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### We can also use the underscore to break up writing large numbers. For example, the integer value 53,182: `53_182`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"53182"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"53_182"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Types"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Every value has a **datatype**. A scalar value is a single value, such as a numeric value.\n",
"- Use the `type()` function to determine a value’s type."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"type(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"type(10.5)"
]
},
{
"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
}