{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

1. Print, Calculator, Variable Types, and Operators

\n", "

09/29/23

\n", "\n", "

1.0 Introduction to Jupyter Notebooks

\n", "First, this is a Jupyter Notebook, which enables a combination of markdown cells (like this one) to provide description alongside actual code that can be run. If you want to change a markdown cell, simply double-click on the text, make any changes you want to make, and click \"Run\" (the little play button) above to execute. You can add HTML-style tags to markdown cells to add details such as formatting and links.\n", "\n", "There are also code cells, which can actually be executed, just like in a normal programming environment. Click on the below code cell and try clicking \"Run\" (or type Shift+Enter). You can also modify the text that's being printed to the screen and try running it again." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

1.1 Basic Variables

\n", "Python has dynamic typing, which means that variable types are set automatically based on the input. This is handy if you're coming from an environment like Fortran that requires you to specify each and every time what kind of variable you want to create!\n", "\n", "Here are some of the most important variable types (more on these in a bit):\n", "\n", "\n", "Arithmetic operators:\n", "\n", "\n", "Comparison operators:\n", "\n", "\n", "Important note: Python is case-sensitive! \"A\" is not the same thing as \"a\"." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Code that is preceded with a hashtag will not be read by the computer.\n", "# These comments are intended for human readers of this code!\n", "# Commenting your code is essential to ensure that you and others\n", "# will be able to interpret it down the road!\n", "\n", "# Now, let's try playing with some numbers...\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that all of the above are examples of Python's dynamic typing: assigning a variable a floating point value (i.e., including a decimal point) results in that variable being a float, whereas assigning a variable an integer value (i.e., no decimal point) results in that variable being an integer.\n", "\n", "You can also convert from one to the other using the int() and float() commands!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Convert a from a float to an integer.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Convert c from an integer to a float.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the conversion from float to int loses information along the way: Python merely truncates everything after the decimal point.\n", "\n", "On the other hand, the conversion from int to float is essentially lossless: the same information is maintained." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Now let's try some mathematical operators.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When applying mathematical operations, Python tries to keep as much information as possible! This means that an integer and a float interacting together will result in a float as an answer. Likewise, division of integers will give you a float by default (this is new in version 3 of Python).\n", "\n", "Why is a*b not exactly -7.56?\n", "\n", "Floats are not exact numbers! There will always be some degree of rounding errors associated with floating point numbers (because we don't have infinite storage to keep an infinite number of digits after the decimal). As a result, it's good practice when comparing floats not to look for equality, but instead to look for \"close enough\" - there are functions to do this that we will see later!\n", "\n", "You can also represent scientific notation in Python!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Let's represent 6 x 10^23 in Python!\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

1.2 Strings

\n", "\n", "Strings are the text variable type in Python: they're surrounded either by single or double quotes (it generally doesn't matter which, as long as you're consistent at the start and finish)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to combine two strings, you can do so easily with the \"+\" command." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# To add a space, simply throw another string in there.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# How about combining a string and a float?\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The TypeError you get in the above example explains the problem: a float cannot be combined in this way with a string. But what if we want to make this change? That's where the str() function comes in handy." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Let's try that again!\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes you want a new line within the same string. This can be accomplished with the \"escape character\", which in Python is a backslash: \\\\. \"\\n\" will start a new line!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Creating a multi-line string using the newline character.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you don't want to deal with newlines being created automatically, you can create a raw string!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This will give you a nonsense result because \\n is being treated as a new line.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Instead, tell Python you're going to make a raw string by adding the letter 'r'.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

1.3 Boolean Variables

\n", "\n", "A Boolean variable can only have two values: True and False (and remember, capitalization counts!) These are the results of the logical tests mentioned earlier!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also combine conditions using and and or.\n", "\n", "and: Both statements must be True in order for the entire statement to be True.\n", "\n", "or: Either statement (or both) can be True in order for the entire statement to be True." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

1.4 NoneType

\n", "\n", "A NoneType variable can only have one value: None (and capitalization counts!). \n", "\n", "Why would we want this? Consider the scenario where you want to make sure there's no value already in a given variable (so, for example, if you're working with thousands of lines of code and the same variable types keep coming up). Setting that variable to None (via something like a = None) means you're guaranteed not to be messing around with any previous values by accident." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

TAKE-HOME POINTS

\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 4 }