{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Table of Contents\n", "\n", "- [Introduction: Conditionals](#introduction--conditionals)\n", "- [College Application Example](#college-application-example)\n", "- [Introduction: Iterations](#introduction--iterations)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction: Conditionals \n", "\n", "We use conditionals all the time in our daily lives without even realizing it. However, they aren't written in Python. We use conditionals in choices ranging from deciding what to wear in the morning to life-changing decisions like selecting a job.\n", "\n", "I would cross the street if the light turns green; otherwise, I would wait. If the sun comes up, I would get out of bed; otherwise, I would go back to sleep again. Okay, it's not quite that simple, but when we make decisions based on circumstances, our brain behaves similarly to a machine: it evaluates the conditions and acts on the results.\n", "\n", "Conditions - usually in the form of `if statements` - are one of the key features of a programming language, and Python is no exception. `If statements` are control flow statements which allow us to run a particular code and make a decision only when a certain condition is met. Most of the time, this decision depends on the value of variables or arithmetic expressions. \n", "\n", "Conditional statements represent the set of instructions that allow us to make these decisions and perform computations depending on whether the code (a specific Boolean constraint) evaluates to `True or False`. Therefore, in a programming language, there are often two parts to a sample of code: One which will be executed, if the condition is `True`, and another one, if it is `False`. In the upcoming chapter, you will get to experiement with conditionals with an extedend example!\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# College Application Example \n", "\n", "## Additional Material: Operators\n", "\n", "Before starting looking at `if and else statements`, we will dive deeper into the basic operators that we will use in the next examples. The `if`, `if-else` or `elif` statements, that we will see, will contain these operators. Here is a short list of operators that we have included in our programs:\n", "\n", "`Assignment operators`: `=` (assign)\n", "\n", "`Comparison operators`: `==` (equal), `!=` (not equal)\n", "\n", "`Logical operators`: `and` (Returns True if both statements are true), `or` (Returns `True` if one or both of the statements are true)\n", "\n", "It must be added that, in Python, `==` and `=` do not have the same meaning. `=` assigns a value to a variable, for example `age=5` implies that the value 5 is assigned to `age`. In contrast, `==` is the equal sign, that we commonoly use in mathematics. The latter symbol is used to check whether two expressions give the same value. For instance, (x==y) would result in `False` if we had assigned different values to x and y.\n", "\n", "## If and else statements\n", "\n", "In this example, we put ourselves in the shoes of a college applicant, who is looking to submit both his essay and grade transcript to his college of choice. The university's application portal verifies whether the application is successful by asking the applicant to type in `Completed` if the applicant wrote his essay and `Sent` if he sent his grades.\n", "The indented block will only be executed (the application will only be successful) if the words `Completed` and `Sent` are typed in. If the user enters any other set of words, nothing happens. Please remember to add quotation marks (\" \") to the instructions that you want the applicant to see, as the text represents a string (a sequence of characters) and not including the quotation marks would give you a Syntax error." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "If you have completed your essay please type in 'Completed': Completed\n", "If you have submitted your grades please type in 'Sent': Sent\n", "Application successful!\n" ] } ], "source": [ "essay_application = input(\"If you have completed your essay please type in 'Completed': \")\n", "grades = input(\"If you have submitted your grades please type in 'Sent': \")\n", "if essay_application == 'Completed' and grades == 'Sent':\n", " print('Application successful!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will strengthen our code in case the applicant disregards the capital letter at the start of `Completed` and `Sent`. After all, the university doesn't want an applicant who has completed all of the required tasks to not be considered because of a technicality. In the following case, the applicant can type in `completed` and `sent` and his application will still be successful. As you can see, we used the expression `or` to make sure that the application portal also accepts the words `completed` and `sent` in addition to `Completed` and `Sent`." ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "If you have completed your essay please type in 'Completed': Completed\n", "If you have submitted your grades please type in 'Sent':sent\n", "Application successful!\n" ] } ], "source": [ "essay_application = input(\"If you have completed your essay please type in 'Completed': \")\n", "grades = input(\"If you have submitted your grades please type in 'Sent': \")\n", "if (essay_application == 'Completed' or essay_application == 'completed') and (grades == 'Sent' or grades == 'sent'):\n", " print('Application successful!'')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will now introduce the `else statement` by adding a code block that lets the applicant know that his application is unsucessful. It must be noted that, there can be an `if` statement without an `else statement`. However, it is not possible to have an `else` statement if previously there is no `if` statement. Furthermore, it is important to know that Python will never execute the `if and the else statements` together as they are mutually exclusive. \n", "\n", "The university also decided to improve user experience by addressing applicants by their name. Hence, the variable name `name` was added to the code, and `.format(name)` inserts the applican't name into the brackets." ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your name please: Alex\n", "If you have completed your essay please type in 'Completed': completed\n", "If you have submitted your grades please type in 'Sent':Sent\n", "Application successful, Alex!\n" ] } ], "source": [ "name = input('Enter your name please: ')\n", "essay_application = input(\"If you have completed your essay please type in 'Completed': \")\n", "grades = input(\"If you have submitted your grades please type in 'Sent':\")\n", "if (essay_application == 'Completed' or essay_application == 'completed') and (grades == 'Sent' or grades == 'sent'):\n", " print('Application successful, {}!'.format(name))\n", "else:\n", " print('Application unsuccessful, {}!'.format(name))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You are free to experiment with the code youself by selecting the cell containing the code and running the code in your Jupyter Notebook. \n", "\n", "## Elif statements\n", "\n", "In the next example we will modify our code so that we get an introduction to the `elif statement`. Let's imagine that the university wants to let applicants know why their application is unsuccessful. Hence, it wants applicants to know whether the applicant failed either the essay or the grade requirement, or whether he failed both requirements.\n", "\n" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your name please: Alex\n", "If you have completed your essay please type in 'Completed': Completed\n", "If you have submitted your grades please type in 'Sent':not sent\n", "Grade requirement failed, your application was unsuccessful, Alex!\n" ] } ], "source": [ "name = input('Enter your name please: ')\n", "essay_application = input(\"If you have completed your essay please type in 'Completed': \")\n", "grades = input(\"If you have submitted your grades please type in 'Sent':\")\n", "if (essay_application == 'Completed' or essay_application == 'completed') and (grades == 'Sent' or grades == 'sent'):\n", " print('Application successful, {}!'.format(name))\n", "elif (essay_application == 'Completed' or essay_application == 'completed') and (grades != 'Sent' and grades != 'sent'):\n", " print('Grade requirement failed, your application was unsuccessful, {}!'.format(name))\n", "elif (essay_application != 'Completed' and essay_application != 'completed') and (grades == 'Sent' or grades == 'sent'):\n", " print('Essay requirement failed, your application was unsuccessful, {}!'.format(name))\n", "else:\n", " print('Both requirements failed, your application was unsuccessful, {}!'.format(name))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "Since the code is getting longer and more complex, let's review it more clearly:\n", "