{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module 2\n", "\n", "## Video 10: Example Functions\n", "**Python for the Energy Industry**\n", "\n", "Python has a number of 'built-in' functions you can use. You are already familiar with some of these, including `print` and `range`. In this lesson we will look at some built in functions in more detail.\n", "\n", "You have seen how `range` works with a single argument. It can also be used with two or three arguments:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2, 3, 4]\n" ] } ], "source": [ "# only one argument - all numbers up to 5\n", "print([i for i in range(5)])" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 3, 4]\n" ] } ], "source": [ "# two arguments - numbers between 2 and 5\n", "print([i for i in range(2,5)])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 3, 5, 7]\n" ] } ], "source": [ "# three arguments - numbers between 1 and 9 at multiples of 2\n", "print([i for i in range(1,9,2)])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function `len` will count the number of items in a list, or the number of letters in a string:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "length of my_list: 7\n", "length of my_string: 11\n" ] } ], "source": [ "my_list = [1,2,4,6,8,13,19]\n", "my_string = 'test_string'\n", "\n", "print('length of my_list:', len(my_list))\n", "print('length of my_string:', len(my_string))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are a number of functions that convert a value from one type to another type. These are:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(1)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(1)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(1)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(1)" ] }, { "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.9" } }, "nbformat": 4, "nbformat_minor": 4 }