Mini-Challenge Put this to the screen. Be imagainative; think of as many ways as you can. You should be able to get at least two.
* ** *** **** *****
This is the most straightforward way.
print("*")
print("**")
print("***")
print("****")
print("*****")
Here we use the newline characteer \n to insert
newlines. That puts it all in one string.
print("*\n**\n***\n****\n*****")
A variation on the first theme....
print("*")
print("*"*2)
print("*"*3)
print("*"*4)
print("*"*5)
Clever use of a variable here
s = "*****" print(s[0:1]) print(s[0:2]) print(s[0:3]) print(s[0:4]) print(s[0:5])
Triple-quoted simplcity
print("""*
**
***
****
*****""")
SERVER:Thu Sep 10:10:34:10Sep20> python xmasTree.py
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
SERVER:Thu Sep 10:10:52:10Sep20> !vi
vi index.php
SERVER:Thu Sep 10:10:52:10Sep20> python funcEx.py
SERVER:Thu Sep 10:11:09:10Sep20> !vi
vi index.php
SERVER:Thu Sep 10:11:09:10Sep20> python funcEx.py
File "funcEx.py", line 5
print(f"square{x} = {square(x)}")
^
SyntaxError: invalid syntax
SERVER:Thu Sep 10:11:11:10Sep20> exit
logout
Connection to utility0 closed.
(base) MAC:Thu Sep 10:11:11:listGames> B
(base) MAC:Thu Sep 10:11:11:4240> ls
convert lab1 names.txt roster.txt students week2
keethan.txt listGames roster.csv stringPlay vpn.txt
(base) MAC:Thu Sep 10:11:11:4240> mkdir functions
(base) MAC:Thu Sep 10:11:11:4240> cd functions/
(base) MAC:Thu Sep 10:11:11:functions> vi funcEx.py
(base) MAC:Thu Sep 10:11:22:functions> python funcEx.py
square(10) = 100
cube(10) = 1000
(base) MAC:Thu Sep 10:11:22:functions> !vi
vi funcEx.py
(base) MAC:Thu Sep 10:11:25:functions> time python funcEx.py
square(10) = 100
cube(10) = 1000
real 0m0.163s
user 0m0.054s
sys 0m0.037s
(base) MAC:Thu Sep 10:11:25:functions> !vi
vi funcEx.py
(base) MAC:Thu Sep 10:11:28:functions> !p
python funcEx.py
File "funcEx.py", line 13
^
SyntaxError: unexpected EOF while parsing
(base) MAC:Thu Sep 10:11:28:functions> !vi
vi funcEx.py
(base) MAC:Thu Sep 10:11:28:functions> !p
python funcEx.py
square(10) = 100
cube(10) = 1000
Traceback (most recent call last):
File "funcEx.py", line 12, in <module>
print(lawOfCosines(3,4, math.PI/2))
AttributeError: module 'math' has no attribute 'PI'
(base) MAC:Thu Sep 10:11:28:functions> !vi
vi funcEx.py
(base) MAC:Thu Sep 10:11:29:functions> !p
python funcEx.py
square(10) = 100
cube(10) = 1000
5.0
(base) MAC:Thu Sep 10:11:29:functions> !vi
vi funcEx.py
(base) MAC:Thu Sep 10:11:29:functions> python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>> help(math.sin)
>>> help(math.floor)
>>> print(math.floor.__doc__)
Return the floor of x as an Integral.
This is the largest integer <= x.
>>> math.pi = 3
>>> math.pi
3
>>>
(base) MAC:Thu Sep 10:11:31:functions> !p
python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.pi
3.141592653589793
>>>
(base) MAC:Thu Sep 10:11:32:functions> vi tryThis.py
(base) MAC:Thu Sep 10:11:33:functions> python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> pow(2,5)
32
>>> type(pow(2,5))
<class 'int'>
>>> import maht
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'maht'
>>> import math
>>> math.pow(4,5)
1024.0
>>> pow4,5)
File "<stdin>", line 1
pow4,5)
^
SyntaxError: invalid syntax
>>> pow(4,5)
1024
>>> 2**.5
1.4142135623730951
>>> 2**(1/3)
1.2599210498948732
>>>
>>> pow(2,1.5)
2.8284271247461903
>>> pow(2,-3)
0.125
>>> pow(10,-3)
0.001
>>> 10**(-3)
0.001
>>>
(base) MAC:Thu Sep 10:11:36:functions> vi tryThis.py
(base) MAC:Thu Sep 10:11:39:functions> python tryThis.py
FAIL
FAIL
(base) MAC:Thu Sep 10:11:39:functions> !vi
vi tryThis.py
(base) MAC:Thu Sep 10:11:42:functions> python tryThis.py
PASS
PASS
(base) MAC:Thu Sep 10:11:42:functions> !vi
vi tryThis.py
(base) MAC:Thu Sep 10:11:44:functions> !p
python tryThis.py
PASS
PASS
32.0
-40.0
212.0
(base) MAC:Thu Sep 10:11:44:functions> !vi
vi tryThis.py
(base) MAC:Thu Sep 10:11:46:functions> !p
python tryThis.py
PASS
PASS
32.0
-40.0
212.0
None
None
None
(base) MAC:Thu Sep 10:11:46:functions> !vi
vi tryThis.py
(base) MAC:Thu Sep 10:11:48:functions> python tryThis.py
PASS
PASS
32.0
-40.0
212.0
0.0
-40.0
100.0
(base) MAC:Thu Sep 10:11:48:functions> !vi
vi tryThis.py
(base) MAC:Thu Sep 10:12:25:functions> ls
funcEx.py tryThis.py
(base) MAC:Thu Sep 10:12:25:functions>