Blog by Frank

CS61A fun part, 2022 summer

CHAPTER 1 Building Abstractions with Functions

Object, Set, Function

In the composing programs, there is an assignment statement.

from urllib.request import urlopen

shakespeare = urlopen('http://composingprograms.com/shakespeare.txt')

# This statement is to save all words in Set words by words
words = set(shakespeare.read().decode().split())

# {} means Set, and it will print all words in "words" above
print({w for w in words})

# [::-1] enumerate each letter in a words, but -1 dictates to step backwards. 
# It will print 'olleh'
a = 'hello'
print(a[::-1])

# Then, let's add some conditions. We want some words like 
# 'reward', 'drawer' in the article, and the length of words is 6.
conditionWords = {w for w in words if len(w) == 6 and w[::-1] in words}

1.5 Control: Short-circuiting

Truth value of a logical expression can be determined without evaluating all of its subexpressions.

<left> and <right>
# if left is a false value v(0, None, False), then the expression evaluates to v.

<left> or <right>
# if left is a true value v(exclude 0, None, False), then the expression evaluates to v.