In [2]:
# last time we did plot of function
plot(sin(x),(-6,6))
Out[2]:
In [4]:
# parametric plot
t=var('t') # want t to be a symbolic variable
parametric_plot([cos(t),sin(t)], (t,0,2*pi))
Out[4]:
In [5]:
# programming 
a=7 # sets a equal to 7, example of assignment
In [6]:
7=a # doesn't make sense, because 7 cannot be changed
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-95234df4a287> in <module>()
----> 1 __tmp__=var("7"); Integer = symbolic_expression(a).function(7)

/opt/sagemath-8.9/local/lib/python2.7/site-packages/sage/calculus/var.pyx in sage.calculus.var.var (build/cythonized/sage/calculus/var.c:1407)()
    122         name = args
    123     G = globals()  # this is the reason the code must be in Cython.
--> 124     v = SR.var(name, **kwds)
    125     if isinstance(v, tuple):
    126         for x in v:

/opt/sagemath-8.9/local/lib/python2.7/site-packages/sage/symbolic/ring.pyx in sage.symbolic.ring.SymbolicRing.var (build/cythonized/sage/symbolic/ring.cpp:9847)()
    867         for s in names_list:
    868             if not isidentifier(s):
--> 869                 raise ValueError(f'The name "{s}" is not a valid Python identifier.')
    870 
    871         formatted_latex_name = None

ValueError: The name "7" is not a valid Python identifier.
In [7]:
3 = 7 # doesn't make sense 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-5c7411689a3b> in <module>()
----> 1 __tmp__=var("3"); Integer = symbolic_expression(Integer(7)).function(3)

/opt/sagemath-8.9/local/lib/python2.7/site-packages/sage/calculus/var.pyx in sage.calculus.var.var (build/cythonized/sage/calculus/var.c:1407)()
    122         name = args
    123     G = globals()  # this is the reason the code must be in Cython.
--> 124     v = SR.var(name, **kwds)
    125     if isinstance(v, tuple):
    126         for x in v:

/opt/sagemath-8.9/local/lib/python2.7/site-packages/sage/symbolic/ring.pyx in sage.symbolic.ring.SymbolicRing.var (build/cythonized/sage/symbolic/ring.cpp:9847)()
    867         for s in names_list:
    868             if not isidentifier(s):
--> 869                 raise ValueError(f'The name "{s}" is not a valid Python identifier.')
    870 
    871         formatted_latex_name = None

ValueError: The name "3" is not a valid Python identifier.
In [8]:
3 == 7  # test for equality 
Out[8]:
False
In [ ]:
# 3==7 is an expression
# a=7 is a statement
In [9]:
3==3
Out[9]:
True
In [10]:
(11*7+34) == 101
Out[10]:
False
In [ ]:
# control statements if, for
In [11]:
if (11*7+34) == 101:
    print("they are equal")
In [18]:
if 11+4 == 15: 
    print("they're equal")
    print(11+4)
print("outside of block now")
they're equal
15
outside of block now
In [19]:
if 11+4 == 16: 
    print("they're equal")
    print(11+4)
print("outside of block now")
outside of block now
In [22]:
a = 7
if a==7:
    print("you won the lottery")
you won the lottery
In [23]:
a = 8
if a==7:
    print("you won the lottery")
In [25]:
a = 8
if a==7:
    print("you won the lottery")
else:
    print("you did not win the lottery")
you did not win the lottery
In [26]:
# other types of comparisons, beyond ==
3 < 7
Out[26]:
True
In [27]:
3 > 7
Out[27]:
False
In [28]:
3 < 3
Out[28]:
False
In [29]:
3 <= 3
Out[29]:
True
In [30]:
a = 8 
if not a == 7:
    print("you did not win lottery")
you did not win lottery
In [32]:
a!=7 # equivalent to not a==7
Out[32]:
True
In [43]:
# can combine True/False expressions with and 
# you win the lottery if your a,b are both right
a = 10
b = 19
if (a==11) and (b==19):
    print("you won the lottery")
if (a==11) or (b==19):
    print("you got one number right")
you got one number right
In [35]:
# variable names and keywords
this_is_a_variable = 88
In [36]:
a1 = 89
In [37]:
1a = 89 # not allowed, can't start variable name with number
  File "<ipython-input-37-5e8ff5915631>", line 1
    1a = Integer(89)
     ^
SyntaxError: invalid syntax
In [39]:
and = 1
  File "<ipython-input-39-c44fa41c2781>", line 1
    and = Integer(1)
      ^
SyntaxError: invalid syntax
In [ ]:
# cannot use reserved keywords (if, and, print, else, True, False)
# for variable names
In [44]:
# order of operations PEMDAS
3 + 2*7 
Out[44]:
17
In [45]:
(3+2)*7
Out[45]:
35
In [46]:
3 + (2*7)
Out[46]:
17
In [47]:
2==2 and (3==4 or 5==5) 
Out[47]:
True
In [48]:
True and (False or True) 
Out[48]:
True
In [49]:
True and (True)
Out[49]:
True
In [50]:
(2==2 and 3==4) or 5==5
Out[50]:
True
In [ ]:
(True and False) or True
In [ ]:
False or True
In [ ]:
# in the above, moving parentheses didn't change answer
# but sometimes
In [51]:
not (1==1 or 3==3)
Out[51]:
False
In [52]:
(not 1==1) or 3==3
Out[52]:
True
In [ ]:
# for statements (loops)
In [53]:
for i in range(10):
    print(i)
0
1
2
3
4
5
6
7
8
9
In [54]:
range(10) # shorthand for list starting at 0, going through 10-1
Out[54]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [55]:
for i in [1,3,4]:
    print(i)
1
3
4
In [56]:
for i in range(5):
    print("hello")
hello
hello
hello
hello
hello
In [57]:
# print out all numbers between 0 and 100
# whose square is less than 56
# shows power of combining loops with conditional statements
for i in range(101):
    # test whether i has square less than 56
    if i^2 < 56:
        print(i)
0
1
2
3
4
5
6
7
In [ ]: