python 101: python for absolute beginners (pytexas 2014)

52
Python 101: Python for Absolute Beginners PyTexas 2014

Upload: paige-bailey

Post on 07-Dec-2014

271 views

Category:

Technology


8 download

DESCRIPTION

If you're absolutely new to Python, and to programming in general, this is the place to start! Here's the breakdown: by the end of this workshop, you'll have Python downloaded onto your personal machine; have a general idea of what Python can help you do; be pointed in the direction of some excellent practice materials; and have a basic understanding of the syntax of the language. Please don't forget to bring your laptop! Audience: "Python 101" is geared toward individuals who are new to programming. If you've had some programming experience (shell scripting, MATLAB, Ruby, etc.), then you'll probably want to check out the more intermediate workshop, "Python 101++".

TRANSCRIPT

Page 1: Python 101: Python for Absolute Beginners (PyTexas 2014)

Python 101:Python for Absolute Beginners

PyTexas 2014

Page 2: Python 101: Python for Absolute Beginners (PyTexas 2014)

What is programming?

Page 3: Python 101: Python for Absolute Beginners (PyTexas 2014)

A computer is a machine that stores pieces of information.

A computer also moves, arranges, and controls that information (or data).

A program is a detailed set of instructions that tells a computer what to do with that data.

Page 4: Python 101: Python for Absolute Beginners (PyTexas 2014)

CodeSkulptor!

Page 5: Python 101: Python for Absolute Beginners (PyTexas 2014)

CodeSkulptor Developed by Scott Rixner of

Rice University to use for COMP 200.

Based on CodeMirror and Skulpt.

www.codeskulptor.org

If you want to learn more about using Python with CodeSkulptor after this class, check out the Coursera course “An Introduction to Interactive Programming in Python”! (9/15 – 11/16)

https://www.coursera.org/course/interactivepython

Page 6: Python 101: Python for Absolute Beginners (PyTexas 2014)

Interacting with CodeSkulptor

Run

Save

Fresh URL

Open Local

Reset

Page 7: Python 101: Python for Absolute Beginners (PyTexas 2014)

Additional Resources

Docs (documentation)

Demos

Viz Mode

Page 8: Python 101: Python for Absolute Beginners (PyTexas 2014)

Let’s see what this thing can do…

Page 9: Python 101: Python for Absolute Beginners (PyTexas 2014)

Recap:

Explore! Makes changes, see how they impact the program as a whole.

When in doubt, check the documentation.

When super in doubt, Google it.

Page 10: Python 101: Python for Absolute Beginners (PyTexas 2014)

Math

Page 11: Python 101: Python for Absolute Beginners (PyTexas 2014)

Math

Try typing this into CodeSkuptor:

>>> print 3 + 12

>>> print 12 - 3

>>> print 9 + 5 – 15 + 12

Operators:

add: +

subtract: -

Note: don’t type the arrows >>> !

Page 12: Python 101: Python for Absolute Beginners (PyTexas 2014)

MathRule: If you want Python to answer in floats, you have to talk to it in floats.

More operators:

divide: /

multiply: *

>>> print 3 * 12

>>> print 12 / 3

>>> print 11 // 3

>>> print 12.0 / 3.0

>>> print 11.0 / 3.0

>>> print 11.0 // 3.0

Page 13: Python 101: Python for Absolute Beginners (PyTexas 2014)

Math

Comparison operators:

== Equal to

!= Not equal to

< Less than

> Greater than

<= Less than or equal to

>= Greater than or equal to

Page 14: Python 101: Python for Absolute Beginners (PyTexas 2014)

Math

Practice:

>>> print 2 < 3True

>>> print 2 <= 2 False

>>> print 3 > 2True

>>> print 2 != 3True

>>> print False < True True

Page 15: Python 101: Python for Absolute Beginners (PyTexas 2014)

Strings

Page 16: Python 101: Python for Absolute Beginners (PyTexas 2014)

Strings

Examples:

Try typing one without quotes:

What’s the result?

>>> “It’s a beautiful day!”

>>> “Goodbye, cruel world.”

>>> Aggies

>>> “Aggies”

>>> “Rice fight, never die!”

>>> “3 + 2”

Page 17: Python 101: Python for Absolute Beginners (PyTexas 2014)

Strings

String operators:

concatenation: +

multiplication: *

Try concatenating:

Try multiplying:

>>> print “Hello” + “ “ + “world!”

>>> print “HAHA” * 250

Page 18: Python 101: Python for Absolute Beginners (PyTexas 2014)

Variables

Page 19: Python 101: Python for Absolute Beginners (PyTexas 2014)

Variables

Calculate a value:

How can you save that value?

Give the value a name:

>>> print 21 + 21

42

>>> ultimate_answer = 42

>>> ultimate_answer

42

Page 20: Python 101: Python for Absolute Beginners (PyTexas 2014)

Variables

Create a variable

and give it a value:

Now assign a

new value:

>>> headmaster = “Dumbledore”

>>> print headmaster

‘Dumbledore’

>>> headmaster = “Hardcastle”

>>> print headmaster

‘Hardcastle’

>>> color = “Brad Neely”

>>> color = 12

Page 21: Python 101: Python for Absolute Beginners (PyTexas 2014)

Variables You can calculate a variable once, but keep the result to use later.

You can keep the same name for a variable, but change the value.

Some other things that we can do with variables:

Get an index from a string:

Do some math:

>>> headmaster = “Dumbledore”

>>> print headmaster[2]

>>> number = 3

>>> print headmaster[number - 2]

Page 22: Python 101: Python for Absolute Beginners (PyTexas 2014)

Types of data

Page 23: Python 101: Python for Absolute Beginners (PyTexas 2014)

Data TypesWe already know about three types of data:

“Whoop!” string

42 integer

3.14159 float

Python can tell us about types using the type() function:

>>> print type(“Whoop!”)

<type ‘str’>

How would we get Python to output int and float types?

Page 24: Python 101: Python for Absolute Beginners (PyTexas 2014)

Data Type: Lists

Page 25: Python 101: Python for Absolute Beginners (PyTexas 2014)

ListsList: a sequence of objects

>>> Beatles = [“John”, “Paul”, “George”, “Ringo”]

>>> grades = [82, 93, 67, 99, 100]

Guess what this will output:

>>> type(Beatles)

>>> type(grades)

Page 26: Python 101: Python for Absolute Beginners (PyTexas 2014)

ListsList: a sequence of objects

>>> Beatles = [“John”, “Paul”, “George”, “Ringo”]

>>> grades = [82, 93, 67, 99, 100]

Guess what this will output:

>>> type(Beatles)

<type ‘list’>

>>> type(grades)

<type ‘list’>

Page 27: Python 101: Python for Absolute Beginners (PyTexas 2014)

ListsIndex: Where an item is in the list

>>> Beatles = [“John”, “Paul”, “George”, “Ringo”]

>>> Beatles[0]

‘John‘

[“John”, “Paul”, “George”, “Ringo”]

0 1 2 3

Python always starts at zero!

Page 28: Python 101: Python for Absolute Beginners (PyTexas 2014)

Data Type: Booleans

Page 29: Python 101: Python for Absolute Beginners (PyTexas 2014)

BooleansA boolean value can be:

Is 1 equal to 1?

Is 15 less than 5?

True or False.

>>> print 1 == 1

True

>>> print 15 < 5

False

Page 30: Python 101: Python for Absolute Beginners (PyTexas 2014)

BooleansWhat happens when we type

Boolean values in the

interpreter?

When the words ‘True’ and

‘False’ begin with upper case

letters, Python knows to

treat them like Booleans

instead of strings or integers.

>>> True

>>> False

>>> true

>>> false

>>> type(True)

>>> type(“True”)

Page 31: Python 101: Python for Absolute Beginners (PyTexas 2014)

Booleansand

If both comparisons are True:

If only one of the

comparisons is True:

If both of the

comparisons are False:

>>> 1==1 and 2==2

True

>>> 1==1 and 2==3

False

>>> 1==2 and 2==3

False

Page 32: Python 101: Python for Absolute Beginners (PyTexas 2014)

Booleansor

If both comparisons are True:

If only one of the

comparisons is True:

If both of the

comparisons are False:

>>> 1==1 or 2==2

True

>>> 1==1 or 2!=2

True

>>> 1==2 or 2==3

False

Page 33: Python 101: Python for Absolute Beginners (PyTexas 2014)

Booleansnot

You can use the word not to reverse the answer that Python gives:

Any expression that is True can become False:

>>> 1==1

True

>>> not 1==1

False

>>> not True

False

Page 34: Python 101: Python for Absolute Beginners (PyTexas 2014)

BooleansYou can also use Booleans in their own expressions:

>>> True and True>>> True and False>>> False and False

>>> True or True>>> False or True>>> False or False

>>> not True and True>>> not True or True

Page 35: Python 101: Python for Absolute Beginners (PyTexas 2014)

Logic

Page 36: Python 101: Python for Absolute Beginners (PyTexas 2014)

if Statements

Page 37: Python 101: Python for Absolute Beginners (PyTexas 2014)

if Statements

Making decisions:

If a condition is met, perform an action:

“If you’re hungry, let’s eat lunch.”

“If you like Frisbee, let’s play!”

>>> state = “Texas”

>>> if state == “Texas”:

print “TX”

TX

Page 38: Python 101: Python for Absolute Beginners (PyTexas 2014)

if Statements

Adding a choice:

Adding a choice in our code with the else clause:

“If you’re hungry, let’s eat lunch. Or else we can eat in an hour.”

“If you like Frisbee, let’s play! Or else we can play rugby.”

>>> if state == “Texas”

print “TX”

else:

print “[inferior state]”

Page 39: Python 101: Python for Absolute Beginners (PyTexas 2014)

if Statements

Adding many choices:

Adding more choices in our code with the elif clause:

“If you like Frisbee, let’s play! Or

else we can play rugby. Or else we can play Bioshock, or Half-Life, or Portal…”

>>> if name == “Paige”

print “Hi Paige!”

elif name == “Walker”:

print “Hi Walker!”

else:

print “Imposter!”

Page 40: Python 101: Python for Absolute Beginners (PyTexas 2014)

Loops

Page 41: Python 101: Python for Absolute Beginners (PyTexas 2014)

LoopsLoops are chunks of code that repeat a task over and over again.

Counting loops repeat a certain number of times.

Conditional loops keep going until a certain thing happens

(or as long as some condition is True).

Page 42: Python 101: Python for Absolute Beginners (PyTexas 2014)

LoopsCounting loops repeat a certain number of times – they keep going until they get to the end of a count.

>>> for mynum in [1, 2, 3, 4, 5]:

print "Hello", mynum

Hello 1

Hello 2

Hello 3

Hello 4

Hello 5

The for keyword is used to create this kind of loop, so it is usually just called a for loop.

Page 43: Python 101: Python for Absolute Beginners (PyTexas 2014)

LoopsConditional loops repeat until something happens (or as long as some condition is True).

>>> count = 0

>>> while (count < 4):

print 'The count is:', count

count = count + 1

The count is: 0

The count is: 1

The count is: 2

The count is: 3

The while keyword is used to create this kind of loop, so it is usually just called a while loop.

Page 44: Python 101: Python for Absolute Beginners (PyTexas 2014)

Algorithms

Page 45: Python 101: Python for Absolute Beginners (PyTexas 2014)

Algorithms

Really just means “set of instructions”

Secret: computers aren’t very smart.

Page 46: Python 101: Python for Absolute Beginners (PyTexas 2014)

How would I make a pot of coffee?

1. Get a flavor of ground coffee.

2. Get a coffee maker.

3. Get filter paper.

4. Get a pot of water.

5. Make sure the coffee maker is plugged in…

…and on, and on, and on.

But to us, it’s just “make a pot of coffee”.

Page 47: Python 101: Python for Absolute Beginners (PyTexas 2014)

Functions

Page 48: Python 101: Python for Absolute Beginners (PyTexas 2014)

Remember how Algorithms are just instructions?

Functions are just a concise way to group instructions into a bundle.

What it's like in our minds:

“Make a pot of coffee.” bundle

In Python, you could say it like this:

make_coffee(coffee_grounds, coffee_pot, water, filter_paper)

^ ^-----------------^---------------^-----------------^

function name function parameters

Page 49: Python 101: Python for Absolute Beginners (PyTexas 2014)

Functions

Let’s define a function in CodeSkulptor:

>>> def beverage():print ‘Have you had a cup of coffee today?’

Now we’ll call the function:

>>> beverage()Have you had a cup of coffee today?

Page 50: Python 101: Python for Absolute Beginners (PyTexas 2014)

Functions

But what if not everyone wants a cup of coffee?

Let’s define a function with parameters:

>>> def beverage(drink):print “Have you had a cup of ” + drink + “ today?’

Now we’ll call the function:

>>> beverage(“Monster Zero”)Have you had a cup of Monster Zero today?

Page 51: Python 101: Python for Absolute Beginners (PyTexas 2014)

Functions

Functions are defined using def.

Functions are called using parentheses ().

Functions take parameters and return outputs.

print displays information, but does not give a value.

return gives a value to the caller.

Page 52: Python 101: Python for Absolute Beginners (PyTexas 2014)

Thanks so much!Any questions?