Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro Python: Variables, Indexing, Numbers, Strings.

Similar presentations


Presentation on theme: "Intro Python: Variables, Indexing, Numbers, Strings."— Presentation transcript:

1 Intro Python: Variables, Indexing, Numbers, Strings

2   Working with python and python objects  Numbers  Strings  Variables  Indexing  Building a python script Agenda

3  Review of shell commands Commands macCommands PCWhat they do cd Change directory pwdchdirPrint working directory a.k.a the folder I am currently in on the terminal lsdirList contents of directory mkdir Make directory echo Write arguments to standard output cattypePrint files >>Redirect standard input to file cpcopyCopy file to new location

4   Type and press enter:  python  How does the prompt change?  You are now working within the Python Shell or python intepretor – this is an interactive session and you know this by the prompt >>>  Type and press enter:  quit()  You have now left the python shell – how does the prompt change? Python – into the world of programming!

5   Numbers  Strings  Lists  Tuples  Dictionaries Python Objects

6   The interpreter will act as a calculator and the expression syntax is nearly identical to using a calculator (spaces aren’t necessary but make the code prettier)  type and press enter:  python  2 + 2  (50 - 5.0)*6 / 4  8/5  8/5.0  Notice 5 vs 5.0  Integer (i.e. 5) and float (5.0) are two types of numbers – to retrieve decimals use floats Numbers

7   Strings = words or numbers, all will be read like words in python if surrounded by quotes  Quotes can be “” or ‘’  Type and press enter (don’t copy/paste from slides or if you do don’t include the powerpoint quotes):  ‘CTGACT’  Notice the output  ‘5’  5  Notice the difference between the output for ‘5’ and 5 “Strings”

8   Type and press enter:  word = ‘python’  phrase = ‘ is awesome’  Notice that there is no output  word  Notice the output ‘python’ is returned, therefore the variable word stores ‘python’ in memory  number = 5  Functions can act on variables. Type and press enter:  type(word)  type(number)  What object type is the variable word?  What object type is number?  Depending on the object math or string concatenation can occur. Type and press enter:  word + phrase  number + number  Take Home message: variables store objects of different types, to retrieve the object, type the name of the variable and press enter Variables – a way to refer to objects (just like math class)

9   Variables can have any name you want to give them, but you should give them a name that is meaningful so you can figure out what they are later.  Variable names can’t include whitespace and can’t begin with a number. Naming Variables

10   You want to load 30.0 ug of protein for a Western and your protein concentration is 2.0 ug/ul. How many ul will you need? 1. Create the variable protein and assign 30.0 and create the variable concentration and assign 2.0  i.e. protein = 30.0 (first one is done for you)  Create the variable answer and assign the arithmetic to this variable – what is the answer? (print the variable)  i.e. answer =  Given the phrase The answer to life, the universe, and everything = and the number 42, assign each to a variable. Using these variables create the phrase “The answer to life, the universe, and everything = 42” as output  1. phrase1 =  2. phrase2 =  3. wholephrase =  # hint assign each variable the same object type and by this I mean all things should be strings in this case Your Turn

11   1)  Protein = 30.0  Concentration = 2.0  Answer = protein/concentration  Answer Answer to the problems

12   Indexing by position can be used with strings or lists (lists - another object we will cover later)  Can access individual pieces of a string with indexing  Type and press enter:  word  word[0]  word[5]  word[-1] Indexing

13  ‘python’ 012345 -6-5-4-3-2 variable[_:_:_] To index: Start position End position increments

14   Indexing starts at zero goes 1 past the end  Type and press enter:  word[0:6]  word[6] #this will not access the last element an error message will be the output  If you leave the start or end position blank, python will assume you want to start at the beginning and end at the end. If you leave the increments blank, python defaults to increments of 1. Type and press enter:  word[:6]  word[0:6:2]  word[::-1] Indexing

15   Use indexing to output ‘atcg’ from ‘actgcag’  Remember to assign ‘actgcag’ to a variable first Your Turn

16   Type and hit enter:  dir(word)  dir function outputs the methods that can be used with a particular object – in this case string object  python uses the. notation for methods that act on objects; syntax is:  object.method()  Synonymous to methods are functions which also act on objects:  function(object)  It is easier to google the syntax for function or method than to bother with dir() Methods and Functions for Strings

17   Type and press enter:  len(word)  word.split(‘t’)  word.find(‘t’)  word.count(‘t’)  Another way to put numbers within strings, type and press enter:  ‘The answer to life the universe and everything is {0} ‘.format(6*7)  To exit python, type and press enter:  quit() Useful methods for strings

18   Open your text editor (TextWrangler or Notepad ++) and type the following lines: print 'This is my first python script’ math_answer = str(1+1) print 'I can do math. 1+1=' + math_answer print 'I can do math. 1+1={0}'.format(1+1) Building python script

19   Once you’re finished put each line into python interpreter and look at the output  Note that str() function will change number objects into strings  Create a directory within coding_club named python_scripts using the command-line (exit out of python before you do this)  Save your python script as first_python_script.py to directory python_scripts  Notice the extension on the first_python_script.py  To run your script you can do either:  python /python_scripts/first_python_script.py  Using relative paths, change into the directory where python_script.py is located and type python first_python_script.py Building python script

20   You have sequenced a vector that you found in the lab that supposedly contains a short hairpin sequence of interest to you. All of your nifty cloning software has broken down (as well as any online freeware tools-yes folks the world has come to an end) and you have to use python to figure out whether your sequence of interest is in the vector and at what position your sequence is in. Your Assignment

21   Vector sequence:  NNNNNNNNNNNNNNNNNNNNNGGCTGTGGCNAGTACTGCGACCTCC TAGCAAACTGGGGCACAGATAATCGATAGTTTGTTTGAATGAGGCTTCA GTACTTTACAGAATCGTTGCCTGCACATCTTGGANCACTTGCTGGGATTA CTTCTTCAGGTTAACCCAACAGAAGGCTCGAGAAGGTATATTGCTGTTG ACAGTGAGCGCGTAGTGTGATGTGTCTGAACTTAGTGAAGCCNNNNNA TGTAANNTTCAGACACATCACACTACATGCCTACTGNCTCGGAATTCAA GGGGCTACTTTANGGNNNCAATTATNNTTGTTNNNNNAAAANNTGAA NANCNTTGNNNNTNNNCTTTGNNANNNTTTTNNNNANNGCNNNNNN NNAAANNGGGNANAAANTNAANNNNNNTTTTTTTCANNNGNANNAN NNANNNNGNCNNGNNNNNNNNNCNNNNNNNNNGNNTNGNNGNN NNNNGNNNNNNCNNNNNNTNANNNNNNNNNNNGNNNNNANNNN NNNNNNNTNNNNNNNNNNNNNNNNNNNNNTNNNNNNCCNNGGN NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN NNNGNNNNNNNNNNNNNNNNNNNANNNNNNNCNNNNNNNNNNT NNNNNNNNNNNNNNNNNTNNNNNNNNNNNNNNNNNNNNNNNN NGANNNNNGNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN NNNNNNN  Short hairpin: GTAGTGTGATGTGTCTGAACT Your assignment: Sequences

22   Build a python script named sequence_locator.py saved to python_scripts folder that:  1. assigns the vector and short-hairpin sequences to variables – use these variables from here on  2. counts the number of occurrences of the short-hairpin sequence within the vector sequence  3.finds the start position of the short-hairpin sequence within the vector sequence  4. finds the end position of the short-hairpin sequence within the vector sequence (this will probably require two steps and some math )  5. Uses the start position and end position of the short-hairpin to find the sequence within the vector that supposedly matches  Hint: run each step interactively until you can get it to work. Save things that work to your script  Hint: you will want to assign each steps output to a variable Your Assignment

23  Python Script output


Download ppt "Intro Python: Variables, Indexing, Numbers, Strings."

Similar presentations


Ads by Google