SlideShare a Scribd company logo
Raspberry Pi: a class in several parts
III: Software
3-1: Introduction to Python
Objectives:
 Basic familiarity with the IDLE tool
 Basic data manipulation using Python syntax
All the software for this sequence should already be on the Pi, as part of Raspbian.
Verify that the IDLE tool is available in the X Windows System.
What's the point? Giving the computer instructions, to tell it to do exactly what you
want. It can't do what it doesn't know, so we humans have to decide how to teach
it, and craft our programs appropriately. Games, web, documents, security, music,
virtually anything is possible.
Basic data manipulation:
Type a number into IDLE and press enter. It examines what you told it, does it, and tell
you the result. In this case, it tells you what number you entered...because there is
nothing else to do.
Try some basic math, e.g. 1 + 1. It tells you the answer is 2. Some basic operators are
as follows:
 + (addition)
 - (subtraction)
 * (multiplication)
 / (division)
 % (modulo or remainder, as in 10 mod 3 == 1)
 ** (power, as in 4**2 = 4 squared = 16)
Try 10 / 3. Notice that the answer is 3. Now try 10 / 3.0. Notice that the answer is
3.3333333. If you tell it that you care about decimal places, you get decimal
numbers. Otherwise, you get integers.
Take a few minutes to use Python as a calculator. There are some editing shortcuts
that might also help at this point.
 Ctrl-End will always get you back to the current prompt, if you move the cursor
elsewhere.
 Alt-p will put the previous entry at the prompt again. If you hit it again, it will
cycle through entries previous to that as well.
 Alt-n will go to the next entry. This is particularly useful if you are looking for
something in particular with Alt-p, but you pass it.
 Ctrl-c will interrupt what is currently happening, clear the current programming
entry, and put your cursor at the prompt. If you don't know what is happening,
this can help you reset to a known state.
 Copy and paste do work. Be careful of indentation if you choose to paste, and
also be careful of the >>> characters.
Variables:
(Attendees who have done some algebra will find this much easier.)
In its basic form, a variable is a box. You can put things in the box, you can take
things out of the box, you can forget what's inside, and then look to see what it is.
When you carry it around, though, it's still a box. Boxes come in different shapes and
sizes, but all of ours are going to be the same size for now.
Let's put the number 12 in a variable, by telling Python “x = 12” – notice that Python
responds by telling us absolutely nothing. Now, let's ask Python what x is:
>>> x
12
>>>
Next, let's use our variable in some arithmetic expressions. Try things like x + 1, x**x, 42
/ x, etc. Notice that it works just like the number 12.
Now, let's change x to something else. Try the following:
>>> x = x - 3
>>>
We can change what's in the box without actually looking at it, which is convenient.
Have a look to see what its value currently is.
>>> x
9
>>>
You can call variables pretty much whatever you like, as long as you use letters and
no spaces. Numbers are okay within a variable name, and multi-word variable
names are usually distinguished by capitalizing all but the first one. So, the following
are all valid: x, y, xy, george, john, paul, ringo, octopus,
thisIsAVeryLongVariableName, theSong, grocerylist. Be descriptive, and try to label
your boxes so you know what's inside them.
Strings:
We've seen how Python can act as a calculator with numbers, and how we can use
words to name variables. What if we want Python to talk to us? If you try to tell it
“hello” you get a nasty-looking error:
>>> hello
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
hello
NameError: name 'hello' is not defined
>>>
Such errors are commonplace and not to be feared. They simply indicate that
something went wrong, and usually provide useful information for fixing the problem.
In this case, Python doesn't know anything about hello – it is trying to interpret it as a
variable or other identifier. We want it to be treated as a literal string of characters,
so we have to put it inside quotation marks:
>>> "hello"
'hello'
>>>
Notice that when Python tells us 'hello' it is between a pair of apostrophes, also called
single quotes. Either single or double quotation marks are acceptable, as long as
they match.
We can also add strings together, and we can put them in variables. For example:
>>> y = 'hello'
>>> y
'hello'
>>> y + " world”
'hello world'
Remember how we have the number 9 in x at the moment? It is often useful to insert
the values of numeric variables into strings, but to do so we must use a different
character: the backtick. It points toward the right, while the apostrophe On US
keyboards, it is usually on the same key as the tilde.
>>> y + " " + 'world' + `x`
'hello world9'
>>>
This may be a reasonable stopping point – the attention to detail required for success
throughout these examples may take some effort for attendees, especially if you
have some who do not have experience with algebra. Pack up the Pi kits as normal.
3-2: Structures
Objectives:
 Learn how to manipulate lists (arrays)
 Learn how to automate repetitive actions (loops)
 Learn how to make decisions in code (conditionals)
Set up the Pi kits. Start the X Windows System (startx)and launch IDLE.
Briefly review the syntax from the last session. We will make use of variables and
strings, especially.
Most of the time we don't deal with just one thing. Consider a trip to the grocery
store: does the shopping list contain only one item? Sometimes, but most of the time
it is rather longer than that. Python lets us represent lists of things easily. We put them
between square brackets [].
>>> groceries = ['milk', 'french fries', 'TACOS', 'ketchup', "lettuce", 'penguins', 'MORE
TACOS']
>>> groceries
['milk', 'french fries', 'TACOS', 'ketchup', 'lettuce', 'penguins', 'MORE TACOS']
>>>
Notice that they are in the same order as they were when you entered them. How
many are there? In order to find out, we can use a function of Python called len (for
length).
Functions are repeatable actions. If you hand a box of stuff to somebody and ask
them to count what's inside, they can do that, and tell you the number. The len
function follows exactly the same principles. We give the len function a list – or a
variable that contains a list – and it tells us how many things are inside it. We do the
giving by putting it inside parentheses ().
>>> len(groceries)
7
>>>
Okay, so we have seven items on the list. Can we get the second item on the list?
Yes! But Python does a weird counting thing. The first item is number zero, the second
item is number one, and so on. The last item on this grocery list is number six. Many
programming languages start counting at zero, so as strange as this might be, it is a
useful thing to remember.
In order to get an item from the list, we enclose the item we want after the variable
name. For the third item, we do the following:
>>> groceries[2]
'TACOS'
>>>
Remember that we start counting from zero, so the third item is at position number
two. This is commonly pronounced “groceries sub two” because we are finding an
item with a particular subscript in the array.
Now, what if we want to alphabetize the grocery list? For this task, we can ask the list
to go off and sort itself. We do so with a dot, and since it already knows what to do,
we don't have to give the sort function anything in particular, so we give it just an
empty parameter list – that is, nothing between the parentheses.
>>> groceries.sort()
>>> groceries
['MORE TACOS', 'TACOS', 'french fries', 'ketchup', 'lettuce', 'milk', 'penguins']
>>>
This idea of asking a thing to do something is very, very powerful: it allows us to model
the world around us. What if we were to write a dog, and then we could ask it to
bark(quietly)and fetch(newspaper)? After receiving the newspaper, could we ask it
if it isCoveredInSlobber()or if it isCurrent()?
At any rate, what if we want to print each item in the grocery list on a separate line?
First of all, we can print something using the print()function:
>>> print("hello")
hello
>>>
This is important because it can be used in virtually any program, not just the
interactive IDLE environment. So now we can print each item in turn.
>>> groceries[0]
'MORE TACOS'
>>> groceries[1]
'TACOS'
>>> groceries[2]
'french fries'
>>> groceries[3]
'ketchup'
I don't know about you, but I'm getting tired of typing “groceries” over and over
again. Let's take a short cut and tell the computer to do that for us. The way we go
about this is using a loop:
for item in groceries:
print(item)
This is a fairly readable syntax. Python will go through the list of groceries, and for
each thing on the list it will call it an “item” and then it will print it.
At this stage it is critical to point out a couple of syntactic elements. First of all, the
colon (:) indicates that we are not done with what we are doing. Second,
indent ation matters. Although it is simply whitespace, many of the most common
errors result from misaligned or missing indentation within program structures.
Let's do the same thing another way. Instead of iterating over some items, let's iterate
over some numbers. The following syntax lets us do this:
>>> for index in range(0, 5):
print(index)
0
1
2
3
4
>>>
Notice that the first number of the range (0) is included, but the last number of the
range (5) is excluded. Remember how len(groceries) told us there are 7 items in the
list? This works out nicely for us, because the zero-based counting scheme means
that item 6 is the last one in the list, and a range from 0 to 7 will include 6 but not 7.
Let's put the pieces together.
>>> for index in range(0, len(groceries)):
print(groceries[index])
MORE TACOS
TACOS
french fries
ketchup
lettuce
milk
penguins
>>>
Now let's number the items.
>>> for index in range(0, len(groceries)):
print(`index` + ": " + groceries[index])
0: MORE TACOS
1: TACOS
2: french fries
3: ketchup
4: lettuce
5: milk
6: penguins
That's great, but wouldn't it be nice (for humans) to start the list at 1? We could do
the following:
>>> for index in range(0, len(groceries)):
print(`index + 1` + ": " + groceries[index])
1: MORE TACOS
2: TACOS
3: french fries
4: ketchup
5: lettuce
6: milk
7: penguins
>>>
This is starting to get complicated. Let's simplify a little bit by splitting up the print
statement.
>>> for index in range(0, len(groceries)):
num = index + 1
print(`num` +": " + groceries[index])
1: MORE TACOS
2: TACOS
3: french fries
4: ketchup
5: lettuce
6: milk
7: penguins
>>>
We can take the “index + 1” from the print and put that into num, then use num in the
print instead. Notice that both lines in the loop are indented. If the indentation does
not match, Python will complain.
Decision making:
There are lots of times when we need to make a decision in a program. If you're
playing tic-tac-toe and get three in a row, then the game is over. If the damage your
character receives is less than your current hit points, then your character lives. If the
user enters invalid input, print a message that indicates what was wrong. If the train is
carrying carrots, then give it top priority; otherwise, give the train priority just behind
Amtrak. (Nobody likes spoiled carrots, after all.)
Of course, in order to make a decision, we have to be able to test things. We do this
using Boolean operators. Here is a brief list of the most common operators:
 == true if two things are equal
 != true if two things are not equal
 > true if the thing on the left is greater than the thing on the right
 < true if the thing on the left is less than the thing on the right
 >= true if the thing on the left is greater than or equal to the thing on the
right
 <= true if the thing on the left is less than or equal to the thing on the right
 && true if the thing on the left AND the thing on the right are both true
 || true if either the thing on the left OR the thing on the right is true
In Python, we can make make decisions using the if/elif/else construct.
if <condition>:
do something important
if <condition>:
do something important
else:
do something less important
if <condition>:
do something important
elif <different condition>:
do something cool
else
do something normal
For example, let's consider the numbers from 0 to 9, and determine whether they are
even or odd.
>>> for x in range(0, 10):
if x == 0:
print(`x` +": found zero!")
elif x % 2 == 0:
print(`x` +": even!")
else:
print(`x` +": odd.")
0: found zero!
1: odd.
2: even!
3: odd.
4: even!
5: odd.
6: even!
7: odd.
8: even!
9: odd.
>>>
Feel free to play with loops, lists, conditionals – these basic constructs can represent
the decision making process for a lot of problems. It's also fun to find the limits of what
Python can do. How many things can you put in a list? How long does it take to
count to a million? How long does it take if you don't print each number?
When you're satisfied, or your brains hurt, or you're out of time, pack up the Pi kits, as
normal.
3-3: Project
Objectives:
 Learn how to handle input
 Learn how to define a function
 Put together a program that acts as a “magic 8-ball”
Set up the Pi kits as normal. Start the X Windows System and open IDLE.
It can be useful to interact with a user. We have seen how to create output, but
what about handling input from the keyboard?
There are two related ways to do this. The first is the input function, which handles
numbers well:
>>> input("Number -->")
Number -->12
12
>>>
Notice how Python prints whatever you tell the input function to use, and waits for
you to type something and press enter. If you enter anything other than a number,
you will get some kind of error. [Exception: it is possible to enter valid Python code
that evaluates to a number. For example, if you type len(“hello”) at the prompt, it will
be accepted and return the number 5.]
That's great, but what if you want to hold on to what the user entered? Use the
input()function as part of a variable assignment.
>>> x = input("What is x? ")
What is x? 42
>>> x
42
>>>
Being limited to numbers only is pretty restrictive. If you want to accept anything the
user enters, you can use the raw_input function instead.
>>> x = raw_input("What is x? ")
What is x? The quick brown fox jumped over the lazy dogs.
>>> x
'The quick brown fox jumped over the lazy dogs.'
>>>
Notice, however, that if you provide a number to raw_input, it is still interpreted as a
string, so you cannot directly perform operations like “x + 1” meaningfully.
Functions:
We have seen several cases where we ask something like len() or sort()or input()to
do a task for us, repeatedly. We can define our own such tasks, and they are called
functions. In a basic sense, they have a name, they may accept some input, and
they may return a value. The easiest way to define a function is with the def keyword,
and we use the return keyword to indicate what comes back.
>>> def plus(a, b):
return a + b
As usual, when we put the colon on the end of the first line, we're saying “I'm not
done” and the subsequent lines are consistently indented. Python allows us to do just
about anything we like within a function definition, including calling (or even
creating) other functions. Now we can call our function just like any other:
>>> plus(3, 2)
5
>>>
There are lots of functions that are already defined for us, but sometimes we have to
tell Python where to find them. There is a random number generator, for example,
that we can use if we ask Python to load it:
>>> import random
The random() function gives us a decimal number between 0 and 1:
>>> random.random()
0.09922611904874357
>>> random.random()
0.5130440719955642
>>> random.random()
0.2534538950733807
>>> random.random()
0.8071376093891092
More frequently, however, we will use random numbers that are integers – consider a
die roll (1d6), for example. The function randint(min, max)helps us with that.
>>> random.randint(1, 6)
5
>>> random.randint(1, 6)
4
>>> random.randint(1, 6)
3
>>> random.randint(1, 6)
6
>>> random.randint(1, 6)
3
>>> random.randint(1, 6)
1
>>>
Now we have all the tools we need to solve some real-world problems. How about
creating a program that acts as a “Magic 8-ball” for us? Ask a question, shake the
Magic 8-ball, and it reveals to us an answer.
First, we need some answers that it can give us. Define them as a list called
“answers.”
>>> answers = ['Yes', 'No', 'Maybe', 'Ask again', '73% chance', 'Orange', "Batman", 42]
Now we need a way to pick one of the answers, randomly. We will do this
repeatedly, so define a function to do this. The input will be the answer list, and the
output will be one of the answers. We will choose a random number between 0 and
the highest index of a list item, which is len(list) – 1. Notice that the name of what we
use in the function does not have to match any existing variable name, and might
well be clearer if it purposely doesn't.
>>> def pickAnAnswer(answerList):
highest = len(answerList) - 1
index = random.randint(0, highest)
return answerList[index]
We also need a way to reveal the answer, which some appropriate print statements.
Optionally, you can make the program appear to be thinking for some period of time
(perhaps for dramatic tension?), and if you choose to do so, be sure to load the time-
related functions.
>>> import time
>>> def delay(howLong):
for x in range(0, howLong):
print('...thinking...')
time.sleep(1)
>>> def revealAnswer(question, answer, thinkingTime):
print('Considering your question: ' + question)
delay(thinkingTime)
print('The Magic 8-ball has spoken! The answer you seek is this:')
print(answer)
Next, we need a way to allow the user to ask a question. We want to be able to do
this over and over again, so another function is in order. This one will be simple.
>>> def askAQuestion(prompt):
return raw_input(prompt + ' --> ')
We have all the components in place. Now we need a loop that will allow us to keep
asking as many questions as we like, or a certain number of questions. We also need
to make sure that we initialize the question to something, so the program knows it
exists.
>>> question = 'none yet'
>>> while (question != 'exit'):
question = askAQuestion("What is your question? ")
if question != "exit":
answer = pickAnAnswer(answers)
revealAnswer(question, answer, 1)
What is your question? --> Should I exit?
Considering your question: Should I exit?
...thinking...
The Magic 8-ball has spoken! The answer you seek is this:
Ask again
What is your question? --> Should I exit?
Considering your question: Should I exit?
...thinking...
The Magic 8-ball has spoken! The answer you seek is this:
Maybe
What is your question? --> How about now?
Considering your question: How about now?
...thinking...
The Magic 8-ball has spoken! The answer you seek is this:
Orange
What is your question? --> Um, should I exit now?
Considering your question: Um, should I exit now?
...thinking...
The Magic 8-ball has spoken! The answer you seek is this:
73% chance
What is your question? --> Okay, how about now?
Considering your question: Okay, how about now?
...thinking...
The Magic 8-ball has spoken! The answer you seek is this:
Yes
What is your question? --> exit
>>>
Here is the complete text of the program.
import random
import time
answers = ['Yes', 'No', 'Maybe', 'Ask again', '73% chance', 'Orange', “Batman”, 42]
def askAQuestion(prompt):
return raw_input(prompt + ' --> ')
def pickAnAnswer(answerList):
highest = len(answerList) - 1
index = random.randint(0, highest)
return answerList[index]
def delay(howLong):
for x in range(0, howLong):
print('...thinking...')
time.sleep(1)
def revealAnswer(question, answer, thinkingTime):
print('Considering your question: ' + question)
delay(thinkingTime)
print('The Magic 8-ball has spoken! The answer you seek is this:')
print(answer)
question = 'none yet'
while (question != 'exit'):
question = askAQuestion("What is your question? ")
if question != “exit”:
answer = pickAnAnswer(answers)
revealAnswer(question, answer, 1)
The answers list can be customized, of course, and can also be modified between
runs of the program. Have some fun with it!
Pack up the kits, and revel in the knowledge that you have begun to take full control
of a Raspberry Pi.

More Related Content

What's hot

Matlab and Python: Basic Operations
Matlab and Python: Basic OperationsMatlab and Python: Basic Operations
Matlab and Python: Basic Operations
Wai Nwe Tun
 
Python 101 1
Python 101   1Python 101   1
Python 101 1
Iccha Sethi
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Two
amiable_indian
 
Python language data types
Python language data typesPython language data types
Python language data types
Hoang Nguyen
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
Dipankar Achinta
 
Learn python in 20 minutes
Learn python in 20 minutesLearn python in 20 minutes
Learn python in 20 minutes
Sidharth Nadhan
 
Python Basics
Python BasicsPython Basics
Python Basics
tusharpanda88
 
python.ppt
python.pptpython.ppt
python.ppt
shreyas_test_1234
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
Introduction to Python Language and Data Types
Introduction to Python Language and Data TypesIntroduction to Python Language and Data Types
Introduction to Python Language and Data Types
Ravi Shankar
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3
Bryan O'Sullivan
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
Rasan Samarasinghe
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)
Pedro Rodrigues
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
Paige Bailey
 
Introduction to Python programming
Introduction to Python programmingIntroduction to Python programming
Introduction to Python programming
Damian T. Gordon
 
Python unit 2 M.sc cs
Python unit 2 M.sc csPython unit 2 M.sc cs
Python unit 2 M.sc cs
KALAISELVI P
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
climatewarrior
 
basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in python
nitamhaske
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
Luigi De Russis
 
Python
PythonPython
Python
대갑 김
 

What's hot (20)

Matlab and Python: Basic Operations
Matlab and Python: Basic OperationsMatlab and Python: Basic Operations
Matlab and Python: Basic Operations
 
Python 101 1
Python 101   1Python 101   1
Python 101 1
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Two
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Learn python in 20 minutes
Learn python in 20 minutesLearn python in 20 minutes
Learn python in 20 minutes
 
Python Basics
Python BasicsPython Basics
Python Basics
 
python.ppt
python.pptpython.ppt
python.ppt
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Introduction to Python Language and Data Types
Introduction to Python Language and Data TypesIntroduction to Python Language and Data Types
Introduction to Python Language and Data Types
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
 
Introduction to Python programming
Introduction to Python programmingIntroduction to Python programming
Introduction to Python programming
 
Python unit 2 M.sc cs
Python unit 2 M.sc csPython unit 2 M.sc cs
Python unit 2 M.sc cs
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in python
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
 
Python
PythonPython
Python
 

Viewers also liked

Basic of pricing 2
Basic of pricing 2Basic of pricing 2
Basic of pricing 2
Ilya Gikhman
 
The Story of XerographyThe Story of Xerography
The Story of XerographyThe Story of XerographyThe Story of XerographyThe Story of Xerography
The Story of XerographyThe Story of Xerography
Tania Niкolova
 
Let there be light
Let there be lightLet there be light
Let there be light
geekinlibrariansclothing
 
Genre queer super slides (1)
Genre queer super slides (1)Genre queer super slides (1)
Genre queer super slides (1)
geekinlibrariansclothing
 
Magic 8 ball prorgramming or structure is fun
Magic 8 ball prorgramming or structure is funMagic 8 ball prorgramming or structure is fun
Magic 8 ball prorgramming or structure is fun
geekinlibrariansclothing
 
03443154 9a25-430a-abe0-d1181f21093d-160523145414
03443154 9a25-430a-abe0-d1181f21093d-16052314541403443154 9a25-430a-abe0-d1181f21093d-160523145414
03443154 9a25-430a-abe0-d1181f21093d-160523145414
Tania Niкolova
 
Xeroxppt 111106001808-phpapp02
Xeroxppt 111106001808-phpapp02Xeroxppt 111106001808-phpapp02
Xeroxppt 111106001808-phpapp02
Tania Niкolova
 
Introduction to pi
Introduction to piIntroduction to pi
Introduction to pi
geekinlibrariansclothing
 
Shine a little LED
Shine a little LEDShine a little LED
Shine a little LED
geekinlibrariansclothing
 
Eservizi corporate prsentation Xerox
Eservizi corporate prsentation XeroxEservizi corporate prsentation Xerox
Eservizi corporate prsentation Xerox
Tania Niкolova
 
Magic 8 ball putting it all together
Magic 8 ball  putting it all togetherMagic 8 ball  putting it all together
Magic 8 ball putting it all together
geekinlibrariansclothing
 
Xerox- Presentation of the brand Xerox
Xerox- Presentation of the brand XeroxXerox- Presentation of the brand Xerox
Xerox- Presentation of the brand Xerox
Tania Niкolova
 
Xerox corporation-fraud-case-1233641589554599-2
Xerox corporation-fraud-case-1233641589554599-2Xerox corporation-fraud-case-1233641589554599-2
Xerox corporation-fraud-case-1233641589554599-2
Tania Niкolova
 

Viewers also liked (13)

Basic of pricing 2
Basic of pricing 2Basic of pricing 2
Basic of pricing 2
 
The Story of XerographyThe Story of Xerography
The Story of XerographyThe Story of XerographyThe Story of XerographyThe Story of Xerography
The Story of XerographyThe Story of Xerography
 
Let there be light
Let there be lightLet there be light
Let there be light
 
Genre queer super slides (1)
Genre queer super slides (1)Genre queer super slides (1)
Genre queer super slides (1)
 
Magic 8 ball prorgramming or structure is fun
Magic 8 ball prorgramming or structure is funMagic 8 ball prorgramming or structure is fun
Magic 8 ball prorgramming or structure is fun
 
03443154 9a25-430a-abe0-d1181f21093d-160523145414
03443154 9a25-430a-abe0-d1181f21093d-16052314541403443154 9a25-430a-abe0-d1181f21093d-160523145414
03443154 9a25-430a-abe0-d1181f21093d-160523145414
 
Xeroxppt 111106001808-phpapp02
Xeroxppt 111106001808-phpapp02Xeroxppt 111106001808-phpapp02
Xeroxppt 111106001808-phpapp02
 
Introduction to pi
Introduction to piIntroduction to pi
Introduction to pi
 
Shine a little LED
Shine a little LEDShine a little LED
Shine a little LED
 
Eservizi corporate prsentation Xerox
Eservizi corporate prsentation XeroxEservizi corporate prsentation Xerox
Eservizi corporate prsentation Xerox
 
Magic 8 ball putting it all together
Magic 8 ball  putting it all togetherMagic 8 ball  putting it all together
Magic 8 ball putting it all together
 
Xerox- Presentation of the brand Xerox
Xerox- Presentation of the brand XeroxXerox- Presentation of the brand Xerox
Xerox- Presentation of the brand Xerox
 
Xerox corporation-fraud-case-1233641589554599-2
Xerox corporation-fraud-case-1233641589554599-2Xerox corporation-fraud-case-1233641589554599-2
Xerox corporation-fraud-case-1233641589554599-2
 

Similar to Course set three full notes

Python cheat-sheet
Python cheat-sheetPython cheat-sheet
Python cheat-sheet
srinivasanr281952
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in python
sunilchute1
 
Notes1
Notes1Notes1
Notes1
hccit
 
Notes2
Notes2Notes2
Notes2
hccit
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
priestmanmable
 
Python basics
Python basicsPython basics
Python basics
Himanshu Awasthi
 
A Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonA Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with Python
Tariq Rashid
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts
Pavan Babu .G
 
Get started python programming part 1
Get started python programming   part 1Get started python programming   part 1
Get started python programming part 1
Nicholas I
 
Python
PythonPython
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And Answers
H2Kinfosys
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
Simplilearn
 
Notes3
Notes3Notes3
Notes3
hccit
 
Learn python
Learn pythonLearn python
Learn python
mocninja
 
Chapter 4 algorithmic efficiency handouts (with notes)
Chapter 4   algorithmic efficiency handouts (with notes)Chapter 4   algorithmic efficiency handouts (with notes)
Chapter 4 algorithmic efficiency handouts (with notes)
mailund
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like Pythonista
Chiyoung Song
 
Python introduction
Python introductionPython introduction
Python introduction
leela rani
 
Write better python code with these 10 tricks | by yong cui, ph.d. | aug, 202...
Write better python code with these 10 tricks | by yong cui, ph.d. | aug, 202...Write better python code with these 10 tricks | by yong cui, ph.d. | aug, 202...
Write better python code with these 10 tricks | by yong cui, ph.d. | aug, 202...
amit kuraria
 
Python Workshop
Python  Workshop Python  Workshop
Python Workshop
Assem CHELLI
 

Similar to Course set three full notes (20)

Python cheat-sheet
Python cheat-sheetPython cheat-sheet
Python cheat-sheet
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in python
 
Notes1
Notes1Notes1
Notes1
 
Notes2
Notes2Notes2
Notes2
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 
Python basics
Python basicsPython basics
Python basics
 
A Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonA Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with Python
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts
 
Get started python programming part 1
Get started python programming   part 1Get started python programming   part 1
Get started python programming part 1
 
Python
PythonPython
Python
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And Answers
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
 
Notes3
Notes3Notes3
Notes3
 
Learn python
Learn pythonLearn python
Learn python
 
Chapter 4 algorithmic efficiency handouts (with notes)
Chapter 4   algorithmic efficiency handouts (with notes)Chapter 4   algorithmic efficiency handouts (with notes)
Chapter 4 algorithmic efficiency handouts (with notes)
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like Pythonista
 
Python introduction
Python introductionPython introduction
Python introduction
 
Write better python code with these 10 tricks | by yong cui, ph.d. | aug, 202...
Write better python code with these 10 tricks | by yong cui, ph.d. | aug, 202...Write better python code with these 10 tricks | by yong cui, ph.d. | aug, 202...
Write better python code with these 10 tricks | by yong cui, ph.d. | aug, 202...
 
Python Workshop
Python  Workshop Python  Workshop
Python Workshop
 

Recently uploaded

Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
RamseyBerglund
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
EduSkills OECD
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
National Information Standards Organization (NISO)
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
khuleseema60
 
How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
Celine George
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
Celine George
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
IsmaelVazquez38
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
سمير بسيوني
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
nitinpv4ai
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 

Recently uploaded (20)

Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
 
How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 

Course set three full notes

  • 1. Raspberry Pi: a class in several parts III: Software 3-1: Introduction to Python Objectives:  Basic familiarity with the IDLE tool  Basic data manipulation using Python syntax All the software for this sequence should already be on the Pi, as part of Raspbian. Verify that the IDLE tool is available in the X Windows System. What's the point? Giving the computer instructions, to tell it to do exactly what you want. It can't do what it doesn't know, so we humans have to decide how to teach it, and craft our programs appropriately. Games, web, documents, security, music, virtually anything is possible. Basic data manipulation: Type a number into IDLE and press enter. It examines what you told it, does it, and tell you the result. In this case, it tells you what number you entered...because there is nothing else to do. Try some basic math, e.g. 1 + 1. It tells you the answer is 2. Some basic operators are as follows:  + (addition)  - (subtraction)  * (multiplication)  / (division)  % (modulo or remainder, as in 10 mod 3 == 1)  ** (power, as in 4**2 = 4 squared = 16) Try 10 / 3. Notice that the answer is 3. Now try 10 / 3.0. Notice that the answer is 3.3333333. If you tell it that you care about decimal places, you get decimal numbers. Otherwise, you get integers. Take a few minutes to use Python as a calculator. There are some editing shortcuts that might also help at this point.  Ctrl-End will always get you back to the current prompt, if you move the cursor elsewhere.  Alt-p will put the previous entry at the prompt again. If you hit it again, it will cycle through entries previous to that as well.  Alt-n will go to the next entry. This is particularly useful if you are looking for something in particular with Alt-p, but you pass it.
  • 2.  Ctrl-c will interrupt what is currently happening, clear the current programming entry, and put your cursor at the prompt. If you don't know what is happening, this can help you reset to a known state.  Copy and paste do work. Be careful of indentation if you choose to paste, and also be careful of the >>> characters. Variables: (Attendees who have done some algebra will find this much easier.) In its basic form, a variable is a box. You can put things in the box, you can take things out of the box, you can forget what's inside, and then look to see what it is. When you carry it around, though, it's still a box. Boxes come in different shapes and sizes, but all of ours are going to be the same size for now. Let's put the number 12 in a variable, by telling Python “x = 12” – notice that Python responds by telling us absolutely nothing. Now, let's ask Python what x is: >>> x 12 >>> Next, let's use our variable in some arithmetic expressions. Try things like x + 1, x**x, 42 / x, etc. Notice that it works just like the number 12. Now, let's change x to something else. Try the following: >>> x = x - 3 >>> We can change what's in the box without actually looking at it, which is convenient. Have a look to see what its value currently is. >>> x 9 >>> You can call variables pretty much whatever you like, as long as you use letters and no spaces. Numbers are okay within a variable name, and multi-word variable names are usually distinguished by capitalizing all but the first one. So, the following are all valid: x, y, xy, george, john, paul, ringo, octopus, thisIsAVeryLongVariableName, theSong, grocerylist. Be descriptive, and try to label your boxes so you know what's inside them.
  • 3. Strings: We've seen how Python can act as a calculator with numbers, and how we can use words to name variables. What if we want Python to talk to us? If you try to tell it “hello” you get a nasty-looking error: >>> hello Traceback (most recent call last): File "<pyshell#22>", line 1, in <module> hello NameError: name 'hello' is not defined >>> Such errors are commonplace and not to be feared. They simply indicate that something went wrong, and usually provide useful information for fixing the problem. In this case, Python doesn't know anything about hello – it is trying to interpret it as a variable or other identifier. We want it to be treated as a literal string of characters, so we have to put it inside quotation marks: >>> "hello" 'hello' >>> Notice that when Python tells us 'hello' it is between a pair of apostrophes, also called single quotes. Either single or double quotation marks are acceptable, as long as they match. We can also add strings together, and we can put them in variables. For example: >>> y = 'hello' >>> y 'hello' >>> y + " world”
  • 4. 'hello world' Remember how we have the number 9 in x at the moment? It is often useful to insert the values of numeric variables into strings, but to do so we must use a different character: the backtick. It points toward the right, while the apostrophe On US keyboards, it is usually on the same key as the tilde. >>> y + " " + 'world' + `x` 'hello world9' >>> This may be a reasonable stopping point – the attention to detail required for success throughout these examples may take some effort for attendees, especially if you have some who do not have experience with algebra. Pack up the Pi kits as normal. 3-2: Structures Objectives:  Learn how to manipulate lists (arrays)  Learn how to automate repetitive actions (loops)  Learn how to make decisions in code (conditionals) Set up the Pi kits. Start the X Windows System (startx)and launch IDLE. Briefly review the syntax from the last session. We will make use of variables and strings, especially. Most of the time we don't deal with just one thing. Consider a trip to the grocery store: does the shopping list contain only one item? Sometimes, but most of the time it is rather longer than that. Python lets us represent lists of things easily. We put them between square brackets []. >>> groceries = ['milk', 'french fries', 'TACOS', 'ketchup', "lettuce", 'penguins', 'MORE TACOS'] >>> groceries ['milk', 'french fries', 'TACOS', 'ketchup', 'lettuce', 'penguins', 'MORE TACOS'] >>> Notice that they are in the same order as they were when you entered them. How many are there? In order to find out, we can use a function of Python called len (for length). Functions are repeatable actions. If you hand a box of stuff to somebody and ask
  • 5. them to count what's inside, they can do that, and tell you the number. The len function follows exactly the same principles. We give the len function a list – or a variable that contains a list – and it tells us how many things are inside it. We do the giving by putting it inside parentheses (). >>> len(groceries) 7 >>> Okay, so we have seven items on the list. Can we get the second item on the list? Yes! But Python does a weird counting thing. The first item is number zero, the second item is number one, and so on. The last item on this grocery list is number six. Many programming languages start counting at zero, so as strange as this might be, it is a useful thing to remember. In order to get an item from the list, we enclose the item we want after the variable name. For the third item, we do the following: >>> groceries[2] 'TACOS' >>> Remember that we start counting from zero, so the third item is at position number two. This is commonly pronounced “groceries sub two” because we are finding an item with a particular subscript in the array. Now, what if we want to alphabetize the grocery list? For this task, we can ask the list to go off and sort itself. We do so with a dot, and since it already knows what to do, we don't have to give the sort function anything in particular, so we give it just an empty parameter list – that is, nothing between the parentheses. >>> groceries.sort() >>> groceries ['MORE TACOS', 'TACOS', 'french fries', 'ketchup', 'lettuce', 'milk', 'penguins'] >>> This idea of asking a thing to do something is very, very powerful: it allows us to model the world around us. What if we were to write a dog, and then we could ask it to bark(quietly)and fetch(newspaper)? After receiving the newspaper, could we ask it if it isCoveredInSlobber()or if it isCurrent()? At any rate, what if we want to print each item in the grocery list on a separate line? First of all, we can print something using the print()function: >>> print("hello") hello
  • 6. >>> This is important because it can be used in virtually any program, not just the interactive IDLE environment. So now we can print each item in turn. >>> groceries[0] 'MORE TACOS' >>> groceries[1] 'TACOS' >>> groceries[2] 'french fries' >>> groceries[3] 'ketchup' I don't know about you, but I'm getting tired of typing “groceries” over and over again. Let's take a short cut and tell the computer to do that for us. The way we go about this is using a loop: for item in groceries: print(item) This is a fairly readable syntax. Python will go through the list of groceries, and for each thing on the list it will call it an “item” and then it will print it. At this stage it is critical to point out a couple of syntactic elements. First of all, the colon (:) indicates that we are not done with what we are doing. Second, indent ation matters. Although it is simply whitespace, many of the most common errors result from misaligned or missing indentation within program structures. Let's do the same thing another way. Instead of iterating over some items, let's iterate over some numbers. The following syntax lets us do this: >>> for index in range(0, 5): print(index) 0 1 2 3 4 >>> Notice that the first number of the range (0) is included, but the last number of the range (5) is excluded. Remember how len(groceries) told us there are 7 items in the list? This works out nicely for us, because the zero-based counting scheme means that item 6 is the last one in the list, and a range from 0 to 7 will include 6 but not 7.
  • 7. Let's put the pieces together. >>> for index in range(0, len(groceries)): print(groceries[index]) MORE TACOS TACOS french fries ketchup lettuce milk penguins >>> Now let's number the items. >>> for index in range(0, len(groceries)): print(`index` + ": " + groceries[index]) 0: MORE TACOS 1: TACOS 2: french fries 3: ketchup 4: lettuce 5: milk 6: penguins That's great, but wouldn't it be nice (for humans) to start the list at 1? We could do the following: >>> for index in range(0, len(groceries)): print(`index + 1` + ": " + groceries[index]) 1: MORE TACOS 2: TACOS 3: french fries 4: ketchup 5: lettuce 6: milk 7: penguins >>> This is starting to get complicated. Let's simplify a little bit by splitting up the print
  • 8. statement. >>> for index in range(0, len(groceries)): num = index + 1 print(`num` +": " + groceries[index]) 1: MORE TACOS 2: TACOS 3: french fries 4: ketchup 5: lettuce 6: milk 7: penguins >>> We can take the “index + 1” from the print and put that into num, then use num in the print instead. Notice that both lines in the loop are indented. If the indentation does not match, Python will complain. Decision making: There are lots of times when we need to make a decision in a program. If you're playing tic-tac-toe and get three in a row, then the game is over. If the damage your character receives is less than your current hit points, then your character lives. If the user enters invalid input, print a message that indicates what was wrong. If the train is carrying carrots, then give it top priority; otherwise, give the train priority just behind Amtrak. (Nobody likes spoiled carrots, after all.) Of course, in order to make a decision, we have to be able to test things. We do this using Boolean operators. Here is a brief list of the most common operators:  == true if two things are equal  != true if two things are not equal  > true if the thing on the left is greater than the thing on the right  < true if the thing on the left is less than the thing on the right  >= true if the thing on the left is greater than or equal to the thing on the right  <= true if the thing on the left is less than or equal to the thing on the right  && true if the thing on the left AND the thing on the right are both true  || true if either the thing on the left OR the thing on the right is true In Python, we can make make decisions using the if/elif/else construct. if <condition>: do something important if <condition>:
  • 9. do something important else: do something less important if <condition>: do something important elif <different condition>: do something cool else do something normal For example, let's consider the numbers from 0 to 9, and determine whether they are even or odd. >>> for x in range(0, 10): if x == 0: print(`x` +": found zero!") elif x % 2 == 0: print(`x` +": even!") else: print(`x` +": odd.") 0: found zero! 1: odd. 2: even! 3: odd. 4: even! 5: odd. 6: even! 7: odd. 8: even! 9: odd. >>> Feel free to play with loops, lists, conditionals – these basic constructs can represent the decision making process for a lot of problems. It's also fun to find the limits of what Python can do. How many things can you put in a list? How long does it take to count to a million? How long does it take if you don't print each number? When you're satisfied, or your brains hurt, or you're out of time, pack up the Pi kits, as normal. 3-3: Project
  • 10. Objectives:  Learn how to handle input  Learn how to define a function  Put together a program that acts as a “magic 8-ball” Set up the Pi kits as normal. Start the X Windows System and open IDLE. It can be useful to interact with a user. We have seen how to create output, but what about handling input from the keyboard? There are two related ways to do this. The first is the input function, which handles numbers well: >>> input("Number -->") Number -->12 12 >>> Notice how Python prints whatever you tell the input function to use, and waits for you to type something and press enter. If you enter anything other than a number, you will get some kind of error. [Exception: it is possible to enter valid Python code that evaluates to a number. For example, if you type len(“hello”) at the prompt, it will be accepted and return the number 5.] That's great, but what if you want to hold on to what the user entered? Use the input()function as part of a variable assignment. >>> x = input("What is x? ") What is x? 42 >>> x 42 >>> Being limited to numbers only is pretty restrictive. If you want to accept anything the user enters, you can use the raw_input function instead. >>> x = raw_input("What is x? ") What is x? The quick brown fox jumped over the lazy dogs. >>> x 'The quick brown fox jumped over the lazy dogs.' >>> Notice, however, that if you provide a number to raw_input, it is still interpreted as a string, so you cannot directly perform operations like “x + 1” meaningfully.
  • 11. Functions: We have seen several cases where we ask something like len() or sort()or input()to do a task for us, repeatedly. We can define our own such tasks, and they are called functions. In a basic sense, they have a name, they may accept some input, and they may return a value. The easiest way to define a function is with the def keyword, and we use the return keyword to indicate what comes back. >>> def plus(a, b): return a + b As usual, when we put the colon on the end of the first line, we're saying “I'm not done” and the subsequent lines are consistently indented. Python allows us to do just about anything we like within a function definition, including calling (or even creating) other functions. Now we can call our function just like any other: >>> plus(3, 2) 5 >>> There are lots of functions that are already defined for us, but sometimes we have to tell Python where to find them. There is a random number generator, for example, that we can use if we ask Python to load it: >>> import random The random() function gives us a decimal number between 0 and 1: >>> random.random() 0.09922611904874357 >>> random.random() 0.5130440719955642 >>> random.random() 0.2534538950733807 >>> random.random() 0.8071376093891092 More frequently, however, we will use random numbers that are integers – consider a die roll (1d6), for example. The function randint(min, max)helps us with that. >>> random.randint(1, 6) 5 >>> random.randint(1, 6) 4 >>> random.randint(1, 6) 3 >>> random.randint(1, 6)
  • 12. 6 >>> random.randint(1, 6) 3 >>> random.randint(1, 6) 1 >>> Now we have all the tools we need to solve some real-world problems. How about creating a program that acts as a “Magic 8-ball” for us? Ask a question, shake the Magic 8-ball, and it reveals to us an answer. First, we need some answers that it can give us. Define them as a list called “answers.” >>> answers = ['Yes', 'No', 'Maybe', 'Ask again', '73% chance', 'Orange', "Batman", 42] Now we need a way to pick one of the answers, randomly. We will do this repeatedly, so define a function to do this. The input will be the answer list, and the output will be one of the answers. We will choose a random number between 0 and the highest index of a list item, which is len(list) – 1. Notice that the name of what we use in the function does not have to match any existing variable name, and might well be clearer if it purposely doesn't. >>> def pickAnAnswer(answerList): highest = len(answerList) - 1 index = random.randint(0, highest) return answerList[index] We also need a way to reveal the answer, which some appropriate print statements. Optionally, you can make the program appear to be thinking for some period of time (perhaps for dramatic tension?), and if you choose to do so, be sure to load the time- related functions. >>> import time >>> def delay(howLong): for x in range(0, howLong): print('...thinking...') time.sleep(1) >>> def revealAnswer(question, answer, thinkingTime): print('Considering your question: ' + question) delay(thinkingTime) print('The Magic 8-ball has spoken! The answer you seek is this:') print(answer) Next, we need a way to allow the user to ask a question. We want to be able to do
  • 13. this over and over again, so another function is in order. This one will be simple. >>> def askAQuestion(prompt): return raw_input(prompt + ' --> ') We have all the components in place. Now we need a loop that will allow us to keep asking as many questions as we like, or a certain number of questions. We also need to make sure that we initialize the question to something, so the program knows it exists. >>> question = 'none yet' >>> while (question != 'exit'): question = askAQuestion("What is your question? ") if question != "exit": answer = pickAnAnswer(answers) revealAnswer(question, answer, 1) What is your question? --> Should I exit? Considering your question: Should I exit? ...thinking... The Magic 8-ball has spoken! The answer you seek is this: Ask again What is your question? --> Should I exit? Considering your question: Should I exit? ...thinking... The Magic 8-ball has spoken! The answer you seek is this: Maybe What is your question? --> How about now? Considering your question: How about now? ...thinking... The Magic 8-ball has spoken! The answer you seek is this: Orange What is your question? --> Um, should I exit now? Considering your question: Um, should I exit now? ...thinking... The Magic 8-ball has spoken! The answer you seek is this: 73% chance What is your question? --> Okay, how about now? Considering your question: Okay, how about now? ...thinking... The Magic 8-ball has spoken! The answer you seek is this: Yes What is your question? --> exit >>>
  • 14. Here is the complete text of the program. import random import time answers = ['Yes', 'No', 'Maybe', 'Ask again', '73% chance', 'Orange', “Batman”, 42] def askAQuestion(prompt): return raw_input(prompt + ' --> ') def pickAnAnswer(answerList): highest = len(answerList) - 1 index = random.randint(0, highest) return answerList[index] def delay(howLong): for x in range(0, howLong): print('...thinking...') time.sleep(1) def revealAnswer(question, answer, thinkingTime): print('Considering your question: ' + question) delay(thinkingTime) print('The Magic 8-ball has spoken! The answer you seek is this:') print(answer) question = 'none yet' while (question != 'exit'): question = askAQuestion("What is your question? ")
  • 15. if question != “exit”: answer = pickAnAnswer(answers) revealAnswer(question, answer, 1) The answers list can be customized, of course, and can also be modified between runs of the program. Have some fun with it! Pack up the kits, and revel in the knowledge that you have begun to take full control of a Raspberry Pi.