0% found this document useful (0 votes)
31 views

Lecture 9 Getting Started With Python 2021

Here are the steps to complete Assignment 5: 1. Print a string that uses double quotation marks inside the string: print("She said, \"Hello!\"") 2. Print a string that spans multiple lines: print("This string \nspans multiple lines") 3. Create a string and print its length using len(): name = "John" print(len(name)) 4. Print two strings, concatenate them, and print the resulting string: first = "Hello " second = "world!" print(first + second)

Uploaded by

Alex Kuhudzai
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Lecture 9 Getting Started With Python 2021

Here are the steps to complete Assignment 5: 1. Print a string that uses double quotation marks inside the string: print("She said, \"Hello!\"") 2. Print a string that spans multiple lines: print("This string \nspans multiple lines") 3. Create a string and print its length using len(): name = "John" print(len(name)) 4. Print two strings, concatenate them, and print the resulting string: first = "Hello " second = "world!" print(first + second)

Uploaded by

Alex Kuhudzai
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 9:

Getting Started with Python


Alex Kuhudzai
alexkuhudzai@gmail.com
Cape Peninsula University of Technology,
Cape Town, South Africa
Breaking it Down
 Once you understand the task that the program will perform, you begin by
breaking down the task into a series of steps. This is similar to the way you would
break down a task into a series of steps that another person can follow. For
example, suppose someone asks you how to boil water. You might break down
that task into a series of steps as follows:
1. Pour the desired amount of water into a pot.
2. Put the pot on a stove burner.
3. Turn the burner to high.
4. Watch the water until you see large bubbles rapidly rising.
When this happens, the water is boiling.
 This is an example of an algorithm, which is a set of well-defined logical steps
that must be taken to perform a task. Notice the steps in this algorithm are
sequentially ordered. Step 1 should be performed before step 2, and so on. If a
person follows these steps exactly as they appear, and in the correct order, he or
she should be able to boil water successfully.
Breaking it Down
 A programmer breaks down the task that a program must perform in a similar
way. An algorithm is created, which lists all of the logical steps that must be
taken. For example, suppose you have been asked to write a program to calculate
and display the gross pay for an hourly paid employee. Here are the steps that you
would take:
1. Get the number of hours worked.
2. Get the hourly pay rate.
3. Multiply the number of hours worked by the hourly pay rate.
4. Display the result of the calculation that was performed in
step 3.
 Of course, this algorithm isn’t ready to be executed on the computer. The steps in
this list have to be translated into code. Programmers commonly use two tools to
help them accomplish this: pseudocode and flowcharts. Let’s look at each of these
in more detail.
Pseudocode
 Because small mistakes like misspelled words and forgotten punctuation
characters can cause syntax errors, programmers have to be mindful of such small
details when writing code. For this reason, programmers find it helpful to write a
program in pseudocode (pronounced “sue doe code”) before they write it in the
actual code of a programming language such as Python.
 The word “pseudo” means fake, so pseudocode is fake code. It is an informal
language that has no syntax rules and is not meant to be compiled or executed.
Instead, programmers use pseudocode to create models, or “mock-ups,” of
programs. Because programmers don’t have to worry about syntax errors while
writing pseudocode, they can focus all of their attention on the program’s design.
Once a satisfactory design has been created with pseudocode, the pseudocode can
be translated directly to actual code. Here is an example of how you might write
pseudocode for the pay calculating program that we discussed earlier:
Pseudocode
Input the hours worked
Input the hourly pay rate
Calculate gross pay as hours worked multiplied by pay rate
Display the gross pay
 Each statement in the pseudocode represents an operation that can be performed
in Python.
 For example, Python can read input that is typed on the keyboard, perform
mathematical calculations, and display messages on the screen.
Flowcharts
 Flowcharting is another tool that programmers use to design programs. A flowchart is a
diagram that graphically depicts the steps that take place in a program. The figure blow
shows how you might create a flowchart for the pay calculating program.
Flowcharts
 Notice there are three types of symbols in the flowchart: ovals, parallelograms,
and a rectangle. Each of these symbols represents a step in the program, as
described here:
• The ovals, which appear at the top and bottom of the
flowchart, are called terminal symbols. The Start
terminal symbol marks the program’s starting point, and
the End terminal symbol marks the program’s
ending point.
• Parallelograms are used as input symbols and output symbols.
They represent steps in which the program reads input or
displays output.
• Rectangles are used as processing symbols. They represent
steps in which the program performs some
process on data, such as a mathematical
calculation.
 The symbols are connected by arrows that represent the “flow” of the program.
To step through the symbols in the proper order, you begin at the Start terminal
Input, Processing, and Output
 Input is data that the program receives. When a program receives data, it usually
processes it by performing some operation with it. The result of the operation is
sent out of the program as output.
 Computer programs typically perform the following three-step process:
1. Input is received.
2. Some process is performed on the input.
3. Output is produced.
 Input is any data that the program receives while it is running. One common form
of input is data that is typed on the keyboard. Once input is received, some
process, such as a mathematical calculation, is usually performed on it. The
results of the process are then sent out of the program as output.
Input, Processing, and Output
The String Data Type
 Strings are one of the fundamental Python data types. The term data type refers to
what kind of data a value represents. Strings are used to represent text.
 We say that strings are a fundamental data type because they can’t be broken
down into smaller values of a different type. Not all data types are fundamental.
 The string data type has a special abbreviated name in Python: str. You can see
this by using type(), which is a function used to determine the data type of a given
value.
The String Data Type
 The output <type 'str'> indicates that the value "Hello, World" is an instance of
the str data type. That is, "Hello, World" is a string.
 Strings have three important properties:
1. Strings contain individual letters or symbols called characters.
2. Strings have a length, defined as the number of characters the
string contains.
3. Characters in a string appear in a sequence, which means that
each character has a numbered position in the string.
 Let’s take a closer look at how strings are created.
String Literals
 As you’ve already seen, you can create a string by surrounding some text with
quotation marks:
string1= ‘Hello, World’ string2= “1234”
 You can use either single quotes (string1) or double quotes (string2) to create a
string as long as you use the same type at the beginning and end of the string.
 Whenever you create a string by surrounding text with quotation marks, the string
is called a string literal. The name indicates that the string is literally written out
in your code.
 The quotes surrounding a string are called delimiters because they tell Python
where a string begins and where it ends. When one type of quotes is used as the
delimiter, the other type can be used inside the string:
string3=“We’re #1!”
string4=‘I said, “Put it over by the Ilama.” ‘
String Literals
 Lets look at the following code. Take a moment to try and figure out why we got
an error message
Determining the Length of a String
 The number of characters contained in a string, including spaces, is called the
length of the string. For example, the string "abc" has a length of 3, and the string
"Don't Panic" has a length of 11.
 Python has a built-in len() function that you can use to determine the length of a
string. To see how it works, type the following into IDLE’s interactive window:

 You can also assign a variable letters to the string and write a code as below
Multi Line Strings
 One way is to break the string up across multiple lines and put a backslash (\) at
the end of all but the last line.
String Concatenation
 This is when two or more strings are joined together
String Concatenation
 The same concatenation can be used to join two related strings such as joining a
first name and a last name into a full name(as shown below)
String Indexing
 Each character in a string has a numbered position called an index. You can
access the character at the nth position by putting the number n between two
square brackets ([]) immediately after the string:

 Take note of the error message due to wrong spelling. flavor[1] returns the
character at position 1 in "fig pie", which is i. Wait. Isn’t f the first character of
"fig pie"? In Python—and in most other programming languages—counting
always starts at zero. To get the character at the beginning of a string, you need to
access the character at position 0:
String Indexing
 The following figure shows the index for each character of the string “fig pie”:
String Indexing
 Strings also support negative indices:

 The last character in a string has index -1, which for "fig pie" is the letter e. The
second to last character i has index -2, and so on. The following figure shows the
negative index for each character in the string "fig pie":
Assignment 5
 Print a string that uses double quotation marks inside the string
 Print a string that spans multiple lines
 Create a string and print its length using len()
 Print two strings, concatenate them, and print the resulting string

You might also like