Download presentation
Presentation is loading. Please wait.
1
Chapter 4: For Loops, Strings, and Tuples
C10061 – Introduction to Computer Programming Kent State University Chapter 4: For Loops, Strings, and Tuples
2
Chapter Topics Covered
A new look at constants Sequences For loops and their uses String functions Sequence operators Indexing sequences String immutability Slicing sequences Tuples Chapter 4: For Loops, Strings, and Tuples
3
A New Look at Constants Recall that constants are variable values that are unchangeable. Valuable in two ways: Make programs clearer and easy to interpret Prevents retyping or mistyping of the same value New twist to our current naming convention—constants use all capital letters Example: string = “Hello, World!” STRING = “Hello, World!” Chapter 4: For Loops, Strings, and Tuples
4
Sequences A sequence is an ordered list of elements.
The elements are arranged in order from the first element in the sequence to the last element. Example: Given five elements in a sequence, the elements are arranged as: Computer’s reference Element 1 Element 2 Element 3 Element 4 Element 5 Chapter 4: For Loops, Strings, and Tuples
5
Sequences One type of sequence is a string.
Every character of the string is an element. Example: Given the string “index”, the sequence elements would be as follows: Computer’s reference i n d e x Chapter 4: For Loops, Strings, and Tuples
6
For Loops Repetitious and execution is based on a sequence.
Unlike while loops that execute based on a condition. Provides for sequential access of data. From element 1 to element x. Each element is processed in the loop body. The last element processed ends the loop. Chapter 4: For Loops, Strings, and Tuples
7
For Loops Loop variables do not need to be part of the loop body.
Actions inside the loop body may not use the variable. Less restrictive in Python than in other programming languages. Other languages require a counter variable to control the for loop (includes generics like i, j, k). Python allows for direct access through the sequence—no counter variable is used. Chapter 4: For Loops, Strings, and Tuples
8
For Loops Syntax for variable-name-for-elements in sequence-to-process: loop body Required colon Sequence operator Reserved word in Python Chapter 4: For Loops, Strings, and Tuples
9
For Loop Uses Counting with sequences: range( ) function
One argument—positive number Sequence range generated is 0 < positive number Example: range(7) will generate the sequence of [0, 1, 2, 3, 4, 5, 6] Chapter 4: For Loops, Strings, and Tuples
10
For Loop Uses Counting with sequences: range( ) function
Three arguments—starting point, ending point, number to count by Sequence range generated is beginning point < ending point with intervals determined by number to count by Example: range(5,-5,-1) will generate the sequence of [5, 4, 3, 2, 1, 0, -1, -2, -3, -4] Chapter 4: For Loops, Strings, and Tuples
11
String Functions Counting sequence lengths: len( ) function
One argument needed Argument may be any sequence A specific string Result of an I/O read The length of the sequence is returned The number of elements in the sequence Includes all characters, spaces and symbols Chapter 4: For Loops, Strings, and Tuples
12
Sequence Operators Checking existence or conditions: in operator
Use anywhere in a Python program To check if an element is a member of a sequence (single member only or an error). To create a condition to check. Syntax: for element-to-check-for in sequence-to-check: Syntax: if element-to-check-for in sequence-to-check: Chapter 4: For Loops, Strings, and Tuples
13
Indexing Sequences Recall that all sequences are stored sequentially, one element after another. Recall that all sequences are stored sequentially, one element after another. What if you want to access the fourth element only? Use indexing for random access. Computer’s reference Element 1 Element 2 Element 3 Element 4 Element 5 Chapter 4: For Loops, Strings, and Tuples
14
Indexing Sequences Steps needed: Assign a sequence to a variable.
Specify the index location as part of the variable. Positive index is for sequential or positive random Negative index is for negative random Example: variable-name[x] where x is the specific element to access. Chapter 4: For Loops, Strings, and Tuples
15
Indexing Sequences Note: Computers begin counting at zero.
The range of sequence elements is 0 to sequence length – 1. Attempting to access beyond the bounds of a sequence will result in an IndexError. Positive index Element 1 Element 2 Element 3 Element 4 Element 5 Negative index Chapter 4: For Loops, Strings, and Tuples
16
String Immutability Sequences may be changeable (mutable) or unchangeable (immutable). Strings fall into the immutable category. If we attempt to change the element in a string through an assignment statement, an error is generated (TypeError). Example: string = “name” string[0] = “s” will generate an error Chapter 4: For Loops, Strings, and Tuples
17
String Immutability To “change” a string, create a new string.
Each character of a string is an element. Elements may be concatenated, thus creating a new string. May be accomplished through a for loop and by using the augmented assignment (+=) operator. Example: user_input = raw_input(“Enter some text: “) new_string = “ “ for i in user_input: new_string =+ i print new_string Chapter 4: For Loops, Strings, and Tuples
18
Slicing Sequences Used to copy continuous sections of elements (slices) in a sequence. Copy a single element like indexing Copy all elements or the entire sequence Copy partial elements within a range Elements are now blocked in by two points A beginning slice point An ending slice point Slice points may be positive or negative Chapter 4: For Loops, Strings, and Tuples
19
Slicing Sequences Example of slicing begin and end points:
Both may be positive Both may be negative One may be positive and one may be negative Element 1 Element 2 Element 3 Element 4 Element 5 Chapter 4: For Loops, Strings, and Tuples
20
Slicing Sequences Avoid the impossible slice
Results from starting point > ending point Results from backward references Produces an empty slice (no error generated) Examples: [4:3], [-1,-3], [1:-5], [-2:0] Element 1 Element 2 Element 3 Element 4 Element 5 Chapter 4: For Loops, Strings, and Tuples
21
Slicing Sequences: Shorthand
Omit the start point [:3] Starts at the first element of the sequence Omit the end point [1:] Ends at the last element of the sequence Omit both the start point and the end point [:] Returns a “copy” of the entire sequence Useful for quick copying and efficient programming Chapter 4: For Loops, Strings, and Tuples
22
Tuples Another type of sequence where the elements may be any of the following: Strings Numbers (integer or float) Strings and numbers combined Any type represented by a variable Chapter 4: For Loops, Strings, and Tuples
23
Tuples Empty tuple Tuple with values
Create by naming the tuple and enclosing the values within ( ) Tuple with values Create by separating values by commas and enclosing them within ( ) Example: empty_tuple = ( ) Example: string_tuple = (“value1”, “value2”) number_tuple = (7,10) combo_tuple = (1,”text”) Chapter 4: For Loops, Strings, and Tuples
24
Tuples Print tuple elements directly and the result will appear on a single line. Example: tuple = (1, “For”, “you and me”) print tuple will generate the result of (1, ‘For’, ‘you and me’) Chapter 4: For Loops, Strings, and Tuples
25
Tuples Print tuple elements using a loop and the elements will print on separate lines. Example: tuple = (“I plan”, “to program”, “in Python”) for i in tuple: print i will generate the result of I plan to program in Python Chapter 4: For Loops, Strings, and Tuples
26
Tuples What else may be done with tuples?
Everything that is done with sequences Obtain a length Check for a condition by using the in operator Index sequentially or randomly Slice Concatenate Final note: Tuples are immutable so copying a tuple requires creating a new tuple. Chapter 4: For Loops, Strings, and Tuples
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.