Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Python Programming – Part II

Similar presentations


Presentation on theme: "Intro to Python Programming – Part II"— Presentation transcript:

1 Intro to Python Programming – Part II
Hydroinformatics October 2016 Dr. Dan Ames Brigham Young University

2 Other Python Editors…

3 The IDLE GUI (Windows) 3

4 ArcMap Command Line 4

5 Mac OSX Terminal 5

6 Codecademy Labs 6

7 Python LISTS >>> x=['a','b','c'] >>> x[1] 'b'
#why isn’t it ‘a’? >>> x[1:] ['b', 'c'] >>> x[1]='d' >>> x ['a', 'd', 'c'] >>> x[1]=99 ['a', 99, 'c'] #Is that weird? >>> y = range(10) >>> y [0,1,2,3,4,5,6,7,8,9] 7

8 Python LISTS You Try it! Write an expression to create a list of the months of the year and extract the 3rd month. Put your answer here: 8

9 Python FUNCTIONS >>> import math #Load the math library
>>> def pythag(a,b): ... return math.sqrt(a**2 + b**2) ... >>> pythag(3,4) 5.0 >>> pythag(5,12) 13.0 >>> pythag(pythag(3,4),12) #How do you pass params into the function? #How do you return results from a function? 9

10 Python FUNCTIONS You Try it!
Write a function to return the volume of any arbitrary cylinder given the diameter and height. Put your answer here: 10

11 Using a script file Start PyCharm, Create a new file, Paste your code into the file Save the file, Run the script using the “Run” menu Drop down to select your script Type your script Run your script Output is displayed here

12 Using a script file Try a simple string concatenation…

13 Using a script file Now let’s use a couple of loops…

14 Using a script file Now try an if-then statement…
x = # A comment. y = “Hello” # Another one. z = 3.45 if z == 3.45 or y == “Hello”: x = x + 1 y = y + “World” # String concat. print x print y

15 Three useful modules What is a module? Like an “extension” or “plugin”
Adds additional functionality to Python, not in the core environment. In the interpreter type, help("modules") to get a list of all currently installed modules. Use a module in your code, first import it: import os import shutil import math

16 Python Modules 50 Top Python Modules (from www.CatsWhoCode.com)
Think Legos…

17 Here’s how to see what modules you currently have loaded…

18 Four useful modules os: functions for working with your operating system including file management shutil: high level functions for working with files MatPlotLib: make graphs, charts, and maps pymysql: functions for accessing and working with data in an SQL database

19 Module: OS Let’s look at the module, “os”
Think of it as a connector into your current operating system Use it to do useful file level activities os.getcwd() returns the current working directory os.curdir returns the current directory os.execl executes an executable file os.abort fails in the hardest way possible os.listdir returns a list of strings of names of entries in a folder os.path.exists() tests whether a path exists os.path.basename() returns the final component of a path name os.path.isdir() returns true for a directory, false for a filename

20 MODULE: shutil Let’s look at the module, “shutil”
Think of it as a high level file/folder manipulator shutil.copy, copyfile, copytree copy things shutil.abspath returns the absolute path to a file Shutil.fnmatch test if a file path matches a pattern shutil.move move a file or folder to another destination shutil.rmtree recursively remove the contents of a directory tree

21 MODULE: MatPlotLib Let’s look at the module, “MatPlotLib”
Makes (pretty) pictures MatPlotLib.pyplot.plot Plot a graph of data. MatPlotLib.pyplot.show Show the user your graph. Tutorial here: Gallery here:

22 Python Challenge #1 Work in teams of two to write a solution and post it in our google doc here: Ask the user for a number. Depending on whether the number is even or odd, print out an appropriate message to the user.

23 Python Challenge #2 Work in teams of two to write a solution and post it in our google doc here: Write a program that asks the user how many Fibonnaci numbers to generate and then generates them. Take this opportunity to think about how you can use functions. Make sure to ask the user to enter the number of numbers in the sequence to generate.

24 Python Challenge #3 Work in teams of two to write a solution and post it in our google doc here: Ask the user for a string and print out whether this string is a palindrome or not. (A palindrome is a string that reads the same forwards and backwards.)

25 Python Challenge #4 Work in teams of two to write a solution and post it in our google doc here: Use this list: a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] Write a function that takes a number as a parameter and returns a list with all values from the original list that are less than or equal to the input value.

26 Python Challenge #5 Work in teams of two to write a solution and post it in our google doc here: Use these two lists: a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] Write a function that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes. Function should look like this: CommonList = findCommon(list1,list2)

27 Python Challenge #6 Work in teams of two to write a solution and post it in our google doc here: Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. Keep the game going until the user types “exit” Keep track of how many guesses the user has taken, and when the game ends, print this out.

28


Download ppt "Intro to Python Programming – Part II"

Similar presentations


Ads by Google