Download 9._Strings

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
9.
Strings & Input
Conceptual Objectives:
1.
2.
3.
4.
Students will familiarize themselves with the Python IDLE interface.
Students will demonstrate the ability to use strings in programs.
Students will be able to concatenate string identifiers.
Students will be able to accept and use input from the user.
New Terminology:


String: A combination of characters
Concatenation: Combining two strings together
Review:
Open the PowerPoint Review from previous lesson. The following format is suggested:
1. Each student is to receive a piece of paper folded into quarters with the letters A, B, C, & D.
2. The PowerPoint is brought up with the first question.
3. The students are to individually consider the question and, upon the signal of the teacher, are
to raise their answers. This allows the teacher to quickly evaluate the students’ understanding
of the topic.
4. If there are many incorrect answers give time to the students to discuss their answers with
one another and then have someone explain the correct answer.
Anticipatory Set:
Some props are needed for this part of the lesson. Before class get a piece of string. Cut some
letters out of some cardstock and tie them to the string. Show them the string, and ask them
what that it is. It is a string of characters or a string of letters. When you talk to a computer
about strings - that is how they think: that a string is a group of letters. This visual should help
them concretely remember the definition of a string.
Content:
IDLE Interface
Can anyone remember what the PLE stands for in RUR-PLE? - Python Learning Environment.
For today’s lesson we’re going to actually move away from Reeborg’s world for a minute
and start in one of the more commonly used Python interfaces called IDLE (meaning
Integrated Development Environment).
It is true that the 3rd tab in RUR-PLE is the Python IDLE. But today we want to work in the
full version of Python 2.6. Go to Start -> Programs -> Programming -> Python 2.6 -> IDLE
(Python GUI). Once opened, you’ll notice there aren’t any play buttons or anything like that.
It works a little different than Reeborg’s world. Let’s try some stuff out. Type:
n = 24
Then hit enter. What happened? – Nothing it seems, but we actually just defined and
initialized the variable n. Type:
print n
And hit enter. What did it do? – It printed out the value of n right below where we were
typing. NOTES: So in IDLE the three greater than symbols mean that Python is waiting for
you to type in an instruction. Then that instruction is executed once you push enter.
Now let’s try to write a function. What’s the first thing I type to define a function? –def.
Type:
def helloWorld():
Then push enter. Make note that the three >>> have now changed to three …
Python recognizes that you are not yet finished with your instruction. Type:
print “Hello World!”
Once you hit enter here you get three periods … on the next line. To tell Python that this
instruction is finished, simply hit enter one more time. Make note that the … turn back into
>>>. We have now defined our first function in IDLE. Go ahead and test it by calling the
function. Type:
helloWorld()
And that’s how we roll in IDLE.
What is a String?
NOTES: A string is simply a group of symbols or characters. When we called the helloWorld()
function it printed out the words “Hello World”. This was a string. In programming, you start
and end a string with quotation marks. Variables can also be strings. Type:
myFirstName = “Bob”
print myFirstName
Concatenation
Oftentimes you will want to put two strings together. NOTES: The process of putting two
strings together is called concatenation. This is done by simply using the plus sign. Type:
myLastName = “Barker”
print myFirstName + myLastName
Make note that there is no space between the two words. You can add a space by literally
adding a space:
print myFirstName + “ “ + myLastName
Another way to concatenate strings is by using the comma notation. Simply type:
print myFirstName, myLastName
The comma automatically adds a space for you.
Guided Practice:
1. RUR_PLE Lesson 29.a - Stringy Stuff
Accepting Input from the User
What are the 3 basic processes to a normal basic computer program? 1. The program
accepts user input (could be a mouse click, a key pressed, a time reached), processes it(It
does something with the inputted material), and then gives some type of result (the answer
to a calculation or problem, etc.). Today, finally, you are going to learn how to do this. In
Python, type:
>>> yourName = raw_input(’What is your name? ‘)
What is your name? Bob
>>> print yourName
Bob
raw_input is a command that tells the computer to prompt the user to enter a value. An
important characteristic of raw_input though is that it takes whatever is inputted and places
it into a string. For example, type:
>>> yourAge = raw_input(’What is your age? ‘)
What is your age? 22
>>> print yourAge
22
>>> print yourAge+10
TypeError: cannot concatenate 'str' and 'int' objects
So what error did we get? -Cannot concatenate 'str' and 'int' objects. You can’t add strings
and integers together. We were trying to add the string ‘22’ to the number 10. That’s like
trying to add apples and oranges. They don’t mix. That is because they are of a different
data type. For now, when you want a user to enter a number use the Python keyword
input() rather than raw_input(). Type:
>>> yourAge = raw_input(’What is your age? ‘)
What is your age? 22
>>> print yourAge + 10
32
Independent Practice:
1. Input Something - I dare you! Lesson
2. Challenge Seven! Lesson
EVERYONE SHOULD DO CHALLENGE SEVEN BEFORE THE NEXT PERIOD