Programming

with Snakes (part 2)

Why Python Rocks!


It's Easy to use: 
It is closer to human language than to machine language.

It's Powerful: 
It's used in academic research, at Google and YouTube.

Runs Everywhere (it's platform independent):
 You can write your program on a Mac and run it on Windows.

Free and Open Source.
Great Community.

Running Python


 We need a way to send commands to the computer.

On the Macintosh we can use the "Terminal"


Applications -> Utilities -> Terminal

Python Commands


To tell the computer we are going to be using python commands we need to run the python interpreter.

Type "python" at the command prompt.

python
>>>

We can now send python commands.

Try some math


Try some math! +, -, /, *, %, **, nest them in ( )'s

>>> 1 + 1
 2 
>>> 2 - 3
 -1 

Adding words?


Python knows the difference between numbers and words.

>>> 1 + 2 + 3
6
>>> "Good" + " morning"
"Good morning"

Try adding a number to a word.

Numbers in Python

In Python ...

Numbers without decimals are called INTEGERS.

1 is an Integer.     1.0 is NOT an integer

>>> type(1)
<type 'int'> 
>>> type(1.0)
<type 'float'> 

Strings in Python


In Python ...

Letters, words, and sentences are called STRINGS. We tell Python that we are intending to use a string by wrapping it in quotes.

>>> type("What's up?")
<type 'str'>
 
Think of a string of characters.

Types


Python needs to classify everything so it knows what  can be done with it. Python knows that ...

The number 1 is an Integer.

The word "Armbrae" is a String.

Integers and Strings are Types in Python.
      Strings are within 'single' or "double" quotes.

Recap


Some programming words that we have learned so far:

Integer.

String.

Type.

Integers and Strings are Types

Why Types?


Python has instructions for what to do with different types.

For example:
How to "+" an integer is different from how to "+" a string

Using Strings


Any group of characters surrounded by single quotes (') or double quotes (") is a String.

"Hello World!"
'Armbrae'
"Love will find a way
through paths where
wolves fear to prey."

Simple String Manipulation


>>> "armbrae".capitalize()
"Armbrae"

>>>"Hello you".replace("you", "Sam")
'Hello Sam'

>>>"No More CAPS".lower()"
'no more caps'

Building Strings


>>>"There are " + "30 " + "days in April"
'There are 30 days in April'

>>>"Today is a " + "Tuesday"
'Today is a Tuesday'

Some Numeric Operations


>>>abs(-99)
99

>>>pow(2,3)
8

>>>5 / 2
2

>>>5 / 2.0
2.5

Functions


abs and pow are builtin functions that you can use with integers

capitalize and replace are string functions

Functions are actions.


Types come with builtin functions.

Error Messages


If you ask Python to do something that it doesn't know how to do, it will give you an error.

Get in the habit of reading the error messages, they will give you a clue on what went wrong.

>>> pow("Lady", "Gaga")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'str'

Boolean Type


Booleans are a special type. There are only two boolean values:

True

False

Booleans are used extensively in programming, we will see more about this type a little later.

Programming in Python - Part 2

By gordie

Programming in Python - Part 2

  • 850