{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "Created by [Nathan Kelber](http://nkelber.com) and Ted Lawless for [JSTOR Labs](https://labs.jstor.org/) under [Creative Commons CC BY License](https://creativecommons.org/licenses/by/4.0/)
\n", "For questions/comments/improvements, email nathan.kelber@ithaka.org.
\n", "___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Python Basics I**\n", "\n", "**Description:** This lesson describes [operators](https://docs.tdm-pilot.org/key-terms/#operator), [expressions](https://docs.tdm-pilot.org/key-terms/#expression), data types, [variables](https://docs.tdm-pilot.org/key-terms/#variable), and basic [functions](https://docs.tdm-pilot.org/key-terms/#function). Complete this lesson if you are familiar with [Jupyter notebooks](https://docs.tdm-pilot.org/key-terms/#jupyter-notebook) or have completed *Getting Started with Jupyter Notebooks*, but do not have any experience with [Python](https://docs.tdm-pilot.org/key-terms/#python) programming. This is part 1 of 3 in the series *Python Basics* that will prepare you to do text analysis using the [Python](https://docs.tdm-pilot.org/key-terms/#python) programming language.\n", "\n", "**Use Case:** For Learners (Detailed explanation, not ideal for researchers)\n", "\n", "**Difficulty:** Beginner\n", "\n", "**Completion Time:** 75 minutes\n", "\n", "**Knowledge Required:** \n", "* [Getting Started with Jupyter Notebooks](./getting-started-with-jupyter.ipynb)\n", "\n", "**Knowledge Recommended:** None\n", "\n", "**Data Format:** None\n", "\n", "**Libraries Used:** None\n", "\n", "**Research Pipeline:** None\n", "___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[![Getting Started with Jupyter Notebooks](https://ithaka-labs.s3.amazonaws.com/static-files/images/tdm/tdmdocs/video/python-basics.png)](https://www.youtube.com/watch?v=90wFLSjlFL8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction\n", "\n", "[Python](https://docs.tdm-pilot.org/key-terms/#python) is the fastest-growing language in computer programming. Learning [Python](https://docs.tdm-pilot.org/key-terms/#python) is a great choice because [Python](https://docs.tdm-pilot.org/key-terms/#python) is:\n", "\n", "* Widely-adopted in the digital humanities and data science\n", "* Regarded as an easy-to-learn language\n", "* Flexible, having wide support for working with numerical and textual data \n", "* A skill desired by employers in academic, non-profit, and private sectors\n", "\n", "The second most-popular language for digital humanities and data science work is [R](https://docs.tdm-pilot.org/key-terms/#r). We plan to create additional support for learning [R](https://docs.tdm-pilot.org/key-terms/#r) soon. If you are interested in helping develop open educational resources for [R](https://docs.tdm-pilot.org/key-terms/#r), please reach out to Nathan Kelber (nathan.kelber@ithaka.org).\n", "\n", "The skills you'll learn in *Python Basics* I-III are general-purpose [Python](https://docs.tdm-pilot.org/key-terms/#python) skills, applicable for any of the text analysis notebooks that you may explore later. They are also widely applicable to many other kinds of tasks in [Python](https://docs.tdm-pilot.org/key-terms/#python) beyond text analysis.\n", "\n", "**Making Mistakes is Important**\n", "\n", "Every programmer at every skill level gets errors in their code. Making mistakes is how we all learn to program. Programming is a little like solving a puzzle where the goal is to get the desired outcome through a series of attempts. You won't solve the puzzle if you're afraid to test if the pieces match. An error message will not break your computer. Remember, you can always reload a notebook if it stops working properly or you misplace an important piece of code. Under the edit menu, there is an option to undo changes. (Alternatively, you can use **command z** on Mac and **control z** on Windows.) To learn any skill, you need to be willing to play and experiment. Programming is no different.\n", "\n", "# Expressions and Operators\n", "\n", "The simplest form of Python programming is an [expression](https://docs.tdm-pilot.org/key-terms/#expression) using an [operator](https://docs.tdm-pilot.org/key-terms/#operator). An [expression](https://docs.tdm-pilot.org/key-terms/#expression) is a simple mathematical statement like:\n", "\n", "> 1 + 1\n", "\n", "The [operator](https://docs.tdm-pilot.org/key-terms/#operator) in this case is `+`, sometimes called \"plus\" or \"addition\". Try this operation in the code box below. Remember to click the \"Run\" button or press Ctrl + Enter (Windows) or shift + return (OS X) on your keyboard to run the code." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Type the expression in this code block. Then run it.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python can handle a large variety of [expressions](https://docs.tdm-pilot.org/key-terms/#expression). Let's try subtraction in the next [code cell](https://docs.tdm-pilot.org/key-terms/#code-cell)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Type an expression that uses subtraction in this cell. Then run it.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also do multiplication (\\*) and division (/). While you may have used an \"×\" to represent multiplication in grade school, [Python](https://docs.tdm-pilot.org/key-terms/#python) uses an asterisk (\\*). In [Python](https://docs.tdm-pilot.org/key-terms/#python),\n", "\n", "> 2 × 2\n", "\n", "is written as\n", "\n", "> 2 * 2\n", "\n", "Try a multiplication and a division in the next [code cell](https://docs.tdm-pilot.org/key-terms/#code-cell)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Try a multiplication in this cell. Then try a division.\n", "# What happens if you combine them? What if you combine them with addition and/or subtraction?\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you run, or **evaluate**, an [expression](https://docs.tdm-pilot.org/key-terms/#expression) in [Python](https://docs.tdm-pilot.org/key-terms/#python), the order of operations is followed. (In grade school, you may remember learning the shorthand \"PEMDAS\".) This means that [expressions](https://docs.tdm-pilot.org/key-terms/#expression) are evaluated in this order:\n", "\n", "1. Parentheses\n", "2. Exponents\n", "3. Multiplication and Division (from left to right)\n", "4. Addition and Subtraction (from left to right)\n", "\n", "[Python](https://docs.tdm-pilot.org/key-terms/#python) can evaluate parentheses and exponents, as well as a number of additional [operators](https://docs.tdm-pilot.org/key-terms/#operator) you may not have learned in grade school. Here are the main [operators](https://docs.tdm-pilot.org/key-terms/#operator) that you might use presented in the order they are evaluated:\n", "\n", "|Operator| Operation| Example | Evaluation |\n", "|---|----|---|---|\n", "|\\*\\*| Exponent/Power| 3 ** 3 | 27 |\n", "|%| Modulus/Remainder| 34 % 6 | 4 |\n", "|/| Division | 30 / 6 | 5|\n", "|\\*| Multiplication | 7 * 8 | 56 |\n", "|-| Subtraction | 18 - 4| 14|\n", "|+| Addition | 4 + 3 | 7 |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Try operations in this code cell.\n", "# What happens when you add in parentheses?\n" ] }, { "cell_type": "markdown", "metadata": { "toc-hr-collapsed": true, "toc-nb-collapsed": true }, "source": [ "# Data Types (Integers, Floats, and Strings)\n", "\n", "All [expressions](https://docs.tdm-pilot.org/key-terms/#expression) evaluate to a single value. In the above examples, our [expressions](https://docs.tdm-pilot.org/key-terms/#expression) evaluated to single numerical value. Numerical values come in two basic forms:\n", "\n", "* [integer](https://docs.tdm-pilot.org/key-terms/#integer)\n", "* [float](https://docs.tdm-pilot.org/key-terms/#float) (or floating-point number)\n", "\n", "An [integer](https://docs.tdm-pilot.org/key-terms/#integer), what we sometimes call a \"whole number\", is a number without a decimal point that can be positive or negative. When a value uses a decimal, it is called a [float](https://docs.tdm-pilot.org/key-terms/#float) or floating-point number. Two numbers that are mathematically equivalent could be in two different data types. For example, mathematically 5 is equal to 5.0, yet the former is an [integer](https://docs.tdm-pilot.org/key-terms/#integer) while the latter is a [float](https://docs.tdm-pilot.org/key-terms/#float). \n", "\n", "Of course, [Python](https://docs.tdm-pilot.org/key-terms/#python) can also help us manipulate text. A snippet of text in Python is called a [string](https://docs.tdm-pilot.org/key-terms/#string). A [string](https://docs.tdm-pilot.org/key-terms/#string) can be written with single or double quotes. A [string](https://docs.tdm-pilot.org/key-terms/#string) can use letters, spaces, line breaks, and numbers. So 5 is an [integer](https://docs.tdm-pilot.org/key-terms/#integer), 5.0 is a [float](https://docs.tdm-pilot.org/key-terms/#float), but '5' and '5.0' are [strings](https://docs.tdm-pilot.org/key-terms/#string). A [string](https://docs.tdm-pilot.org/key-terms/#string) can also be blank, such as ''. \n", "\n", "|Familiar Name | Programming name | Examples |\n", "|---|---|---|\n", "|Whole number|integer| -3, 0, 2, 534|\n", "|Decimal|float | 6.3, -19.23, 5.0, 0.01|\n", "|Text|string| 'Hello world', '1700 butterflies', '', '1823'|\n", "\n", "The distinction between each of these data types may seem unimportant, but [Python](https://docs.tdm-pilot.org/key-terms/#python) treats each one differently. For example, we can ask [Python](https://docs.tdm-pilot.org/key-terms/#python) whether an [integer](https://docs.tdm-pilot.org/key-terms/#integer) is equal to a [float](https://docs.tdm-pilot.org/key-terms/#float), but we cannot ask whether a [string](https://docs.tdm-pilot.org/key-terms/#string) is equal to an [integer](https://docs.tdm-pilot.org/key-terms/#integer) or a [float](https://docs.tdm-pilot.org/key-terms/#float).\n", "\n", "To evaluate whether two values are equal, we can use two equals signs between them. The expression will evaluate to either `True` or `False`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Run this code cell to determine whether the values are equal\n", "42 == 42.0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Run this code cell to compare an integer with a string\n", "15 == 'fifteen'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Run this code cell to compare an integer with a string\n", "15 == '15'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we use the addition [operator](https://docs.tdm-pilot.org/key-terms/#operator) on [integers](https://docs.tdm-pilot.org/key-terms/#integer) or [floats](https://docs.tdm-pilot.org/key-terms/#float), they are added to create a sum. When we use the addition [operator](https://docs.tdm-pilot.org/key-terms/#operator) on [strings](https://docs.tdm-pilot.org/key-terms/#string), they are combined into a single, longer [string](https://docs.tdm-pilot.org/key-terms/#string). This is called [concatenation](https://docs.tdm-pilot.org/key-terms/#concatenation). " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Combine the strings 'Hello' and 'World'\n", "'Hello' + 'World'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the [strings](https://docs.tdm-pilot.org/key-terms/#string) are combined exactly as they are written. There is no space between the [strings](https://docs.tdm-pilot.org/key-terms/#string). If we want to include a space, we need to add the space to the end of 'Hello' or the beginning of 'World'. We can also concatenate multiple [strings](https://docs.tdm-pilot.org/key-terms/#string)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Combine three strings\n", "'Hello ' + 'World' + '!'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we use addition [operator](https://docs.tdm-pilot.org/key-terms/#operator), the values must be all numbers or all [strings](https://docs.tdm-pilot.org/key-terms/#string). Combining them will create an error." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Try adding a string to an integer\n", "'55' + 23" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, we receive the error `can only concatenate str (not \"int\") to str`. [Python](https://docs.tdm-pilot.org/key-terms/#python) assumes we would like to join two [strings](https://docs.tdm-pilot.org/key-terms/#string) together, but it does not know how to join a [string](https://docs.tdm-pilot.org/key-terms/#string) to an [integer](https://docs.tdm-pilot.org/key-terms/#integer). Put another way, [Python](https://docs.tdm-pilot.org/key-terms/#python) is unsure if we want:\n", "\n", ">'55' + 23 \n", "\n", "to become\n", ">'5523'\n", "\n", "or \n", ">78" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We *can* multiply a [string](https://docs.tdm-pilot.org/key-terms/#string) by an [integer](https://docs.tdm-pilot.org/key-terms/#integer). The result is simply the [string](https://docs.tdm-pilot.org/key-terms/#string) repeated the appropriate number of times." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Multiply a string by an integer\n", "'Hello World!' * 5" ] }, { "cell_type": "markdown", "metadata": { "toc-hr-collapsed": true, "toc-nb-collapsed": true }, "source": [ "# Variables\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A [variable](https://docs.tdm-pilot.org/key-terms/#variable) is like a container that stores information. There are many kinds of information that can be stored in a [variable](https://docs.tdm-pilot.org/key-terms/#variable), including the data types we have already discussed ([integers](https://docs.tdm-pilot.org/key-terms/#integer), [floats](https://docs.tdm-pilot.org/key-terms/#float), and [string](https://docs.tdm-pilot.org/key-terms/#string)). We create (or *initialize*) a [variable](https://docs.tdm-pilot.org/key-terms/#variable) with an [assignment statement](https://docs.tdm-pilot.org/key-terms/#assignment-statement). The [assignment statement](https://docs.tdm-pilot.org/key-terms/#assignment-statement) gives the variable an initial value." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Initialize an integer variable and add 22 \n", "new_integer_variable = 5\n", "new_integer_variable + 22" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The value of a [variable](https://docs.tdm-pilot.org/key-terms/#variable) can be overwritten with a new value. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Overwrite the value of my_favorite_number when the commented out line of code is executed. \n", "# Remove the # in the line \"#my_favorite_number = 9\" to turn the line into executable code.\n", "\n", "my_favorite_number = 7\n", "my_favorite_number = 9\n", "my_favorite_number" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Overwriting the value of a variable using its original value\n", "cats_in_house = 1\n", "cats_in_house = cats_in_house + 2\n", "cats_in_house" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Initialize a string variable and concatenate another string\n", "new_string_variable = 'Hello '\n", "new_string_variable + 'World!'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can create a [variable](https://docs.tdm-pilot.org/key-terms/#variable) with almost any name, but there are a few guidelines that are recommended.\n", "\n", "## Variable Names Should be Descriptive\n", "\n", "If we create a [variable](https://docs.tdm-pilot.org/key-terms/#variable) that stores the day of the month, it is helpful to give it a name that makes the value stored inside it clear like `day_of_month`. From a logical perspective, we could call the [variable](https://docs.tdm-pilot.org/key-terms/#variable) almost anything (`hotdog`, `rabbit`, `flat_tire`). As long as we are consistent, the code will execute the same. When it comes time to read, modify, and understand the code, however, it will be confusing to you and others. Consider this simple program that lets us change the `days` [variable](https://docs.tdm-pilot.org/key-terms/#variable) to compute the number of seconds in that many days. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Compute the number of seconds in 3 days\n", "days = 3\n", "hours_in_day = 24\n", "minutes_in_hour = 60\n", "seconds_in_minute = 60\n", "\n", "days * hours_in_day * minutes_in_hour * seconds_in_minute" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could write a program that is logically the same, but uses confusing [variable](https://docs.tdm-pilot.org/key-terms/#variable) names." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hotdogs = 60\n", "sasquatch = 24\n", "example = 3\n", "answer = 60\n", "\n", "answer * sasquatch * example * hotdogs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This code gives us the same answer as the first example, but it is confusing. Not only does this code use [variable](https://docs.tdm-pilot.org/key-terms/#variable) names that are confusing, it also does not include any comments to explain what the code does. It is not clear that we would change `example` to set a different number of days. It is not even clear what the purpose of the code is. As code gets longer and more complex, having clear [variable](https://docs.tdm-pilot.org/key-terms/#variable) names and explanatory comments is very important. " ] }, { "cell_type": "markdown", "metadata": { "toc-hr-collapsed": true, "toc-nb-collapsed": true }, "source": [ "## Variable Naming Rules\n", "\n", "In addition to being descriptive, [variable](https://docs.tdm-pilot.org/key-terms/#variable) names must follow 3 basic rules:\n", "\n", "1. Must be one word (no spaces allowed)\n", "2. Only letters, numbers and the underscore character (\\_)\n", "3. Cannot begin with a number\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Which of these variable names are acceptable? \n", "# Comment out the variables that are not allowed in Python and run this cell to check if the variable assignment works. \n", "# If you get an error, the variable name is not allowed in Python.\n", "\n", "$variable = 1\n", "a variable = 2\n", "a_variable = 3\n", "4variable = 4\n", "variable5 = 5\n", "variable-6 = 6\n", "variAble = 7\n", "Avariable = 8\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variable Naming Style Guidelines\n", "\n", "The three rules above describe absolute rules of [Python](https://docs.tdm-pilot.org/key-terms/#python) [variable](https://docs.tdm-pilot.org/key-terms/#variable) naming. If you break those rules, your code will create an error and fail to execute properly. There are also style *guidelines* that, while they won't break your code, are generally advised for making your code readable and understandable. These style guidelines are written in the [Python Enhancement Proposals (PEP) Style Guide](https://www.python.org/dev/peps/pep-0008/).\n", "\n", "The current version of the style guide advises that [variable](https://docs.tdm-pilot.org/key-terms/#variable) names should be written:\n", ">lowercase, with words separated by underscores as necessary to improve readability.\n", "\n", "If you have written code before, you may be familiar with other styles, but these notebooks will attempt to follow the PEP guidelines for style. Ultimately, the most important thing is that your [variable](https://docs.tdm-pilot.org/key-terms/#variable) names are consistent so that someone who reads your code can follow what it is doing. As your code becomes more complicated, writing detailed comments with `#` will also become more important." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "snake_case_variable\n", "kebab-case-variable\n", "CamelCaseVariable" ] }, { "cell_type": "markdown", "metadata": { "toc-hr-collapsed": true, "toc-nb-collapsed": true }, "source": [ "# Functions\n", "\n", "Many different kinds of programs often need to do very similar operations. Instead of writing the same code over again, you can use a [function](https://docs.tdm-pilot.org/key-terms/#function). Essentially, a [function](https://docs.tdm-pilot.org/key-terms/#function) is a small snippet of code that can be quickly referenced. There are three kinds of [functions](https://docs.tdm-pilot.org/key-terms/#function):\n", "\n", "* Native [functions](https://docs.tdm-pilot.org/key-terms/#function) built into [Python](https://docs.tdm-pilot.org/key-terms/#python)\n", "* [Functions](https://docs.tdm-pilot.org/key-terms/#function) others have written that you can import\n", "* [Functions](https://docs.tdm-pilot.org/key-terms/#function) you write yourself\n", "\n", "We'll address [functions](https://docs.tdm-pilot.org/key-terms/#function) you write yourself in *Python Basics* II. For now, let's look at a few of the native [functions](https://docs.tdm-pilot.org/key-terms/#function). One of the most common [functions](https://docs.tdm-pilot.org/key-terms/#function) used in [Python](https://docs.tdm-pilot.org/key-terms/#python) is the `print()` [function](https://docs.tdm-pilot.org/key-terms/#function) which simply prints a [string](https://docs.tdm-pilot.org/key-terms/#string)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A print function that prints: Hello World!\n", "print('Hello World!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could also define a [variable](https://docs.tdm-pilot.org/key-terms/#variable) with our [string](https://docs.tdm-pilot.org/key-terms/#string) ```'Hello World!'``` and then pass that [variable](https://docs.tdm-pilot.org/key-terms/#variable) into the `print()` function. It is common for functions to take an input, called an [argument](https://docs.tdm-pilot.org/key-terms/#argument), that is placed inside the parentheses (). " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define a string and then print it\n", "our_string = 'Hello World!'\n", "print(our_string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is also an `input()` [function](https://docs.tdm-pilot.org/key-terms/#function) for taking user input." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A program to greet the user by name\n", "print('Hi. What is your name?') # Ask the user for their name\n", "user_name = input() # Take the user's input and put it into the variable user_name\n", "print('Pleased to meet you, ' + user_name) # Print a greeting with the user's name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We defined a [string](https://docs.tdm-pilot.org/key-terms/#string) [variable](https://docs.tdm-pilot.org/key-terms/#variable) ```user_name``` to hold the user's input. We then called the `print()` [function](https://docs.tdm-pilot.org/key-terms/#function) to print the [concatenation](https://docs.tdm-pilot.org/key-terms/#concatenate) of 'Pleased to meet you, ' and the user's input that was captured in the [variable](https://docs.tdm-pilot.org/key-terms/#variable) ```user_name```. Remember that we can use a ```+``` to [concatenate](https://docs.tdm-pilot.org/key-terms/#concatenate), meaning join these [strings](https://docs.tdm-pilot.org/key-terms/#string) together. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could also use an `f string` to print a variable name within a larger string." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Placing an f before a string, allows us to reference variables in brackets {}\n", "print(f'Hello {user_name}, pleased to meet you!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can [concatenate](https://docs.tdm-pilot.org/key-terms/#concatenate) many [strings](https://docs.tdm-pilot.org/key-terms/#string) together, but we cannot [concatenate](https://docs.tdm-pilot.org/key-terms/#concatenate) [strings](https://docs.tdm-pilot.org/key-terms/#string) with [integers](https://docs.tdm-pilot.org/key-terms/#integer) or [floats](https://docs.tdm-pilot.org/key-terms/#float)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Concatenating many strings within a print function\n", "print('Hello, ' + 'all ' + 'these ' + 'strings ' + 'are ' + 'being ' + 'connected ' + 'together.')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Trying to concatenate a string with an integer causes an error\n", "print('There are ' + 7 + 'continents.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can transform one [variable](https://docs.tdm-pilot.org/key-terms/#variable) type into another [variable](https://docs.tdm-pilot.org/key-terms/#variable) type with the `str()`, `int()`, and `float()` [functions](https://docs.tdm-pilot.org/key-terms/#function). Let's convert the [integer](https://docs.tdm-pilot.org/key-terms/#integer) above into a [string](https://docs.tdm-pilot.org/key-terms/#string) so we can [concatenate](https://docs.tdm-pilot.org/key-terms/#concatenate) it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('There are ' + str(7) + ' continents.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mixing [strings](https://docs.tdm-pilot.org/key-terms/#string) with [floats](https://docs.tdm-pilot.org/key-terms/#float) and [integers](https://docs.tdm-pilot.org/key-terms/#integer) can have unexpected results. See if you can spot the problem with the program below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A program to tell a user how many months old they are\n", "print('How old are you?') # Ask the user their age\n", "user_age = input() # Take the user input and put it into the variable user_age\n", "number_of_months = int(user_age) * 12 # Define a new variable number_of_months that multiplies the user's age by 12\n", "print('That is more than ' + str(number_of_months) + ' months old!' ) # Print a response that tells the user they are at least number_of_months old" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to compute the [variable](https://docs.tdm-pilot.org/key-terms/#variable) ```number_of_months```, we multiply ```user_age``` by 12. The problem is that ```user_age``` is a [string](https://docs.tdm-pilot.org/key-terms/#string). Multiplying a [string](https://docs.tdm-pilot.org/key-terms/#string) by 12 simply makes the string repeat 12 times. After the user gives us their age, we need that input to be converted to an [integer](https://docs.tdm-pilot.org/key-terms/#integer). At the same time, we have to convert the [integer](https://docs.tdm-pilot.org/key-terms/#integer) ```number_of_months``` back to a [string](https://docs.tdm-pilot.org/key-terms/#string) for our final `print()` function. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A program to tell a user how many months old they are\n", "print('How old are you?') # Ask the user their age\n", "user_age = input() # Take the user input and put it into the variable user_age\n", "number_of_months = int(user_age) * 12 # Define a new variable number_of_months that multiplies the user's age by 12\n", "print('That is more than ' + str(number_of_months) + ' months old!' ) # Print a response that tells the user they are at least number_of_months old" ] }, { "cell_type": "markdown", "metadata": { "toc-hr-collapsed": true }, "source": [ "___\n", "# Lesson Complete\n", "\n", "Congratulations! You have completed *Python Basics* I. There are two more lessons in *Python Basics*:\n", "\n", "* *Python Basics* II\n", "* *Python Basics* III\n", "\n", "## Python Basics I Quiz\n", "\n", "If you would like to check your understading of this lesson, you can [take this quick quiz](https://docs.google.com/forms/d/e/1FAIpQLSdTPq_BotRY_eqJIfIXT2OoWkv9MwgOKngWcTYJfilwbQ6eAQ/viewform?usp=sf_link).\n", "\n", "## Start Next Lesson: [Python Basics II](./python-basics-2.ipynb)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.6" }, "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 }, "toc-autonumbering": false, "toc-showcode": false, "toc-showmarkdowntxt": false, "toc-showtags": false }, "nbformat": 4, "nbformat_minor": 4 }