Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 5215
#Solution from FirstSteps
#Exercise 1 (ws) Evaluate the number e with 15 decimal digits e; N(e);e.n(digits=15)
e 2.71828182845905 2.71828182845905
#Exercise 2 (ws) (a)Evaluate three built-in functions of your choice for inputs of your choice. # (b) Explain why SageMath refuses to give the result for exp(3) and force it to evaluate the function exp(3.);
20.0855369231877
#Exercise 3 (ws) #Remove the pound sign before the command "parametric_plot", and create a parametric plot using the #command (choose your favorite color and specify the size of the plot). #Save the plot in a file with name 'my_first_plot.png' parametric_plot((cos(x),sin(x)^3),(x,0,2*pi),color="lightblue",figsize=[5 ,5 ])
#Exercise 4. Define a function that finds the minimal element of the list. Test your function on example of your choic
def minimal_el(L): m=L[0] for j in range(1,len(L)): if L[j]<m: m=L[j] return m c=range(1,10) minimal_el(c) a=[i^2 for i in range (2,4)] minimal_el(a)
1 4
#Exercise 5. Experiment with commands in the file sage-quickref-BasicMath.pdf. Include two examples using commands of your choice. Your commands should be different from arithmetic operations and the commands used in this worksheedef
#ex1 factor(8.)
8.00000000000000
#ex2 plot(x^3+2*x-4,-5,5,figsize=[4,3])
-------------------------------------------------------------------------------------------------------- #Solutions from NB
#Exercise 1. (a) Make a list L of length 10. #(b) Select 4th element of L; make a sublist of elements with indices from 4 to 8. #(c) Learn command "append" and append one element to your list. L=[0,1,2,3,4,5,6,7,8,9] L.append(10) print L print(L[4:8]) print(L[6])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [4, 5, 6, 7] 6
#Exercise 2. Design a similar exercise for tuples tuple(n^3 for n in range(20) if n%3==0)
(0, 27, 216, 729, 1728, 3375, 5832)
#Exercise 3. Design a set of tuples L=set({5,10,12,24,25,24,5});L
set([24, 25, 10, 12, 5])
#Exercise 4. Make a dictionary. grade = {'cat' : 1, 'dog' : 2}; grade
{'dog': 2, 'cat': 1}
#Exercise 5. Make two strings and concatenate them to obtain a meaningful word. str1='Dogs are ';str2='awesome!' str1+str2
'Dogs are awesome!'
#Exercise 6. Write a snippet of code that takes an integer and determines if the integer is odd or even. #Note: this is an example of a user-defined function. def my_parity(n): if n%2==0: print n,'is even' else: print n,'is odd' my_parity(17) my_parity(88)
17 is odd 88 is even
#Exercise 7. def add_numbers(x,y,z=None): if (z==None): return x+y else: return x+y+z print(add_numbers(1, 2)) print(add_numbers(1, 2, 3))
3 6