Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to...

83
Introduction to Python Sophia Bethany Coban Problem Solving By Computer March 26, 2014

Transcript of Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to...

Page 1: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Introduction to Python

Sophia Bethany Coban

Problem Solving By Computer

March 26, 2014

Page 2: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Introduction to Python

Python is a general-purpose, high-level programminglanguage.

It offers readable codes, and its syntax allows us to writeprograms in fewer lines, compared to C/C++.

There are 2 main version branches of Python:* 2.x branch* 3.x branch

and these versions are incompatible!

However, the latest major version of 2.x (2.7) is close to 3.x.

Python 2.6 or 2.7 is a good place to start but Python 3.x ismore important! So make sure you learn the differencesbetween them.

Sophia Bethany Coban Problem Solving By Computer 2/23

Page 3: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Introduction to Python

Python is a general-purpose, high-level programminglanguage.

It offers readable codes, and its syntax allows us to writeprograms in fewer lines, compared to C/C++.

There are 2 main version branches of Python:* 2.x branch* 3.x branch

and these versions are incompatible!

However, the latest major version of 2.x (2.7) is close to 3.x.

Python 2.6 or 2.7 is a good place to start but Python 3.x ismore important! So make sure you learn the differencesbetween them.

Sophia Bethany Coban Problem Solving By Computer 2/23

Page 4: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Introduction to Python

Python is a general-purpose, high-level programminglanguage.

It offers readable codes, and its syntax allows us to writeprograms in fewer lines, compared to C/C++.

There are 2 main version branches of Python:* 2.x branch* 3.x branch

and these versions are incompatible!

However, the latest major version of 2.x (2.7) is close to 3.x.

Python 2.6 or 2.7 is a good place to start but Python 3.x ismore important! So make sure you learn the differencesbetween them.

Sophia Bethany Coban Problem Solving By Computer 2/23

Page 5: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Introduction to Python

Python is a general-purpose, high-level programminglanguage.

It offers readable codes, and its syntax allows us to writeprograms in fewer lines, compared to C/C++.

There are 2 main version branches of Python:* 2.x branch* 3.x branch

and these versions are incompatible!

However, the latest major version of 2.x (2.7) is close to 3.x.

Python 2.6 or 2.7 is a good place to start but Python 3.x ismore important! So make sure you learn the differencesbetween them.

Sophia Bethany Coban Problem Solving By Computer 2/23

Page 6: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Introduction to Python

Python is a general-purpose, high-level programminglanguage.

It offers readable codes, and its syntax allows us to writeprograms in fewer lines, compared to C/C++.

There are 2 main version branches of Python:* 2.x branch* 3.x branch

and these versions are incompatible!

However, the latest major version of 2.x (2.7) is close to 3.x.

Python 2.6 or 2.7 is a good place to start but Python 3.x ismore important! So make sure you learn the differencesbetween them.

Sophia Bethany Coban Problem Solving By Computer 2/23

Page 7: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Introduction to Python

Python is a general-purpose, high-level programminglanguage.

It offers readable codes, and its syntax allows us to writeprograms in fewer lines, compared to C/C++.

There are 2 main version branches of Python:* 2.x branch* 3.x branch

and these versions are incompatible!

However, the latest major version of 2.x (2.7) is close to 3.x.

Python 2.6 or 2.7 is a good place to start but Python 3.x ismore important! So make sure you learn the differencesbetween them.

Sophia Bethany Coban Problem Solving By Computer 2/23

Page 8: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

About IPython

Python lacks a shiny GUI (compared to MATLAB).

BUT there exists a solution called IPython!

IPython notebook is an interface that looks a lot like aMathematica notebook. It has quickly became the standardas an interactive, numerical computing tool for Python.

We will not go into how IPython works but you are welcometo experiment with it as you like.

The Windows computers in ATB do not have IPython(Anaconda recommended) but it can be downloaded viahttp://ipython.org/install.html (the download and the set uptake about 3 minutes).

Sophia Bethany Coban Problem Solving By Computer 3/23

Page 9: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

About IPython

Python lacks a shiny GUI (compared to MATLAB).

BUT there exists a solution called IPython!

IPython notebook is an interface that looks a lot like aMathematica notebook. It has quickly became the standardas an interactive, numerical computing tool for Python.

We will not go into how IPython works but you are welcometo experiment with it as you like.

The Windows computers in ATB do not have IPython(Anaconda recommended) but it can be downloaded viahttp://ipython.org/install.html (the download and the set uptake about 3 minutes).

Sophia Bethany Coban Problem Solving By Computer 3/23

Page 10: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

About IPython

Python lacks a shiny GUI (compared to MATLAB).

BUT there exists a solution called IPython!

IPython notebook is an interface that looks a lot like aMathematica notebook. It has quickly became the standardas an interactive, numerical computing tool for Python.

We will not go into how IPython works but you are welcometo experiment with it as you like.

The Windows computers in ATB do not have IPython(Anaconda recommended) but it can be downloaded viahttp://ipython.org/install.html (the download and the set uptake about 3 minutes).

Sophia Bethany Coban Problem Solving By Computer 3/23

Page 11: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

About IPython

Python lacks a shiny GUI (compared to MATLAB).

BUT there exists a solution called IPython!

IPython notebook is an interface that looks a lot like aMathematica notebook. It has quickly became the standardas an interactive, numerical computing tool for Python.

We will not go into how IPython works but you are welcometo experiment with it as you like.

The Windows computers in ATB do not have IPython(Anaconda recommended) but it can be downloaded viahttp://ipython.org/install.html (the download and the set uptake about 3 minutes).

Sophia Bethany Coban Problem Solving By Computer 3/23

Page 12: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

About IPython

Python lacks a shiny GUI (compared to MATLAB).

BUT there exists a solution called IPython!

IPython notebook is an interface that looks a lot like aMathematica notebook. It has quickly became the standardas an interactive, numerical computing tool for Python.

We will not go into how IPython works but you are welcometo experiment with it as you like.

The Windows computers in ATB do not have IPython(Anaconda recommended) but it can be downloaded viahttp://ipython.org/install.html (the download and the set uptake about 3 minutes).

Sophia Bethany Coban Problem Solving By Computer 3/23

Page 13: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Why Python?

Python is easy to read!

A good transition between a language like MATLAB to moreC/C++ and scripting languages.

It is free and open source, which is why most companiesprefer Python over MATLAB.

Something else to add on your CV!!

Sophia Bethany Coban Problem Solving By Computer 4/23

Page 14: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Why Python?

Python is easy to read!

A good transition between a language like MATLAB to moreC/C++ and scripting languages.

It is free and open source, which is why most companiesprefer Python over MATLAB.

Something else to add on your CV!!

Sophia Bethany Coban Problem Solving By Computer 4/23

Page 15: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Why Python?

Python is easy to read!

A good transition between a language like MATLAB to moreC/C++ and scripting languages.

It is free and open source, which is why most companiesprefer Python over MATLAB.

Something else to add on your CV!!

Sophia Bethany Coban Problem Solving By Computer 4/23

Page 16: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Why Python?

Python is easy to read!

A good transition between a language like MATLAB to moreC/C++ and scripting languages.

It is free and open source, which is why most companiesprefer Python over MATLAB.

Something else to add on your CV!!

Sophia Bethany Coban Problem Solving By Computer 4/23

Page 17: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Learning Objectives

In these slides, we will go through

Basic maths and string operations,

Variables, their types and lists (arrays),

If statements, for/while loops, functions and

Modules, packages, and how to use them.

But most of all, the one thing we need to learn today is howimportant TABBING is in Python.We will give examples both in MATLAB and Python, and compare

the codes.

Sophia Bethany Coban Problem Solving By Computer 5/23

Page 18: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Learning Objectives

In these slides, we will go through

Basic maths and string operations,

Variables, their types and lists (arrays),

If statements, for/while loops, functions and

Modules, packages, and how to use them.

But most of all, the one thing we need to learn today is howimportant TABBING is in Python.We will give examples both in MATLAB and Python, and compare

the codes.

Sophia Bethany Coban Problem Solving By Computer 5/23

Page 19: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Learning Objectives

In these slides, we will go through

Basic maths and string operations,

Variables, their types and lists (arrays),

If statements, for/while loops, functions and

Modules, packages, and how to use them.

But most of all, the one thing we need to learn today is howimportant TABBING is in Python.We will give examples both in MATLAB and Python, and compare

the codes.

Sophia Bethany Coban Problem Solving By Computer 5/23

Page 20: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Learning Objectives

In these slides, we will go through

Basic maths and string operations,

Variables, their types and lists (arrays),

If statements, for/while loops, functions and

Modules, packages, and how to use them.

But most of all, the one thing we need to learn today is howimportant TABBING is in Python.We will give examples both in MATLAB and Python, and compare

the codes.

Sophia Bethany Coban Problem Solving By Computer 5/23

Page 21: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Learning Objectives

In these slides, we will go through

Basic maths and string operations,

Variables, their types and lists (arrays),

If statements, for/while loops, functions and

Modules, packages, and how to use them.

But most of all, the one thing we need to learn today is howimportant TABBING is in Python.We will give examples both in MATLAB and Python, and compare

the codes.

Sophia Bethany Coban Problem Solving By Computer 5/23

Page 22: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Learning Objectives

In these slides, we will go through

Basic maths and string operations,

Variables, their types and lists (arrays),

If statements, for/while loops, functions and

Modules, packages, and how to use them.

But most of all, the one thing we need to learn today is howimportant TABBING is in Python.We will give examples both in MATLAB and Python, and compare

the codes.

Sophia Bethany Coban Problem Solving By Computer 5/23

Page 23: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Learning Objectives

In these slides, we will go through

Basic maths and string operations,

Variables, their types and lists (arrays),

If statements, for/while loops, functions and

Modules, packages, and how to use them.

But most of all, the one thing we need to learn today is howimportant TABBING is in Python.We will give examples both in MATLAB and Python, and compare

the codes.

Sophia Bethany Coban Problem Solving By Computer 5/23

Page 24: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Running Python - Mac OSX

In these slides, we either run Python in a Terminal (similar to usingMATLAB’s command window), or use a text editor for the codeand run the script in the Terminal for an output:

Sophia Bethany Coban Problem Solving By Computer 6/23

Page 25: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Running Python - Mac OSX

In these slides, we either run Python in a Terminal (similar to usingMATLAB’s command window), or use a text editor for the codeand run the script in the Terminal for an output:

Python in Terminal

Sophia Bethany Coban Problem Solving By Computer 6/23

Page 26: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Running Python - Mac OSX

In these slides, we either run Python in a Terminal (similar to usingMATLAB’s command window), or use a text editor for the codeand run the script in the Terminal for an output:

Python with a text editor (TextWrangler)

Sophia Bethany Coban Problem Solving By Computer 6/23

Page 27: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Running Python - Linux

Same steps can be taken to run Python in Linux as well (notsurprising):

Sophia Bethany Coban Problem Solving By Computer 7/23

Page 28: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Running Python - Linux

Same steps can be taken to run Python in Linux as well (notsurprising):

Python in Terminal

Sophia Bethany Coban Problem Solving By Computer 7/23

Page 29: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Running Python - Linux

Same steps can be taken to run Python in Linux as well (notsurprising):

Python with a text editor (Gedit)

Sophia Bethany Coban Problem Solving By Computer 7/23

Page 30: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Running Python - Windows

In the ATB clusters, run Python via Start>Python IDLE. Thisprogram is similar to MATLAB in a sense that it has its own Editorand debug mode.

Python Shell

Sophia Bethany Coban Problem Solving By Computer 8/23

Page 31: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Running Python - Windows

In the ATB clusters, run Python via Start>Python IDLE. Thisprogram is similar to MATLAB in a sense that it has its own Editorand debug mode.

IPython QT Console

Sophia Bethany Coban Problem Solving By Computer 8/23

Page 32: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Basic Operations

Python (as any other language) has addition, subtraction,multiplication and division implemented.

Sophia Bethany Coban Problem Solving By Computer 9/23

Page 33: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Basic Operations

Python (as any other language) has addition, subtraction,multiplication and division implemented.

If we were to calculate

c = 2 + 5− 3× 6/9,

would Python follow the correct order?

Sophia Bethany Coban Problem Solving By Computer 9/23

Page 34: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Basic Operations

Python (as any other language) has addition, subtraction,multiplication and division implemented.

If we were to calculate

c = 2 + 5− 3× 6/9,

would Python follow the correct order?

MATLAB Python

Sophia Bethany Coban Problem Solving By Computer 9/23

Page 35: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Variables and Types

Every variable in Python is an object and you do not need todeclare their type before using them (something we do in C/C++).

Below would be declared as an integer in Python:

>>> n int = 5

This would be a floating point number:

>>> n float = 5.0

We also have strings declared in single or double quotationmarks:

>>> a string = “Hello World!”

Finding type of a variable:

Sophia Bethany Coban Problem Solving By Computer 10/23

Page 36: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

String Operations

You can do a lot with strings in Python.

Sophia Bethany Coban Problem Solving By Computer 11/23

Page 37: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

String Operations

You can do a lot with strings in Python.

Print can also work as fprintf:

Sophia Bethany Coban Problem Solving By Computer 11/23

Page 38: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

String Operations

You can do a lot with strings in Python.

Print can also work as fprintf:

MATLAB Python

Sophia Bethany Coban Problem Solving By Computer 11/23

Page 39: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

String Operations

You can add two strings together:

MATLAB Python

or multiply a string with a number:

MATLAB Python

There is a lot you can do with strings in Python!

Sophia Bethany Coban Problem Solving By Computer 12/23

Page 40: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

String Operations

You can add two strings together:

MATLAB Python

or multiply a string with a number:

MATLAB Python

There is a lot you can do with strings in Python!

Sophia Bethany Coban Problem Solving By Computer 12/23

Page 41: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Lists

Lists are similar to arrays or vectors but they can contain variablesof any type.

We can declare a list in the following way:

>>> mylist = [1, 2, 3]

or initialise as empty, and append elements individually:

>>> mylist = [ ]>>> mylist.append(“Hello!”)

Let us try appending a string to a list of integers:

Sophia Bethany Coban Problem Solving By Computer 13/23

Page 42: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Lists

To access a specific element in a list, we use

>>> print = mylist[< index >]

BUT indexing starts from 0 in Python (like in C/C++ butnot in MATLAB)!

Sophia Bethany Coban Problem Solving By Computer 14/23

Page 43: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Conditions and If Statements

We have the boolean values True and False to evaluateconditions in Python (same as in MATLAB):

Sophia Bethany Coban Problem Solving By Computer 15/23

Page 44: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Conditions and If Statements

We have the boolean values True and False to evaluateconditions in Python (same as in MATLAB):

The syntax for if statemets is a little different:

MATLAB Python

Sophia Bethany Coban Problem Solving By Computer 15/23

Page 45: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Conditions and If Statements

We have the boolean values True and False to evaluateconditions in Python (same as in MATLAB):

The syntax for if statemets is a little different:

MATLAB Python

Sophia Bethany Coban Problem Solving By Computer 15/23

Page 46: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Conditions and If Statements

We have the boolean values True and False to evaluateconditions in Python (same as in MATLAB):

The syntax for if statemets is a little different:

MATLAB Python

Of course in an editor, you would not have to press enter foroutput BUT you would have to put the tab!

Sophia Bethany Coban Problem Solving By Computer 15/23

Page 47: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Conditions and If Statements

For when we have multiple conditions, we use and and or(much like && and || in MATLAB).

We also have not for inverting the boolean value – similar to“∼=” (not equal) in MATLAB.

When we have more than one statement to check? We useif/elseif in MATLAB but in Python, this is if/elif.

Sophia Bethany Coban Problem Solving By Computer 16/23

Page 48: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Conditions and If Statements

For when we have multiple conditions, we use and and or(much like && and || in MATLAB).

We also have not for inverting the boolean value – similar to“∼=” (not equal) in MATLAB.

When we have more than one statement to check? We useif/elseif in MATLAB but in Python, this is if/elif.

CODE:

Output??

Sophia Bethany Coban Problem Solving By Computer 16/23

Page 49: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Conditions and If Statements

For when we have multiple conditions, we use and and or(much like && and || in MATLAB).

We also have not for inverting the boolean value – similar to“∼=” (not equal) in MATLAB.

When we have more than one statement to check? We useif/elseif in MATLAB but in Python, this is if/elif.

CODE:

Output??

Sophia Bethany Coban Problem Solving By Computer 16/23

Page 50: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Loops

Just as in MATLAB, we have two different types of looping inPython: For and While.

The syntax is very similar to the if statements:

Put a colon after the for/while <statement> line, andUse tabs to continue with the loop or go back a tab to endthe loop.

Sophia Bethany Coban Problem Solving By Computer 17/23

Page 51: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Loops

Just as in MATLAB, we have two different types of looping inPython: For and While.

The syntax is very similar to the if statements:Put a colon after the for/while <statement> line, andUse tabs to continue with the loop or go back a tab to endthe loop.

MATLAB Python

Sophia Bethany Coban Problem Solving By Computer 17/23

Page 52: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Loops

Just as in MATLAB, we have two different types of looping inPython: For and While.

The syntax is very similar to the if statements:Put a colon after the for/while <statement> line, andUse tabs to continue with the loop or go back a tab to endthe loop.

MATLAB Python

Sophia Bethany Coban Problem Solving By Computer 17/23

Page 53: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Loops

Below is an example of a while loop (and more tabbing!) inPython.

Sophia Bethany Coban Problem Solving By Computer 18/23

Page 54: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Loops

Below is an example of a while loop (and more tabbing!) inPython.

MATLAB Python

Sophia Bethany Coban Problem Solving By Computer 18/23

Page 55: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Loops

Below is an example of a while loop (and more tabbing!) inPython.

MATLAB Python

Sophia Bethany Coban Problem Solving By Computer 18/23

Page 56: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Loops

We also have Break and Continue in Python, which workexactly the same way as in MATLAB (and in C/C++):

Code (note they are now in .m and .py scripts)

Sophia Bethany Coban Problem Solving By Computer 19/23

Page 57: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Loops

We also have Break and Continue in Python, which workexactly the same way as in MATLAB (and in C/C++):

Code (note they are now in .m and .py scripts)

MATLAB Python

Sophia Bethany Coban Problem Solving By Computer 19/23

Page 58: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Loops

We also have Break and Continue in Python, which workexactly the same way as in MATLAB (and in C/C++):

Code (note they are now in .m and .py scripts)

MATLAB Python

Output??

Sophia Bethany Coban Problem Solving By Computer 19/23

Page 59: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Loops

We also have Break and Continue in Python, which workexactly the same way as in MATLAB (and in C/C++):

Code (note they are now in .m and .py scripts)

MATLAB Python

Output??

MATLAB Python

Sophia Bethany Coban Problem Solving By Computer 19/23

Page 60: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Loops

We also have Break and Continue in Python, which workexactly the same way as in MATLAB (and in C/C++):

Code (note they are now in .m and .py scripts)

MATLAB Python

Notice we used range(1,6) for t = 1:5 – This is the range from 1to 6, (with 6 not included).We could have also used xrange.

(HW) Learn the difference between range and xrange!

Sophia Bethany Coban Problem Solving By Computer 19/23

Page 61: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Functions

We create and call functions in the following ways.

Sophia Bethany Coban Problem Solving By Computer 20/23

Page 62: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Functions

We create and call functions in the following ways.

A function without any input arguments:

MATLAB

Output:

Python

Output:

Sophia Bethany Coban Problem Solving By Computer 20/23

Page 63: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Functions

We create and call functions in the following ways.

A function with one input and one output argument:

MATLAB

Output:

Python

Output:

Sophia Bethany Coban Problem Solving By Computer 20/23

Page 64: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Functions

We create and call functions in the following ways.

A function with one input and one output argument:

MATLAB

Output:

Python

Output:

Sophia Bethany Coban Problem Solving By Computer 20/23

Page 65: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Functions

We create and call functions in the following ways.

A function with multiple input and output arguments:

MATLAB Python

Sophia Bethany Coban Problem Solving By Computer 20/23

Page 66: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Functions

We create and call functions in the following ways.

A function with multiple input and output arguments:

MATLAB – Output Python – Output

Sophia Bethany Coban Problem Solving By Computer 20/23

Page 67: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Modules and Packages

How to call a function in a different script? Use import!

Sophia Bethany Coban Problem Solving By Computer 21/23

Page 68: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Modules and Packages

How to call a function in a different script? Use import!

Import is used to call modules.

Sophia Bethany Coban Problem Solving By Computer 21/23

Page 69: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Modules and Packages

How to call a function in a different script? Use import!

Import is used to call modules.

A module is a .py script with a number of functions.

Sophia Bethany Coban Problem Solving By Computer 21/23

Page 70: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Modules and Packages

How to call a function in a different script? Use import!

Import is used to call modules.

A module is a .py script with a number of functions.

Let us call the previous function we used to add and comparenumbers:

Sophia Bethany Coban Problem Solving By Computer 21/23

Page 71: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Modules and Packages

How to call a function in a different script? Use import!

Calling functions in another .py script:

Output:

Sophia Bethany Coban Problem Solving By Computer 21/23

Page 72: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Modules and Packages

How to call a function in a different script? Use import!

Calling functions in another .py script:

Output:

Sophia Bethany Coban Problem Solving By Computer 21/23

Page 73: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Modules and Packages

How to call a function in a different script? Use import!

Import is used to call modules.

A module is a .py script with a number of functions.

We can use dir to print the functions in the importedmodule:

Sophia Bethany Coban Problem Solving By Computer 21/23

Page 74: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Modules and Packages

How to call a function in a different script? Use import!

Import is used to call modules.

A module is a .py script with a number of functions.

We can use dir to print the functions in the imported module:

Printing functions in a module:

Sophia Bethany Coban Problem Solving By Computer 21/23

Page 75: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Modules and Packages

How to call a function in a different script? Use import!

Import is used to call modules.

A module is a .py script with a number of functions.

We can use dir to print the functions in the importedmodule.

We can also use help to learn about the functions in theimported module:

Sophia Bethany Coban Problem Solving By Computer 21/23

Page 76: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Modules and Packages

How to call a function in a different script? Use import!

Import is used to call modules.

We can use dir to print the functions in the importedmodule.

We can also use help to learn about the functions in theimported module:

Using help on functions in a module:

Sophia Bethany Coban Problem Solving By Computer 21/23

Page 77: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Modules and Packages

How to call a function in a different script? Use import!

Import is used to call modules.

A module is a .py script with a number of functions.

We can use dir to print the functions in the importedmodule.

We can also use help to learn about the functions in theimported module.

Packages are a collection of modules put together (can beuseful for different projects).(HW) Find out how to create packages!

Sophia Bethany Coban Problem Solving By Computer 21/23

Page 78: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Closing Notes

Learning a new coding language often means a different wayof thinking.

Python is available in all platforms and running scripts is verycommon in/for all the devices, so it is very well integrated.

We would be very impressed with you for being able to codein Python!

You must remember that you would be more independent infinding solutions to your problems if you choose to code inPython.

Don’t be put off by the lack of a fancy gui and remember,practice makes perfect.

Sophia Bethany Coban Problem Solving By Computer 22/23

Page 79: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Closing Notes

Learning a new coding language often means a different wayof thinking.

Python is available in all platforms and running scripts is verycommon in/for all the devices, so it is very well integrated.

We would be very impressed with you for being able to codein Python!

You must remember that you would be more independent infinding solutions to your problems if you choose to code inPython.

Don’t be put off by the lack of a fancy gui and remember,practice makes perfect.

Sophia Bethany Coban Problem Solving By Computer 22/23

Page 80: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Closing Notes

Learning a new coding language often means a different wayof thinking.

Python is available in all platforms and running scripts is verycommon in/for all the devices, so it is very well integrated.

We would be very impressed with you for being able to codein Python!

You must remember that you would be more independent infinding solutions to your problems if you choose to code inPython.

Don’t be put off by the lack of a fancy gui and remember,practice makes perfect.

Sophia Bethany Coban Problem Solving By Computer 22/23

Page 81: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Closing Notes

Learning a new coding language often means a different wayof thinking.

Python is available in all platforms and running scripts is verycommon in/for all the devices, so it is very well integrated.

We would be very impressed with you for being able to codein Python!

You must remember that you would be more independent infinding solutions to your problems if you choose to code inPython.

Don’t be put off by the lack of a fancy gui and remember,practice makes perfect.

Sophia Bethany Coban Problem Solving By Computer 22/23

Page 82: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Closing Notes

Learning a new coding language often means a different wayof thinking.

Python is available in all platforms and running scripts is verycommon in/for all the devices, so it is very well integrated.

We would be very impressed with you for being able to codein Python!

You must remember that you would be more independent infinding solutions to your problems if you choose to code inPython.

Don’t be put off by the lack of a fancy gui and remember,practice makes perfect.

Sophia Bethany Coban Problem Solving By Computer 22/23

Page 83: Introduction to Python - School of Mathematicsscoban/python_lecture_psbc.pdf · Introduction to Python Python is a general-purpose, high-level programming language. It o ers readable

Useful Links

These slides can be found onwww.maths.manchester.ac.uk/∼scoban/python lecture psbc.pdf

Also check out:

Teach Yourself Python:http://www.codecademy.com/tracks/python

Python Standard Library (choose your version on top):http://docs.python.org/2/library/

Differences between the versions:https://wiki.python.org/moin/Python2orPython3

A huge collection of Python videos: http://pyvideo.org/

Educational/interesting videos about IPython:http://ipython.org/videos.html

Sophia Bethany Coban Problem Solving By Computer 23/23