Log in / Register
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

3 Repetitive Tasks

When my children were very little I played with them and read books to them. If they were particularly entertained I would get the, “Do it again!”, command from them. And, of course, I did it or read it again. Who can say no to a three year-old when they are being so cute. They never seemed to grow tired of repetition when they found something entertaining. Eventually, I grew tired of it myself and would give them the, “One more time .. .”, warning.

Computers are very good at doing repetitive tasks, often called iteration in Computer Science lingo. Computers don't get tired and they don't get bored. Usually, when a task is repeated, it is repeated for the same type of data over and over again. For instance, sending out paychecks is a repetitive job since each employee's deductions must be computed and then a paycheck must be printed or electronically deposited. For large companies, this job would require many people since each person would only be able to compute the withholdings for a relatively small number of people. In fact, before the advent of electronic computers, the word Computer referred to people whose job it was to carry out these kinds of calculations. That certainly must have been a mundane and repetitive job. Electronic computers on the other hand don't get tired, can work around the clock, and can work at lightning speed.Repeating a task in a programming language is often called iteration or a loop. In this chapter you learn about loops in Python. You learn how to write various kinds of loops and more importantly, you learn when to write various kinds of loops.

When doing a task over and over again it is probably the case that the data that the computer needs to do its job is located in some sort of list or sequence. Python has built-in support for lists. In addition, Python also supports strings, which are sequences of characters. Since so much of what computers do are repetitive tasks, it is important to know how to repeat code and how to manipulate strings and lists. This chapter explores the use of strings and lists. You learn that strings and lists are types of objects and discover what you can do with these objects. In Computer Science sequences and iteration go hand in hand.

So, what is a string? In the first chapter a string literal was defined as any sequence of characters surrounded by either single or double quotes. A string literal is used to

Fig. 3.1 A string object

represent a specific string object in Python. So a string literal is written in a Python program when you have a specific string object that you want to use in your program. So what is an object? Every value in Python is an object. Types of objects include integers, floats, and strings. An object is a value along with methods that can either

change the value of the object or give us more information about its value.

Example 3.1 Consider the string literal “How are you?”. The letters in quotesare written to construct a string object. The string object has both a value, thestring itself, and methods that may operate on that value. If we write the codebelow we get the reference called s pointing to the string object containing“How are you?” as shown in Fig. 3.1.

s = "How are you?"

We can interact with an object by sending messages to the object. We send a message by writing the object reference or variable name, followed by a dot (i.e. a period), followed by the method we want to call on the object. In parentheses we may pass some information to the method. The additional information are called arguments. So, calling a method on an object that is pointed to by a reference with zero or more arguments looks like this:


Sometimes it helps us to think about this interaction as sending messages to the object and getting the object to respond to these messages. So sending a message to an object or calling a method on the object are the same thing. Whatever we decide to call it, the result is the same. The object's method does something for us.

Methods can either retrieve some information about an object or they can alter the object in some way. The lower and upper methods of the string class return a new copy of the string with the characters converted to lower or upper case. The strip method returns a copy of a string with leading and trailing blanks removed. All the methods on strings are provided in Chap. 10.

Example 3.2 When the following code is executed, t refers to a new string “how are you?”. Notice the first letter of the string that t refers to is now lower case. To call the method called lower() on s you write s.lower().

s ="How are you?"

t = s.lower ()

p r i n t (t)

Practice 3.1 Write a short program that asks the user to enter a sentence. Then print the sentence back to the screen with all lower case letters capitalized and all upper case letters in lower case.

Types in Python are sometimes called classes. The term class is just another name for type in Object-Oriented Programming languages. In Object-Oriented Programming (i.e. OOP) terminology a type is a class and a value is an object. These are just different names for the same thing in Python because every type is also a class and every value is an object.

Strings have many methods that can be called on them. To find out what methods you can call on a string you can use the internet and search for python string class or you can go to the Python Shell Window in the Wing IDE or some other IDE and type help(str). Remember that str is the name of the string class in Python. Chapter 10 contains a table of most of the available string operators and methods as well.

Practice 3.2 Use Chap. 10 to help you write a program that asks the user to enter “yes” or “no”. If they enter a string with any capital letters the program should print a message that says, “Next time please use all lower case letters”.

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science