Cycle 1 Programs
Cycle 1 Programs
2.Introduction to Python3
a.Printing your biodata on the screen.
print("Name:" ,"Geetha Devi")
print("Gender:", "female")
print("Age:",35)
print("Address:"," Hyderabad")
print("Mother Name:","swaroopa")
print("E-Mail Address:,""[email protected]")
output:
Gender: female
Age: 35
Address: Hyderabad
E-Mail Address:,[email protected]
if sum_v == num:
print("The entered number is a perfect number")
else:
print("The entered number is not a perfect number")
file1.seek(0)
print("Output of Readline function is ")
print(file1.readline())
print()
file1.seek(0)
print("Output of Read(9) function is ")
print(file1.read(9))
print()
file1.seek(0)
print("Output of Readline(9) function is ")
print(file1.readline(9))
print()
file1.seek(0)
print("Output of Readlines function is ")
print(file1.readlines())
print()
file1.close()
output:
Output of Read function is
Hello
This is Delhi
This is Paris
This is London
output
Enter any number: 121
The original number is: 121
Is the number palindrome?: True
Enter any number: 123
The original number is: 123
Is the number palindrome?: False
c) Write a function Collatz(x) which does the following: if x is
odd, x=3x+1; if x is even, then x=x/2. Return the number of
steps it takes for x=1.
def printCollatz(x):
while x != 1:
print(x, end=',')
if x & 1: # Check if x is odd using bitwise AND
x = 3*x + 1
else: # If x is even
x = x // 2
print(x) # Print the final 1 when loop ends
print(solve(4, 2))
output
0.05399096651318806
4.The Package numpy
import numpy as np
array = np.random.randint(low=1, high=99999, size=(2,2))
print(array)
array = np.random.randint(99999, size=(3,3))
print(array)
output
2X2 matrix
[[86201 50547]
[76360 14299]]
3X3 matrix
Method1
matrix1 = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]
output
[114, 160, 60]
[74, 97, 73]
[119, 157, 112]
Method-2
import numpy as np
mat1 = ([1, 6, 5],[3 ,4, 8],[2, 12, 3])
mat2 = ([3, 4, 6],[5, 6, 7],[6,56, 7])
res = np.dot(mat1,mat2)
print(res)
output
[[ 63 320 83]
[ 77 484 102]
[ 84 248 117]]
import numpy as np
mat1 = np.array([[1, 6, 5],
[3, 4, 8],
[2, 12, 3]])
output
Matrix Addition Result:
[[ 4 10 11]
[ 8 10 15]
[ 8 68 10]]
print(x)
A*X=B
A is the coefficient matrix (n × n)
output
[-0.58226371 3.22870478 -1.98599767]
5.THE PACKAGE SCIPY:
a) Finding if two sets of data have the same mean value.
data1 = (11, 3, 4, 5, 7, 9, 2)
output
Mean of data set 1 is 5.857142857142857
Mean of data set 2 is -7.5
Mean of data set 3 is 2.4285714285714284
Mean of data set 4 is 49/24
Mean of data set 5 is 2
import csv
X = []
Y = []
X.append(int(row[0]))
Y.append(int(row[1]))
plt.plot(X, Y)
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
c)Fitting a function through a set of data points using polyfit
function.
import numpy as np
import matplotlib.pyplot as mp
np.random.seed(12)
x = np.linspace( 0, 1, 25 )
y = np.cos(x) + 0.3*np.random.rand(25)
p = np.poly1d( np.polyfit(x, y, 4) )
t = np.linspace(0, 1, 250)
mp.show()
lines = 0
words = 0
symbols = 0
lines += 1
words += len(line.split())
symbols += len(line.strip('\n'))
file.close()
print("Lines:", lines)
print("Words:", words)
print("Symbols:", symbols)
output
Lines: 3
Words: 7
Symbols: 31
b) Read text from a file and return a list of all n letter words
beginning with a vowel.
res = []
vow = "aeiou"
flag = False
if sub.startswith(ele):
flag = True
break
if flag:
res.append(sub)
output:
The original list is : ['all', 'love', 'and', 'get', 'educated', 'by', 'gfg']
converted_data = ""
for i in range(len(data)):
if data[i] in conversion_code:
converted_data += conversion_code[data[i]]
else:
converted_data += data[i]
print(converted_data)
output
Dvoxlnv gl...
def splitString(s):
l1 = []
words = s.split()
for word in words:
l1.append(len(word))
print(l1)
return l1
output
[5, 5, 3, 3, 4, 7, 9, 6, 2, 12, 10, 6, 6, 8, 6, 2, 4, 4, 9, 5, 9, 1, 8, 10,
2, 6]