Advertisement
class_connect

Untitled

Jun 13th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.20 KB | None | 0 0
  1. Department of Computer Science and Engineering
  2. KPR Institute of Engineering and Technology
  3.  
  4. B.E. – COMPUTER SCIENCE AND ENGINEERING
  5.  
  6. LABORATORY RECORD
  7.  
  8. U21CSG02 – PYTHON PROGRAMMING
  9.  
  10. (Regulation 2021)
  11.  
  12. KPR INSTITUTE OF ENGINEERING AND TECHNOLOGY
  13. (Autonomous)
  14. COIMBATORE – 641 407
  15.  
  16. LABORATORY RECORD
  17. Name
  18.  
  19. : ……………………………………………………………….
  20.  
  21. Register Number
  22.  
  23. : ……………………………………………………………….
  24.  
  25. Subject Code & Title: ……………………………………………………………….
  26. Department
  27.  
  28. : ……………………………………………………………….
  29.  
  30. Year & Semester
  31.  
  32. : ……………………………………………………………….
  33.  
  34. This is the certified record of work done by………………………………………….
  35. Register Number ………………………………………………………
  36.  
  37. Staff In- Charge
  38.  
  39. Head of the Department
  40.  
  41. Place:
  42. Date:
  43.  
  44. He/ She has submitted the record for the End Semester PracticalExamination held
  45. on ……………………
  46.  
  47. Internal Examiner
  48.  
  49. Department of Computer Science and Engineering
  50. KPR Institute of Engineering and Technology
  51. -----------------------------------------------------------------------------------------------VISION AND MISSION OF THE INSTITUTION
  52. Vision
  53.  
  54. To become a premier institute of academic excellence by imparting technical, intellectual and
  55. professional skills to students for meeting the diverse need of the industry, society, the nation and the
  56. world at large.
  57. Mission
  58.  
  59. ❖ Commitment to offer value-based education and enhancement of practical skills
  60. ❖ Continuous assessment of teaching and learning process through scholarly activities
  61. ❖ Enriching research and innovation activities in collaboration with industry and institute of repute
  62. ❖ Ensuring the academic process to uphold culture, ethics and social responsibility
  63.  
  64. VISION AND MISSION OF THE DEPARTMENT
  65. Vision
  66. To be a premier centre for education, dissemination of knowledge and research in the frontier areas of
  67. computer science and engineering to serve the community with moral values.
  68. Mission
  69. •
  70.  
  71. Provide holistic education incorporating the state-of-the-art technologies to produce successful
  72.  
  73. professionals.
  74. •
  75.  
  76. Facilitate the students to pursue higher education and research in the areas related to Computer Science
  77.  
  78. and Engineering.
  79. •
  80.  
  81. Promote strong collaborations with the industries and steer the students to nurture their interest in
  82.  
  83. continuous learning to meet the changing needs of the society.
  84. Program Educational Objectives
  85. Graduates of B.E. CSE program, four years after graduation, will
  86. PEO 1: Acquire knowledge and skills on cutting edge technologies in the field of computer science and
  87. engineering.
  88. PEO 2: Inculcate a passion for continuous learning through further studies and research in the field of
  89. computer science and engineering.
  90. PEO 3: Develop innovative ideas upholding the rich value systems to solve the changing technological
  91. needs.
  92.  
  93. Program Outcomes
  94. PO 1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
  95. fundamentals, and an engineering specialization to the solution of complex engineering problems
  96. PO 2. Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
  97. problems reaching substantiated conclusions using first principles of mathematics, natural sciences, and
  98. engineering sciences
  99. PO 3. Design/development of solutions: Design solutions for complex engineering problems and design
  100. system components or processes that meet the specified needs with appropriate consideration for the
  101. public health and safety, and the cultural, societal, and environmental considerations
  102. PO 4. Conduct investigations of complex problems: Use research-based knowledge and research
  103. methods including design of experiments, analysis and interpretation of data, and synthesis of the
  104. information to provide valid conclusions
  105. PO 5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
  106. engineering and IT tools including prediction and modeling to complex engineering activities with an
  107. understanding of the limitations
  108. PO 6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
  109. societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
  110. professional engineering practice
  111. PO 7. Environment and sustainability: Understand the impact of the professional engineering solutions
  112. in societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
  113. development
  114. PO 8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
  115. of the engineering practice
  116. PO 9. Individual and team work: Function effectively as an individual, and as a member or leader in
  117. diverse teams, and in multidisciplinary settings
  118. PO 10. Communication: Communicate effectively on complex engineering activities with the
  119. engineering community and with society at large, such as, being able to comprehend and write effective
  120. reports and design documentation, make effective presentations, and give and receive clear instructions
  121. PO 11. Project management and finance: Demonstrate knowledge and understanding of the
  122. engineering and management principles and apply these to one's own work, as a member and leader in a
  123. team, to manage projects and in multidisciplinary environments
  124. PO 12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
  125. independent and life-long learning in the broadest context of technological change
  126.  
  127. Programme Specific Objectives
  128. PSO 1: Solve complex engineering problems by building integrated systems across various domains of
  129. Computational Intelligence.
  130. PSO 2: Apply technical skills to produce solutions that meet the requirements of the emerging
  131. technologies in Computer Science and Engineering.
  132.  
  133. INDEX
  134. S.NO
  135.  
  136. Date
  137.  
  138. PAGE
  139. NO
  140.  
  141. NAME OF THE EXPERIMENTS
  142.  
  143. 01
  144.  
  145. Programs on selection and iteration operations
  146.  
  147. 02
  148.  
  149. String and its operations
  150.  
  151. 03
  152.  
  153. Programs using List
  154.  
  155. 04
  156.  
  157. Programs using Tuples
  158.  
  159. 05
  160.  
  161. Programs using Sets
  162.  
  163. 06
  164.  
  165. Programs using Dictionary
  166.  
  167. 07
  168.  
  169. Exception Handling
  170.  
  171. 08
  172.  
  173. File Handling
  174.  
  175. 09
  176.  
  177. Programs using numpy, pandas
  178.  
  179. 10
  180.  
  181. Programs using dataframes
  182.  
  183. Average Marks:
  184.  
  185. Marks
  186.  
  187. SIGN
  188.  
  189. EX.NO : 01
  190. PROGRAMS ON SELECTION AND ITERATION OPERATIONS
  191. DATE :
  192. A. Area and permitter of circle
  193. AIM :
  194. To write a python program to calculate the area and perimeter of a circle.
  195. ALGORITHM :
  196. Step 1 : Get the length and breadth from the user as l and b
  197. Step 2 : Compute the area using the formula l*b
  198. Step 3 : Compute the perimeter using the formula 2*(l+b)
  199. Step 4 : Print the are and the perimeter of the circle
  200. PROGRAM :
  201. L = int(input(“Enter Length:”))
  202. B = int(input(“Enter Breadth:”))
  203. Area = L*B
  204. Perimeter = 2*(L+B)
  205. print(“Area of Rectangle is “,Area)
  206. print(“Perimeter of Rectangle is “,Perimeter)
  207.  
  208. OUTPUT:
  209. Enter Length:10
  210. Enter Breadth:5
  211. Area of Rectangle is 50
  212. Perimeter of Rectangle is 30
  213.  
  214. RESULT:
  215. Thus the python program to compute the area and perimeter of the circle has been executed
  216. successfully.
  217.  
  218. B. Odd or even
  219. AIM :
  220. To write a python program to find the given number is odd or even.
  221. ALGORITHM :
  222. Step 1 : Get the input from the user
  223. Step 2 : If the number is divisible by 2, print the number as even number
  224. Step 3 : Else display the number as odd number
  225. PROGRAM :
  226. N = int(input(“Enter number:”))
  227. If N%2==0:
  228. print(N,”is Even”)
  229. else:
  230. print(N,”is Odd”)
  231.  
  232. OUTPUT:
  233. Enter number:10
  234. 10 is Even
  235.  
  236. RESULT:
  237. Thus the python program to check the given number is odd or even has been executed successfully.
  238.  
  239. C. Sum of digits of the given number
  240. AIM :
  241. To write a python program to find the sum of the digits of the given number.
  242. ALGORITHM :
  243. Step 1 : Get the input n from the user
  244. Step 2: Initialize the sum value to zero
  245. Step 3 : Until n>0 repeat the steps 4 to 6
  246. Step 4 : Compute the value of r using r=n%
  247. Step 5 : Add the value of r to sum
  248. Step 6 : Perform n=n//10
  249. Step 7 : Display the sum
  250. PROGRAM :
  251. N = int(input(“Enter number:”))
  252. Sum = 0
  253. While(n>0):
  254. r = N%10
  255. sum += r
  256. N = N//10
  257. Print(“Sum of digits are “,sum)
  258. OUTPUT:
  259. Enter number:123
  260. Sum of digits are 6
  261.  
  262. RESULT:
  263. Thus the python program to find the sum of digits of the given number has been executed successfully.
  264.  
  265. D. Fibonacci Series
  266. AIM :
  267. To write a python program to display the numbers in Fibonacci series.
  268. ALGORITHM :
  269. Step 1 : Get the input n from the user
  270. Step 2: Initialize the values of n1 and n2 to -1 and 1
  271. Step 3 : For n time repeat the steps 4 to 6
  272. Step 4 : Display the sum of n1 and n2.
  273. Step 5 : Assign the value of n2 to n1
  274. Step 6 : Assign the sum to n2
  275. PROGRAM :
  276. Terms = int(input(“Enter the number of terms:”))
  277. N1 = -1
  278. N2 = 1
  279. print(“Fibonacci Series are “)
  280. for i in range(terms):
  281. Sum=N1+N2
  282. print(Sum)
  283. N1=N2
  284. N2=Sum
  285. OUTPUT:
  286. Enter the number of terms:6
  287. Fibonacci Series are
  288. 0
  289. 1
  290. 1
  291. 2
  292. 3
  293. 5
  294.  
  295. RESULT:
  296. Thus the python program to display numbers in Fibonacci series has been executed successfully.
  297.  
  298. E. Prime or not
  299. AIM :
  300. To write a python program to determine whether the given number is prime or not.
  301. ALGORITHM :
  302. Step 1 : Get the input n from the user
  303. Step 2: For I values 2 to n-1 repeat the steps 3 to 4
  304. Step 3 : if n is divisible by i, display the number as not prime, Goto step 6
  305. Step 4 : If not divisible by i, increment I by 1.
  306. Step 5 : If the number is not divisible by any number between 2 to n-1, display it as prime number
  307. Step 6 : Stop the program
  308. PROGRAM :
  309. N = int(input(“Enter number:”))
  310. If N==1:
  311. print(N,”is not a Prime number”)
  312. Elif(N>1):
  313. for i in range(2,N):
  314. if (N%i) == 0:
  315. print(N,”is not a Prime number”)
  316. break
  317. else:
  318. print(N,”is a Prime number”)
  319. else:
  320. print(N,”is not a Prime number”)
  321.  
  322. OUTPUT:
  323. Enter number:13
  324. 13 is Prime number
  325.  
  326. RESULT:
  327. Thus the python program to find the given number if prime or not has been executed successfully.
  328.  
  329. F. Compute Salary of an employee
  330. AIM :
  331. To write a python program to compute salary of an employee.
  332. ALGORITHM :
  333. Step 1 : Get the input salary and gender from the user
  334. Step 2: If the gender is male, the bonus is computed as 5% of salary
  335. Step 3 : if the gender if female, the bonus is 10% of the salary
  336. Step 4 : If the salary is less than 10000 additional 2% bonus of salary is given
  337. Step 5 : Display the salary with bonus.
  338. Step 6 : Stop the program
  339. PROGRAM :
  340. Salary = int(input(“Enter Salary”))
  341. Gender = input(“Enter Gender”)
  342. Bonus =0
  343. if (Salary < 10000):
  344. Bonus = Salary * 0.02
  345. else:
  346. Bonus =0
  347. if (Gender == “M”):
  348. Bonus += Salary * 0.05
  349. else:
  350. Bonus += Salary * 0.1
  351. print(“Salary is”, Salary +Bonus)
  352.  
  353. OUTPUT:
  354. Enter Salary: 8000
  355. Enter Gender: M
  356. Salary is 8560
  357.  
  358. RESULT:
  359. Thus the python program to compute salary based on the given condition has been executed
  360. successfully.
  361.  
  362. G. Program to perform specific operation based on user input
  363. AIM :
  364. To write a python program get an integer input from a user. If the number is odd, then find the factorial
  365. of a number and find the number of digits in the factorial of the number. If the number is even, then check
  366. the given number is palindrome or not.
  367. ALGORITHM :
  368. Step 1 : Get a integer as input from the user
  369. Step 2 : Check the input number is odd or even
  370. Step 3 : If the number is even then print the number is palindrome or not
  371. Step 4 : If the number is odd the find the factorial of the number
  372. Step 5 : Print the number of digits in the factorial of the number
  373. PROGRAM :
  374. num=int(input('Enter a number:'))
  375. if num%2==0:
  376. rev=0
  377. temp=num
  378. while(num>0):
  379. digit=num%10
  380. rev=rev*10+digit
  381. num=num//10
  382. if(rev==temp):
  383. print('The given number is a palindrome')
  384. else:
  385. print('The given number is not a palindrome')
  386. else:
  387. def factorial(num):
  388. if num==1:
  389. return num
  390. else:
  391. return num*factorial(num-1)
  392. print('The factorial of',num,'is',factorial(num))
  393. count=0
  394. n=factorial(num)
  395. while(n>0):
  396. n=n//10
  397.  
  398. count=count+1
  399. print('The number of digits is the factorial of a number is',count)
  400.  
  401. OUTPUT :
  402.  
  403. Enter a number:5
  404. The factorial of 5 is 120
  405. The number of digits is the factorial of a number is 3
  406. Enter a number:454
  407. The given number is a palindrome
  408.  
  409. Department of CSE
  410. Preparation (Algorithm)
  411.  
  412. (4)
  413.  
  414. Observation (Program)
  415.  
  416. (4)
  417.  
  418. Results (Output)
  419.  
  420. (4)
  421.  
  422. Interpretation (Validation)
  423.  
  424. (4)
  425.  
  426. Viva-Voce
  427.  
  428. (4)
  429.  
  430. Total
  431.  
  432. (20)
  433.  
  434. RESULT :
  435. Thus the python program to get an integer input from a user and to find if the number is odd, then find
  436. the factorial of a number and find the number of digits in the factorial of the number and if the number is
  437. even, then check the given number is palindrome or not has been executed and the output is verified.
  438.  
  439. EX.NO : 02
  440. String and its operations
  441. DATE :
  442.  
  443. A. Palindrome or not
  444. AIM :
  445. To write a python program to find whether the given string is palindrome or not.
  446. ALGORITHM :
  447. Step 1 : Get the input string from the user
  448. Step 2: Determine the reverse of the given string
  449. Step 3 : if the reverse and original string or equal, display the string is a palindrome
  450. Step 4 : Else display the string is not a palindrome
  451. Step 5 : Stop the program
  452. PROGRAM :
  453. S = input(“Enter String: ”)
  454. If s == s[::-1]:
  455. print("Yes,It is Palindrome")
  456. else:
  457. print("No,It is not Palindrome")
  458. OUTPUT:
  459. Enter String: malayalam
  460. Yes,It is Palindrome
  461.  
  462. RESULT:
  463. Thus the python program to check the given string is palindrome or not has been executed
  464. successfully.
  465.  
  466. B. No of vowels
  467. AIM :
  468. To write a python program to find the number of vowels in the given string.
  469. ALGORITHM :
  470. Step 1 : Get the input string from the user
  471. Step 2: initialize the count to zero
  472. Step 3 : Iterate through the elements of the string, If the character is a vowel increment count by 1
  473. Step 4 : Display the count
  474. Step 5 : Stop the program
  475. PROGRAM :
  476. def vowel_count(str):
  477. count = 0
  478. vowel = "aeiouAEIOU"
  479. for alphabet in str:
  480. if alphabet in vowel:
  481. count = count + 1
  482. print("No. of vowels :", count)
  483. str = input(“Enter the String:”)
  484. vowel_count(str)
  485. OUTPUT:
  486. Enter the String: Welcome
  487. No. of vowels : 3
  488.  
  489. RESULT:
  490. Thus the python program to display the number of vowels in the string has been executed
  491. successfully.
  492.  
  493. C. Display pattern
  494. AIM :
  495. To write a python program to display the given string in butterfly pattern.
  496. ALGORITHM :
  497. Step 1 : Get the input string from the user
  498. Step 2: compute the length of string
  499. Step 3 : iterate I from 0 to len-1
  500. Step 4 : for every iteration display the ith and len-ith characters, display space for remaining
  501. characters
  502. Step 5 : Stop the program
  503. PROGRAM :
  504. S = input(“Enter the string :”)
  505. L= len(s)
  506. for i in range(l):
  507. j=l-1-i
  508. for k in range(l):
  509. if (k == i or k == j):
  510. print(s[k], end="")
  511. else:
  512. print(" ", end="")
  513. print()
  514. OUTPUT:
  515. Enter the String: Python
  516. p
  517.  
  518. n
  519. y
  520.  
  521. o
  522. th
  523.  
  524. y
  525. p
  526.  
  527. o
  528. n
  529.  
  530. RESULT:
  531. Thus the python program to display the number of vowels in the string has been executed
  532. successfully.
  533.  
  534. d. To check whether the second string can be obtained from the first by deletion of none, one or
  535. more characters
  536. AIM:
  537. To write a python program to check whether the second string can be obtained from the first by
  538. deletion of none, one or more characters.
  539. ALGORITHM:
  540. Step 1 : Get two strings as input from the user
  541. Step 2 : Find the length of the two strings
  542. Step 3 : Using for loop iterate over the two stings
  543. Step 4 : Find the second sting can be obtained from the first string
  544. Step 5 : If the second string and the string obtained are equal
  545. Step 6 : Compare and print yes when the two strings are equal else print no
  546. PROGRAM:
  547.  
  548. str1=str(input("Enter the first string\n"))
  549. str2=str(input("Enter the second string\n"))
  550. str3=''
  551. temp=0
  552. len1=len(str1)
  553. len2=len(str2)
  554. for iter1 in range(0,len2):
  555. for iter2 in range(temp,len1):
  556. if(str2[iter1]==str1[iter2]):
  557. str3=str3+str1[iter2]
  558. temp=iter2+1
  559. break
  560. if(str2==str3):
  561. print("yes")
  562. else:
  563. print("no")
  564.  
  565. OUTPUT:
  566.  
  567. Enter the first string
  568. welcome
  569. Enter the second string
  570. come
  571. yes
  572. Enter the first string
  573. welcome
  574. Enter the second string
  575. well
  576. no
  577.  
  578. Department of CSE
  579. Preparation (Algorithm)
  580.  
  581. (4)
  582.  
  583. Observation (Program)
  584.  
  585. (4)
  586.  
  587. Results (Output)
  588.  
  589. (4)
  590.  
  591. Interpretation (Validation)
  592.  
  593. (4)
  594.  
  595. Viva-Voce
  596.  
  597. (4)
  598.  
  599. Total
  600.  
  601. (20)
  602.  
  603. RESULT :
  604. Thus the python program to check whether the second string can be obtained from the first by
  605. deletion of none, one or more characters has been executed and the output is verified.
  606.  
  607. EX.NO : 03
  608. Programs using List
  609. DATE :
  610. a. Display list elements using positive and negative indexing
  611. AIM:
  612. To write a Python Program to use positive and negative indexing in Lists
  613. ALGORITHM:
  614. Step 1 : Get the number of elements to be in the list
  615. Step 2 : Get the elements for the list
  616. Step 3 : Get the elements to be searched in the list
  617. Step 4 : Print the positive index from the list
  618. Step 5 : Calculate the negative index by (length of list – index of the searched element)
  619. Step 6 : Print the negative index from the list
  620. PROGRAM:
  621. n=int(input("Enter number of elements in list "))
  622. l=list()
  623. for i in range(0,n):
  624. e=input("enter the value")
  625. l.append(e)
  626. print ("Original list : " + str(l))
  627. k=input("Enter the element to be searched in the lsit ")
  628. res=len(l)-l.index(k)
  629. print("The required Negative index : -",str(res))
  630. print("The required Positive index : ",l.index(k))
  631. '''res = ~l[::-1].index(k)
  632. print("The required Negative index : ",str(res))'''
  633. OUTPUT:
  634. Enter number of elements in list 10
  635. enter the value100
  636. enter the value67
  637. enter the value56
  638. enter the value4
  639. enter the value26
  640. enter the value45
  641.  
  642. enter the value89
  643. enter the value78
  644. enter the value80
  645. Original list : ['100', '67', '56', '4', '26', '45', '89', '78', '80', '34']
  646. Enter the element to be searched in the lsit 100
  647. The required Negative index : - 10
  648. The required Positive index : 0
  649.  
  650. RESULT :
  651. Thus the Python program to perform list and its operations on indexing has been executed and the
  652. output is verified.
  653.  
  654. b. Sum of elements in the list
  655. AIM:
  656.  
  657. To write a Python Program to perform sum of elements in the List
  658. ALGORITHM:
  659.  
  660. Step 1 : Get the number of elements to be in the list
  661. Step 2 : Get the elements for the list
  662. Step 3 : Initialize sum to zero
  663. Step 4 : For each element in the list, add the value to sum
  664. Step 4 : Display the sum
  665. PROGRAM:
  666. L=[]
  667. sum =0
  668. N = int(input(‘Enter number of elements: ”))
  669. for i in range(n):
  670. L.append(int(input()))
  671. for i in L:
  672. sum =sum + i
  673. print(“List is ”,L)
  674. print(“sum of elements are “,sum)
  675. OUTPUT:
  676. Enter number of elements: 4
  677. 10
  678. 20
  679. 30
  680. 40
  681. List is [10,20,30,40]
  682. Sum of elements are 100
  683. RESULT :
  684. Thus the Python program to compute the sum of elements of the list has been executed and the
  685. output is verified.
  686.  
  687. c. Sum of odd and even numbers in the list
  688. AIM:
  689. To write a Python Program to perform sum of odd and even elements in the List
  690. ALGORITHM:
  691. Step 1 : Get the number of elements to be in the list
  692. Step 2 : Get the elements for the list
  693. Step 3 : Initialize oddsum and evensum to zero
  694. Step 4 : For each element in the list, repeat steps 5 and 6
  695. Step 5 : If the number is odd, add it to oddsum
  696. Step 6 : else add the number to evensum
  697. Step 7 : Display the oddsum and evensum
  698. PROGRAM:
  699. L = []
  700. Evensum ,Oddsum = 0,0
  701. N = int(input(“Enter number of elements: ”))
  702. for i in range(n):
  703. L.append(int(input()))
  704. for i in L:
  705. If i%2 ==0:
  706. Evensum +=i
  707. else:
  708. Oddsum+=i
  709. print(“List is ”,L)
  710. print(“Sum of even numbers are ”,Evensum)
  711. print(“Sum of odd are ”,Oddsum)
  712. OUTPUT:
  713. Enter number of elements: 4
  714. 11
  715. 22
  716. 33
  717. 44
  718. List is [11,22,33,44]
  719. Sum of even numbers are 66
  720. Sum of odd numbers are 44
  721.  
  722. RESULT :
  723. Thus the Python program to compute the sum of odd and even elements of the list has been
  724. executed and the output is verified.
  725.  
  726. d. Sorting a list and finding second maximum element in the list
  727. AIM:
  728. To write a Python Program to find the second maximum element in the List
  729. ALGORITHM:
  730. Step 1 : Get the number of elements to be in the list
  731. Step 2 : Get the elements for the list
  732. Step 3 : Sort the elements of the list in ascending order
  733. Step 4 : Display the second last element as second maximum element
  734. PROGRAM:
  735. L = []
  736. N = int(input(‘Enter number of elements: ”))
  737. for i in range(n):
  738. L.append(int(input()))
  739. print(“List is “,L)
  740. for i in range (len(L):
  741. for j in range (i+1,len(L):
  742. If L[i]>L[j]:
  743. L[i
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement