SageMath Lecture 8
SageMath Lecture 8
Out[50]: 11.0
In [53]: a = int(input('Enter the value of a:'))
b = int(input('Enter the value of b:'))
s=a+b
print("the sum is")
s
Enter the value of a:5
Enter the value of b:8
the sum is
Out[53]: 13
In [55]: a = float(input('Enter the value of a:'))
b = float(input('Enter the value of b:'))
p=a*b
print("the product is")
p
Enter the value of a:15
Enter the value of b:43
the product is
Out[55]: 645.0
1
0.1.2 $. Generating sequence of numbers
0.1.3 Lists
In [56]: L = [2,3,5,7,11,13,17,19,23,29]
In [57]: len(L)
Out[57]: 10
In [58]: L[0]
Out[58]: 2
In [59]: L[1:5]
In [60]: L[9]
Out[60]: 29
In [62]: L[-1]
Out[62]: 29
In [63]: L[-3]
Out[63]: 19
In [64]: L.append(32);L
In [65]: L[2]=10
L
Out[65]: [2, 3, 10, 7, 11, 13, 17, 19, 23, 29, 32]
In [66]: M=[5,17,26,32,85]
In [67]: L+M
Out[67]: [2, 3, 10, 7, 11, 13, 17, 19, 23, 29, 32, 5, 17, 26, 32, 85]
In [70]: A=[[1,2,3],[4,5,6],[7,8,9]]
A
In [71]: list(range(10))
Out[71]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2
In [72]: list(range(1,30,4))
Out[72]: [1, 5, 9, 13, 17, 21, 25, 29]
In [73]: list(srange(1,10,1/2))
Out[73]: [1, 3/2, 2, 5/2, 3, 7/2, 4, 9/2, 5, 11/2, 6, 13/2, 7, 15/2, 8, 17/2, 9, 19/2]
In [6]: list(srange(1,6,0.8))
Out[6]: [1.00000000000000,
1.80000000000000,
2.60000000000000,
3.40000000000000,
4.20000000000000,
5.00000000000000,
5.80000000000000]
0.1.4 Tuples
In [74]: T=(34,56,35,78,98,67)
In [76]: len(T)
Out[76]: 6
In [77]: T[0]
Out[77]: 34
In [78]: T[2:4]
Out[78]: (35, 78)
3
0.1.6 $. Use of conditionals
0.1.7 If statement
In [25]: x=2021
if(x.is_prime()):
print("the number", x, " is prime!")
else:
print("the number", x, " is NOT prime!")
In [24]: x = 2015
if x.is_prime():
print(x, "is prime")
else:
print(x, "=", x.factor())
2015 = 5 * 13 * 31
In [85]: a=3.56
b=3.56
if a>b:
print("a is greater than b")
elif a==b:
print("a is equal to b")
else:
print("a is less than b")
a is equal to b
In [86]: a=8
b=15
c=10
if a>b and a>c:
print("a is greater than b and c")
elif b>c and b>a:
print("b is greater than c and a")
elif c>a and c>b:
print("c is greater than a and b")
4
print(year,"is a leap year")
elif (year%400 == 0):
print(year,"is a leap year")
else:
print(year, "is NOT a leap year")
Enter year:2016
2016 is a leap year
Enter a:25
Enter b:46
Enter c:33
b is greater than c and a
5
In [92]: def quadratic(a,b,c):
disc = b**2 - 4*a*c
if(disc>=0):
print("Roots are real")
x1 = (-b+sqrt(disc))/(2*a)
x2 = (-b-sqrt(disc))/(2*a)
print("x1=",x1, "x2 = ",x2)
else:
print("roots are imaginary")
In [93]: quadratic(1,6,9)
Enter x:3.2
for x= 3.2 f(x)= -32.71212321324248
for k= 0 , s= 0
for k= 1 , s= 1
for k= 2 , s= 4
for k= 3 , s= 9
for k= 4 , s= 16
In [15]: s=0
for i in range (1,5):
s=s+i
s=5*s
print(s)
6
970
In [16]: s=0
for i in range (1,5):
s=s+i
s=5*s
print(s)
50
In [20]: s=0
for x in srange(0.3,1.5,0.1):
s=x+1/x
print("The sum =",s)
Enter n100
The sum of first n natural numbers = 5050
7
In [14]: n=int(input("Enter n"))
s=0
i=1
while i<=n:
s=s+i
i=i+1
print("s=",s)
Enter n20
s= 210
Enter n10
The sum of first n natural numbers = 25
r = a%b
while r != 0:
a=b
b=r
r = a%b
print"gcd=",b
enter a35
Enter b95
gcd= 5
if marks>=90:
print("Grade = A")
if marks>=80 and marks<90:
print("Grade = B")
8
if marks>=70 and marks<80:
print("Grade = C")
if marks>=60 and marks<70:
print("Grade = D")
if marks>= 0 and marks<60:
print("Grade = F")
if marks <0:
print("error!")
Enter Marks70
Grade = C
Enter n10
Required sum =2.92896825396825
Enter n20
20!=2432902008176640000
9
elif i>sqrt(n):
print("{} is prime".format(n))
break
Enter n2057
2057 is not prime
0.1.19 Program to find the GCD of two numbers usig Euclidean Algorithm
In [49]: a=int(input("Enter a"))
b=int(input("Enter b"))
if a==0 and b==0 :
print("GCD undefined")
elif a*b==0 :
print("GCD = {}".format(max(abs(a),abs(b))))
else :
r=a%b
while r!= 0 :
a=b
b=r
r=a%b
print("GCD={}".format(b))
Enter a36
Enter b84
GCD=12
0.1.20 Program to find all the primes between two given integers.
In [84]: lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
print("the prime numbers between {}".format(lower),"and {}".format(upper),"are")
for n in range(lower,upper+1):
if n==2 :
print(n)
else :
for i in range(2,n):
if n%i==0 :
break
elif i>sqrt(n):
print(n)
break
10
Enter lower range: 1
Enter upper range: 10
the prime numbers between 1 and 10 are
2
3
5
7
count=1
for n in range(1,a):
if n==2 :
print(n)
else:
for i in range(2,n):
if n%i==0 :
break
elif i>sqrt(n):
print(n)
count=count+1
break
11
43
47
53
59
61
67
71
73
79
83
89
97
number of primes less than 100 is 25
In [23]: NextPrime(3)
Out[23]: 5
0.2 Statistics
In [116]: data = [1, 2, 3, 4, 5]
n = len(data)
data[4]
Out[116]: 5
12
Mean is: 254/5
[1, 6, 18, 22, 30, 32, 34, 37, 40, 43, 47, 50, 66, 70, 74, 78, 82, 93, 94, 99]
0.2.4 Median
In [137]: data = [78, 43, 6, 50, 47, 94, 37, 70, 66, 32, 1, 34, 93, 30, 99, 82, 22, 74, 18, 40,
n = len(data)
data.sort()
if n % 2 == 0:
median1 = data[n//2]
median2 = data[n//2 - 1]
median = (median1 + median2)/2
else:
median = data[n//2]
print("Median is: " + str(median))
Median is: 47
0.2.5 Mode
In [122]: import collections
num_list = [21, 13, 19, 13,19,13]
print(num_list)
13
# calculate the frequency of each item
data = collections.Counter(num_list)
data_list = dict(data)
In [ ]:
14