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

These lecture notes cover the topics of slices and boolean values in the context of the cpsc 121 (fall 2012) course. The notes explain how slices return new lists, the use of the full slice operator, and list slice exercises. Additionally, the notes discuss boolean values, their significance, and python's implementation of them.

Typology: Study notes

2012/2013

Uploaded on 09/28/2013

noob
noob 🇮🇳

4.4

(25)

105 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture Notes CPSC 121 (Fall 2012)
Today ...
Slices (cont.)
Boolean values
Homework
HW 8 due
HW 9 out
S. Bowers 1 of 5
pf3
pf4
pf5

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