Page 8 - PYTHON-12
P. 8

Predict the output of the Python code given below:

                 d1={'a':1,'p':2,'p':3,'l':4,'e':5}
                 t1 ='apple','papaya', 'lemon', 'peach','mango'
                 i=0
                 for j in d1:
                    d1[j]=t1[i]
                    i+=1
                 print(d1)
             Ans.  [15, 20, 15, 9]
                                                            OR
                 {'a': 'apple', 'p': 'papaya', 'l': 'lemon', 'e': 'peach'}
              24.  Ms. Bhawna is a school librarian. She has to enter the records of the newly purchased books in the
                 ‘Book’ table. Help her write a small Python program to insert a record in the table Book with attributes
                 (title, isbn)                                                                              (2)
                                                            OR
                  Nilesh is developing a software for his company to check for an unauthorized access on the company’s
                 database. Help him write the code to create the connection in which database’s name is Python, name of
                 host, user and password can be taken by the user. Also, print that connection.

             Ans.  import mysql.connector as mydb
                 con = mydb.connect(host="localhost",user="root",passwd="",database="test")
                 cursor=con.cursor()
                 query="INSERT into book(title,isbn) values('{}'{})".format('Python
                 Fundamentals',66888)
                 cursor.execute(query)
                 con.close()
                                                            OR

                 import mysql.connector
                 mycon = mysql.connector.connect(host = "localhost", user = "root",
                 passwd ="sss",database ="Python")
                 print(mycon)
              25.  Convert the following for loop to while loop:                                            (2)

                 sum=0
                 for a in range(1,50):
                    if a%2==1:
                      sum+=a
                    else:
                      sum+=a**2
                 print(sum)
             Ans.  sum=0
                 a=1
                 while a<=50:
                    if a%2==1:
                      sum+=a
                    else:
                      sum+=a**2
                    a+=1
                 print(sum)


            Appendices                                                                                     A.33
   3   4   5   6   7   8   9   10   11   12   13