Truthiness and None
Here is the None
object.
>>> type(None)
<class 'NoneType'>
Here we cast to boolean to see the truthiness of objects.
>>> bool(1)
True
>>> bool(o)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'o' is not defined
>>> bool(0)
False
>>> bool(42)
True
>>> bool(-42)
True
>>> bool(0.0)
False
>>> bool(1.0)
True
>>> ##zero is falsy, nonzero is truthy
>>> bool("")
False
>>> bool("ewq")
True
>>> #empty strings are falsy
>>> bool([])
False
>>> bool([5])
True
>>> bool((1,2,3))
True
>>> bool(5,)
True
>>> bool((5,))
True
the symbol table is a dictionary of all variables and their values.
Before you make any variables, the symbol table consists of Python's linguisitic infrastructure.
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
Now make a variable and watch what happens. Our variable appears in the symbol table.
>>> x = 5
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'x']
This is no surprise.
>>> y = 4
>>> print(x + y)
9
Here is the symbol table.
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'x', 'y']
print
and its Many Powers (§ 8)
# what does this do?
# this is the comment token
# python ignores everything from it to the end of the line.
print("Hello")
print(1,2,3, True, "cows", [1,2,3])
print(1,2,3, True, "cows", [1,2,3], sep = " * ")
print(1,2,3, True, "cows", [1,2,3], sep = "|")
print(1,2,3, True, "cows", [1,2,3], end = "FOO")
print("a", end="")
print("b", end="")
print("a", end="")
print("c", end="")
print("d", end="")
The Symbol Table and Expressions (§ 9)
When you create a variable you write on the symbol table.
Think of two ways or more that you can make the little triangle of stars
* ** *** **** ***** ******
Here are the most straighforward solutions.
print("*")
print("**")
print("***")
print("****")
print("*****")
print("******")
print('''*
**
***
****
*****
******''')
These two are oily but cute.
#Ari E
print('*','**','***','****','*****','******', sep ='\n')
print("*\n**\n***\n****\n*****\n******")
Here comes the hotdog parade.
#Riley
for i in range(7):
print("*" * i)
# Chris A
print('\n'.join(['*'*i for i in range(1, 7)]))
#Al Pagar
star = "*"
for i in range(6):
print(star)
star = star + "*"
Now try this.
* * * * * * * * * * * * * * * * * * * * * * * * *
Here are two easy ways.
#Ari E
print('''
*
* *
* * *
* * * *
* * * * *
* * * * * *
* *
* *''')
#Me
print("" *")
print("" * *")
print("" * * *")
print("" * * * *")
print("" * * * * *")
print("" * * * * * *")
print("" * *")
print("" * *")
This works but it's a wee bit gamy.
print(" *\n * *\n * * *\n * * * *\n * * * * *\n * * * * * *\n* * * * * * * \n * *\n * *")
Now the hotdogs come out to play!
for i in range(1,7): print(' '*(7-i), ' ', ('* '*i))
for i in range(1, 3): print(' '*7, '* *')
for i in range(1, 9):
if i > 6:
print(f'{"* *":^30}')
else:
spaced = ('* '*i).strip()
print(f'{spaced:^30}')
print(" *\n * *\n * * *\n * * * *\n * * * * *\n * * * * * *\n* * * * * * * \n * *\n * *")