{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Introducing Python\n", "## 09/05/2023\n", "\n", "print view
\n", "notebook" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "\n", "\n", "\n", "\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Types and Variables\n", "\n", "*value* - your data\n", "\n", "*type* - what kind of data it is\n", "\n", "*variable* - name of your data, how you access it" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Types\n", "\n", "Built In:\n", "* Numerical: integers, floating point, complex\n", "* Boolean (True, False)\n", "* None\n", "* Sequences: strings, tuples, lists, sets, dictionaries\n", "* Callable (functions)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# type\n", " " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(3)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(3.0)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(\"Hello\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "builtin_function_or_method" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(min)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Numbers\n", "\n", " ints vs floats" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Arithmetic Operators\n", "\n", "`+`, `-`, `*`, `/` Hopefully self-explanatory\n", "\n", "`%` modulus (remainder after division)\n", "\n", "`**` exponentiation $x^y$ = `x**y`\n", "\n", "`//` integer (floor) division **division is different in Python2 vs 3**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "5+1 * 3/2" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "5+1 * 3//2" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Assignment Operators\n", "\n", "Can perform an operation while assigning\n", "\n", "a *op*= b\n", "\n", "is\n", "\n", "a = a *op* b" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 10\n", "x += 1\n", "x" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Strings\n", "\n", " strings are a sequence of characters\n", "\n", " ### String literals\n", " * \"you can use double quotes\"\n", " * 'you can using single quotes (more common python style)'\n", " * \"the difference is how easy it is to include a ' character\" 'or a \" character'\n", " * special characters are _escaped_ with a backslash\n", " * so must always escape backslash itself\n", " * '\\n' newline\n", " * '\\\\\\\\' backslash\n", " * '\\t' tab\n", " * '\\'' single quote\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Multiline string literals" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'you can end a line with a slashand it will continue on the next line'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"you can end a line with a slash\\\n", "and it will continue on the next line\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Adjacent string literals are automatically concatenated " ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hi bye'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hi ' 'bye'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Triple quoted strings - for large blocks of text with newlines, commonly used as documentation:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'There are three\\nquotes at the\\nstart and\\nend'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'''There are three\n", "quotes at the\n", "start and\n", "end'''" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "print('\"\\\\t'\"'\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Variables\n", "\n", "Data values are accessed through _references_ to the value.\n", "\n", "A reference is a name for the memory location of the value.\n", "Every value exists somewhere in memory and has an address.\n", "\n", "A variable is created when it is _bound_ to a value. It is impossible to have an uninitialized variable in python (but can be None).\n", " \n", "The type of a variable is the type of the value it is bound to" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "x = 3\n", "y = x\n", "y = y + 1\n", "print(x,y)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Objects\n", "\n", "Everything is an object!\n", "\n", "An object is a value with set of _attributes_ \n", "\n", "Attributes that are callable are called _methods_ and can work on the object data\n", "\n", "Attributes are accessed with the '.' operator\n", "\n", "The attributes of an object can be listed with dir" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Strings are Objects\n", "\n", "A string is an object that has several methods for manipulating the string data.\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HELLO WORLD\n", "['Hello', 'World']\n" ] } ], "source": [ "s = 'Hello World'\n", "print(s.upper())\n", "print(s.split())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String Methods Documentation" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']\n" ] } ], "source": [ "print(dir(s))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Numbers are Objects\n", "\n", "Since everything is an object..." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "x = 3.0" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.is_integer()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['__abs__', '__add__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']\n" ] } ], "source": [ "print(dir(x))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Container Objects\n", "\n", "A container object has _items_ that are accessed with the [] operator\n", "\n", "They hold an arbitrary number of item objects\n", "\n", "The len method returns the number of items\n", "\n", "Strings are an example of a container object" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"Hello\"\n", "len(s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "s = \"Hello\"" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Lists\n", "\n", "A list is an ordered container of arbitrary objects.\n", "\n", "A list is defined by comma separated items in square brackets `[]`." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 3.0, 'cat', 11]\n" ] } ], "source": [ "mylist = [1,3.0,\"cat\", 9+2]\n", "print (mylist)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists are objects and have a number of built-in methods:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']\n" ] } ], "source": [ "print(dir(mylist))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "l = []\n", "l.append(5)\n", "l.append(1)\n", "l.append(3)\n", "l.sort()" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Functions (callables)\n", "\n", "\n", "A function is an encapsulation of code; a set of statements that can\n", "be executed on request and returns a value.\n", "\n", "Functions are objects.\n", "\n", "A method is a function that is an attribute of an object.\n", "\n", "A function takes _arguments_ and returns a result (maybe None)\n", "\n", "The value of a callable type is the address of executable code" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(\"Luke, I am your father.\") # this function takes one argument\n", "divmod(13,4); # this function takes two arguments" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Defining a function" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def square(x):\n", " return x * x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - def starts definition\n", " - The function name is an identifier like a variable name\n", " - good function names are a critical part of good coding style\n", " - bad: foo, dostuff, process_data\n", " - also bad: ReadInFromFileComputeCorrelationAndOutput\n", " - good: ReadExpressionData, ComputeCorrelation, OutputCorrelationMatrix \n", " - _Parameters_ definine what arguments that function takes\n", " - parameters are _bound_ to the values passed to the function\n", " - Statements are the body of the function; **must be indented**\n", " - Return statement exits function and returns specified value \n", " - if omitted, None is returned\n", " - Function definition ends when no more indentation (**whitespace significant!**)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "def twice(x):\n", "\treturn x*2\n", "\n", "dbl = twice #functions are objects\n", "print(dbl(4))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Function Scope\n", "\n", "A function's parameters are bound to the passed value.\n", "That is, it's the same as if the parameter was set equal to the passed value (ex, x=4).\n", "\n", "Parameters and variables bound (assigned to) in the function have _local scope_\n", "\n", "_global_ variables defined outside the function can only be read.\n" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4 5 4\n" ] } ], "source": [ "x=4 \n", "y=3 \n", "def incr(x):\n", " x = x + 1 \n", " return x\n", "print(x,incr(x),x)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "x=4 \n", "y=3 \n", "def incr():\n", " y = y + 1 \n", " return y\n", "print(y,incr())" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Default Parameters\n", "------------------\n", "Default values for parameters can be given. This makes it easy to have\n", "optional arguments that take reasonable defaults if not specified." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def foo(x,y=0,z=1):\n", "\treturn (x+y)*z\n", "\n", "foo(2)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo(2,3)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo(2,3,4)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Calling Functions\n", "-----------------\n", "Functions are called using parens (). \n", "\n", "It is an error to call a function\n", "with an incompatible number of arguments. \n", "\n", "_Named_ arguments allow you to specify arguments in a different order\n", "than defined. \n", "\n", "Unnamed arguments (passed in the order defined) must all be\n", "specified before any named arguments." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo(z=2,y=1,x=3)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo(y=1,x=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "def foo(x,y=0,z=1):\n", " return (x+y)*z" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Lambda Functions\n", "----------------\n", "The lambda keyword can be used to generate an anonymous (unnamed) function\n", "object. \n", "\n", "The return value of this anonymous lambda function is the value of the specified expression.\n", "\t\n", "Lambda expressions tend to make your code more compact but less readable, \n", "so it's probably best to avoid their use for all but the simplest functions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "foo = lambda x: x**2 #this squares x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "is equivalent to" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def foo(x):\n", "\treturn x**2" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Built-in Functions\n", "------------------\n", "There are a huge number of functions built into the python language and\n", "even more are included in standard modules. A few examples:\n", "\n", " * `abs` - absolute value\n", " * `len` - length of a sequence (string, list, etc)\n", " * `min`,`max` - returns smallest/largest item in a sequence\n", " * `sorted` - given a list, returns a sorted copy of that list\n", " * `type` - returns type of an object\n", " * `map` - applies a function to every element of a sequence and returns a list of the result\n", " * `filter` - applies a function to every element of a sequence and returns a\n", " list of just those elements where the function evaluates to true" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[104, 101, 108, 108, 111]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(map(ord, \"hello\")) #ord returns ASCII code of string of length 1" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1.0, 0.4, 4]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(filter(lambda x: x > 0, [1.0,0.4,-0.3,-1.3,4]))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# What is truth?\n", "\n", "Every object has a Boolean value" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(False, False, True)" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(None),bool(False),bool(True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For numerical types 0 is false" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(False, False, True)" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0),bool(0.0),bool(-100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Empty collections are false" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(False, False, True, True)" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool([]),bool(''),bool([False]),bool([0])" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Comparison Operators\n", "\n", "Return a boolean value\n", "\n", " `<,>,!=,==,<=,>=`" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 < 3" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"hello\" != \"hi\"" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(True, False)" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3] == [1,2,3], [1,2,3] == [1,2,3.14]" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 3; y = 4;\n", "x >= y" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "list(filter(round, [1.0,0.4,-0.3,-1.3,4]))" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Introspection\n", "\n", "`dir` and `help` can be used to figure out what methods an object has and what they do." ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function round in module builtins:\n", "\n", "round(number, ndigits=None)\n", " Round a number to a given precision in decimal digits.\n", " \n", " The return value is an integer if ndigits is omitted or None. Otherwise\n", " the return value has the same type as the number. ndigits may be negative.\n", "\n" ] } ], "source": [ "help(round)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "File Objects\n", "------------\n", "A file object provides an interface for reading and writing files.\n", "\n", "Files, unlike memory, are **accessed sequentially** (like reading a book).\n", "\n", "To create a file object use the `open` function:\n", "\n", "`fileobject = open(filename, mode)`\n", "\n", "Where filename is a string that is either a relative path from the \n", "current working directory (e.g., file.txt if file.txt is in the current \n", "directory) or an absolute path (e.g. `/home/user/dkoes/tmp/file.txt`)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "File Mode\n", "----------\n", "\n", "mode is a string that specifies what you are going to do with the file.\n", " * 'r' - file must already exist and will only be read from (default)\n", " * 'w' - file is created or truncated (delete what's already there) and can only be written to\n", " * 'a' - file is open appended to (does not delete existing file) and can only be written to\n", "\n", "It is also possible to open files for both read/write access ('r+') but this\n", "is tricky and generally not necessary.\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Manipulating File Objects (Methods)\n", "------------------------\n", " * `close` - closes the file when you are done with it\n", " * `read` - return the entire file as a string (can also specify optional size argument)\n", " * `readline` - return a single line from the file, returned string includes '\\n'\n", " * `readlines` - return lists of all lines\n", " * `write` - writes a passed string to the file\n", " * `seek` - set current position of the file; seek(0) starts back at beginning \n", "\n" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'>lcl|NC_000017.10_cdsid_NP_009225.1 [gene=BRCA1] [protein=breast cancer type 1 susceptibility protein isoform 1] [protein_id=NP_009225.1] [location=complement(join(41197695..41197819,41199660..41199720,41201138..41201211,41203080..41203134,41209069..41209152,41215350..41215390,41215891..41215968,41219625..41219712,41222945..41223255,41226348..41226538,41228505..41228631,41234421..41234592,41242961..41243049,41243452..41246877,41247863..41247939,41249261..41249306,41251792..41251897,41256139..41256278,41256885..41256973,41258473..41258550,41267743..41267796,41276034..41276113))]\\n'" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = open('../files/brca1.fasta')\n", "f.readline()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "f = open('../files/brca1.fasta')\n", "f.read()\n", "line = f.readline()" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Exercise\n", "\n", "What percent of this string consists of g or c?\n", "\n", "atattaggtttttacctacccaggaaaagccaaccaacctcgatctcttgtagatctgttctctaaacgaactttaaaatctgtgtagctgtcgctcggctgcatgcctagtgcacctac\n", "\n", "Create a `.py` text file using Jupyter.\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Exercise" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!wget https://MSCBIO2025.github.io/files/brca1.fasta" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How can you extract the gene name (second column) from the first line of`brca1.fasta`?\n", "\n", "How many As, Ts, Cs, and Gs are there on the _second_ line of `brca1.fasta`?\n", "\n", "Write a function that takes a file name as an argument and prints out the gene name and percentage of G's and C's in the first line of the sequence.\n", "\n", "**Hint**: Checkout `split`, `count`, and `strip` methods of `str`" ] } ], "metadata": { "celltoolbar": "Slideshow", "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.12" }, "vscode": { "interpreter": { "hash": "64bee37de6b8c5f8ef99ba624a6c369c62048a36306104572da23a722d067c49" } } }, "nbformat": 4, "nbformat_minor": 4 }