lakha

Untitled

Dec 2nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.60 KB | None | 0 0
  1. # for inserting items in database
  2.  
  3. from tkinter import *
  4. #import mysql.connector as mysql
  5. import pymysql
  6. def insertcall():
  7. conn=pymysql.connect(user='root',password='root',host='localhost',db='temp')
  8. cursor=conn.cursor()
  9. # cursor.execute("Use temp")
  10. sql="insert into emp1 values('"+fname.get()+"','"+lname.get()+"',"+age.get()+","+income.get()+")"
  11. cursor.execute(sql)
  12. conn.commit()
  13.  
  14. win=Tk()
  15. win.title("Employee Details")
  16.  
  17. Label(win,text="First Name").grid(row=0,column=0)
  18. Label(win,text="Last Name").grid(row=1,column=0)
  19. Label(win,text="Age").grid(row=2,column=0)
  20. Label(win,text="Income").grid(row=3,column=0)
  21.  
  22. fname=Entry(win)
  23. lname=Entry(win)
  24. age=Entry(win)
  25. income=Entry(win)
  26.  
  27. fname.grid(row=0,column=1)
  28. lname.grid(row=1,column=1)
  29. age.grid(row=2,column=1)
  30. income.grid(row=3,column=1)
  31.  
  32. b1=Button(win,text='Quit',command=win.destroy)
  33. b1.grid(row=4,column=0,sticky=W,pady=4)
  34. b2=Button(win,text='Insert',command=insertcall)
  35. b2.grid(row=4,column=1,sticky=W,pady=4)
  36. mainloop()
  37.  
  38. #Program to show data from database
  39.  
  40.  
  41. from tkinter import *
  42. from tkinter import messagebox
  43. #import mysql.connector as mysql
  44. import pymysql
  45.  
  46. def showLastInfo():
  47. conn=pymysql.connect(user='root',password='root',host='127.0.0.1')
  48. cursor=conn.cursor()
  49. cursor.execute("use temp")
  50. sql="SELECT * FROM emp1"
  51. cursor.execute(sql)
  52. results=cursor.fetchall()
  53.  
  54. for row in results:
  55. fn=row[0]
  56. ln=row[1]
  57. age=row[2]
  58. inc=row[3]
  59.  
  60. #Display last Inserted information in message box
  61. messagebox.showinfo(title='Last Info',message="First Name:"+fn+"\nLast Name:"+ln+"\nAge:"+str(age)+"\nIncome:"+str(inc))
  62. conn.comit()
  63.  
  64. win=Tk()
  65. win.title("Check Inserted data")
  66. B1=Button(win,text='Check Inserted Info',command=showLastInfo)
  67. B1.pack()
  68. mainloop()
  69.  
  70.  
  71.  
  72. #updating information in database
  73.  
  74.  
  75. from tkinter import *
  76. from tkinter import messagebox
  77. import pymysql
  78.  
  79. #creating connection object and selecting current database to use.
  80. conn=pymysql.connect(user='root',password='root',host='localhost',db='temp')
  81. cursor=conn.cursor()
  82.  
  83.  
  84. def callvalues():
  85. lname.config(state=NORMAL)
  86. age.config(state=NORMAL)
  87. income.config(state=NORMAL)
  88. cursor.execute("Use temp")
  89. sql="SELECT * FROM emp1 WHERE fNAME='"+fname.get()+"'"
  90. cursor.execute(sql)
  91. results=cursor.fetchall()
  92. print(results)
  93. for row in results:
  94. dbfname=row[0]
  95. dblname=row[1]
  96. dbage=row[2]
  97. dbincome=row[3]
  98. lname.insert(0,dblname)
  99. age.insert(0,dbage)
  100. income.insert(0,dbincome)
  101. conn.commit()
  102.  
  103. def updatecall():
  104. cursor.execute("USE temp")
  105. sql="UPDATE emp1 SET lname='"+lname.get()+"',age="+age.get()+",income="+income.get()+" WHERE fname='"+fname.get()+"'"
  106. cursor.execute(sql)
  107. conn.commit()
  108. messagebox.showinfo(title='Last Info',message="Information Updated")
  109.  
  110. win=Tk()
  111.  
  112. Label(win,text=" Enter First Name").grid(row=0)
  113. fname=Entry(win)
  114. fname.grid(row=0,column=1)
  115.  
  116.  
  117. b1=Button(win,text='Click here to get values',command=callvalues)
  118. b1.grid(row=1,sticky=N,pady=4,columnspan=2)
  119.  
  120. Label(win,text="Last Name").grid(row=2)
  121. Label(win,text="Age").grid(row=3)
  122. Label(win,text="Income").grid(row=4)
  123.  
  124. lname=Entry(win,state=DISABLED)
  125. lname.grid(row=2,column=1)
  126. age=Entry(win,state=DISABLED)
  127. age.grid(row=3,column=1)
  128. income=Entry(win,state=DISABLED)
  129. income.grid(row=4,column=1)
  130.  
  131. b2=Button(win,text='Update',command=updatecall)
  132. b2.grid(row=5,columnspan=2,sticky=N,pady=4)
  133. mainloop()
  134.  
  135.  
  136. # deleting and modyfying information in database::
  137.  
  138.  
  139. from tkinter import *
  140. from tkinter import messagebox
  141. import pymysql
  142.  
  143. #creating connection object and selecting current database to use.
  144. conn=pymysql.connect(user='root',password='root',host='localhost',db='temp')
  145. cursor=conn.cursor()
  146.  
  147.  
  148. def deletecall():
  149. cursor.execute("Use temp")
  150. sql="DELETE FROM emp1 WHERE fNAME='"+fname.get()+"'"
  151. cursor.execute(sql)
  152. print(sql)
  153. cursor.execute(sql)
  154. conn.commit()
  155. messagebox.showinfo(title='Conformation',message="Information Deleted")
  156.  
  157. def checkcall():
  158. cursor.execute("USE temp")
  159. sql="SELECT * FROM emp1 WHERE fNAME='"+fname.get()+"'"
  160. cursor.execute(sql)
  161.  
  162. #IF no recordset return for INSERT,UPDATE,CREATE,etc
  163. if cursor.description is None:
  164. conn.commit()
  165. else:
  166. messagebox.showinfo(title='Last Info',message="No record found")
  167.  
  168. win=Tk()
  169.  
  170. Label(win,text=" Enter First Name").grid(row=0)
  171. fname=Entry(win)
  172. fname.grid(row=0,column=1)
  173.  
  174.  
  175. b1=Button(win,text='Delete values',command=deletecall)
  176. b1.grid(row=1,sticky=N,pady=4,column=0)
  177. b1=Button(win,text='check',command=checkcall)
  178. b1.grid(row=1,sticky=N,pady=4,column=1)
  179. mainloop()
  180.  
  181.  
  182.  
  183.  
  184.  
  185. from tkinter import *
  186. from tkinter import messagebox
  187. import pymysql
  188.  
  189. #creating connection object and selecting current database to use.
  190. conn=pymysql.connect(user='root',password='root',host='localhost',db='temp')
  191. cursor=conn.cursor()
  192.  
  193.  
  194. def deletecall():
  195. cursor.execute("Use temp")
  196. sql="DELETE FROM emp1 WHERE fNAME='"+fname.get()+"'"
  197. cursor.execute(sql)
  198. print(sql)
  199. cursor.execute(sql)
  200. conn.commit()
  201. messagebox.showinfo(title='Conformation',message="Information Deleted")
  202.  
  203. def checkcall():
  204. cursor.execute("USE temp")
  205. sql="SELECT * FROM emp1 WHERE fNAME='"+fname.get()+"'"
  206. cursor.execute(sql)
  207.  
  208. #IF no recordset return for INSERT,UPDATE,CREATE,etc
  209. if cursor.description is None:
  210. conn.commit()
  211. else:
  212. messagebox.showinfo(title='Last Info',message="No record found")
  213.  
  214. win=Tk()
  215.  
  216. Label(win,text=" Enter First Name").grid(row=0)
  217. fname=Entry(win)
  218. fname.grid(row=0,column=1)
  219.  
  220.  
  221. b1=Button(win,text='Delete values',command=deletecall)
  222. b1.grid(row=1,sticky=N,pady=4,column=0)
  223. b1=Button(win,text='check',command=checkcall)
  224. b1.grid(row=1,sticky=N,pady=4,column=1)
  225. mainloop()
  226.  
  227.  
  228.  
  229. 5.a. Write a Python script to sort (ascending and descending) a dictionary by value.
  230. import operator
  231. x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
  232. sorted_x = sorted(x.items(), key=operator.itemgetter(1))
  233. print(sorted_x)
  234. sorted_x = sorted(x.items(), key=operator.itemgetter(1), reverse=True)
  235. print(sorted_x)
  236.  
  237.  
  238.  
  239.  
  240. 1. Write the program for the following:......................................................................
  241. a.
  242. Create a program that asks the user to enter their name and their age. Print
  243. out a message addressed to them that tells them the year that they will turn
  244. 100 years old.
  245. from datetime import datetime
  246. name = input("What's your name?\n")
  247. age = int(input("How old are you?\n"))
  248. hundred = int((100-age)+datetime.now().year)
  249. print("Hi, %s. You are %s year old and you will turn 100
  250. year old in %s." %(name,age,hundred))
  251.  
  252.  
  253. b. Enter the number from the user and depending on whether the number is
  254. even or odd, print out an appropriate message to the user.
  255. num=int(input("Enter the Number?\n"))
  256. if num%2==0:
  257. print ("Number is Even.")
  258. else:
  259. print ("Number is Odd.")
  260.  
  261.  
  262.  
  263. c. Write a program to generate the Fibonacci series.
  264. v = int (input("Enter the range number:\n"))
  265. a=0
  266. b=1
  267. for n in range(0,v):
  268. if(n<=1):
  269. c=n
  270. else:
  271. c=a+b
  272. a=b
  273. b=c
  274. print(c)
  275.  
  276.  
  277. d. Write a function that reverses the user defined value.
  278. def revnum(num):
  279. reverse=0
  280. while num!=0:
  281. rem=num%10
  282. reverse=rem+reverse*10
  283. num=num//10
  284. print("Reverse Number:%d"%reverse)
  285. num=int(input("Enter any Number:"))
  286. revnum(num)
  287.  
  288.  
  289. e. Write a function to check the input value is Armstrong and also write the
  290. function for Palindrome.
  291. def armnum(num):
  292. sum=0
  293. temp=num
  294. while temp>0:
  295. digit=temp%10
  296. sum=sum+digit**3
  297. temp=temp//10
  298. if num==sum:
  299. print(num,"is a Armstrong Number.")
  300. else:
  301. print(num,"is not an Armstrong Number.")
  302. def palnum(num):
  303. sum=0
  304. temp=num
  305. while num!=0:
  306. rem=num%10
  307. sum=rem+sum*10
  308. num=num//10
  309. if temp==sum:
  310.  
  311.  
  312. print(temp,"is a palindrome Number.")
  313. else:
  314. print(temp,"is not a palindrome Number.")
  315. num=int(input("Enter any Number:"))
  316. armnum(num)
  317. palnum(num)
  318. f. Write a recursive function to print the factorial for a given number.
  319. def fact(x):
  320. if x==1:
  321. return 1
  322. else:
  323. return (x*fact(x-1))
  324. num=int(input("Enter any Number:\n"))
  325. print("The Factorial of",num,"is",fact(num))
  326.  
  327.  
  328.  
  329. 2. Write the program for the following:
  330. a. Write a function that takes a character (i.e. a string of length 1) and
  331. returns True if it is a vowel, False otherwise.
  332. def vowel(ch):
  333. if(ch=="A" or ch=="a" or ch=="E" or ch=="e" or
  334. ch=="I" or ch=="i" or ch=="O" or ch=="o" or ch=="U" or
  335. ch=="u"):
  336. print(ch,"is a Vowel.")
  337. else:
  338. print(ch,"is not a Vowel.")
  339. ch=input("Enter any Character:")
  340. vowel(ch)
  341.  
  342.  
  343. b. Define a function that computes the length of a given list or string.
  344. def complen(s):
  345. count=0
  346. for i in s:
  347. count=count+1
  348. return count
  349. print(complen([5,4,6,9,7,8,7]))
  350. print(complen('Santosh'))
  351.  
  352.  
  353.  
  354. c. Define a procedure histogram() that takes a list of integers and
  355. prints a histogram to the screen. For example, histogram([4, 9,
  356. 7]) should print the following:
  357. ****
  358. *********
  359. *******
  360. def histogram(items):
  361. for n in items:
  362. output=''
  363. times=n
  364. while(times>0):
  365. output=output+'*'
  366. times=times-1
  367. print(output)
  368. histogram([4,9,15])
  369.  
  370.  
  371.  
  372. 3. Write the program for the following:
  373. a.
  374. A pangram is a sentence that contains all the letters of the English
  375. alphabet at least once, for example: The quick brown fox jumps over the
  376. lazy dog. Your task here is to write a function to check a sentence to see
  377. if it is a pangram or not.
  378. import string, sys
  379. def ispangram(str1, alphabet=string.ascii_lowercase):
  380. alphaset = set(alphabet)
  381. return alphaset <= set(str1.lower())
  382. print(ispangram('The quick brown fox jumps over the lazy
  383. dog'))
  384.  
  385.  
  386.  
  387. b.
  388. Take a list, say for example this one:
  389. a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  390. and write a program that prints out all the elements of the list that are less
  391. than 5.
  392. a=[1,1,2,3,5,8,13,21,34,55,89]
  393. for i in a:
  394. if i<5:
  395. print(i)
  396.  
  397.  
  398.  
  399. 4. Write the program for the following:
  400. a Write a program that takes two lists and returns True if they have at least
  401. one common member.
  402. def common_data(list1, list2):
  403. result = False
  404. for x in list1:
  405. for y in list2:
  406. if x == y:
  407. result = True
  408. return result
  409. print(common_data([1, 2, 3, 4, 5], [5, 6, 7, 8, 9]))
  410. print(common_data([1, 2, 3, 4, 5], [6, 7, 8, 9]))
  411. b. Write a Python program to print a specified list after removing the 0th,
  412. 2nd, 4th and 5th elements.
  413. name = ['Name1','Name2','Name3','Name4','Name5','Name6','Name7']
  414. name = [x for (i,x) in enumerate(name) if i not in
  415. (0,2,4,5)]
  416. print(name)
  417.  
  418.  
  419.  
  420. c. Write a Python program to clone or copy a list
  421. original_list = [10, 22, 44, 23, 4]
  422. new_list = list(original_list)
  423. print(original_list)
  424. print(new_list)
  425.  
  426.  
  427.  
  428. 5. Write the program for the following:
  429. a. Write a Python script to sort (ascending and descending) a dictionary by
  430. value.
  431. import operator
  432. d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
  433. print('Original dictionary : ',d)
  434. sorted_d = sorted(d.items(), key=operator.itemgetter(0))
  435. print('Dictionary in ascending order by value :
  436. ',sorted_d)
  437. sorted_d = sorted(d.items(),
  438. key=operator.itemgetter(0),reverse=True)
  439. print('Dictionary in descending order by value :
  440. ',sorted_d)
  441.  
  442.  
  443.  
  444. b. Write a Python script to concatenate following dictionaries to create a
  445. new one.
  446. Sample Dictionary : dic1={1:10, 2:20} dic2={3:30, 4:40}
  447. dic3={5:50,6:60}
  448. Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
  449. dic1={1:10, 2:20}
  450. dic2={3:30, 4:40}
  451. dic3={5:50,6:60}
  452. dic4 = {}
  453. for d in (dic1, dic2, dic3): dic4.update(d)
  454. print(dic4)
  455.  
  456.  
  457. c. Write a Python program to sum all the items in a dictionary.
  458. my_dict = {'data1':100,'data2':-54,'data3':247}
  459. print(my_dict)
  460. print(sum(my_dict.values()))
  461.  
  462.  
  463.  
  464. 6. Write the program for the following:
  465. a. Write a Python program to read an entire text file.
  466. Python.txt
  467. Python is a widely used high-level programming language
  468. for general-purpose programming,
  469. created by Guido van Rossum and first released in 1991.
  470. Python interpreters are available for many operating
  471. systems, allowing Python code to run on a wide variety of
  472. systems.
  473. Code:
  474. f=open('Python.txt','r')
  475. t=f.read()
  476. print(t)
  477. f.close()
  478.  
  479.  
  480. b. Write a Python program to append text to a file and display the text.
  481. f=open('Python.txt','a+')
  482. f.write('Python Programming Practical.\n')
  483. f=open('Python.txt','r')
  484. t=f.read()
  485. print(t)
  486. f.close()
  487. c. Write a Python program to read last n lines of a file.
  488. f=open('Python.txt','r')
  489. t=f.readlines()
  490. print(t[-1])
  491. f.close()
  492.  
  493.  
  494.  
  495. 7. Write the program for the following:
  496. a. Design a class that store the information of student and display the
  497. same
  498. class student:
  499. def info(self,studname, studaddr):
  500. print("Name:",studname,"\nAddress:",studaddr)
  501. obj = student()
  502. obj.info('Santosh','Mumbai')
  503. b. Implement the concept of inheritance using python
  504. class st:
  505. def s1(self):
  506. print('Base Class')
  507. class st1(st):
  508. def s2(self):
  509. print('Derived Class')
  510. t=st1()
  511. t.s1()
  512. t.s2()
  513.  
  514.  
  515.  
  516. c.
  517. Create a class called Numbers, which has a single class attribute called
  518. MULTIPLIER, and a constructor which takes the parameters x and y (these
  519. should all be numbers).
  520. i. Write a method called add which returns the sum of the attributes x and y.
  521. ii. Write a class method called multiply, which takes a single number parameter a
  522. and returns the product of a and MULTIPLIER.
  523. iii. Write a static method called subtract, which takes two number parameters, b
  524. and c, and returns b - c.
  525. iv. Write a method called value which returns a tuple containing the values of x
  526. and y. Make this method into a property, and write a setter and a deleter for
  527. manipulating the values of x and y.
  528. class Numbers:
  529. MULTIPLIER = 3
  530. def __init__(self, x, y):
  531. self.x = x
  532. self.y = y
  533. def add(self):
  534. return self.x + self.y
  535. @classmethod
  536. def multiply(cls, a):
  537. return cls.MULTIPLIER * a
  538. @staticmethod
  539. def subtract(b, c):
  540. return b - c
  541. @property
  542. def value(self):
  543. return (self.x, self.y)
  544. @value.setter
  545. def value(self, xy_tuple):
  546. self.x, self.y = xy_tuple
  547. @value.deleter
  548. def value(self):
  549. del self.x
  550. del self.y
  551. T=Numbers(2,4)
  552. print(T.add())
  553. print(T.multiply(2))
  554. print(Numbers.subtract(4,3))
  555.  
  556.  
  557.  
  558.  
  559.  
  560. 8.
  561. a.
  562. Write the program for the following:
  563. Open a new file in IDLE (“New Window” in the “File” menu) and save it as geometry.py in the
  564. directory where you keep the files you create for this course. Then copy the functions you wrote
  565. for calculating volumes and areas in the “Control Flow and Functions” exercise into this file and
  566. save it.
  567. Now open a new file and save it in the same directory. You should now be
  568. able to import your own module like this:
  569. import geometry
  570. Try and add print dir(geometry) to the file and run it.
  571. Now write a function pointyShapeVolume(x, y, squareBase) that calculates
  572. the volume of a square pyramid if squareBase is True and of a right circular
  573. cone if squareBase is False. x is the length of an edge on a square if
  574. squareBase is True and the radius of a circle when squareBase is False. y is
  575. the height of the object. First use squareBase to distinguish the cases. Use the
  576. circleArea and squareArea from the geometry module to calculate the base
  577. areas.
  578. geometry.py
  579. import math
  580. def sphereArea(r):
  581. return 4*math.pi*r**2
  582. def sphereVolumne(r):
  583. return 4*math.pi*r**3/3
  584. def sphereMetrics(r):
  585. return sphereArea(r),sphereVolumne(r)
  586. def circleArea(r):
  587. return math.pi*r**2
  588. def squareArea(x):
  589. return x**2
  590. square.py
  591. import geometry
  592. def pointyShapeVolume(x,h,square):
  593. if square:
  594. base=geometry.squareArea(x)
  595. else:
  596. base=geometry.circleArea(x)
  597. return h*base/3.0
  598. print(dir(geometry))
  599. print(pointyShapeVolume(4,2.6,True))
  600. print(pointyShapeVolume(4,2.6,False))
  601. b. Write a program to implement exception handling.
  602. try:
  603. num=int(input('Enter Number:'))
  604. re=10/num
  605. except(ValueError, ZeroDivisionError):
  606. print('Something is Wrong.')
  607. else:
  608. print('Division is:',re)
  609.  
  610.  
  611.  
  612.  
  613. 9. Write the program for the following:
  614. a. Try to configure the widget with various options like: bg=”red”, family=”times”,
  615. size=18
  616. import tkinter as tk
  617. win=tk.Tk()
  618. win.title('Practical')
  619. def redclick():
  620. label.config(text='Helvetica Font')
  621. label.config(bg='red')
  622. label.config(font=('Helvetica',16))
  623. def greenclick():
  624. label.config(text='Cambria Font')
  625. label.config(bg='green')
  626. label.config(font=('Cambria',18))
  627. def yellowclick():
  628. label.config(text='Arial Font')
  629. label.config(bg='yellow')
  630. label.config(font=('Calbari',14))
  631. label=tk.Label(win,text="Python Practical",bg='white')
  632. label.pack()
  633. B1=tk.Button(win,text='Red
  634. Click',relief='raised',command=redclick)
  635. B1.pack(side='left')
  636. B2=tk.Button(win,text='Green
  637. Click',relief='raised',command=greenclick)
  638. B2.pack(side='left')
  639. B3=tk.Button(win,text='Yellow
  640. Click',relief='raised',command=yellowclick)
  641. B3.pack(side='left')
  642. win.mainloop()
  643.  
  644.  
  645.  
  646.  
  647. b.
  648. Try to change the widget type and configuration options to experiment with other
  649. widget types like Message, Button, Entry, Checkbutton, Radiobutton, Scale etc.
  650. from tkinter import *
  651. def swap():
  652. if v.get():
  653. e.pack_forget()
  654. mb.pack(anchor='w',side='right')
  655. l2.config(text='Use Menu Below.')
  656. l2.config(bg='yellow')
  657. l2.config(font=('Helvetica',16,'italic'))
  658. else:
  659. mb.pack_forget()
  660. e.pack(anchor='w',side='left')
  661. l2.config(text='Use Entry Box Below.')
  662. l2.config(bg='Green')
  663. l2.config(font=('Cambria', 16, 'bold'))
  664. e.focus()
  665. t=Tk()
  666. v=IntVar(t)
  667. c=Checkbutton(t,command=swap,text='Select to use
  668. menu.',variable=v)
  669. c.pack(anchor='w')
  670. f1=Frame(t)
  671. l1=Label(f1,text='Select the Menu item of your choice:')
  672. l1.pack(side='left')
  673. l2=Label(f1,text='Use Entry Box
  674. Below.',bg='green',font=('Cambria',16,'bold'))
  675. l2.pack(side='top')
  676. f=Frame(f1)
  677. f.pack(side='left')
  678. e=Entry(f,width=35)
  679. mb=Menubutton(f,width=25,text='Veg',indicatoron=1,relief='sun
  680. ken',anchor='w')
  681. m=Menu(mb,tearoff=0);mb.configure(menu=m)
  682. for s in 'Veg nonVeg Chinese French'.split():
  683. m.add_command(label=s,command=lambda s=s:
  684. mb.configure(text=s))
  685. f.pack()
  686. f1.pack()
  687. swap()
  688. b=Button(t,text='Place Order',relief='raised',
  689. fg='red',command=t.destroy);
  690. b.pack(side='top')
  691. f.mainloop()
  692.  
  693.  
  694. 1. a. Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old.
  695. name = input("Enter name : ")
  696. age = int(input("Enter age : "))
  697. yrdiff = 2017 + (100 - age)
  698. print("You will be turn to 100 in ", yrdiff, " year")
  699. Output:
  700. Enter name : Ravi
  701. Enter age : 30
  702. You will be turn to 100 in 2087 year
  703.  
  704. 1. b. Enter the number from the user and depending on whether the number is even or odd, print out an appropriate message to the user.
  705. no = int(input("Enter number : "))
  706. if no % 2 == 0:
  707. print("Number is Even")
  708. else:
  709. print("Number is Odd")
  710.  
  711. 1. c. Write a program to generate the Fibonacci series.
  712. def fibo(n):
  713. if n == 0:
  714. return 0
  715. elif n == 1:
  716. return 1
  717. else:
  718. return fibo(n-1) + fibo(n-2)
  719. #main
  720. n = int(input("Enter number : "))
  721. print("Fibonacci series : ")
  722. for i in range(n+1):
  723. print(fibo(i), " ", end = '')
  724.  
  725.  
  726.  
  727. 2. c. Define a procedure histogram() that takes a list, for example, histogram([4, 9, 7]) and print: **** ********* *******
  728. def histogram(list):
  729. for n in list:
  730. print(n * '*')
  731. #main
  732. histogram([4, 8, 9, 3])
  733.  
  734.  
  735. 4. b. Write a Python program to print a specified list after removing the 0th, 2nd, 4th and 5th elements.
  736. list1 = [10, 0, 3, 5, -100, 87, 65]
  737. print(list1)
  738. del list1[5]
  739. del list1[4]
  740. del list1[2]
  741. del list1[0]
  742. print(list1)
  743.  
  744.  
  745.  
  746. 5. c. Write a Python program to sum all the items in a dictionary.
  747. dict1 = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
  748. listofvalues = dict1.values()
  749. sum = 0
  750. for n in listofvalues:
  751. sum += n
  752. print("Sum = ", sum)
Add Comment
Please, Sign In to add comment