Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages,...
-
Upload
sharon-manning -
Category
Documents
-
view
216 -
download
0
description
Transcript of Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages,...
![Page 1: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/1.jpg)
Introduction to Python 1February 8, 2016
Xi WangYang Zhang
![Page 2: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/2.jpg)
Advantages of Python
1. Easy to learn2. Clean and readable codes3. A lot of useful packages, especially for web
scraping and text mining4. Growing popularity
![Page 3: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/3.jpg)
Intro to Python
Installing Python https://www.python.org/
Online Learning resources How to Think Like a Computer Scientist
http://www.openbookproject.net/thinkcs/python/english2e/
Code Academy https://www.codecademy.com/tracks/python
Edx: Introduction to Computer Science and Programming Using Python https://www.edx.org/
![Page 4: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/4.jpg)
Running Python Program
1. Python Shell Windows: command prompt Mac: terminal
2. Script
3. Integrated Development Environment (IDE)
![Page 5: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/5.jpg)
Running Python Program in Command Prompt
![Page 6: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/6.jpg)
Running Python Program Using a Script
1. Write a program in a text editor and save it as a .py file.
![Page 7: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/7.jpg)
Running Python Program Using a Script
2. Run the script in a Python shell.
![Page 8: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/8.jpg)
Running Python Programin Mac Terminal
![Page 9: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/9.jpg)
Running Python Programin an IDE
An IDE normally consists of a source code editor, build automation, and a debugger.
Python IDEs https://en.wikipedia.org/wiki/Comparison_of_integrated_development_environments
Example: Enthought Canopy
![Page 10: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/10.jpg)
Running Python Programin an IDE
![Page 11: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/11.jpg)
Syntax Basics
1. Variables2. Functions3. Conditionals4. Iteration
Slides and example codes can be downloaded from http://yang-zhang.weebly.com/teaching.html
![Page 12: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/12.jpg)
Variables
Everything in a Python program belongs to a data type:
>>> type(5)<type ‘int’>>>> type(1.23)<type ‘float’>
![Page 13: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/13.jpg)
Variables
>>> type(“Hello, World!”)<type ‘str’>>>> type(‘Hello, Iowa!’)<type ‘str’>>>> type(‘5’)<type ‘str’>
>>> type(True)<type ‘bool’>>>> type(False)<type ‘bool’>
![Page 14: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/14.jpg)
Variables
Data types can be changed:>>> float(5)5.0>>> str(5)‘5’>>> int(1.9)1>>> int(‘5’)5>>> int(True)1
![Page 15: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/15.jpg)
Variables
A variable is a name that refers to a value.
The assignment statement creates new variables and gives them values:
>>> message = “What’s up?”>>> n = 18>>> pi = 3.14
![Page 16: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/16.jpg)
Variables
Print statement shows the content of a variable:
>>> print messageWhat’s up?>>> print n18>>> print pi3.14
![Page 17: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/17.jpg)
Variables
Variables can be changed:>>> count = 10>>> count = count – 1>>> print count9
>>> count -= 1>>> print count8
![Page 18: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/18.jpg)
Variables
Python is a calculator:>>> x = 10>>> y = 2>>> z1 = 3>>> z2 = 3.0
![Page 19: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/19.jpg)
Variables
>>> x / y5>>> x / z13>>> x /z23.3333333333333335
![Page 20: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/20.jpg)
Functions
A function is a named sequence of statements that performs a desired operation.
Python has many useful built-in functions. For example:
>>> type(5)Int>>> abs(-10)10>>> max(1,2,3)3
![Page 21: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/21.jpg)
Functions
In Python, the syntax for a function definition is:
def NAME(PARAMETERS):STATEMENTSBe cautious with indentation!
![Page 22: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/22.jpg)
Functions
For example:>>> def myMean(a,b):
return (a+b)/2
>>> myMean(3,5)4
![Page 23: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/23.jpg)
Functions
Default parameter values>>> def discArea(r, pi=3):
return pi*(r**2)
>>> discArea(2)12>>> discArea(2, 3.14)12.56>>> discArea(r=2, pi=3.14)12.56
![Page 24: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/24.jpg)
Functions
A module is a neatly packaged set of functions. Some come with Python, others need to be installed.
For example, to know your current working directory:
>>> import os>>> os.getcwd()'C:\\Users\\Yang'
>>> from os import getcwd>>> getcwd()'C:\\Users\\Yang'
![Page 25: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/25.jpg)
Conditionals
There are only two Boolean values. True False
A Boolean expression returns a Boolean value.>>> 5 == 5True>>> 5 == 6False
![Page 26: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/26.jpg)
Conditionals
x = 5y = 6
>>> x == yFalse>>> x != yTrue>>> x > yFalse>>> x < yTrue
What will happen if we type x = y?
![Page 27: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/27.jpg)
Conditionals
Logical operators connect multiple Boolean expressions.
>>> 3 > 0 and 3 < 5True>>> 3 < 2 or 3 < 1False>>> not(5 > 6)True
![Page 28: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/28.jpg)
Conditionals Conditional statements take a form like this:if BOOLEAN EXPRESSION:
STATEMENTS>>> x = 5>>> if x > 0: print 'x is positive.'x is positive.
![Page 29: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/29.jpg)
Conditionals
Alternative Execution>>> def isEven(x):
if x % 2 == 0: return True else: return False
>>> isEven(5)False>>> isEven(6)True
![Page 30: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/30.jpg)
Conditionals
Conditionals can be chained:>>> x = 5>>> y = 6
>>> if x < y: print x, "is less than", y elif x > y: print x, "is greater than", y else: print x, "and", y, "are equal"5 is less than 6
![Page 31: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/31.jpg)
Conditionals
Conditionals can be nested:>>> if x == y: print x, "and", y, "are equal" else: if x < y: print x, "is less than", y else:
print x, "is greater than", y5 is less than 6
![Page 32: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/32.jpg)
Iteration
Iteration is repeated execution of a set of statements.1. The while statement2. The for statement
![Page 33: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/33.jpg)
Iteration
The while statement>>> def countdown(n): while n > 0: print n n = n-1 print "Blastoff!">>> countdown(3)321Blastoff!
![Page 34: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/34.jpg)
Iteration
The for statement>>> for x in 'Iowa': print xIowa
![Page 35: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/35.jpg)
ISRC Workshops
Introduction to Python Introduction to R Stata Programming Introduction to ArcGIS Multilevel Modeling in R and Stata
http://ppc.uiowa.edu/node/3608
![Page 36: Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.](https://reader035.fdocuments.in/reader035/viewer/2022062413/5a4d1b797f8b9ab0599b84c5/html5/thumbnails/36.jpg)
Thank you!