Skip to content

Instantly share code, notes, and snippets.

@jendiamond
Last active September 15, 2023 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jendiamond/7181752261ecd63712ac to your computer and use it in GitHub Desktop.
Save jendiamond/7181752261ecd63712ac to your computer and use it in GitHub Desktop.
Starting Out with Python 3rd Edition - Tony Gaddis Chapters 7-13

Starting Out with Python 3rd Edition Chapters 7-13 - Tony Gaddis

Chapter 7

Sequences

sequence - a sequence is an object that holds data, stored one after another. You can perform operations on a sequence to examine and manipulate the items stored in it.

A list is a sequence and a tuple is a sequence.

Lists

List - a list is an object that contains data items. Lists are mutable / changeable. The list can hold items of different data types. Lists serve the same purpose as Arrays and provide many more built-in capabilities.

element - each item stored in a list.

stuff = [1,"fork", 3, "spoon"]
print(spoon)

list() function - converts certain types of objects to list like a range

numbers = list(range(5))

step-value_numbers = list(range(1, 10, 2)

the variable numbers is assigned to be the value of the iterable range passed as an argument to the list function.
numbers will print [0,1,2,3,4]

the variable step-value_numbers is assigned to be the value of the iterable range passed as an argument to the list function. The first value is the start value of the range, the second value is the ending limit of the range and the third value is the step value - the value it will skip in the range.
Here it skips 2 so step-value_numbers will print [1,3,5,7,9]

The Repetition Operator *

* - The * is the repetition operator. It makes multiple copies of a list and joins them together.

list * n

numbers = [3] * 5
print(numbers)

>>[3,3,3,3,3]

Iterating over a List with the for Loop

numbers = [111,110,101,102]
for n in numbers:
  print(n)

Indexing

index - the index is the number at which each element of the list is located starting at zero.

negative index - you can use negative indexes to identify an element's position relative to the end of the list.
The last element of the list is always -1.

IndexError - an ~IndexError` will be raised if you call an index that is not included in the list.

The len Function

len - is a function that returns the length of a sequence such as a list.

jens_list = ["Lars", "Pikku", "Friendly", "Kathy", "David", "Jason"]
size = len(jens_list)
index = 0

while index < len(jens_list):
  print(jens_list[index])
  index += 1

Lists are Mutable

mutable - can be changed

numbers = [1,2,3,4,5]
print(numbers)
numbers[3] = 59
print(numbers)

If you want to use indexing expressions to fill a list with values you first have to create a list.

 numbers = [0] * 5
 index = 0
 while index < len(numbers):
   numbers[index] = "fill""
   index += 1

Concatenating Lists

list1 =[1,2,3,4,5]
print(list1)

list2 = [6,7,8,9]
print(list2)

list3 = list1 + list2
print(list3)

# or list1 += list2

List Slicing

p.299 slice - selects a range or span of elements from a sequence. Like a range slice also can use a step value.

list_name[start : end]
list_name[start : end  : step_value]

days = ["Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"]
mid_days = days[2:5]
print(mid_days)
print(days[4:])
print(days[1:7:2])

This prints:

['Wed', 'Thurs', 'Fri']
['Fri', 'Sat', 'Sun']
['Tues', 'Thurs', 'Sat']

Finding Items in a List with the in Operator

in operator - searches for items in a list

item in list 

List Methods and Useful Built-in Functions

  • append(item)
  • index(item)
  • insert(index, item)
  • sort()
  • remove(item)
  • reverse()

The append method

p.303 append(item) - adds items to a list. The item is attached to the end of the list.

The index method

**** -
**** -
**** -
**** -

The insert method

insert - inserts an item into a list

insert(subscript, item_to_be_inserted)

names = ["Steven, "Alex", "Zare"]
names.insert(2, "Jen")
names.insert(len(names), "Sam")

**** -
**** -
**** -

The sort method

**** -
**** -
**** -
**** -

The remove method

**** -
**** -
**** -
**** -

The reverse method

**** -
**** -
**** -
**** -

The del statement

**** -
**** -
**** -
**** -

The min and max functions

**** -
**** -
**** -
**** -

Copying Lists

list1 = [1,2,3,4]
listcopy = [] + list1

**** -
**** -
**** -
**** -

Processing Lists

**** -
**** -
**** -
**** -

Averaging the Values in a List

**** -
**** -
**** -
**** -

Passing a List as an Argument to a function

**** -
**** -
**** -
**** -

Returning a List from a Function

**** -
**** -
**** -
**** -

Processing a List

**** -
**** -
**** -
**** -

Working with Lists and Files

**** -
**** -
**** -
**** -

Two Dimensional Lists

**** -
**** -
**** -
**** -

Tuples

**** -
**** -
**** -
**** -

Converting Between Lists and Tuples

**** -
**** -
**** -
**** -
**** -
**** -
**** -

Chapter 8

Basic String Operations

**** -
**** -
**** -

Accessing the Individual Charaters in a String

Iterating over a String with a for loop

Indexing

**** -
**** -
**** -
**** -

IndexError Exceptions

**** -
**** -
**** -
**** -

The len function

**** -
**** -
**** -
**** -

String Concatenation

**** -
**** -
**** -
**** -
**** -

Strings are Immutable

**** -
**** -
**** -
**** -

String Slicing

**** -
**** -
**** -
**** -

Extracting Characters from a String

**** -
**** -
**** -
**** -

Testing, Searching and Manipulating Strings

Testing Strings with it and not in

**** -
**** -
**** -
**** -

String Methods

**** -
**** -

String Testing Methods

isalnum -
iaalpha -
isdigit -
islower -
isspace -
isupper -

Modification Methods

lower() -
lstrip() -
lstrip(char) -
rstrip() -
rstrip(char) -
strp() -
strip(char) -
upper() -

Searching and Replacing

p.355 substring - a String that appearswithin other strings
**** -
**** -

printing substring with a slice of 2---

names = ["Aaron", "Mary", "Lars", "Jen", "Dan", "Heather"]
  for i in range(0,len(names), 2):
    print(names[i:i+2])

Search and Replace Methods

endswith(substring) -

find(substring) - searches for a specified substring within a string. The method returns the lowest index of the substring. If it is not found the method returns -1.

replace(old, new) -

startswith(substring) -

**** -

Validating Characters in a Password

**** -
**** -
**** -
**** -

The Repetition Operator

**** -
**** -
**** -
**** -

Splitting a String

**** -
**** -
**** -
**** -

Chapter 9

**** -
**** -
**** -
**** -

Chapter 10

**** -
**** -
**** -
**** -

Chapter 11

**** -
**** -
**** -
**** -

Chapter 12

**** -
**** -
**** -
**** -

Chapter 13

**** -
**** -
**** -
**** -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment