Intro Python: Variables, Indexing, Numbers, Strings.

Slides:



Advertisements
Similar presentations
Introduction to Python
Advertisements

COMP234 Perl Printing Special Quotes File Handling.
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
Guide To UNIX Using Linux Third Edition
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Python programs How can I run a program? Input and output.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Python Programming Fundamentals
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
Introduction to Python
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 4: Writing programs.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Sed. Class Issues vSphere Issues – root only until lab 3.
Matlab for Engineers Matlab Environment Chapter 2.
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Foundations of Programming: Java
Fundamentals of Programming I Overview of Programming
Development Environment
Python Lesson 12 Mr. Kalmes.
Introduction to Programming
Prepared by: Eng. Maryam Adel Abdel-Hady
SQL and SQL*Plus Interaction
Variables, Expressions, and IO
Shell Script Assignment 1.
CompSci 230 Software Construction
CSE 303 Concepts and Tools for Software Development
Intro to PHP & Variables
Engineering Innovation Center
Topics Introduction to File Input and Output
Number and String Operations
File IO and Strings CIS 40 – Introduction to Programming in Python
Learning Outcomes –Lesson 4
Lecture 2 Introduction to MATLAB
Lesson 2: Input and Variables
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Topics Designing a Program Input, Processing, and Output
Fundamentals of Python: First Programs
Reference semantics, variables and names
Stata Basic Course Lab 2.
Topics Introduction to File Input and Output
Beginning Python Programming
Chapter 5 Bourne Shells Scripts
Topics Introduction to File Input and Output
Class code for pythonroom.com cchsp2cs
The Python interpreter
Introduction to Python
Python Creating a calculator.
Presentation transcript:

Intro Python: Variables, Indexing, Numbers, Strings

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

 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

  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!

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

  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   ( )*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

  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”

  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)

  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

  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

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

  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

 ‘python’ variable[_:_:_] To index: Start position End position increments

  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

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

  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

  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

  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

  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

  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

  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

  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

 Python Script output