View
216
Download
0
Category
Preview:
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
Introduction to Python 1February 8, 2016
Xi WangYang Zhang
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
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/
Running Python Program
1. Python Shell Windows: command prompt Mac: terminal
2. Script
3. Integrated Development Environment (IDE)
Running Python Program in Command Prompt
Running Python Program Using a Script
1. Write a program in a text editor and save it as a .py file.
Running Python Program Using a Script
2. Run the script in a Python shell.
Running Python Programin Mac Terminal
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
Running Python Programin an IDE
Syntax Basics
1. Variables2. Functions3. Conditionals4. Iteration
Slides and example codes can be downloaded from http://yang-zhang.weebly.com/teaching.html
Variables
Everything in a Python program belongs to a data type:
>>> type(5)<type ‘int’>>>> type(1.23)<type ‘float’>
Variables
>>> type(“Hello, World!”)<type ‘str’>>>> type(‘Hello, Iowa!’)<type ‘str’>>>> type(‘5’)<type ‘str’>
>>> type(True)<type ‘bool’>>>> type(False)<type ‘bool’>
Variables
Data types can be changed:>>> float(5)5.0>>> str(5)‘5’>>> int(1.9)1>>> int(‘5’)5>>> int(True)1
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
Variables
Print statement shows the content of a variable:
>>> print messageWhat’s up?>>> print n18>>> print pi3.14
Variables
Variables can be changed:>>> count = 10>>> count = count – 1>>> print count9
>>> count -= 1>>> print count8
Variables
Python is a calculator:>>> x = 10>>> y = 2>>> z1 = 3>>> z2 = 3.0
Variables
>>> x / y5>>> x / z13>>> x /z23.3333333333333335
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
Functions
In Python, the syntax for a function definition is:
def NAME(PARAMETERS):STATEMENTSBe cautious with indentation!
Functions
For example:>>> def myMean(a,b):
return (a+b)/2
>>> myMean(3,5)4
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
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'
Conditionals
There are only two Boolean values. True False
A Boolean expression returns a Boolean value.>>> 5 == 5True>>> 5 == 6False
Conditionals
x = 5y = 6
>>> x == yFalse>>> x != yTrue>>> x > yFalse>>> x < yTrue
What will happen if we type x = y?
Conditionals
Logical operators connect multiple Boolean expressions.
>>> 3 > 0 and 3 < 5True>>> 3 < 2 or 3 < 1False>>> not(5 > 6)True
Conditionals Conditional statements take a form like this:if BOOLEAN EXPRESSION:
STATEMENTS>>> x = 5>>> if x > 0: print 'x is positive.'x is positive.
Conditionals
Alternative Execution>>> def isEven(x):
if x % 2 == 0: return True else: return False
>>> isEven(5)False>>> isEven(6)True
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
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
Iteration
Iteration is repeated execution of a set of statements.1. The while statement2. The for statement
Iteration
The while statement>>> def countdown(n): while n > 0: print n n = n-1 print "Blastoff!">>> countdown(3)321Blastoff!
Iteration
The for statement>>> for x in 'Iowa': print xIowa
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
Thank you!
Recommended