Python 101

Input and Output

Diogo Silva


Input and ouput channels

There are three input/output channels that allow the program to interact with the environments and its users:


Input from user/keyboard

Python has two built-in functions for reading data provided by the user via the keyboard:

sequence = raw_input("Please provide a DNA sequence:\n>")
print sequence

  • input([prompt]): this function is similar to raw_input(), except that it assumes the input is a valid python expression and returns the evaluated result to you. It can interpret if you are providing a string or a number, by using quotations marks or not.
  • sequence = input ("Please provide a DNA sequence:\n>")
    n_loci = input ("Please provide the number of loci:\n>")
    

    Printing the output on the terminal

    
    
    
    
    
    
    
    

    Dealing with files

    Open and Create file objects

    
    
    

    Pay special attention that:


    Dealing with files

    Methods for reading file objects

    
    
    
    
    
    
    
    

    Dealing with files

    Methods for reading file objects

    
    
    

    This is also much faster and memory efficient than assigning the whole content of a file to a variable because only one line is actually stored in memory in each loop iteration


    Dealing with files

    Writing to files

    
    
    
    
    
    
    
    

    Closing files

    A file is automatically closed when the program ends. However, if you are done with a file, you can close it and free up any system resources with the close() method

    /

    #