1. Python Program to Swap Two Variables. Program: A=Int(Input("Enter the First Number
Total Page:16
File Type:pdf, Size:1020Kb
1. Python program to swap two variables. Program: a=int(input("Enter the first number: ")) b=int(input("Enter the first number: ")) print("Nos. before swap ",a,"and",b) a,b=b,a print("Nos. after swap ",a,"and",b) Output: Enter the first number: 10 Enter the first number: 20 Nos. before swap 10 and 20 Nos. after swap 20 and 10 2. Python program to generate a random number. Program: import random print("Generated random number is ",random.randint(10,20)) Output: Generated random number is 16 3. Python program to convert Kilometers to Miles. Program: a=int(input("Enter the number of Kilometers: ")) print("Miles : ",a*5/8) Output: Enter the number of Kilometers: 13 Miles : 8.125 4. Python program to perform selection sort in ascending order. Program: def printArray(arr): print (' '.join(str(i) for i in arr)) def selectionsort(arr): N = len(arr) for i in range(0, N): small = arr[i] pos = i for j in range(i + 1, N): if arr[j] < small: small = arr[j] pos = j temp = arr[pos] arr[pos] = arr[i] arr[i] = temp print ("After pass ", str(i), " :") printArray(arr) arr = [10, 7, 3, 1, 9, 7, 4, 3] print ("Initial Array :") printArray(arr) selectionsort(arr) Output: Initial Array : 10 7 3 1 9 7 4 3 After pass 0 : 1 7 3 10 9 7 4 3 After pass 1 : 1 3 7 10 9 7 4 3 After pass 2 : 1 3 3 10 9 7 4 7 After pass 3 : 1 3 3 4 9 7 10 7 After pass 4 : 1 3 3 4 7 9 10 7 After pass 5 : 1 3 3 4 7 7 10 9 After pass 6 : 1 3 3 4 7 7 9 10 After pass 7 : 1 3 3 4 7 7 9 10 5. Write a python program to generate and print a list of first and last 5 elements where the values are square of numbers between 1 and 30 (both included). Program: a=[i*i for i in list(range(1,31))] print("Given list is : ") print(a) print("First five elements are : ") print(a[0:5]) print("Last five elements are : ") print(a[-1:-6:-1]) Output: Given list is : [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900] First five elements are : [1, 4, 9, 16, 25] Last five elements are : [900, 841, 784, 729, 676] 6. Insert the following documents into a movies collection inside Database Hollywood. title : Fight Club writer : Chuck Palahniuk year : 1999 actors : [ Brad Pitt Edward Norton ] title : Pulp Fiction writer : Quentin Tarantino year : 1994 actors : [ John Travolta Uma Thurman ] title : Inglorious Basterds writer : Quentin Tarantino year : 2009 actors : [ Brad Pitt Diane Kruger Eli Roth ] title : The Hobbit: An Unexpected Journey writer : J.R.R. Tolkein year : 2012 franchise : The Hobbit title : The Hobbit: The Desolation of Smaug writer : J.R.R. Tolkein year : 2013 franchise : The Hobbit title : The Hobbit: The Battle of the Five Armies writer : J.R.R. Tolkein year : 2012 franchise : The Hobbit synopsis : Bilbo and Company are forced to engage in a war against an array of combatants and keep the Lonely Mountain from query the movies collection to 1. get all documents 2. get all documents where actors include "Brad Pitt" Program Code: MongoDB Enterprise > use HOLLYWOOD switched to db HOLLYWOOD MongoDB Enterprise > db.createCollection("movies") { "ok" : 1 } MongoDB Enterprise > db.movies.insert({"title":"Flight Club","Writer":"Chuck Palahniuk","year":1999,"actors":["Brad Pitt","Edward Norton"]}) WriteResult({ "nInserted" : 1 }) MongoDB Enterprise > db.movies.insert({"title":"Pulp Fiction","Writer":"Quentin Tarantino","year":1994,"actors":["John Travolta","Uma Thurman"]}) WriteResult({ "nInserted" : 1 }) MongoDB Enterprise > db.movies.insert({"title":"Inglorious Basterds","Writer":"Quentin Tarantino","year":2009,"actors":["Brad Pitt","Dian Kruger","Eli Roth"]}) WriteResult({ "nInserted" : 1 }) MongoDB Enterprise > db.movies.insert({"title":"The Hobbit: An Unexpected Journey","Writer":"J.R.R. Tolkein","year":2012,"Franchise":"The Hobbit"}) WriteResult({ "nInserted" : 1 }) MongoDB Enterprise > db.movies.insert({"title":"The Hobbit: The Desolation of Smaug","Writer":"J.R.R. Tolkein","year":2013,"Franchise":"The Hobbit"}) WriteResult({ "nInserted" : 1 }) MongoDB Enterprise > db.movies.insert({"title":"The Battle of the five Armies","Writer":"J.R.R. Tolkein","year":2012,"Franchise":"The Hobbit"}) WriteResult({ "nInserted" : 1 }) MongoDB Enterprise > db.movies.find().pretty() OUTPUT { "_id" : ObjectId("5b4064fd8b01484ded167724"), "title" : "Flight Club", "Writer" : "Chuck Palahniuk", "year" : 1999, "actors" : [ "Brad Pitt", "Edward Norton" ] } { "_id" : ObjectId("5b4065618b01484ded167725"), "title" : "Pulp Fiction", "Writer" : "Quentin Tarantino", "year" : 1994, "actors" : [ "John Travolta", "Uma Thurman" ] } { "_id" : ObjectId("5b4065ca8b01484ded167726"), "title" : "Inglorious Basterds", "Writer" : "Quentin Tarantino", "year" : 2009, "actors" : [ "Brad Pitt", "Dian Kruger", "Eli Roth" ] } { "_id" : ObjectId("5b40665a8b01484ded167727"), "title" : "The Hobbit: An Unexpected Journey", "Writer" : "J.R.R. Tolkein", "year" : 2012, "Franchise" : "The Hobbit" } { "_id" : ObjectId("5b4066808b01484ded167728"), "title" : "The Hobbit: The Desolation of Smaug", "Writer" : "J.R.R. Tolkein", "year" : 2013, "Franchise" : "The Hobbit" } { "_id" : ObjectId("5b4066a48b01484ded167729"), "title" : "The Battle of the five Armies", "Writer" : "J.R.R. Tolkein", "year" : 2012, "Franchise" : "The Hobbit" } MongoDB Enterprise > db.movies.find({"actors":"Brad Pitt"}).pretty() OUTPUT { "_id" : ObjectId("5b4064fd8b01484ded167724"), "title" : "Flight Club", "Writer" : "Chuck Palahniuk", "year" : 1999, "actors" : [ "Brad Pitt", "Edward Norton" ] } { "_id" : ObjectId("5b4065ca8b01484ded167726"), "title" : "Inglorious Basterds", "Writer" : "Quentin Tarantino", "year" : 2009, "actors" : [ "Brad Pitt", "Dian Kruger", "Eli Roth" ] } .