Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Python: Day one

Similar presentations


Presentation on theme: "Introduction to Python: Day one"— Presentation transcript:

1 Introduction to Python: Day one
Stephanie J Spielman, phd Big data in biology summer school, 2018 Center for computational biology and bioinformatics University of Texas at austin

2 Topics we’ll cover: Day One, May 21st Day Two, May 22nd
Basic operations Python data structures, Part 1 Control flow, Part 1 Day Two, May 22nd Control flow, Part 2 Python data structures, Part 2 Functions (possibly day 3) Day Three, May 23rd File input/output Practice! Day Four, May 24th Python modules and "best practices" Regular expressions and file manipulation Biopython (time permitting)

3 why learn computer programming?
Speed Automation Repeatability

4 why learn python? Higher-level language with extensive functionality
Well-documented Widely-used Very readable and user-friendly Excellent for handling text and files The main drawback is speed

5 computers are stupid No, really.

6 Let’s begin with unix UNIX is a computer operating system
Mac and Linux are built on UNIX, but PCs are not 

7 Let’s begin with unix UNIX is a computer operating system
Mac and Linux are built on UNIX, but PCs are not  We interact with this system using a shell. The shell language is accessed in the terminal aka command line aka console

8 navigating Unix systems
Files and directories in UNIX systems are organized hierarchically

9 navigating Unix systems
Every file/directory has a specific address, or path, in the hierarchy

10 navigating Unix systems
Every file/directory has a specific address, or path, in the hierarchy Levels are separated by slashes: /

11 navigating Unix systems
Every file/directory has a specific address, or path, in the hierarchy A Levels are separated by slashes: / B C The path from A to C is B/C

12 navigating Unix systems
Every file/directory has a specific address, or path, in the hierarchy A Move up the hierarchy with .. Levels are separated by slashes: / B C

13 navigating Unix systems
Every file/directory has a specific address, or path, in the hierarchy A Move up the hierarchy with .. Levels are separated by slashes: / B C The path from C to A is ../..

14 navigating Unix systems
The top-level directory is called root, and is denoted with single slash / Generally, this is where secret important things happen. If you don’t understand computers well, don’t mess around in here!

15 navigating Unix systems
Your home directory is where you live ~ Desktop Documents Downloads

16 navigating Unix systems
Your home directory is where you live ~ Desktop Documents Downloads The full path to my home directory is /Users/sjspielman/

17 navigating Unix systems
We move around in our file system with the command cd (change directory) Absolute, or full, path is the path from the root Relative path is the path from the working directory (i.e., where you are) DEMONSTRATE CD HERE!!! INCLUDE ~, .., . cd on its own. relative path vs absolute path demo.

18 basic unix commands All UNIX commands are actually little computer programs UNIX Command What it does cd Change Directory pwd Print Working Directory (gives the full path) ls List (contents of a directory mv Move a file or directory (original not retained!) cp Copy a file or directory (original is retained) rm Remove a file or directory *forever* (NOT A TRASHCAN) mkdir Make a new directory man Manual (shows documentation for a given command)

19 Find your console! On a Mac or Linux? Using pythonanywhere.com ?
Launch a terminal session and type python2 or python3 Open your text editor (i.e. BBEdit) Using pythonanywhere.com ? Open your browser and login to the home page

20 enter, python! Python is an interpreted language
We can code either using the interpreter directly or using scripts (text files with python code) Python is an object-oriented language Each variable is an object with a name, value, and type The type determines what you can do with the variable

21 critical psa: text editors
Microsoft Word is not a text editor!!!!!!! I’m so serious!!! GUI TextEdit and Notepad (NotePad++ should be ok) Textwrangler/BBEdit for Macs Sublime 3 for any system Atom for the dedicated and deeply interested CLI Vim/vi, emacs, nano, pico (b/c puns)

22 Integers and floats # Define some integer variables a = 5 b = -33
c = 0 Integers are *counting numbers* # Define some float variables d = 5.67 e = -33.2 f = 0. Floats have *decimals*

23 Integers and floats # Define some integer variables a = 5 b = -33
c = 0 # Define some float variables d = 5.67 e = -33.2 f = 0. The name of this variable is e The value of this variable is -33.2 The type of this variable is float

24 Integers and floats # Define some integer variables a = 5 b = -33
c = 0 Comments are denoted with hashtags # Define some float variables d = 5.67 e = -33.2 f = 0. # This half of line is ignored!

25 Mathematical operations
+ - * / % **

26 Mathematical operations
# Define variables and math them a = 5 b = -33 c = a + b #c now has a value -28 What type of variable is c? a = 5.0 b = -33 c = a + b #c now has a value -28.0 print(c) -28 What type of variable is c?

27 Use the print function to check your code
# Use print statements to see what your computer is doing a = 5 b = 2 c = a / b print(c) 2.5 Always be printing! Without print(), there is NO WAY to know if your code is working as expected.

28 oh, the ways you’ll print*
# Print a single value a = 5 print(a) 5 # Print several values b = 6 print(a, b) 5 6 print(a, "is a number.", b, "is also a number.") 5 is a number. 6 is also a number. print(a, b, sep="!!!!!") 5!!!!!6 print(a, "is a number.", b, "is also a number.") , *In Python3

29 Python2 vs python3 # Python3 print(5 / 7) 0.7142857142857143 # Python2

30 exercise break

31 Modifying the value in place
Mathematical symbols followed by an equals sign will change the variable value in place +=, -=, *=, /= # Multiply by 8 b = 2.5 b *= 8 print(b) 20.0 # Increment by 5 a = 77 a += 5 print(a) 82

32 logical operators Evaluate a condition as True or False using logical operators Variables with True/False values are of a type called boolean Operator Meaning == Equal to != Not equal to >, < Greater than; less than >=, <= Greater than or equal to ; less than or equal to is, is not Use for comparing boolean

33 performing logical comparisons
b = 120 c = -8.34 print(b > a) outcome = a == 6 True print(outcome is True) print(a == 6) x = None print(x is None) print(7 != c) print( (a+b) <= c) False

34 combining logical statements
Use the Python operators and and or to combine logical statements and: both conditions must be True or: only one condition must be True

35 combining logical statements
print(a == 6 and b == 120) True print(a == 6 or b == 92) print(b < 10 or a > 55) False print(b != 7 and c <= 11) print(c == and a == b)

36 program control flow with if statements

37 program control flow with if statements
... ... Python code if logical expression : ... Code run if True

38 program control flow with if statements
... ... Python code if logical expression : ... Code run if True

39 program control flow with if statements
b = 5 if a > b: print("a is bigger") a is bigger

40 if-else statements ... ... Python code if logical expression :
... Code run if True else: ... Code run if False

41 if-else statements a = 7 b = 5 if a > b: print("a is bigger") else:
print("a is not bigger") a is bigger

42 if-elif-else statements
... ... Python code if logical expression : ... Code run if True elif other logical expr : else: ... Code run if *all* False

43 if-elif-else statements
b = 5 if a > b: print("a is bigger") elif a < b: print("b is bigger") else: print("a is equal to b") a is bigger

44 if-elif-else statements
No need to end with else You can have as many elif as you want a = 7 b = 5 if a > b: print("a is bigger") elif a < b: print("b is bigger") elif a==b: print("a is equal to b") a is bigger

45 exercise break

46 python container variables
Sounds scarier than it is! String List Dictionary (tomorrow)

47 python strings Strings are denoted with quotes and contain characters
Strings are immutable # Define some strings s1 = "Stephanie" s2 = 'Another string' s3 = "534" # This is not an integer! i1 = int(s3) # But we can convert it to one

48 Fun with strings name() is a function <object>.name() is a method s1 = "python" len(s1) ### Length of a container variable 6 s1.count("p") ## Count occurrences of a character 1 s1.replace("o", "BLAH") ## Replace arg1 with arg2 'pythBLAHn' print(s1) ## .replace() did NOT change the string! 'python' The len() function takes a single argument

49 Fun with strings s1 = "python" s1.upper() ## Reveal uppercase string
s1.lower() ## Reveal lowercase string 'python' s1.capitalize() ## Reveal capitalized string 'Python' ## Redefine string to modify s1 = s1.capitalize() print(s1)

50 concatenating strings
s1 = "python" s2 = "is so cool" ## Use the + sign to add strings combined = s1 + s2 print(combined) "pythonis so cool" ## with some spaces! combined = s1 + " " + s2

51 Python lists Lists are defined with brackets: [ ] # Define some lists
c = [1, 3.1, -5, 7, 9.001, "woah", "dude"] d = [ [1, 2, 3], [11, 22, 33], [7.55, -9] ] What do you notice about the variable types that comprise the items of these lists?

52 indexing in python d = [1, 3, 5, 7, 9, 11, 13]
 Indexing starts from 0! # Index the second entry in d using brackets [] print(d[1]) 3 # Index a slice of the list with [x:y] print(d[1:4]) [3, 5, 7] # x and y defaults are 0 and "last index" print(d[3:]) # assumes go through end of list [7, 9, 11, 13] print(d[:5]) # assumes start at beginning of list [1, 3, 5, 7, 9] In [x:y], x is inclusive and y is exclusive

53 indexing in python d = [1, 3, 5, 7, 9, 11, 13]
 "Regular" indexing  Negative indexing # Index the last entry in d using brackets [] print(d[-1]) 13 # Index the last 2 entries in d using brackets [] print(d[-2:]) [11, 13] # Strings are indexed in the same way as lists s = "Washington D.C." print(s[1]) a

54 Fun with lists mylist = [10, 11, 6, 9, 2, 19, 5, 14]
len(mylist) ### Length of a container variable 8 sum(mylist) ### Add up all items 76 mylist.count(10) ### Count occurrences of an item 1 mylist.index(6) ### Return the index of a value in the list mylist.append(1000) ### Add item in place to end of list print(mylist) [10, 11, 6, 9, 2, 19, 5, 14, 1000]

55 Lists are mutable and strings are immutable
# Use indexing to replace entries in a a = [-8, -5, -3, 0, 1] a[2] = 77 print(a) [-8, -5, 77, 0, 1] # But not with strings!! a = "I will never ever change." a[2] = "A" TypeError: 'str' object does not support item assignment # Instead you must reassign, to itself or a new variable new_a = a[:2] + "A" + a[3:] print(new_a) 'I Aill never ever change.'

56 The in operator in returns True/False if a character/item is in a container (list, string) mylist = [10, 11, 6, 9, 2, 19, 5, 14] print(5 in mylist) True print("11" in mylist) False print("11" not in mylist)

57 in… in action mylist = [10, 11, 6, 9, 2, 19, 5, 14] if 10 in mylist:
print("Yes 10 is there!") else: print("Nope, sorry") Yes 10 is there!

58 exercise break


Download ppt "Introduction to Python: Day one"

Similar presentations


Ads by Google