wk0.1 A curious case of conditionals

Intro to git and github

Read these:

A few challenges

Complete reverse_string and primes challenges in challenge folder.

Reviewing some ideas from yesterday

# Example from yesterday def t(num): if 10 <= num < 15: print("hot") elif num > 15: print("hotter") else: print("cold") # What does this function do? for i in range(4): for j in range(10): if j % 3 == 0: continue if j > 7 and j % 2 == 0: break else: print('i equals', i) print('j equals', j) print('------------')

In [8]:
def t(num):
    if 10<= num < 15:
        print("hot")
    elif num > 15:
        print("hotter")
    else:
        print("cold")
t(5)


cold

In [12]:
i in range(4):
    for j in range(10):
        if j % 3 == 0:
            continue
        if j > 7 and j % 2 == 0:
            break
        else:
            print('i equals', i)
            print('j equals', j)
            print('-----------')


  File "<ipython-input-12-3b39866f40bc>", line 1
    i in range(4):
                 ^
SyntaxError: invalid syntax

In [27]:
a = 1
assert a == 1

While Conditional.: return discussion

more on conditionals:

Try evaluating each of the lines below in your python REPL. What's going on?

a = [1, 2, 3]
b = a
b is a
b == a
b = a[:]
b is a
b == a

If you have a hypothesis about the difference between is and ==, devise some tests you could try out to disprove your hypothesis. Implement them. Were you correct?

  • in and not in
  • and, or
  • comparison priority

    not has the highest priority and or the lowest, so that A and not B or C is equivalent to (A and (not B)) or C.

  • Short circuit operators

More string and list practice

  • Add elements from a list L to the end of another list at least three different ways.

In [ ]:
l = [1, 2, 3]
b = [4, 5, 6]

l+b
l[:]+b[:2]
l[::-1]+b[1:]

l.extend(b)

l
  • Insert elements into a list at least two different ways.

In [51]:
l = [1, 2, 3]
b = [4, 5, 6]

b[2] = 3
b[::-1] = l
b[2]=l[1]
b


Out[51]:
[3, 2, 2]
  • Insert an element into a string (this might take more than one step...).

In [8]:
l = [1, 2, 3]
str2 = 'World!'

str2+str(l[2])

str2 = list(str2)
str2.insert(3, 'HI')
str2 = ''.join(str2)
str2

str1 = 'Hello World!'
str1 = str1[:3] + 'lllll' + str1[4:]
str1


Out[8]:
'Hellllllo World!'
  • Remove an item from a list by value.

In [12]:
l = [1, 2, 3]
l.remove(2)
l


Out[12]:
[1, 3]
  • Remove an item from a list by index.

In [13]:
l = [1, 2, 3]
l.pop(0)
l


Out[13]:
[2, 3]
  • Remove all items from a list two different ways.

In [20]:
l = [1, 2, 3]
del l[:]
l.clear()
l


Out[20]:
[]
  • Return the index in the list of the first item whose value is x.

In [23]:
l = [1, 2, 3]
l.index(2)


Out[23]:
1
  • Return the number of times x appears in the list.

In [25]:
l.count(1)


Out[25]:
1
  • Capitalize the first element in a string

In [35]:
str1 = 'hello'
str1.capitalize()


Out[35]:
'Hello'
  • Capitalize all elements in a string

In [41]:
str1.upper()


Out[41]:
'HELLO WORLD !'
  • Strip leading, trailing, and all whitespace (ex. leading: ' asdf ' --> 'asdf ', trailing:' asdf ' --> ' asdf', all:' asdf ' --> 'asdf').

In [62]:
str1 = 'Hello World ! '
str1.strip().strip('H').strip('')


Out[62]:
'ello World !'
  • Split a sentence into a list of words (no whitespace), how would you split on comma or semi-colon?

In [82]:
a = 'Split a sentence into a list'
a.split()

a = a.replace(' ', ':')

a.split(':')


Out[82]:
['Split', 'a', 'sentence', 'into', 'a', 'list']
  • Read up on how to format strings