In [3]:
plot(sin(x),(-6,6))
Out[3]:
In [5]:
theta = var('theta') # tells computer that theta is symbolic var
parametric_plot([cos(theta),sin(theta)], (theta,0,2*pi))
Out[5]:
In [6]:
# variables and assignment and equality
a = 7
In [7]:
a
Out[7]:
7
In [9]:
7 = b # doesn't make sense 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-a9469ff2f5f6> in <module>()
----> 1 __tmp__=var("7"); Integer = symbolic_expression(b ).function(7)# doesn't make sense

/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 [10]:
a == 7 # tests for equality 
Out[10]:
True
In [11]:
7 == a
Out[11]:
True
In [12]:
a == 8
Out[12]:
False
In [13]:
a = 7 # example of a statement
a == 7 # example of expression
Out[13]:
True
In [14]:
7 = 3 # doesn't make sense
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-29ac5c749238> in <module>()
----> 1 __tmp__=var("7"); Integer = symbolic_expression(Integer(3)).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 [15]:
7 == 3
Out[15]:
False
In [16]:
(11*3 - 77 + 101) == 3
Out[16]:
False
In [17]:
3+2*5 # order of operations: multiplication done first
Out[17]:
13
In [18]:
(3+2)*5
Out[18]:
25
In [19]:
# other comparison 
3 < 4
Out[19]:
True
In [20]:
5 > 7
Out[20]:
False
In [21]:
3<3
Out[21]:
False
In [22]:
3 <= 3
Out[22]:
True
In [ ]:
# control statements: if and for 
In [23]:
if 3<4:
    print("3 is less than 4")
3 is less than 4
In [26]:
a = 8
if a<7:
    print("a is less than 7")
In [28]:
a = 6
if a<7:
    print("a is less than 7")
    print("and that's great") # in if bloc
a is less than 7
and that's great
In [30]:
a = 8
if a<7:
    print("a is less than 7")
print("let's move on") # not in the if bloc 
let's move on
In [ ]:
# indentation is important 
In [33]:
a = 8
if a<7:
    print("a is less than 7")
else: 
    print("a is at least 7")
a is at least 7
In [35]:
# equivalently
a = 8
if a<7:
    print("a is less than 7")
if a>=7: 
    print("a is at least 7")
a is at least 7
In [ ]:
# digression: keywords (if, else, print) and variable names
In [39]:
# variable names
a=5
number = 5
the_number = 18
In [40]:
# can start with uppercase letter, but it's uncommon
Numeric_data = 8
In [41]:
a1 = 2 # allowed
In [42]:
1a = 2 # not allowed 
  File "<ipython-input-42-d96a72b3bad7>", line 1
    1a = Integer(2)
     ^
SyntaxError: invalid syntax
In [44]:
# can't have keyword as a variable name
if = 45
  File "<ipython-input-44-12314e10283a>", line 2
    if = Integer(45)
       ^
SyntaxError: invalid syntax
In [56]:
#lottery game 
a = 11
b = 13
if (a == 11) and (b == 13):  # and is logical connector
    print("you win the lottery")
if (a == 11) or (b == 13):  # or is another logical connector
    print("you got at least one number right")
you win the lottery
you got at least one number right
In [48]:
a = 11
b = 4
(a == 11) and (b == 13)
Out[48]:
False
In [49]:
True and False
Out[49]:
False
In [57]:
not True
Out[57]:
False
In [58]:
not False
Out[58]:
True
In [64]:
# test if a is NOT equal to 7
a=7
if not a==7:
    print("a is not 7")
In [68]:
a=8
a!=7
Out[68]:
True
In [71]:
(3 < 4) or ((4>19) and (5<7)) 
Out[71]:
True
In [ ]:
# loops get computer to do lots of things
# for loop
In [74]:
for i in [1,2,3,4]:
    print(i)
1
2
3
4
5
In [75]:
range(10) # returns a list of numbers 0,...,10-1
Out[75]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [77]:
for i in range(10):
    print(i)
0
1
2
3
4
5
6
7
8
9
In [82]:
for i in range(10):
    print("hello")
    print(i^2)
print("we're all done")
hello
0
hello
1
hello
4
hello
9
hello
16
hello
25
hello
36
hello
49
hello
64
hello
81
we're all done
In [84]:
# print out all numbers i between 0 and 9 such that 
# i^3 -10*i < 17
run_sum = 0
for i in range(10):
    if i^3- 10*i < 17:
        run_sum = run_sum + i
        print(i)
print(run_sum)
0
1
2
3
6
In [ ]: