{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "\n", "# An Introduction to Python\n", "\n", "Dr Matteo Degiacomi\n", "Department of Physics, Durham University\n", "\n", "\n", "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Table of Contents:\n", "\n", " * 1. [Introduction](#intro)\n", " * 2. [Tests](#tests)\n", " * 3. [Functions](#functions)\n", " * 4. [Lists](#lists)\n", " * 5. [Strings](#strings)\n", " * 6. [Sets and Dictionaries](#sets)\n", " * 7. [Type casting](#casting)\n", " * 8. [Mutable and Immutable objects](#mutable)\n", " * 9. [Loops](#loops)\n", " * 10. [reading and writing files](#files)\n", " \n", " \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. INTRODUCTION " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook will provide commands used within the tutorial. Every cell can contain free text (a.k.a. *markdown*, using markup language à la Wikipedia), or code. By selecting a cell and either pressing the \"play\" simbol above or **SHIFT+ENTER**, the cell content is executed. Try the cell below!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello! This is a Python tutorial!\n" ] } ], "source": [ "print(\"Hello! This is a Python tutorial!\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "The **print** command allows printing things on screen. You can print multiple things separated by commas" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"Two plus two is four\")\n", "print(2, \" plus \" , 2, \" is \", 2 + 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", " TASK 0 : Create a new cell below this line using the menu above: Insert > Insert Cell below. Type some mathematical expression and execute the cell. \n", " \n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to store results of an operation for later usage, you can **declare a variable**. Note that = does not mean equality in mathematical terms, but assignment. `n=n+1` means that n+1 will be first computed, then assigned to the variable n." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result = (10.4 + 22)*13 + 44.0/72.0\n", "print(result)\n", "\n", "result = result + 1\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "you can **comment** your code using #" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# some mathematical equations will follow\n", "x = 5 # defining value of x\n", "y = 5*x**2 # this is a parabola\n", "print(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", " TASK 1 : Compute and print the euclidean (i.e. straight line) distance between two 2D points A=(Ax,Ay)=(0,0) and B=(Bx,By)=(1,1). \n", " \n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ax = 0\n", "ay = 0\n", "bx = 1\n", "by = 1\n", "distance = # here you calculate the euclidean distance\n", "# here you can ask Python to print the result, stored in the variable called \"distance\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. TESTS " ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "**tests** verify if a condition is satisfied or not. Their result is a **Boolean** (True or False). The `if` statement allows executing a piece of code only if a condition is satisfied. It can have an optional `else` part, indicating commands to execute *if* the test result is *False*." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Enter a weekday into the cell below and run the code. Note that your weekday will be a **string**. So, the day name should be enclosed in single or double quotes, such as \"banana\" and '\"how are things?\" \"all good!\"'\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "day = #enter a day \"Monday\", ...\"Friday\", ...\"Sunday\"\n", "if day == \"Friday\":\n", " print(\"Burrito?\")\n", "else:\n", " print(\"Sandwich?\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "The `if` statement can have any number of `elif` tests (i.e. alternative conditions). Only one group of statements is executed — those controlled by the first test that passes. Assign a grade in the first line below, and execute the code." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grade = #Give a grade (A, B, C ... F) in \" \"\n", "if grade == \"A\":\n", " print(\"Congratulations!\")\n", "elif grade == \"B\":\n", " print(\"That's pretty good.\")\n", "elif grade == \"C\":\n", " print(\"Well, it's passing, anyway.\")\n", "else:\n", " print(\"ok, this is embarrassing\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "**indentation** is required and must be consistent.\n", "Standard indentation is 4 spaces or one tab. Thumb rule: a line terminates with a column? Indent the next lines!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "day = #enter a day \"Monday\", ...\"Friday\", ...\"Sunday\"\n", "print(\"It's Lunch time...\")\n", "if day == \"Sunday\":\n", " print(\"Yay, it's Sunday!\")\n", " print(\"Pub lunch anyone?\")\n", "print(\"I'm hungry!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", " TASK 2 : Compute the euclidean distance between two 2D points, print a warning signal if the points have the same coordinates, print the distance otherwise.\n", " \n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ax = 0\n", "ay = 0\n", "bx = 1\n", "by = 1\n", "# complete this code!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. FUNCTIONS " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A **function** is a section of code that, given some input parameters (a.k.a. arguments), performs some operations and returns an output. A function works like a black box: when calling it, the only thing you see is the output it returns, its internal mechanisms are hidden." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python offers a list of built-in functions, i.e. functions that are available by default. See the list here: https://docs.python.org/3/library/functions.html. Below are a couple of examples." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "largest = max(2, 4)\n", "number = abs(-5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Packages** containing additional functions can be imported in your code. Some packages are available by default with Python, some are downloadable from third parties… you can also make your own packages!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import sys # import the sys package in the environment # access its functions like this: sys.exit(0)\n", "\n", "from time import * # wild import\n", "# All functions implemented in os are directly callable\n", "# from within your code\n", "\n", "from math import cos # import just a specific function # access directly in the code like this: cos(0.1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can create your own functions. This is especially useful when the same operation is required in multiple parts of your code." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def some_math(x, y):\n", " constant = 2.5\n", " result = (abs(y)*abs(x))/constant\n", " return result #return a value to the caller\n", "\n", "result = some_math(2., 3.)\n", "print(result) #2.4\n", "\n", "result2 = some_math(4., 7.)\n", "print(result2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The *scope* is the region of code a variable is accessible (visible) from. The region inside a function is called the **local scope**, the main code is called the **global scope**. Variables declared in a local scope are not accessible outside it. Variable definitions are searched first in local scope, then in global scope, then in imported packages.\n", "\n", "In the example below the variable `result` is in the local scope of the function `my_function`, and the variable `number` is in the global scope. What happens when you try to access `number` from within `my_function`? And when you try to access `result` from the global scope? Edit the code to find out!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from math import sin, sqrt\n", "def my_function(val):\n", " #print(number) #will this work?\n", " if val > 0:\n", " result = sqrt(val)\n", " else:\n", " result = sin(val)\n", " return result\n", "\n", "number = 0.7\n", "dist = my_function(number)\n", "#print(result) #and this, will it work?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", " TASK 2 : Define and call a function computing the euclidean distance between two 2D points. The function should take four parameters (x and y coordinates of the two points). \n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# define a function called \"distance\", here\n", "\n", "dist = distance(0, 0, 1, 1) #this is a call to your function\n", "print(dist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. LISTS " ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "**lists** are sequences of elements enclosed in brackets. You can refer to an individual value following the list variable name with a bracketed number. A subset of the list can be obtained by providing an interval (first number included, last number excluded). **WARNING**: positions in list start from 0!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "my_list = [3.14, 42.0, 101.0]\n", "print(my_list[1])\n", "print(my_list[0:2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can edit value stored in any position in the list. \n", "\n", "Lists can support heterogeneous data. Try to edit the cells above to set up a list containing heterogeneous data, e.g. `[3.14, \"hello\", [101.0, 1]]`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "my_list[1] = 1.44" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "The function `len` tells you the number of items the list" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "len(my_list)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "`append` allows adding elements to a list. Any item can be appended to a list, even another list." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "my_list.append(5.2)\n", "print(my_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "my_list.append([5.2, 99.99])\n", "print(my_list)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "`extend` allows adding a list of elements to an existing list" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "my_list.extend([5.2, 99.99]) \n", "print(my_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5. STRINGS " ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Strings can be read like lists" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "hi = \"hello world!\" \n", "print(hi[1])\n", "print(hi[0:5])\n", "print(len(hi))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, strings are *not* lists. For instance, try to see what happens if you run the cell below." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "hi[1] = 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings come with lots of useful methods helping their manipulation. Check the examples below." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\"hello world\".split()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\" hello \".strip()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\"Hello WoRLd\".lower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6. SETS AND DICTIONARIES " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Sets** are an unordered collection of unique elements defined with curly braces. They cannot be indexed, items can be only added or removed. Only unique elements will be added!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "my_set = {3.14, 42.0, 101.0}\n", "my_set.add(10.0)\n", "my_set.remove(3.14)\n", "print(my_set)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**dictionaries** store couples of data, one being the key and the other the value. Keys are unique in the dictionary." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "numbers = {'one': 1, 'two': 2, 'three': 3 } \n", "print(numbers['two'])\n", "numbers['four'] = 4 # a new key and value association\n", "print(numbers)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### 7. TYPE CASTING " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In some cases, Python can figure out what type data should be at declaration. The function `type` reports on the type of a variable. In some cases, Python can figure out what type data should be at declaration." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = 1\n", "b = 3.14\n", "c = a+b\n", "print(type(a), type(b), type(c))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dedicated functions can help explicitly converting objects from a type to another" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "print(float(\"3.14\"))\n", "print(int(\"42\"))\n", "print(str(42))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Complex datatypes have their creating functions as well. Analyze what happens with the `list` function:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "print(list(\"42\"))\n", "print(list({'one': 1, 'two': 2}))\n", "print(set({'one': 1, 'two': 2}))\n", "print(set([\"4\", \"2\", \"2\"]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The *set* function creates sets, which means that some information may be lost. This is however useful if you are trying to identify unique elements in a list!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a_list = [1, 2, 4, 5, 1, 3, 1, 4, 1, 5, 3, 2, 1]\n", "a_set = set(a_list)\n", "a_list_unique = list(a_set)\n", "\n", "print(a_list)\n", "print(a_list_unique)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 8. MUTABLE AND IMMUTABLE OBJECTS " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copying a variable creates a pointer linking a variable name to a position in memory where data is stored." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "**immutable objects**: value in memory position cannot be modified. Modification actually saves the result in a new memory position. Applies to basic types (int, float, boolean, string,…).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = 1\n", "y = x\n", "x = 2\n", "print(x, y)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "**mutable objects**: Memory position can be modified. Effect: changing that memory position, affects all variables pointing to it. Applies to complex data structures (e.g. lists, dictionaries, sets…).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = [1,1]\n", "y = x\n", "x[0] = 2\n", "print(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copying the content of a mutable object into a new memory position must be done explicitly. For lists, this can be done by extracting the values stored in it. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = [1, 1]\n", "y = x[:] # shortcut for x[0:len(x)]\n", "y[1] = 2\n", "print(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `deepcopy` function, within the *copy* package, allows copying the values of any data structure in a new memory position." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from copy import deepcopy\n", "y = deepcopy(x)\n", "x[0] = 2\n", "print(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", " TASK 4 : Define a function computing the euclidean distance between two 2D points. Represent points as lists containing 2 elements (i.e. x and y coordinate).\n", " \n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# write here a function that measures the euclidean distance between two numbers\n", "\n", "#these two lists contain each the coordinate of a 2D point\n", "p1 = [0, 0]\n", "p2 = [1, 1]\n", "\n", "# call your function here, to measure the distance between p1 and p2" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### 9. LOOPS \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Loops allow repeating group of commands multiple times (iterating). There are several way of looping.\n", "\n", "A `while` loop performs the same statements over and over until some test becomes False." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "p = [42.0, 10.1, 3.14]\n", "n = 0\n", "while n < 3:\n", " print(n, p[n])\n", " n = n + 1\n", "#Warning! If n is not incremented, this loop will never end! Infinite loops are usually bad." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A `for` loop performs the same statements for each value in a list" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for n in [1, 2, 3]:\n", " print(\"This is the number\", n)\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`range` is an iterable. It creates a sequence of integers, from the first number up to but not including the second number. As an *optional* parameter, the step size can be defined (default step is 1).\n", "range does not store numbers in memory, it is just the rule that generates them: “start from 0, stop at 99999 in steps of 1”." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "range(0, 12, 3) # produce numbers from 0 to 12 excluded, in steps of 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get all numbers defined by range, use type casting: " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "list(range(0, 4))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "range is typically used in a for loop:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for i in range(10):\n", " print(i**2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", " TASK 5 : Define and call a function computing the euclidean distance between two n-dimensional points. Hint: you will need a loop! \n", " \n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# define here a function that calculated the euclidean distance between n-dimensional points\n", "\n", "# these are two 3D points\n", "p1 = [0, 0, 0]\n", "p2 = [1, 1, 1]\n", "\n", "# calculate their euclidean distance calling your function, here!\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### 10. READING AND WRITING FILES " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The format of paths in Unix-based systems and Windows differs.\n", "- for Mac/Linux `filename = \"/home/Matteo/newfolder\"`\n", "- for Windows `filename = \"C:\\\\Users\\\\Matteo\\\\newfolder\"`\n", "\n", "In Windows the usage of backslash can cause problems (the symbol \\n, for instance, means \"return to the next line\"). Using two backslashes allows you to use Windows-like paths:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "filename1 = \"C:\\Users\\Matteo\\newfolder\"\n", "print(filename1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "filename2 = \"C:\\\\Users\\\\Matteo\\\\newfolder\"\n", "print(filename2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `os` package offers operating system-dependent functionalities. For instance, it can help checking if a file exists:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "file not found!\n", "file found!\n" ] } ], "source": [ "import os as O\n", "\n", "if not O.path.isfile(\"absentfile.dat\"):\n", " print(\"file not found!\")\n", "else:\n", " print(\"file found!\") \n", " \n", "if not O.path.isfile(\"data/myfile.dat\"):\n", " print(\"file not found!\")\n", "else:\n", " print(\"file found!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "other useful functions provided by the os package:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dirname = #insert the name of a directory\n", "O.path.isdir(dirname) #check if folder exists\n", "O.chdir(dirname) #change working directory\n", "O.getcwd() #get current working directory\n", "O.path.abspath(dirname) #get absolute path" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Below there are two examples of how to **read a file** line by line. the `open` command creates a **file handle**, providing methods to manipulate a file. The handle can be in read (second parameter equal to \"r\"), write (\"w\") or append (\"a\") mode. Remember to close the file after you have finished manipulating it using the `close()` method." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.0\t2.6\n", "\n", "2.1\t3.9\n", "\n", "3.2\t5.1\n", "\n", "4.6\t6.7\n", "\n", "5.2\t7.1\n", "\n", "6.3\t8.7\n", "\n", "7.5\t9.9\n", "\n", "8.1\t10.4\n", "\n", "9.3\t11.1\n" ] } ], "source": [ "fin = open(\"data/myfile.dat\", \"r\")\n", "\n", "line = fin.readline() #put first lines in a list \n", "while line: #if not end of file\n", " print(line)\n", " line = fin.readline()\n", "fin.close() #close the file (destroy the handle)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.0\t2.6\n", "\n", "2.1\t3.9\n", "\n", "3.2\t5.1\n", "\n", "4.6\t6.7\n", "\n", "5.2\t7.1\n", "\n", "6.3\t8.7\n", "\n", "7.5\t9.9\n", "\n", "8.1\t10.4\n", "\n", "9.3\t11.1\n" ] } ], "source": [ "#There Is More Than One Way To Do It (TIMTOWDI)\n", "fin = open(\"data/myfile.dat\", \"r\")\n", "for line in fin:\n", " print(line)\n", "fin.close()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "To save what you read for later usage, create an empty list and append to it data.\n", "Note that, unless type casted, numbers are treated as characters." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fin = open(\"data/myfile.dat\", \"r\")\n", "data = [] #init an empty list\n", "for line in fin:\n", " columns = line.split()\n", " #columns is a list of strings e.g. [\"1.0\", \"2.6\"]\n", " data.append(columns)\n", "fin.close()\n", "# data is a list of lists, i.e. [[\"1.0\",\"2.6\"],[\"2.1\",\"3.9\"],...]\n", "# Try to print it!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", " TASK 6 : Open the file \"1PRE.pdb\". Parse it, and print all the lines containing atoms part of a tryptophan, residue name \"TRP\".\n", " \n", "Advanced: append lines of interest in a list, and print the list after file parsing is complete. \n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# open the file in write mode\n", "\n", "# read the file line-by-line\n", "\n", "# for each line, check whether the residue \"TRP\" is present\n", "\n", "# close the file handle" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "If file is open if **write mode** (\"w\"), strings can be inserted in it. Creation of a new line should be explicitely stated with \\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "#enter the path where to save the file\n", "fout = open(\"myfile2.dat\", \"w\")\n", "fout.write(\"hello\\n\")\n", "fout.close()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "data can be formatted into a string using *formatting characters* such as %s (generic string) %5.2f (floating point, 5 characters of which 2 digits after comma), %-8.2f (floating point with left alignment),…" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 12.345\n", "print(\"%s %4.2f %3.1f\"%(a, a, a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", " TASK 6 : Parse \"1PRE.pdb\" and print in an output file the x, y and z coordinates of all atom belonging to a tryptophan.\n", " \n", "Advanced: make sure that columns are nicely aligned. \n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# upgrade the code from exercise 6 here!" ] } ], "metadata": { "anaconda-cloud": {}, "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.8.10" } }, "nbformat": 4, "nbformat_minor": 1 }