{ "metadata": { }, "nbformat": 4, "nbformat_minor": 5, "cells": [ { "id": "metadata", "cell_type": "markdown", "source": "
Python is a typed language, data has a type, and different types of data cannot always be connected immediately and might need some conversion step before they can be used together. For instance if you add a number to a number, what should happen? If you add a number to a message, what do you expect will happen?
\n\n\nAgenda\nIn this tutorial, we will cover:
\n\n
\n- Types
\n
Every value in a program has a specific type.
\nName | \nPython Code | \nRepresents | \n
---|---|---|
Integer | \nint | \nrepresents positive or negative whole numbers like 3 or -512. | \n
Floating point number | \nfloat | \nrepresents real numbers like 3.14159 or -2.5. | \n
Character string | \nstr | \ntext, written with either ' or \" quotes (they must match) | \n
Use the built-in function type
to find out what type a value has. This works on values as well as variables. But remember: the value has the type — the variable is just a label.
Check the type of values with the type()
function:
You can also check the types of variables
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-3", "source": [ "fitness = 'average'\n", "print(type(fitness))" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">A value’s type determines what the program can do to it. Some operations may work
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-5", "source": [ "print(5 - 3)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">And some operations may not work:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-7", "source": [ "print('hello' - 'h')" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">For instance, you can use the +
and *
operators on strings.
Some methods only accept specific types, or only work on specific types.
\nThe built-in function len
returns the length of your data. Which of the following would you expect to work? len(string)
? len(int)
?
Not all types support all operations, adding an integer to a string doesn’t make much sense:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-13", "source": [ "print(1 + '2')" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">This does not work because it’s ambiguous: should 1 + '2'
be 3
(a number) or '12'
(a string)? Some types can be converted to other types by using the type name as a function.
Here is a quick chart showing which operations are allowed for each pair:
\nLeft\\Right | \nint | \nfloat | \nstr | \n
---|---|---|---|
int | \n+-*/ | \n+-*/ | \n* | \n
float | \n+-*/ | \n+-*/ | \n
| \n
str | \n* | \n
| \n+ | \n
As you can see you can do 3 * \"test\"
and \"test\" * 3
, but it doesn’t work with floats.
Integers and floating-point numbers can be mixed in arithmetic. Python 3 (which we use) automatically converts integers to floats as needed.
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-17", "source": [ "print(f'half is {1 / 2.0}')\n", "print(f'three squared is {3.0 ** 2}')" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">If we make one cell in a spreadsheet depend on another, and update the latter,\nthe former updates automatically. However, this does not happen in programming languages.
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-19", "source": [ "variable_one = 1\n", "variable_two = 5 * variable_one\n", "variable_one = 2\n", "print(f'first is {variable_one} and second is {variable_two}')" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">The computer reads the value of first
when doing the multiplication, creates\na new value, and assigns it to second
. After that, second
does not remember\nwhere it came from. Every computation happens line-by-line.
\n\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-21", "source": [ "# Test out solutions here!\n", "" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Question: Fractions\nWhat type of value is 3.14159?\nHow can you find out?
\n\n👁 View solution
\n\nIt is a floating-point number (often abbreviated “float”).\nIt is possible to find out by using the built-in function
\ntype()
.\nprint(type(3.14159))\n<class 'float'>\n
\n\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-23", "source": [ "# Test out solutions here!\n", "" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Question: Automatic Type Conversion\nWhat type of value is the result of (3.25 + 4)?
\n\n👁 View solution
\n\nIt is a float:\nintegers are automatically converted to floats as necessary.
\n\nresult = 3.25 + 4\nprint(f'result is {type(result)}')\n
\n7.25 is <class 'float'>\n
\n\nQuestion: Choose a Type\nWhat type of value (integer, floating point number, or character string)\nwould you use to represent each of the following? Try to come up with more than one good answer for each problem. For example, in # 1, when would counting days with a floating point variable make more sense than using an integer?
\n\n
\n- Number of days since the start of the year.
\n- Time elapsed from the start of the year until now in days.
\n- Serial number of a piece of lab equipment.
\n- A lab specimen’s age
\n- Current population of a city.
\n- Average population of a city over time.
\n\n👁 View solution
\n\nThe answers to the questions are:
\n\n
\n- Integer, since the number of days would lie between 1 and 365.
\n- Floating point, since fractional days are required
\n- Character string if serial number contains letters and numbers, otherwise integer if the serial number consists only of numerals
\n- This will vary! How do you define a specimen’s age? whole days since collection (integer)? date and time (string)?
\n- Choose floating point to represent population as large aggregates (eg millions), or integer to represent population in units of individuals.
\n- Floating point number, since an average is likely to have a fractional part.
\n
\n\n\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-25", "source": [ "# Test out solutions here!" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Question: Division Types\nIn Python 3, the
\n//
operator performs integer (whole-number) floor division, the/
operator performs floating-point\ndivision, and the%
(or modulo) operator calculates and returns the remainder from integer division:\nprint(f'5 // 3: {5 // 3}')\nprint(f'5 / 3: {5 / 3}')\nprint(f'5 % 3: {5 % 3}')\n
\n5 // 3: 1\n5 / 3: 1.6666666666666667\n5 % 3: 2\n
If
\nnum_subjects
is the number of subjects taking part in a study,\nandnum_per_survey
is the number that can take part in a single survey,\nwrite an expression that calculates the number of surveys needed\nto reach everyone once.👁 View solution
\n\nWe want the minimum number of surveys that reaches everyone once, which is\nthe rounded up value of
\nnum_subjects/ num_per_survey
. This is\nequivalent to performing a floor division with//
and adding 1. Before\nthe division we need to subtract 1 from the number of subjects to deal with\nthe case wherenum_subjects
is evenly divisible bynum_per_survey
.\nnum_subjects = 600\nnum_per_survey = 42\nnum_surveys = (num_subjects - 1) // num_per_survey + 1\n\nprint(num_subjects, 'subjects,', num_per_survey, 'per survey:', num_surveys)\n
\n600 subjects, 42 per survey: 15\n
\n\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-27", "source": [ "# Test out solutions here!" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Question: Strings to Numbers\nWhere reasonable,
\nfloat()
will convert a string to a floating point number,\nandint()
will convert a floating point number to an integer:\nprint(\"string to float:\", float(\"3.4\"))\nprint(\"float to int:\", int(3.4))\n
\nstring to float: 3.4\nfloat to int: 3\n
If the conversion doesn’t make sense, however, an error message will occur.
\n\n\nInput: Python\n\nprint(\"string to float:\", float(\"Hello world!\"))\n
\n\nOutput\n\nTraceback (most recent call last):\n File \"<stdin>\", line 1, in <module>\nValueError: could not convert string to float: 'Hello world!'\n
Given this information, what do you expect the following program to do?
\nWhat does it actually do?
\nWhy do you think it does that?
\n\nprint(\"fractional string to int:\", int(\"3.4\"))\n
\n👁 View solution
\n\nWhat do you expect this program to do? It would not be so unreasonable to expect the Python 3
\nint
command to\nconvert the string “3.4” to 3.4 and an additional type conversion to 3. After all, Python 3 performs a lot of other\nmagic - isn’t that part of its charm?\nint(\"3.4\")\n
However, Python 3 throws an error.
\n\nTraceback (most recent call last):\n File \"<stdin>\", line 1, in <module>\nValueError: invalid literal for int() with base 10: '3.4'\n
Why? To be consistent, possibly. If you ask Python to perform two consecutive\ntypecasts, you must convert it explicitly in code.
\n\nint(float(\"3.4\"))\n
\n3\n
\n\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-29", "source": [ "# Test out solutions here!" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ ">Question: Arithmetic with Different Types\nWhich of the following will return the floating point number
\n2.0
?\nNote: there may be more than one right answer.\nfirst = 1.0\nsecond = \"1\"\nthird = \"1.1\"\n
\n
\n- \n
first + float(second)
- \n
float(second) + float(third)
- \n
first + int(third)
- \n
first + int(float(third))
- \n
int(first) + int(float(third))
- \n
2.0 * second
\n👁 View solution
\n\nAnswer: 1 and 4 give exactly 2.0.\nAnswer 5 gives the value
\n2
which may be considered equivalent, but is not returning a float specifically.