Docsity

Lecture Notes for CPSC 121 (Fall 2012) - Slices and Boolean Values, Study notes of Computer Science

Partial preview of the text

Download Lecture Notes for CPSC 121 (Fall 2012) - Slices and Boolean Values and more Study notes Computer Science in PDF only on Docsity!

Today ...

  • Slices (cont.)
  • Boolean values

Homework

  • HW 8 due
  • HW 9 out

Slices always return new (shallow copied) lists

list3 = [’a’, [’b’, ’c’], ’d’]

list4 = list3[1:3]

list [[’b’, ’c’], ’d’]

list4[0][1] = ’e’

list [[’b’, ’e’], ’d’]

list [’a’, [’b’, ’e’], ’d’]

This is also true of list concatenation

list5 = [1, [10, 100]]

list6 = list5 + [2]

list [1, [10, 100], 2]

list5[1][0] = 50

list [1, [50, 100]]

list [1, [50, 100], 2]

List Slice Exercises

Assume that:

my_str = "go bulldogs"

Define expressions that Use slices to generate the following strings:

  1. "go"
  2. "bulldogs"
  3. "bull"
  4. "dog"
  5. "gu"
  6. "bulldogs go"
  7. "bull-dogs"

Boolean values

What is a Boolean value?

  • A value representing either true or false
  • After George Boole (logician that invented Boolean logic, circa 1847)

Python has special Boolean values

  • True represents true
  • False represents false
  • Both are of type bool

type(True) <class ’bool’>

type(False) <class ’bool’>

A detail of Python bool values

  • bool is a special type of int
  • You can think of True as 1 and False as 0