plot(sin(x),(-6,6))
theta = var('theta') # tells computer that theta is symbolic var
parametric_plot([cos(theta),sin(theta)], (theta,0,2*pi))
# variables and assignment and equality
a = 7
a
7 = b # doesn't make sense
a == 7 # tests for equality
7 == a
a == 8
a = 7 # example of a statement
a == 7 # example of expression
7 = 3 # doesn't make sense
7 == 3
(11*3 - 77 + 101) == 3
3+2*5 # order of operations: multiplication done first
(3+2)*5
# other comparison
3 < 4
5 > 7
3<3
3 <= 3
# control statements: if and for
if 3<4:
print("3 is less than 4")
a = 8
if a<7:
print("a is less than 7")
a = 6
if a<7:
print("a is less than 7")
print("and that's great") # in if bloc
a = 8
if a<7:
print("a is less than 7")
print("let's move on") # not in the if bloc
# indentation is important
a = 8
if a<7:
print("a is less than 7")
else:
print("a is at least 7")
# equivalently
a = 8
if a<7:
print("a is less than 7")
if a>=7:
print("a is at least 7")
# digression: keywords (if, else, print) and variable names
# variable names
a=5
number = 5
the_number = 18
# can start with uppercase letter, but it's uncommon
Numeric_data = 8
a1 = 2 # allowed
1a = 2 # not allowed
# can't have keyword as a variable name
if = 45
#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")
a = 11
b = 4
(a == 11) and (b == 13)
True and False
not True
not False
# test if a is NOT equal to 7
a=7
if not a==7:
print("a is not 7")
a=8
a!=7
(3 < 4) or ((4>19) and (5<7))
# loops get computer to do lots of things
# for loop
for i in [1,2,3,4]:
print(i)
range(10) # returns a list of numbers 0,...,10-1
for i in range(10):
print(i)
for i in range(10):
print("hello")
print(i^2)
print("we're all done")
# 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)