{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Variables & Data Types — Workbook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*If you want to save your work from this notebook, you should be sure to make a copy of it on your own computer.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this workbook, we're going to explore Python variables and data types. \n", "\n", "Variables are one of the fundamental building blocks of Python. A variable is like a tiny container where you store values and data, such as filenames, words, numbers, collections of words and numbers, and more." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Additionally, there are four essential Python data \"types,\" which each have different powers and capabilities:" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "remove-cell" ] }, "source": [ "| Data Type | Explanation | Example |\n", "| ------------- |:-------------:| -----:|\n", "| String | Text | ```\"lemonade\"``` |\n", "| Integer | Whole Numbers | ```40``` |\n", "| Float | Decimal Numbers | ```40.2``` |\n", "| Boolean | True/False | ```False``` |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code vs Markdown vs Comments" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"hello Intro CA!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "print(\"hello Intro CA!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"hello Intro CA!\") # This is a comment and doesn't affect the code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variable Assignment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How can we assign the value 5 to the variable `new_variable`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_variable ... 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Check Data Type" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What type of Python data is `new_variable`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can display variables in two different ways with Jupyter notebooks. Notice the differences." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(new_variable)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_variable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python Expressions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a Python expression. What do you guess is the resulting value?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_variable * 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_variable / 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variable Re-Assignment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's re-assign `new_variable.`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_variable ... \"Chaos is how I learn\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What type of Python data is `new_variable` now?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(new_variable)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a Python expression. What do you guess is the resulting value?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_variable * 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a Python expression. What do you guess is the resulting value?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_variable / 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## True / False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create a variable that will report whether it's warm outside." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "warm_outside = temperature > 50" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "temperature = 27" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "warm_outside" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What type of Python data is `warm_outside`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(warm_outside)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## F-strings (Formatted String Literal)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A special kind of string that we're going to use in this class is called an *f-string*. An f-string, short for formatted string literal, allows you to insert a variable directly into a string." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An f-string must begin with an `f` outside the quotation marks. Then, inside the quotation marks, the inserted variable must be placed within curly brackets `{}`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`f\"{variable} in a string\"`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "subject = \"Making mistakes\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"FILL-IN-THE-BLANK is how I learn\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ✨Group Work Madlibs✨" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this exercise, you're going to ask your partner(s) a few biographical questions about themselves and then put their answers into the correct variables.\n", "\n", "I'll go first!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "name = 'Prof. Walsh' \n", "age = 1000 \n", "place = 'Chicago' \n", "favorite_food = 'tacos' \n", "dog_years_age = age * 7.5 \n", "student = False " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "hide-output" ] }, "outputs": [], "source": [ "print(f'✨This is...{name}!✨')\n", "\n", "print(f\"\"\"{name} likes {favorite_food} and once lived in {place}.\n", "{name} is {age} years old, which is {dog_years_age} in dog years.\n", "The statement '{name} is a student' is {student}.\"\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's do the same thing but with biographical info about your partner! Ask your partner a few questions and then fill in the variables below accordingly." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "name = #Your code here\n", "age = #Your code here\n", "home_town = #Your code here\n", "favorite_food = #Your code here\n", "dog_years_age = #Your code here\n", "student = # Your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now print the introduction below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f'✨This is...{name}!✨')\n", "\n", "print(f\"\"\"{name} likes {favorite_food} and once lived in {place}.\n", "{name} is {age} years old, which is {dog_years_age} in dog years.\n", "The statement \"{name} is a student\" is {student}.\"\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables in a Real Script" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's explore how variables are used in a real Python script.\n", "\n", "The code below will calculate the most frequent words in a text file. The text file information is stored in the variable `filepath_of_text`.\n", "\n", "So to count the most frequent words in a different text file, you need to change the value assigned to `filepath_of_text`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import Libraries and Modules\n", "\n", "import re\n", "from collections import Counter\n", "\n", "# Define Functions\n", "\n", "def split_into_words(any_chunk_of_text):\n", " lowercase_text = any_chunk_of_text.lower()\n", " split_words = re.split(\"\\W+\", lowercase_text)\n", " return split_words\n", "\n", "# Define Filepaths and Assign Variables\n", "\n", "filepath_of_text = ... # Pick a text here!!!!!!\n", "number_of_desired_words = 40\n", "\n", "stopwords = ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours',\n", "'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers',\n", " 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves',\n", " 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are',\n", " 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does',\n", " 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until',\n", " 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into',\n", " 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down',\n", " 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here',\n", " 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more',\n", " 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so',\n", " 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now', 've', 'll', 'amp']\n", "\n", "# Read in File\n", "\n", "full_text = open(filepath_of_text, encoding=\"utf-8\").read()\n", "\n", "# Manipulate and Analyze File\n", "\n", "all_the_words = split_into_words(full_text)\n", "meaningful_words = [word for word in all_the_words if word not in stopwords]\n", "meaningful_words_tally = Counter(meaningful_words)\n", "most_frequent_meaningful_words = meaningful_words_tally.most_common(number_of_desired_words)\n", "\n", "# Output Results\n", "\n", "most_frequent_meaningful_words" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8" } }, "nbformat": 4, "nbformat_minor": 4 }