Applied Python Programming (Cycle-1)-1
Applied Python Programming (Cycle-1)-1
3.
4.
5.
6. INTRODUCING TO PYTHON3:
print("Name:")
print("Gender:")
print("Date of Birth:")
print("Age:")
print("Address:")
print("Father Name:")
print("Mother Name:")
print("E-Mail Address:")
b) Printing all the primes less than a given number.
file1.write("Hello \n")
file1.writelines(L)
file1.close()
print(file1.read())
print()
file1.seek(0)
print(file1.readline())
print()
file1.seek(0)
print(file1.read(9))
print()
file1.seek(0)
print(file1.readline(9))
print()
file1.seek(0)
print(file1.readlines())
print()
file1.close()
output
Output of Read function is
Hello
This is Delhi
This is Paris
This is London
Output of Readline function is
Hello
Output of Read(9) function is
Hello
Th
METHOD-1:
import math
def isPalindrome(num):
return int(num != 0) and ((num % 10) * (10**int(math.log(num, 10)))
+ isPalindrome(num // 10))
METHOD-2:
import math
def isPalindrome(num):
return int(num != 0) and ((num % 10) * (10 ** int(math.log(num, 10)))
+ isPalindrome(num // 10))
def printCollatz(x):
while x != 1:
print(x,end=',')
if x & 1:
x = 3*x+1
else:
x=x//2
print(x)
printCollatz(6)
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
printCollatz(6):
Step x Value Condition New x Value
1 6 Even 6 / 2 = 3
2 3 Odd 3*3+1 = 10
3 10 Even 10 / 2 = 5
4 5 Odd 3*5+1 = 16
5 16 Even 16 / 2 = 8
6 8 Even 8 / 2 = 4
7 4 Even 4 / 2 = 2
8 2 Even 2 / 2 = 1
6,3,10,5,16,8,4,2,1
import math
def solve(x,m):
s=1
result = math.exp(-((x-m)**2)/2*(s**s))/((2 * math.pi * s)**0.5)
return result
print(solve(4,2))
import math
0.05399096651318806
8. THE PACKAGE NUMPY:
a) Creating a matrix of given order mxn containing random
numbers in the range 1 to 99999.
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)
import numpy as np
high=99999: The highest possible value will be 99999, but it will not
include 99999 itself.
[[ 12345 67890]
[ 54321 98765]]
METHOD-1:
matrix1 = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
matrix2 = [[5,8,1],
[6,7,3],
[4,5,9]]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
print (res)
METHOD-2:
import numpy as np
res = np.dot(mat1,mat2)
print(res)
c) Write a program to solve a system of n linear equations in
n variables using matrix inverse.
import numpy as np
print(x)
9. THE PACKAGE SCIPY:
a) Finding if two sets of data have the same mean value.
data1 = (11, 3, 4, 5, 7, 9, 2)
data2 = (-1, -2, -4, -7, -12, -19)
data3 = (-1, -13, -6, 4, 5, 19, 9)
data4 = (fr(1, 2), fr(44, 12), fr(10, 3), fr(2, 3))
data5 = {1:"one", 2:"two", 3:"three"}
METHOD-1:
import matplotlib.pyplot as plt
import csv
X = []
Y = []
METHOD-2:
import matplotlib.pyplot as plt
import numpy as np
plt.bar(X, Y)
plt.title('Line Graph using NUMPY')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
c) Fitting a function through a set of data points using polyfit
function.
METHOD-1:
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.plot(x, y, 'o', t, p(t), '-')
mp.show()
METHOD-2:
import numpy as np
import matplotlib.pyplot as
mp np.random.seed(12)
x=np.linspace(-20,20,10)
y=2*x+5
coeff = np.polyfit(x,y,2)
xn = np.linspace(-20,20,100)
yn = np.poly1d(coeff)
mp.plot( xn,yn(xn),x,y,'o')
d) Plotting a histogram of a given data set.
plt.hist(x,10)
plt.show()
10.THE STRING PACKAGE:
a) Read text from a file and print the number of lines , words
and characters.
METHOD-1:
file = open("text.txt")
lines = 0
words = 0
symbols = 0
METHOD-2:
lines = 0
words = 0
letters = 0
pos = 'out'
for symbol in line:
if symbol != ' ' and pos == 'out':
words += 1
pos = 'in'
elif symbol == '':
pos = 'out'
print("Lines:", lines)
print("Words:", words)
print("Letters:", letters)
b) Read text from a file and return a list of all n letter words
beginning with a vowel.
METHOD-1:
test_list = ["all", "love", "and", "get", "educated", "by", "gfg"]
res = []
vow = "aeiou"
for sub in test_list:
flag = False
METHOD-2:
test_list = ["all", "love", "and", "get", "educated", "by", "gfg"]
res = []
vow = "aeiou"
for sub in test_list:
METHOD-3:
test_list = ["all", "love", "and", "get", "educated", "by", "gfg"]
conversion_code = {
'A': 'Z', 'B': 'Y', 'C': 'X', 'D': 'W', 'E': 'V', 'F': 'U',
'G': 'T', 'H': 'S', 'I': 'R', 'J': 'Q', 'K': 'P', 'L': 'O',
'M': 'N', 'N': 'M', 'O': 'L', 'P': 'K', 'Q': 'J', 'R': 'I',
'S': 'H', 'T': 'G', 'U': 'F', 'V': 'E', 'W': 'D', 'X': 'C',
'Y': 'B', 'Z': 'A',
'a': 'z', 'b': 'y', 'c': 'x', 'd': 'w', 'e': 'v', 'f': 'u',
'g': 't', 'h': 's', 'i': 'r', 'j': 'q', 'k': 'p', 'l': 'o',
'm': 'n', 'n': 'm', 'o': 'l', 'p': 'k', 'q': 'j', 'r': 'i',
's': 'h', 't': 'g', 'u': 'F', 'v': 'e', 'w': 'd', 'x': 'c',
'y': 'b', 'z': 'a'
}
def splitString(str):
l1=[]
s=str.split()
for words in s:
l1.append(len(words))
print(l1)
return l1
a=splitString(str)
fig,ax=plt.subplots(figsize=(10,7))
ax.hist(a)
plt.show()