36
Introduction to Python 1 February 8, 2016 Xi Wang Yang Zhang

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

Embed Size (px)

DESCRIPTION

 Installing Python  https://www.python.org/ https://www.python.org/  Online Learning resources  How to Think Like a Computer Scientist  sh2e/ sh2e/  Code Academy  https://www.codecademy.com/tracks/python https://www.codecademy.com/tracks/python  Edx: Introduction to Computer Science and Programming Using Python  https://www.edx.org/ https://www.edx.org/

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Thank you!