{ "cells": [ { "cell_type": "markdown", "id": "08148b7c-867a-4e97-b12a-93c1f6924586", "metadata": {}, "source": [ "# Demo: Python Basic Data Types\n", "Let's now look specifically at how Python stores and lets you work with basic data types:\n", "* Booleans (True / False)\n", "* Numbers\n", "* Strings (Text)\n", "\n", "But first we need to look a little more about how to use functions, since we'll be using them with the data types." ] }, { "cell_type": "markdown", "id": "123456789-930485093240532940945-0324095320945904325", "metadata": { "tags": [] }, "source": [" _Choose Social Media Platform: Reddit | Discord | __Bluesky__ | No Coding_ "] }, { "cell_type": "markdown", "id": "2afceb90-d9a5-4a21-a20b-11a52c3c9259", "metadata": {}, "source": [ "## Calling Functions\n", "A function is a named section of pre-written code, which we can \"call,\" making that section of code run.\n", "\n", "We have already been calling three different functions in particular: `display`, `login`, and `send_post`:\n", "- `display(\"Ethics might be relevant!\")`\n", "- `client.send_post(\"This post was made by a computer program!\")`\n", "\n", "The structure of a standard function call has the following pieces:\n", "`function_name(input_parameters)`\n", "\n", "So we first write the name of the function (e.g., `display` or `channel.send`), then we put matching parentheses after that, and inside the parentheses we put the input arguments, which are data or options for how the function should run.\n", "\n", "### input arguments\n", "- These inputs are called \"parameters\" or \"arguments\"\n", "- Inputs go in parentheses after the function name\n", "- If there are multiple inputs, they are separated by commas\n", "- You can also specify which input you are giving within the parenthesis by putting parameter_name=value\n", "\n", "\n", "Additionally, some functions produce a result, which can be saved in a variable, or used in a calculation or some other fashion. When we save the result of running a function into a variable, it looks like this:\n", "\n", "`save_result_variable = function_name(input_parameters)`\n", "\n", "### function results\n", "* Functions can have outputs that are called \"returns\" or \"results\"\n", "* When the code interpreter sees the function call, it runs the code in the function with the inputs, and then puts the output in the place where that function call was\n", "* The results of the function can be stored in a variable, used in a formula, or used as an argument for another function\n", "\n", "That was all a bit theoretical, so set's look at some data types now, and some specific functions that we can use with them." ] }, { "cell_type": "markdown", "id": "d6b4fe01-60eb-40fa-803d-eb16cd3ea8bb", "metadata": {}, "source": [ "## Boolean (True / False)\n", "\n", "In Python there are two options for boolean: `True` and `False`\n", "\n", "Note that the first letter is capitalized and the rest is lower case. Python only lets you write these True and False values in that way.\n", "\n", "Let's save a boolean value in a variable, and then display it:" ] }, { "cell_type": "code", "execution_count": null, "id": "bc705294-4e14-4354-ba1b-28a0488745dc", "metadata": {}, "outputs": [], "source": [ "does_user_have_blue_checkmark = True\n", "\n", "display(does_user_have_blue_checkmark)" ] }, { "cell_type": "markdown", "id": "653cdcbe-ecfb-484a-a300-bfb686914654", "metadata": {}, "source": [ "Display lets us see what was in the variable, but we can also use a new function `type` to see what type of value is in the variable (it should be 'bool' which is short for boolean):" ] }, { "cell_type": "code", "execution_count": null, "id": "aa9da4e2-7f55-4c27-bcbc-45078c8640c5", "metadata": {}, "outputs": [], "source": [ "type(does_user_have_blue_checkmark)" ] }, { "cell_type": "markdown", "id": "d0114256-adcd-4ca6-8028-42f7db48902c", "metadata": {}, "source": [ "## Numbers" ] }, { "cell_type": "markdown", "id": "d1e34bb0-abd8-40b3-989c-a7c4a33f101b", "metadata": {}, "source": [ "### integers and floats\n", "Python allows you to use two main types of numbers:\n", "- Integers (whole numbers), called \"int\"\n", "- A \"floating point\" number with a decimal point, called a \"float\"" ] }, { "cell_type": "code", "execution_count": null, "id": "3f67f0b2-09cd-4132-a25d-f8a994ac660c", "metadata": {}, "outputs": [], "source": [ "type(5)" ] }, { "cell_type": "code", "execution_count": null, "id": "ed0f1cea-0c4e-47da-986e-5b8f0e6837c1", "metadata": {}, "outputs": [], "source": [ "type(5.5)" ] }, { "cell_type": "markdown", "id": "3dd81379-1407-441e-a465-4376b57083b4", "metadata": {}, "source": [ "We can now do normal math operations on the numbers, like addition `+`, subtraction `-`, multiplication `*`, and division `/`.\n", "\n", "\n", "Even though there are two types of numbers, most of the time they work together pretty seemlessly, switching to whichever type makes the most sense. For example, let's look at what happens when we add and int and a float:" ] }, { "cell_type": "code", "execution_count": null, "id": "2ec48f7c-a88e-4bc5-8b2a-16c7dc286e70", "metadata": {}, "outputs": [], "source": [ "example_num = 5 + 5.5" ] }, { "cell_type": "code", "execution_count": null, "id": "8720627a-d2f7-4d57-b6ac-3ad6fc45fc17", "metadata": {}, "outputs": [], "source": [ "display(example_num)" ] }, { "cell_type": "code", "execution_count": null, "id": "d26a33ef-9347-4427-b108-b60f02cdc2d2", "metadata": {}, "outputs": [], "source": [ "type(example_num)" ] }, { "cell_type": "markdown", "id": "572f61fc-80c1-41a9-97f0-bf8375a552dd", "metadata": {}, "source": [ "### functions for numbers\n", "Python provides functions that we can use with numbers, like:\n", "- find the maximum number in a set of numbers with `max()`\n", "- find the minimum number in a set of numbers with `min()`\n", "- round a floating point number into an integer with `round()`\n", " \n", "Each of these functions produces a result at the end, which we can save into a variable" ] }, { "cell_type": "code", "execution_count": null, "id": "cdd1f138-3b02-48a9-926f-36181574721b", "metadata": {}, "outputs": [], "source": [ "# Demo of using the max() function\n", "my_score = 74\n", "your_score = 92\n", "someone_elses_score = 83\n", "\n", "highest_score = max(my_score, your_score, someone_elses_score)\n", "\n", "display(highest_score)" ] }, { "cell_type": "code", "execution_count": null, "id": "2d3d50ba-6517-4222-bde5-f14b3ed02072", "metadata": {}, "outputs": [], "source": [ "# Demo of using the min() function\n", "bread_1_price = 2.30\n", "bread_2_price = 2.15\n", "bread_3_price = 1.79\n", "\n", "cheapest_bread_price = min(bread_1_price, bread_2_price, bread_3_price)\n", "\n", "display(cheapest_bread_price)" ] }, { "cell_type": "code", "execution_count": null, "id": "a3ed122a-45d2-4027-a108-ac68c05802e8", "metadata": {}, "outputs": [], "source": [ "# Demo using the round function\n", "float_number = 14.6224\n", "\n", "rounded_number = round(float_number)\n", "\n", "display(rounded_number)" ] }, { "cell_type": "markdown", "id": "7b603c90-551b-4558-a5ae-3967cff645b1", "metadata": {}, "source": [ "### number comparisons\n", "Python also lets us [compare numbers in various ways](https://www.w3schools.com/python/gloss_python_comparison_operators.asp), producing a boolean True or False value depending on if the comparison was true or false.\n", "\n", "For example, we can see if one number is bigger than another by using the greater than comparison: `>`" ] }, { "cell_type": "code", "execution_count": null, "id": "adb3f646-0bba-49c8-b748-cd6292663d87", "metadata": {}, "outputs": [], "source": [ "money_in_wallet = 10\n", "cost_of_item = 7\n", "\n", "has_enough_money = money_in_wallet > cost_of_item\n", "\n", "display(has_enough_money)" ] }, { "cell_type": "markdown", "id": "b2ba8cbd-6492-4a4a-88c8-77e85b1d2d96", "metadata": {}, "source": [ "We can check if two numbers are equal by using two equals signs: `==`\n", "\n", "Note: this is an unfortunately confusing system that [most programming languages are now stuck with](https://www.quora.com/Why-do-most-programming-languages-have-the-equal-sign-as-an-assignment-operator-This-option-seems-to-be-nonintuitive-Isn%E2%80%99t-it-better-to-use-the-equal-sign-in-conditional-statements) since it became the standard: \n", "- One equals sign (`=`) means save the value into a variable\n", "- Two equals signs (`==`) mean check if two numbers are the same" ] }, { "cell_type": "code", "execution_count": null, "id": "0c125016-8f1a-4e09-b7a9-f499896b8862", "metadata": {}, "outputs": [], "source": [ "my_follower_count = 23\n", "your_follower_count = 23\n", "\n", "same_number_of_followers = my_follower_count == your_follower_count\n", "\n", "display(same_number_of_followers)" ] }, { "cell_type": "markdown", "id": "9132292c-e01b-4085-b28e-bd721a30546a", "metadata": {}, "source": [ "## Strings (text)\n", "In order to make a string (piece of text) in Python, you can write your text with either double quotes `\"` or single quotes `'` at the beggining and end." ] }, { "cell_type": "code", "execution_count": null, "id": "ae733dff-b6bd-483d-87e5-17b05e980569", "metadata": {}, "outputs": [], "source": [ "tweet_text_1 = \"what nice weather today\"\n", "display(tweet_text_1)" ] }, { "cell_type": "code", "execution_count": null, "id": "f8986ef3-910f-4ce0-ad02-55b644c7cd6c", "metadata": {}, "outputs": [], "source": [ "tweet_text_2 = 'what horrible weather today'\n", "display(tweet_text_2)" ] }, { "cell_type": "markdown", "id": "639c6662-9f03-4698-9295-c55cfffb4073", "metadata": {}, "source": [ "Note: If you are copying from a word processor like word, you might get angled quotes like `’` or `”`, which Python doesn't like. So, if you try to run code like this:\n", "- `tweet_text_3 = “What normal weather today”`\n", "\n", "you will get an error message like this:\n", "- ![Red error message box. The top says: \"Input in [14] tweet_text_3 = “What normal weather today”\". The bottom says: \"SyntaxError: invalid character '“' (U+201C)](bad_quote_error.png)" ] }, { "cell_type": "markdown", "id": "50d4a476-067a-4d70-886e-86802650f88d", "metadata": {}, "source": [ "### adding strings together\n", "If we want to add strings together to make a larger string, we can do that with the `+` operation." ] }, { "cell_type": "code", "execution_count": null, "id": "1dc584c9-6437-4314-adfd-dc5cdc427770", "metadata": {}, "outputs": [], "source": [ "first_name = \"Kyle\"\n", "last_name = \"Thayer\"\n", "\n", "full_name = first_name + \" \" + last_name \n", "# Note: I had to add a space between the first and last name, or it would come out as KyleThayer\n", "\n", "display(full_name)" ] }, { "cell_type": "markdown", "id": "6daef0f5-2f65-4c6b-8628-172e28f2eea2", "metadata": {}, "source": [ "If we want to add something like a number, like my age, into the string though, it won't let us do it directly.\n", "\n", "If we try running:\n", "- `example_text = \"how old are you? I am \" + 3`\n", "\n", "Then we will get an error that 3 was an int and not a string, so it can't be added:\n", "- ![Error message: Says it is a type error and points to the line: 'example_text = \"how old are you? I am \" + 3'. At the bottom it says: \"TypeError: can only concatenate str (not \"int\") to str\"](add_int_to_str_error.png)\n", "\n", "To fix this, we have to turn the number into a string before we add it. We do this by using the `str()` function:" ] }, { "cell_type": "code", "execution_count": null, "id": "a995f17a-a09e-4f16-a840-b5053e3efc0f", "metadata": {}, "outputs": [], "source": [ "example_text = \"how old are you? I am \" + str(3)\n", "display(example_text)" ] }, { "cell_type": "markdown", "id": "091fc24e-f05c-4b62-9d18-a769f1d871b7", "metadata": {}, "source": [ "Adding strings and numbers together is particularly useful for displaying information in a more readable fashion:" ] }, { "cell_type": "code", "execution_count": null, "id": "e4c71501-8166-43d4-a692-0e24b3e260bd", "metadata": {}, "outputs": [], "source": [ "num_likes = 78\n", "num_retweets = 32\n", "num_quote_tweets = 17\n", "\n", "display(\"The number of likes was: \" + str(num_likes))\n", "display(\"The number of retweets was: \" + str(num_retweets))\n", "display(\"The number of quote tweets was: \" + str(num_quote_tweets))" ] }, { "cell_type": "markdown", "id": "0ea8f8b8-b846-42a8-91f9-a88f9669e50c", "metadata": {}, "source": [ "### actions with strings\n", "There are various actions Python let's us do with strings. For example, let's look for a smaller string (\"cat\") and see if it is in the bigger one (producing a True or False boolean value) using `in`" ] }, { "cell_type": "code", "execution_count": null, "id": "60400411-0f80-4832-8dfb-09d7ccb31876", "metadata": {}, "outputs": [], "source": [ "animal_tweet_1 = \"I like cats!\"\n", "\n", "tweet_1_has_cat = \"cat\" in animal_tweet_1\n", "\n", "display(tweet_1_has_cat)" ] }, { "cell_type": "code", "execution_count": null, "id": "8704ea8d-e457-4d53-8e8a-2c4e7b262183", "metadata": {}, "outputs": [], "source": [ "animal_tweet_2 = \"I like dogs!\"\n", "\n", "tweet_2_has_cat = \"cat\" in animal_tweet_2\n", "\n", "display(tweet_2_has_cat)" ] }, { "cell_type": "code", "execution_count": null, "id": "9fb09258-527b-4fad-8fe9-c025ca9da75e", "metadata": {}, "outputs": [], "source": [ "animal_tweet_3 = \"I like caterpillars!\"\n", "\n", "tweet_3_has_cat = \"cat\" in animal_tweet_3\n", "\n", "display(tweet_3_has_cat)" ] }, { "cell_type": "markdown", "id": "7033efb6-a88c-491f-b474-8a89e7608ab8", "metadata": {}, "source": [ "We can also do actions like make a string all uppercase or all lowercase using the `upper()` and `lower()` functions.\n", "\n", "Unlike previous uses of functions, for these we write the name of the variable we are using, then a `.` and then name of the function: `save_result_variable = variable_name.function_name()`" ] }, { "cell_type": "code", "execution_count": null, "id": "6f08e74f-e0a7-4c25-9231-d8c868ee56f5", "metadata": {}, "outputs": [], "source": [ "normal_message = \"I hope you are doing OK\"" ] }, { "cell_type": "code", "execution_count": null, "id": "b8da5c7d-a4f5-4ec9-9708-ee548cf2c880", "metadata": {}, "outputs": [], "source": [ "loud_message = normal_message.upper()\n", "display(loud_message)" ] }, { "cell_type": "code", "execution_count": null, "id": "3a5df617-76bf-470a-84c8-cc7b7955823a", "metadata": {}, "outputs": [], "source": [ "quiet_message = normal_message.lower()\n", "display(quiet_message)" ] }, { "cell_type": "markdown", "id": "00bfedd5-0389-4773-a7f5-524b4100dd20", "metadata": {}, "source": [ "We can also see how many characters long a string is by using the function `len()` like this:" ] }, { "cell_type": "code", "execution_count": null, "id": "eab1528e-6897-4176-8468-7d4aee4061d8", "metadata": {}, "outputs": [], "source": [ "message_length = len(normal_message)\n", "display(message_length)" ] }, { "cell_type": "markdown", "id": "8d21522c-dc23-41b9-ab45-dd32a31709b7", "metadata": {}, "source": [ "_You can read more about Python data types at [w3schools explanation of Python data types](https://www.w3schools.com/python/python_datatypes.asp)_" ] } ], "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.10.4" }, "vscode": { "interpreter": { "hash": "369f2c481f4da34e4445cda3fffd2e751bd1c4d706f27375911949ba6bb62e1c" } } }, "nbformat": 4, "nbformat_minor": 5 }