wk0.1 A curious case of conditionals

Intro to git and github

Feedback form: https://docs.google.com/forms/d/1A0rjsJUUBlQTXOpHN0wGglDEG4Mte5DorovPTS_cbsI/viewform?usp=send_form

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 [55]:
4 is 2**2


Out[55]:
True

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

In [63]:
# a in range(10)

assert a == 1
30 in range(10)


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-63-5814fa79f306> in <module>()
      1 # a in range(10)
      2 
----> 3 assert a == 1
      4 30 in range(10)

AssertionError: 
  • and, or

In [83]:
x = False
y = 0

x/y and x


---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-83-4bd792ffccf8> in <module>()
      2 y = 0
      3 
----> 4 x/y and x

ZeroDivisionError: division by zero
  • 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 [90]:
a = [1, 2, 3]
L = [4, 5, 6]

a[len(a):]


Out[90]:
[]
  • Insert elements into a list at least two different ways.

In [96]:
a[:4] + ['b'] + a[4:]


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

In [105]:
str1 = "hello"
str1 = list(str1)
str1.insert(3, 'demo')
str1 = ''.join(str1)
str1

# str1 = "Hello world"
# str1 = str1[:3] + 'llllll' + str1[4:]
# str1


Out[105]:
'heldemolo'
  • Remove an item from a list by value.

In [110]:
lst  = [1 , 2, 3, 4]
lst.remove(3)
lst


Out[110]:
[1, 2, 4]
  • Remove an item from a list by index.

In [114]:
lst.pop(0)
lst


Out[114]:
[4]
  • Remove all items from a list two different ways.

In [121]:
lst = [1, 2, 3, 4]

lst.clear()
lst


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

In [123]:
lst = [1, 2, 2, 3, 4]
lst.index(2)


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

In [126]:
lst.count(1)


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

In [143]:
lst = [1, 2, 3]
lst.append(5)

lst


Out[143]:
[1, 2, 3, 5]

In [146]:
str1 ='abc'
b = list(str1).append(5)
b


Out[146]:
['a', 'b', 'c', 5]
  • Capitalize all elements in a string

In [166]:
def %(duh):
    """ Append 5 to the end of the list"""
    return duh + '5'


  File "<ipython-input-166-ebe37b1dcb76>", line 1
    def %(duh):
        ^
SyntaxError: invalid syntax

In [ ]:
def area(xadsf787adf, asdfjljk4):
    return xadsf787adf*asdfjljk4

In [164]:
foo = 7
bar = 10
area(foo, bar)


Out[164]:
70

In [152]:
str1 = 'asdf'
#string_changer(str1)


Out[152]:
'asdf5'
  • Strip leading, trailing, and all whitespace (ex. leading: ' asdf ' --> 'asdf ', trailing:' asdf ' --> ' asdf', all:' asdf ' --> 'asdf').

In [188]:
def namer(n, a):
    print('My name is {name} and my age is {age}'.format(age=a, name=n))
    
namer('Richie', 8)


My name is Richie and my age is 8

In [ ]:
str1.split()
  • Split a sentence into a list of words (no whitespace), how would you split on comma or semi-colon?

  • Read up on how to format strings

Intro to pins assignment

  • Intro to try/except and error handling
    • Syntax errors vs. exceptions

In [ ]:
10 * (1/0)

In [ ]:
4 + spam*3

In [ ]:
'2' + 2

Handing exceptions


In [ ]:
while True:
    try:
        x = int(input("Please enter a number: "))
        break
    except ValueError:
        print("Oops!  That was no valid number.  Try again...")