Intro to Python Programming – Part II

Slides:



Advertisements
Similar presentations
Scripts and Flow Control. Scripts So far we have been entering commands directly into the command line But there is a better way Script files (and functions)
Advertisements

Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Python Programming Fundamentals
An Introduction to Textual Programming
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
 We are going to learn about programming in general…How to think logically and problem solve. The programming language we will use is Python. This is.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Intro Python: Variables, Indexing, Numbers, Strings.
Intro to Python Adriane Huber Debbie Bartlett Python Lab #1Python Lab #1 1.
Lesson 4 Using Variables in Python – Creating a Simple ChatBot Program.
Chapter 1 : The Linux System Part 2 Lecture 2 11/14/
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
Decision Structures, String Comparison, Nested Structures
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.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
PROBLEM SOLVING WARM-UP Fill in the spaces using any operation to solve the following (!, (), -/+,÷,×): = 6.
Web Database Programming Using PHP
Development Environment
CST 1101 Problem Solving Using Computers
Whatcha doin'? Aims: To start using Python. To understand loops.
Python’s Modules Noah Black.
Intro to Python Programming – Part III
A Playful Introduction to Programming by Jason R. Briggs
Strings, conditional statements, Math
Introduction to Python
Introduction to Programming
Data Virtualization Tutorial: Introduction to SQL Script
USING PYTHON to Automate data management tasks
Web Database Programming Using PHP
Matlab Training Session 4: Control, Flow and Functions
Topics Introduction to Repetition Structures
PYTHON: AN INTRODUCTION
Introduction to Python
Introduction to Programming
PHP Introduction.
Engineering Innovation Center
1 Python Lab #1 Intro to Python Adriane Huber Debbie Bartlett.
LING 408/508: Computational Techniques for Linguists
Operation System Program 4
Learning to Program in Python
Learning to Program in Python
Use of Mathematics using Technology (Maltlab)
Python Lessons 13 & 14 Mr. Kalmes.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Learning to Program in Python
Introduction to TouchDevelop
Web DB Programming: PHP
Python programming exercise
CS288 Lab Exercise 2.
Intro to PHP.
CS 1111 Introduction to Programming Spring 2019
Flowcharts and Pseudo Code
Programming In Lesson 4.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Introduction to Python
Lab 4: Introduction to Scripting
Python Modules.
Input and Output Python3 Beginner #3.
General Computer Science for Engineers CISC 106 Lecture 03
Starter Activities GCSE Python.
Numpy, pylab, matplotlib (follow-up)
L L Line CSE 420 Computer Games Lecture #3 Introduction to Python.
The Python interpreter
Presentation transcript:

Intro to Python Programming – Part II Hydroinformatics October 2016 Dr. Dan Ames Brigham Young University dan.ames@byu.edu

Other Python Editors… http://pythonfiddle.com/

The IDLE GUI (Windows) 3

ArcMap Command Line 4

Mac OSX Terminal 5

Codecademy Labs 6

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

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: https://goo.gl/aznZt4 8

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

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: https://goo.gl/aznZt4 10

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

Using a script file Try a simple string concatenation…

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

Using a script file Now try an if-then statement… x = 34 - 23 # 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

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

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

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

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

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

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

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: http://matplotlib.org/1.3.1/users/pyplot_tutorial.html Gallery here: http://matplotlib.org/1.3.1/gallery.html

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

Python Challenge #2 Work in teams of two to write a solution and post it in our google doc here: https://goo.gl/aznZt4 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.

Python Challenge #3 Work in teams of two to write a solution and post it in our google doc here: https://goo.gl/aznZt4 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.) http://www.practicepython.org/solution/2014/03/19/06-string-lists-solutions.html

Python Challenge #4 Work in teams of two to write a solution and post it in our google doc here: https://goo.gl/aznZt4 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.

Python Challenge #5 Work in teams of two to write a solution and post it in our google doc here: https://goo.gl/aznZt4 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)

Python Challenge #6 Work in teams of two to write a solution and post it in our google doc here: https://goo.gl/aznZt4 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. http://www.practicepython.org/solution/2014/04/10/09-guessing-game-one-solutions.html