"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"IFrame('https://trinket.io/embed/python3/3fe4c8f3f4', 700, 500)"
]
},
{
"cell_type": "markdown",
"id": "7da93389-8557-4631-be5f-e2f2bbaab347",
"metadata": {},
"source": [
"Now that you understand how to create a number in memory, let's try to add two numbers together. In the example below, we will add 1 and 1 together."
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "9502c766-fdac-497f-bbd8-bd6d3b6452ec",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"print(1+1)"
]
},
{
"cell_type": "markdown",
"id": "b915d6c3-0bf5-4e2d-8a1a-5c7093c57e57",
"metadata": {},
"source": [
"In the Trinket application above, try to perform the other mathematical operations."
]
},
{
"cell_type": "markdown",
"id": "improving-egyptian",
"metadata": {},
"source": [
"## Booleans"
]
},
{
"cell_type": "markdown",
"id": "married-topic",
"metadata": {},
"source": [
"The term boolean comes from Boolean algebra, which is a type of mathematics that works in binary logic. Binary is the basis for all computers, save for the more nascent quantum computers. Binary is 0 or 1; off or on; true or false. A boolean object in programming languages is either `True` or `False`. True is 1, while False is 0. In Python we can express these concepts with capitalized T or F in True or False. Let's make one such object now."
]
},
{
"cell_type": "markdown",
"id": "connected-registrar",
"metadata": {},
"source": [
"### Examples of Booleans"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "funny-belize",
"metadata": {},
"outputs": [],
"source": [
"bool1 = True"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "outdoor-canada",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"print(bool1)"
]
},
{
"cell_type": "markdown",
"id": "reliable-catalog",
"metadata": {},
"source": [
"## Conclusion"
]
},
{
"cell_type": "markdown",
"id": "designing-relation",
"metadata": {},
"source": [
"This chapter has introduced you two some of the essential types of data: strings, integers, floats, and booleans. It has also introduced you to some of the key methods and operations that you can perform on strings and numbers. Before moving onto the next chapter, I recommend spending some time and testing out these methods on your own data. Try and manipulate an input text to locate and retrieve specific information."
]
},
{
"cell_type": "markdown",
"id": "240abc11-41c3-4d3e-bc87-45b3c93025c8",
"metadata": {},
"source": [
"## Challenge Questions"
]
},
{
"cell_type": "markdown",
"id": "cb042f66-e5b4-466e-a599-ad57e1981b3e",
"metadata": {},
"source": [
"What would be the output of the command below?"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "f18e5a4a-4813-4630-9ffb-e4eda770d2a5",
"metadata": {
"tags": [
"hide-output"
]
},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type('What type of data is this?')"
]
},
{
"cell_type": "markdown",
"id": "957e6150-2775-4d95-99c2-2edd52495007",
"metadata": {},
"source": [
"What about this?"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "10178f9e-db03-4871-bf0e-8cf0c81b39da",
"metadata": {
"tags": [
"hide-output"
]
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "EOL while scanning string literal (2127757825.py, line 1)",
"output_type": "error",
"traceback": [
"\u001b[1;36m Input \u001b[1;32mIn [19]\u001b[1;36m\u001b[0m\n\u001b[1;33m print(\"Hello, world!')\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m EOL while scanning string literal\n"
]
}
],
"source": [
"print(\"Hello, world!')"
]
},
{
"cell_type": "markdown",
"id": "34db6699-71c9-4fca-8cb6-7f4d2cc873cd",
"metadata": {},
"source": [
"And what about this?"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "fc82f416-b6e0-4968-a668-671ed1d30d24",
"metadata": {
"tags": [
"hide-output"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"john\n"
]
}
],
"source": [
"person = \"John\"\n",
"person = person.lower()\n",
"person2 = person\n",
"person = person.upper()\n",
"print(person2)"
]
},
{
"cell_type": "markdown",
"id": "40b6fbd5-0667-4577-8eb0-d363a1bdd8e1",
"metadata": {},
"source": [
"Expand below to see why the output looks the way it does."
]
},
{
"cell_type": "markdown",
"id": "b0ca6910-3731-4cf7-9b3b-5bafba450749",
"metadata": {
"tags": [
"hide-input",
"hide-cell"
]
},
"source": [
"If you did not get this right, let's talk about why. Remember, programming is a sequential set of operations.\n",
"\n",
"| Number | Line | Meaning |\n",
"| -- | ----------- | ----------- |\n",
"| 1| person = \"John\" | establishes that the variable is a string and that it is \"John\" |\n",
"| 2| person = person.lower() | this will lowercase the string so at this stage, it would be \"john\" |\n",
"| 3| person2 = person | this creates person2 as a variable and points it to person |\n",
"|4| person = person.upper() | this changes the person variable and uppercases it. The variable person2, however, remains the same|"
]
},
{
"cell_type": "markdown",
"id": "93ce0ac6-253a-489d-ba14-d2b0963e1316",
"metadata": {},
"source": [
"## Quiz"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "528555be-0a24-40b7-b7c9-f39cb808c2b0",
"metadata": {
"tags": [
"hide",
"remove_cell"
]
},
"outputs": [],
"source": [
"from jupyterquiz import display_quiz\n",
"import md2json\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "6e9dae9f-dd6b-4ca3-a6dd-74f3b125174a",
"metadata": {
"tags": [
"remove_cell"
]
},
"outputs": [],
"source": [
"quiz = '''\n",
"# What type of data is associated with text?\n",
"mc\n",
"* Strings\n",
"-c t\n",
"-f Correct! Strings are how we create text in Python.\n",
"\n",
"* Integers\n",
"-c f\n",
"-f While a type of data, Integers are not text\n",
"\n",
"* Floats\n",
"-c f\n",
"-f While a type of data, Floats are not text\n",
"\n",
"* Boolean\n",
"-c f\n",
"-f Booleans are not text.\n",
"\n",
"\n",
"# How do we create strings?\n",
"mc\n",
"* With \"\"\n",
"-c t\n",
"-f Correct!\n",
"\n",
"* With ''\n",
"-c t\n",
"-f Correct!\n",
"\n",
"* With :\n",
"-c f\n",
"-f\n",
"\n",
"* With <>\n",
"-c f\n",
"-f\n",
"\n",
"# What are the two types of numbers?\n",
"mc\n",
"* Integers\n",
"-c t\n",
"-f\n",
"\n",
"* Floats\n",
"-c t\n",
"-f\n",
"\n",
"* Strings\n",
"-c f\n",
"-f\n",
"\n",
"* Booleans\n",
"-c f\n",
"-f\n",
"\n",
"# What are the types of Booleans?\n",
"mc\n",
"* True\n",
"-c t\n",
"-f\n",
"\n",
"* False\n",
"-c t\n",
"-f\n",
"\n",
"* true\n",
"-c f\n",
"-f Close. Remember, case matters in Python.\n",
"\n",
"* false\n",
"-c f\n",
"-f Close. Remember, case matters in Python.\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "53d344eb-b1f4-4dbd-8a2c-b9aacef3897d",
"metadata": {
"tags": [
"remove_input"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"myquiz = md2json.convert(quiz)\n",
"myquiz = json.loads(myquiz)\n",
"display_quiz(myquiz)"
]
}
],
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}