├── PythonAssignment.md ├── PythonAssignmentSolution.md └── Python_OOP_Assignment.md /PythonAssignment.md: -------------------------------------------------------------------------------- 1 | ## Assignment Part-1 2 | Q1. Why do we call Python as a general purpose and high-level programming language? 3 | 4 | Q2. Why is Python called a dynamically typed language? 5 | 6 | Q3. List some pros and cons of Python programming language? 7 | 8 | Q4. In what all domains can we use Python? 9 | 10 | Q5. What are variable and how can we declare them? 11 | 12 | Q6. How can we take an input from the user in Python? 13 | 14 | Q7. What is the default datatype of the value that has been taken as an input using input() function? 15 | 16 | Q8. What is type casting? 17 | 18 | Q9. Can we take more than one input from the user using single input() function? If yes, how? If no, why? 19 | 20 | Q10. What are keywords? 21 | 22 | Q11. Can we use keywords as a variable? Support your answer with reason. 23 | 24 | Q12. What is indentation? What's the use of indentaion in Python? 25 | 26 | Q13. How can we throw some output in Python? 27 | 28 | Q14. What are operators in Python? 29 | 30 | Q15. What is difference between / and // operators? 31 | 32 | Q16. Write a code that gives following as an output. 33 | ``` 34 | iNeuroniNeuroniNeuroniNeuron 35 | ``` 36 | 37 | Q17. Write a code to take a number as an input from the user and check if the number is odd or even. 38 | 39 | Q18. What are boolean operator? 40 | 41 | Q19. What will the output of the following? 42 | ``` 43 | 1 or 0 44 | 45 | 0 and 0 46 | 47 | True and False and True 48 | 49 | 1 or 0 or 0 50 | ``` 51 | 52 | Q20. What are conditional statements in Python? 53 | 54 | Q21. What is use of 'if', 'elif' and 'else' keywords? 55 | 56 | Q22. Write a code to take the age of person as an input and if age >= 18 display "I can vote". If age is < 18 display "I can't vote". 57 | 58 | Q23. Write a code that displays the sum of all the even numbers from the given list. 59 | ``` 60 | numbers = [12, 75, 150, 180, 145, 525, 50] 61 | ``` 62 | 63 | 64 | Q24. Write a code to take 3 numbers as an input from the user and display the greatest no as output. 65 | 66 | Q25. Write a program to display only those numbers from a list that satisfy the following conditions 67 | 68 | - The number must be divisible by five 69 | 70 | - If the number is greater than 150, then skip it and move to the next number 71 | 72 | - If the number is greater than 500, then stop the loop 73 | ``` 74 | numbers = [12, 75, 150, 180, 145, 525, 50] 75 | ``` 76 | 77 | Q26. What is a string? How can we declare string in Python? 78 | 79 | Q27. How can we access the string using its index? 80 | 81 | Q28. Write a code to get the desired output of the following 82 | ``` 83 | string = "Big Data iNeuron" 84 | desired_output = "iNeuron" 85 | ``` 86 | 87 | Q29. Write a code to get the desired output of the following 88 | ``` 89 | string = "Big Data iNeuron" 90 | desired_output = "norueNi" 91 | ``` 92 | 93 | Q30. Resverse the string given in the above question. 94 | 95 | Q31. How can you delete entire string at once? 96 | 97 | Q32. What is escape sequence? 98 | 99 | Q33. How can you print the below string? 100 | ``` 101 | 'iNeuron's Big Data Course' 102 | ``` 103 | 104 | Q34. What is a list in Python? 105 | 106 | Q35. How can you create a list in Python? 107 | 108 | Q36. How can we access the elements in a list? 109 | 110 | Q37. Write a code to access the word "iNeuron" from the given list. 111 | ``` 112 | lst = [1,2,3,"Hi",[45,54, "iNeuron"], "Big Data"] 113 | ``` 114 | 115 | Q38. Take a list as an input from the user and find the length of the list. 116 | 117 | Q39. Add the word "Big" in the 3rd index of the given list. 118 | ``` 119 | lst = ["Welcome", "to", "Data", "course"] 120 | ``` 121 | 122 | Q40. What is a tuple? How is it different from list? 123 | 124 | Q41. How can you create a tuple in Python? 125 | 126 | Q42. Create a tuple and try to add your name in the tuple. Are you able to do it? Support your answer with reason. 127 | 128 | Q43. Can two tuple be appended. If yes, write a code for it. If not, why? 129 | 130 | Q44. Take a tuple as an input and print the count of elements in it. 131 | 132 | Q45. What are sets in Python? 133 | 134 | Q46. How can you create a set? 135 | 136 | Q47. Create a set and add "iNeuron" in your set. 137 | 138 | Q48. Try to add multiple values using add() function. 139 | 140 | Q49. How is update() different from add()? 141 | 142 | Q50. What is clear() in sets? 143 | 144 | Q51. What is frozen set? 145 | 146 | Q52. How is frozen set different from set? 147 | 148 | Q53. What is union() in sets? Explain via code. 149 | 150 | Q54. What is intersection() in sets? Explain via code. 151 | 152 | Q55. What is dictionary ibn Python? 153 | 154 | Q56. How is dictionary different from all other data structures. 155 | 156 | Q57. How can we delare a dictionary in Python? 157 | 158 | Q58. What will the output of the following? 159 | ``` 160 | var = {} 161 | print(type(var)) 162 | ``` 163 | 164 | Q59. How can we add an element in a dictionary? 165 | 166 | Q60. Create a dictionary and access all the values in that dictionary. 167 | 168 | Q61. Create a nested dictionary and access all the element in the inner dictionary. 169 | 170 | Q62. What is the use of get() function? 171 | 172 | Q63. What is the use of items() function? 173 | 174 | Q64. What is the use of pop() function? 175 | 176 | Q65. What is the use of popitems() function? 177 | 178 | Q66. What is the use of keys() function? 179 | 180 | Q67. What is the use of values() function? 181 | 182 | Q68. What are loops in Python? 183 | 184 | Q69. How many type of loop are there in Python? 185 | 186 | Q70. What is the difference between for and while loops? 187 | 188 | Q71. What is the use of continue statement? 189 | 190 | Q72. What is the use of break statement? 191 | 192 | Q73. What is the use of pass statement? 193 | 194 | Q74. What is the use of range() function? 195 | 196 | Q75. How can you loop over a dictionary? 197 | 198 | 199 | ### Coding problems 200 | Q76. Write a Python program to find the factorial of a given number. 201 | 202 | Q77. Write a Python program to calculate the simple interest. Formula to calculate simple interest is SI = (P*R*T)/100 203 | 204 | Q78. Write a Python program to calculate the compound interest. Formula of compound interest is A = P(1+ R/100)^t. 205 | 206 | Q79. Write a Python program to check if a number is prime or not. 207 | 208 | Q80. Write a Python program to check Armstrong Number. 209 | 210 | Q81. Write a Python program to find the n-th Fibonacci Number. 211 | 212 | Q82. Write a Python program to interchange the first and last element in a list. 213 | 214 | Q83. Write a Python program to swap two elements in a list. 215 | 216 | Q84. Write a Python program to find N largest element from a list. 217 | 218 | Q85. Write a Python program to find cumulative sum of a list. 219 | 220 | Q86. Write a Python program to check if a string is palindrome or not. 221 | 222 | Q87. Write a Python program to remove i'th element from a string. 223 | 224 | Q88. Write a Python program to check if a substring is present in a given string. 225 | 226 | Q89. Write a Python program to find words which are greater than given length k. 227 | 228 | Q90. Write a Python program to extract unquire dictionary values. 229 | 230 | Q91. Write a Python program to merge two dictionary. 231 | 232 | Q92. Write a Python program to convert a list of tuples into dictionary. 233 | ``` 234 | Input : [('Sachin', 10), ('MSD', 7), ('Kohli', 18), ('Rohit', 45)] 235 | Output : {'Sachin': 10, 'MSD': 7, 'Kohli': 18, 'Rohit': 45} 236 | ``` 237 | 238 | Q93. Write a Python program to create a list of tuples from given list having number and its cube in each tuple. 239 | ``` 240 | Input: list = [9, 5, 6] 241 | Output: [(9, 729), (5, 125), (6, 216)] 242 | ``` 243 | 244 | Q94. Write a Python program to get all combinations of 2 tuples. 245 | ``` 246 | Input : test_tuple1 = (7, 2), test_tuple2 = (7, 8) 247 | Output : [(7, 7), (7, 8), (2, 7), (2, 8), (7, 7), (7, 2), (8, 7), (8, 2)] 248 | ``` 249 | 250 | Q95. Write a Python program to sort a list of tuples by second item. 251 | ``` 252 | Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)] 253 | Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)] 254 | ``` 255 | 256 | Q96. Write a python program to print below pattern. 257 | ``` 258 | * 259 | * * 260 | * * * 261 | * * * * 262 | * * * * * 263 | ``` 264 | Q97. Write a python program to print below pattern. 265 | ``` 266 | * 267 | ** 268 | *** 269 | **** 270 | ***** 271 | ``` 272 | 273 | Q98. Write a python program to print below pattern. 274 | ``` 275 | * 276 | * * 277 | * * * 278 | * * * * 279 | * * * * * 280 | ``` 281 | 282 | Q99. Write a python program to print below pattern. 283 | ``` 284 | 1 285 | 1 2 286 | 1 2 3 287 | 1 2 3 4 288 | 1 2 3 4 5 289 | ``` 290 | 291 | Q100. Write a python program to print below pattern. 292 | ``` 293 | A 294 | B B 295 | C C C 296 | D D D D 297 | E E E E E 298 | ``` -------------------------------------------------------------------------------- /PythonAssignmentSolution.md: -------------------------------------------------------------------------------- 1 | ## Assignment Part-1 2 | Q1. Why do we call Python as a general purpose and high-level programming language? 3 | Ans1. Python is called general purpose because it can used to design and develop a wide variety of applications, across multiple domain. High-level programming language means it's more user-friendly. Coding in Python is like writing a story in English. 4 | 5 | Q2. Why is Python called a dynamically typed language? 6 | Ans2. Dynamically typed language means that variables are checked against types only when the program is executing. We don't need to declare the variable type, the interpreter automatically interprets it. 7 | 8 | Q3. List some pros and cons of Python programming language? 9 | 10 | Ans3. 11 | Pros: 12 | - Easy to use 13 | - Easy to integrate 14 | - Multi-paradigm approach 15 | - High library support 16 | 17 | Cons: 18 | - Slow speed of execution compared to C,C++ 19 | - Absence from mobile computing and browsers 20 | 21 | Q4. In what all domains can we use Python? 22 | 23 | Ans4. 24 | - Graphic design, image processing applications, Games, and Scientific/ computational Applications 25 | - Web frameworks and applications 26 | - Database Access 27 | - Language Development 28 | - Prototyping 29 | - Software Development 30 | - Data Science and Machine Learning 31 | - Scripting 32 | 33 | Q5. What are variable and how can we declare them? 34 | 35 | Ans5. A variable is a symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name. 36 | ```python 37 | var = 17 38 | ``` 39 | 40 | Q6. How can we take an input from the user in Python? 41 | 42 | Ans6. By using input() function. 43 | 44 | Q7. What is the default datatype of the value that has been taken as an input using input() function? 45 | Ans7. string 46 | 47 | Q8. What is type casting? 48 | 49 | Ans8. Type casting means changing the datatype of the variable. We can only type casting the datatype having higher bit value to the lower bit value. 50 | 51 | Q9. Can we take more than one input from the user using single input() function? If yes, how? If no, why? 52 | 53 | Ans9. Yes. 54 | ```python 55 | x, y, z = input("Enter three values: ").split(",") 56 | print("Total number of students: ", x) 57 | print("Number of boys is : ", y) 58 | print("Number of girls is : ", z) 59 | print() 60 | ``` 61 | ```python 62 | x = [int(x) for x in input("Enter multiple value: ").split(",")] 63 | print("Number of list is: ", x) 64 | ``` 65 | Q10. What are keywords? 66 | 67 | Ans10. Keywords in Python are reserved words that can not be used as a variable name, function name, or any other identifier. 68 | 69 | Q11. Can we use keywords as a variable? Support your answer with reason. 70 | 71 | Ans11. Yes we can but we shoudn't as it will override the properties of the keyword. 72 | 73 | Q12. What is indentation? What's the use of indentaion in Python? 74 | 75 | Ans12. Indentation is the whitespace used in Python. Indentation is used to create block of statement. 76 | 77 | Q13. How can we throw some output in Python? 78 | 79 | Ans13. Using print() function. 80 | 81 | Q14. What are operators in Python? 82 | 83 | Ans14. Symbols or keywords used to perform certain operations on values or variable are known as operators. There are different types of operators like 84 | - Arithmetic operators 85 | - Comparison Operators 86 | - Logical Operators 87 | - Bitwise Operators 88 | - Assignment Operators 89 | 90 | 91 | Q15. What is difference between / and // operators? 92 | 93 | Ans15. / is used for float division and // is used of floor (integer) division. 94 | 95 | Q16. Write a code that gives following as an output. 96 | ``` 97 | iNeuroniNeuroniNeuroniNeuron 98 | ``` 99 | Ans16. 100 | ```python 101 | "iNeuron"*3 102 | ``` 103 | 104 | Q17. Write a code to take a number as an input from the user and check if the number is odd or even. 105 | Ans17. 106 | ```python 107 | num = float(input("Enter a number: ")) 108 | if num%2 == 0: 109 | print(f"{num} is even") 110 | else: 111 | print(f"{num} is odd") 112 | ``` 113 | 114 | Q18. What are boolean operator? 115 | Ans18. True , False , not , and , or are the only built-in Python Boolean operators. 116 | 117 | Q19. What will the output of the following? 118 | ```python 119 | 1 or 0 120 | 121 | 0 and 0 122 | 123 | True and False and True 124 | 125 | 1 or 0 or 0 126 | ``` 127 | Ans.19 Output of the following code will be 128 | ```python 129 | 1 or 0 -> 1 130 | 0 and 0 -> 0 131 | True and False and True -> False 132 | 1 or 0 or 0 -> 1 133 | ``` 134 | 135 | Q20. What are conditional statements in Python? 136 | 137 | Ans20. In large projects we have to control the flow of execution of our program and we want to execute some set of statements only if the given condition is satisfied, and a different set of statements when it’s not satisfied. 138 | 139 | 140 | Q21. What is use of 'if', 'elif' and 'else' keywords? 141 | 142 | Ans21. if is the first condition check for the condition. 143 | 144 | if "if" is False then elif's condition is checked. 145 | 146 | else is checked when all the upper condition fails. 147 | 148 | 149 | Q22. Write a code to take the age of person as an input and if age >= 18 display "I can vote". If age is < 18 display "I can't vote". 150 | Ans22. 151 | ```python 152 | age = int(input("Enter you age: ")) 153 | if age >= 18: 154 | print("I can vote") 155 | else: 156 | print("I can't vote") 157 | ``` 158 | 159 | Q23. Write a code that displays the sum of all the even numbers from the given list. 160 | ```python 161 | numbers = [12, 75, 150, 180, 145, 525, 50] 162 | ``` 163 | Ans23. 164 | ```python 165 | numbers = [12, 75, 150, 180, 145, 525, 50] 166 | add = 0 167 | for num in numbers: 168 | if num%2 == 0: 169 | add = add+num 170 | else: 171 | continue 172 | print(add) 173 | ``` 174 | 175 | Q24. Write a code to take 3 numbers as an input from the user and display the greatest no as output. 176 | Ans24. 177 | ```python 178 | x, y, z = input("Enter 3 numbers seprated by comma: ").split(",") 179 | if int(x) > int(y) and int(x) > int(z): 180 | print(f"{x} is greatest") 181 | elif int(y) > int(z): 182 | print(f"{y} is greatest") 183 | else: 184 | print(f"{z} is greatest") 185 | 186 | ``` 187 | 188 | 189 | 190 | Q25. Write a program to display only those numbers from a list that satisfy the following conditions 191 | 192 | - The number must be divisible by five 193 | 194 | - If the number is greater than 150, then skip it and move to the next number 195 | 196 | - If the number is greater than 500, then stop the loop 197 | ``` 198 | numbers = [12, 75, 150, 180, 145, 525, 50] 199 | ``` 200 | Ans25. 201 | ```python 202 | numbers = [12, 75, 150, 180, 145, 525, 50] 203 | lst = [] 204 | for num in numbers: 205 | if num > 150: 206 | if num > 500: 207 | break 208 | elif num%5==0: 209 | lst.append(num) 210 | 211 | print(lst) 212 | 213 | ``` 214 | Q26. What is a string? How can we declare string in Python? 215 | 216 | Ans26. In Python, Strings are arrays of bytes representing Unicode characters. 217 | 218 | Q27. How can we access the string using its index? 219 | 220 | Ans27. Sqaure brackets can used to access the elements of the string. 221 | 222 | Q28. Write a code to get the desired output of the following 223 | ``` 224 | string = "Big Data iNeuron" 225 | desired_output = "iNeuron" 226 | ``` 227 | Ans28. 228 | ```python 229 | string[9:16] 230 | ``` 231 | Q29. Write a code to get the desired output of the following 232 | ``` 233 | string = "Big Data iNeuron" 234 | desired_output = "norueNi" 235 | ``` 236 | Ans29. 237 | ```python 238 | string[15:8:-1] 239 | ``` 240 | 241 | Q30. Resverse the string given in the above question. 242 | Ans30. 243 | ```python 244 | string[::-1] 245 | ``` 246 | 247 | Q31. How can you delete entire string at once? 248 | 249 | Ans31. We can delete the entire string at once by using del keyword. 250 | ```python 251 | str1 = "Hello" 252 | del(str1) 253 | ``` 254 | 255 | Q32. What is escape sequence? 256 | 257 | Ans32. The "backslash (\)" character as an escape character. In other words, it has a special meaning when we use it inside the strings. As the name suggests, the escape character escapes the characters in a string for a brief moment to introduce unique inclusion. 258 | 259 | Q33. How can you print the below string? 260 | ``` 261 | 'iNeuron's Big Data Course' 262 | ``` 263 | Ans33. 'iNeuron\'s Big Data Course' 264 | 265 | Q34. What is a list in Python? 266 | 267 | Ans34. Python list are dynamically sized array, declared in languages like C++ and Java. A list is a collection of things, enclosed in [ ] and separated by commas. 268 | 269 | Q35. How can you create a list in Python? 270 | 271 | Ans35. You can create a list by opening and closing the square brackets. 272 | 273 | Q36. How can we access the elements in a list? 274 | 275 | Ans36. We can access the elements in a list by indexing. 276 | 277 | Q37. Write a code to access the word "iNeuron" from the given list. 278 | ``` 279 | lst = [1,2,3,"Hi",[45,54, "iNeuron"], "Big Data"] 280 | ``` 281 | Ans37. 282 | ```python 283 | lst = [1,2,3,"Hi",[45,54, "iNeuron"], "Big Data"] 284 | lst[4][2] 285 | ``` 286 | Q38. Take a list as an input from the user and find the length of the list. 287 | 288 | Ans38. 289 | ```python 290 | n = input("Enter number of elements seprated by space: ").split(" ") 291 | print(len(n)) 292 | ``` 293 | 294 | Q39. Add the word "Big" in the 3rd index of the given list. 295 | ``` 296 | lst = ["Welcome", "to", "Data", "course"] 297 | ``` 298 | Ans39. 299 | ```python 300 | lst = ["Welcome", "to", "Data", "course"] 301 | lst.insert(2, "Big") 302 | ``` 303 | 304 | Q40. What is a tuple? How is it different from list? 305 | 306 | Ans40. Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers. 307 | Tuples are immutable where as list are mutable. We can also faster through the tuples than the list. 308 | 309 | Q41. How can you create a tuple in Python? 310 | 311 | Ans41. We can create tuple using round brackets (). 312 | 313 | Q42. Create a tuple and try to add your name in the tuple. Are you able to do it? Support your answer with reason. 314 | 315 | Ans42. No, I can't as tuples are immutable. The work around is it typecast tuple to list and then append. 316 | ```python 317 | tup = () 318 | tup = list(tup) 319 | tup.append("Vishal") 320 | tup = tuple(tup) 321 | tup 322 | ``` 323 | 324 | Q43. Can two tuple be appended. If yes, write a code for it. If not, why? 325 | 326 | Ans43.Yes, we can. 327 | ```python 328 | tup1 = ("Vishal ") 329 | tup2 = ("Singh") 330 | tup1+tup2 331 | ``` 332 | 333 | Q44. Take a tuple as an input and print the count of elements in it. 334 | 335 | Ans44. 336 | ```python 337 | x = input("Enter the values separeted by space: ").split(" ") 338 | x = tuple(x) 339 | print(len(x)) 340 | ``` 341 | 342 | Q45. What are sets in Python? 343 | 344 | Ans45. A set is an unordered collection of data types that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. 345 | 346 | Q46. How can you create a set? 347 | 348 | Ans46. We can create set using curly brackets {}. Keep in mind empty {} will result in dictionary hence there must be some value in the brackets. 349 | 350 | Q47. Create a set and add "iNeuron" in your set. 351 | 352 | Ans47. 353 | ```python 354 | set1 = {"iNeuron"} 355 | set1 356 | ``` 357 | 358 | Q48. Try to add multiple values using add() function. 359 | 360 | Ans48. 361 | ```python 362 | set1.add("Big") 363 | set1.add("Data") 364 | set1 365 | ``` 366 | 367 | Q49. How is update() different from add()? 368 | 369 | Ans49. We can add more than one element in a single go using update(), but using add() it's not possible. 370 | 371 | Q50. What is clear() in sets? 372 | 373 | Ans50. To remove all the elements from the set, clear() function is used. 374 | 375 | Q51. What is frozen set? 376 | 377 | Ans51. Frozen sets in Python are immutable objects that only support methods and operators that produce a result without affecting the frozen set or sets to which they are applied. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. 378 | 379 | Q52. How is frozen set different from set? 380 | 381 | Ans52. 382 | - Frozen sets are immutable where as sets are mutable. 383 | - Sets can't be used as keys in dictionary where as frozen sets can be used. 384 | 385 | Q53. What is union() in sets? Explain via code. 386 | 387 | 388 | Ans53. Python set Union() Method returns a new set which contains all the items from the original set. 389 | ```python 390 | set1 = {2, 4, 5, 6} 391 | set2 = {4, 6, 7, 8} 392 | set3 = {7, 8, 9, 10} 393 | 394 | print("set1 U set2 : ", set1 | set2) 395 | 396 | print("set1 U set2 U set3 :", set1 |set2 | set3) 397 | ``` 398 | 399 | Q54. What is intersection() in sets? Explain via code. 400 | 401 | Ans54. Python set intersection() method returns a new set with an element that is common to all set 402 | ```python 403 | set1 = {2, 4, 5, 6} 404 | set2 = {4, 6, 7, 8} 405 | set3 = {4, 6, 8} 406 | 407 | print("set1 intersection set2 : ", set1.intersection(set2)) 408 | 409 | print("set1 intersection set2 intersection set3 :", set1.intersection(set2, set3)) 410 | ``` 411 | 412 | Q55. What is dictionary ibn Python? 413 | 414 | Ans55. Dictionary in Python is a collection of keys values, used to store data values like a map, which, unlike other data types which hold only a single value as an element. 415 | 416 | Q56. How is dictionary different from all other data structures. 417 | 418 | Ans56. Dictionary is having key and value pair where as all other data structures have only values in them. 419 | 420 | Q57. How can we delare a dictionary in Python? 421 | 422 | Ans57. We can create dictionary using curly brackets {}. 423 | 424 | Q58. What will the output of the following? 425 | ``` 426 | var = {} 427 | print(type(var)) 428 | ``` 429 | Ans58. dict 430 | 431 | Q59. How can we add an element in a dictionary? 432 | 433 | Ans59. 434 | ```python 435 | Dict = {} 436 | Dict[0] = "Hello" 437 | Dict[1] = "Course: ["Data Science", "Big Data"]" 438 | ``` 439 | 440 | Q60. Create a dictionary and access all the values in that dictionary. 441 | 442 | Ans60. 443 | ```python 444 | Dict = {"Name": "Vishal", "Experience": 3, "Organisation":"iNeuron"} 445 | for i, j in Dict.items(): 446 | print(f"Key is {i} and value is {j}") 447 | ``` 448 | 449 | Q61. Create a nested dictionary and access all the element in the inner dictionary. 450 | 451 | Ans61. 452 | ```python 453 | Dict = {"Name": {"f_name":"Vishal", "l_name":"Singh"}, "Experience": 3, "Organisation":"iNeuron"} 454 | for i, j in Dict["Name"].items(): 455 | print(f"Key is {i} and value is {j}") 456 | ``` 457 | Q62. What is the use of get() function? 458 | 459 | Ans62. get() is also to access the elements in dictionary. 460 | ```python 461 | Dict = {"Name": "Vishal", "Experience": 3, "Organisation":"iNeuron"} 462 | print(Dict.get("Name")) 463 | ``` 464 | Q63. What is the use of items() function? 465 | 466 | Ans63. items() method is used to return the list with all dictionary keys with values. 467 | ```python 468 | Dict = {"Name": "Vishal", "Experience": 3, "Organisation":"iNeuron"} 469 | print(Dict.items()) 470 | ``` 471 | Q64. What is the use of pop() function? 472 | 473 | Ans64. 474 | ```python 475 | Dict = {"Name": "Vishal", "Experience": 3, "Organisation":"iNeuron"} 476 | print(Dict.pop("Name")) 477 | ``` 478 | Q65. What is the use of popitems() function? 479 | 480 | Ans65. popitem() method removes the last inserted key-value pair from the dictionary and returns it as a tuple. 481 | ```python 482 | Dict = {"Name": "Vishal", "Experience": 3, "Organisation":"iNeuron"} 483 | print(Dict.popitem()) 484 | ``` 485 | Q66. What is the use of keys() function? 486 | 487 | Ans66. keys() method returns a view object that displays a list of all the keys in the dictionary. 488 | ```python 489 | Dict = {"Name": "Vishal", "Experience": 3, "Organisation":"iNeuron"} 490 | print(Dict.keys()) 491 | ``` 492 | Q67. What is the use of values() function? 493 | 494 | 495 | Ans67. values() is an inbuilt method in Python programming language that returns a view object. The view object contains the values of the dictionary, as a list. 496 | ```python 497 | Dict = {"Name": "Vishal", "Experience": 3, "Organisation":"iNeuron"} 498 | print(Dict.values()) 499 | ``` 500 | 501 | Q68. What are loops in Python? 502 | 503 | Ans68. Loops are used the iterate over a block of statement multiple times. 504 | 505 | Q69. How many type of loop are there in Python? 506 | 507 | Ans69. There is for and while loop in Python 508 | 509 | Q70. What is the difference between for and while loops? 510 | 511 | Ans70. When we know the exact number of iterations, we can use for loop. When we want the to run till a certain condition is true we can use while loop. 512 | 513 | Q71. What is the use of continue statement? 514 | 515 | Ans71. Continue Statement skips the execution of the program block from after the continue statement and forces the control to start the next iteration. 516 | 517 | Q72. What is the use of break statement? 518 | 519 | Ans72. break statement in Python is used to bring the control out of the loop when some external condition is triggered. break statement is put inside the loop body 520 | 521 | Q73. What is the use of pass statement? 522 | 523 | Ans73. The pass statement is a null statement. But the difference between pass and comment is that comment is ignored by the interpreter whereas pass is not ignored. 524 | 525 | Q74. What is the use of range() function? 526 | 527 | Ans74. range() function returns a sequence of numbers, in a given range. The most common use of it is to iterate sequence on a sequence of numbers 528 | 529 | Q75. How can you loop over a dictionary? 530 | 531 | Ans75. 532 | ```python 533 | statesAndCapitals = { 534 | 'Gujarat': 'Gandhinagar', 535 | 'Maharashtra': 'Mumbai', 536 | 'Rajasthan': 'Jaipur', 537 | 'Bihar': 'Patna' 538 | } 539 | 540 | for state in statesAndCapitals: 541 | print(state) 542 | ``` 543 | 544 | ### Coding problems 545 | Q76. Write a Python program to find the factorial of a given number. 546 | 547 | Ans76. 548 | ```python 549 | def factorial(n): 550 | if n < 0: 551 | return 0 552 | elif n == 0 or n == 1: 553 | return 1 554 | else: 555 | fact = 1 556 | while(n>1): 557 | fact *= n 558 | n -= 1 559 | return fact 560 | 561 | n=6 562 | print(f"Factorial of {n} is {factorial(n)}") 563 | ``` 564 | Q77. Write a Python program to calculate the simple interest. Formula to calculate simple interest is SI = (P*R*T)/100 565 | 566 | Ans77. 567 | ```python 568 | def SI(p,r,t): 569 | si = (p*r*t)/100 570 | print(f"Simple interest is {si}") 571 | return si 572 | 573 | SI(8, 8, 6) 574 | ``` 575 | Q78. Write a Python program to calculate the compound interest. Formula of compound interest is A = P(1+ R/100)^t. 576 | 577 | Ans78. 578 | ```python 579 | def CI(p, r, t): 580 | amount = p*(1+r/100)**t 581 | ci = amount - p 582 | print(f"Compound intrest is {ci}") 583 | return ci 584 | 585 | CI(10000, 10.25, 5) 586 | ``` 587 | Q79. Write a Python program to check if a number is prime or not. 588 | 589 | Ans79. 590 | ```python 591 | from math import sqrt 592 | 593 | def is_prime(n): 594 | prime_flag = 0 595 | 596 | if(n > 1): 597 | for i in range(2, int(sqrt(n)) + 1): 598 | if (n % i == 0): 599 | prime_flag = 1 600 | break 601 | if (prime_flag == 0): 602 | print(f"{n} is a prime number.") 603 | else: 604 | print(f"{n} is not a prime number.") 605 | else: 606 | print(f"{n} is not a prime number.") 607 | 608 | is_prime(134) 609 | ``` 610 | Q80. Write a Python program to check Armstrong Number. 611 | 612 | Ans80. 613 | ```python 614 | def check_armstrong(n): 615 | s = n 616 | b = len(str(n)) 617 | sum1 = 0 618 | while n != 0: 619 | r = n % 10 620 | sum1 = sum1+(r**b) 621 | n = n//10 622 | if s == sum1: 623 | print(f"The given number {s} is armstrong number") 624 | else: 625 | print(f"The given number {s} is not armstrong number") 626 | 627 | check_armstrong(153) 628 | ``` 629 | Q81. Write a Python program to find the n-th Fibonacci Number. 630 | 631 | Ans81. 632 | ```python 633 | def Fibonacci(n): 634 | if n<= 0: 635 | print("Incorrect input") 636 | elif n == 1: 637 | return 0 638 | elif n == 2: 639 | return 1 640 | else: 641 | return Fibonacci(n-1)+Fibonacci(n-2) 642 | 643 | print(Fibonacci(7)) 644 | ``` 645 | Q82. Write a Python program to interchange the first and last element in a list. 646 | 647 | Ans82. 648 | ```python 649 | def swap_list(newList): 650 | size = len(newList) 651 | temp = newList[0] 652 | newList[0] = newList[size - 1] 653 | newList[size - 1] = temp 654 | 655 | return newList 656 | 657 | newList = [15, 12, 35, 17, 9, 56, 29] 658 | 659 | print(swap_list(newList)) 660 | ``` 661 | ```python 662 | def swap_list(newList): 663 | 664 | newList[0], newList[-1] = newList[-1], newList[0] 665 | 666 | return newList 667 | 668 | newList = [15, 12, 35, 17, 9, 56, 29] 669 | print(swap_list(newList)) 670 | ``` 671 | Q83. Write a Python program to swap two elements in a list. 672 | 673 | Ans83. 674 | ```python 675 | def swapPositions(list, pos1, pos2): 676 | 677 | list[pos1], list[pos2] = list[pos2], list[pos1] 678 | return list 679 | 680 | List = [15, 12, 35, 17, 9, 56, 29] 681 | pos1, pos2 = 1, 3 682 | 683 | print(f"Original list: {List}") 684 | print(f"Swapped list: {swapPositions(List, pos1, pos2)}") 685 | ``` 686 | Q84. Write a Python program to find N largest element from a list. 687 | 688 | Ans84. 689 | ```python 690 | def n_max_elements(list1, N): 691 | final_list = [] 692 | 693 | for i in range(0, N): 694 | max1 = 0 695 | 696 | for j in range(len(list1)): 697 | if list1[j] > max1: 698 | max1 = list1[j]; 699 | 700 | list1.remove(max1); 701 | final_list.append(max1) 702 | 703 | print(final_list) 704 | 705 | list1 = [2, 6, 41, 85, 0, 3, 7, 6, 10] 706 | N = 3 707 | 708 | n_max_elements(list1, N) 709 | ``` 710 | Q85. Write a Python program to find cumulative sum of a list. 711 | 712 | Ans85. 713 | ```python 714 | def cumulative_sum(lists): 715 | cu_list = [] 716 | length = len(lists) 717 | cu_list = [sum(lists[0:x:1]) for x in range(0, length+1)] 718 | return cu_list[1:] 719 | 720 | lists = [10, 20, 30, 40, 50] 721 | print(f"Cumulative sum of the list is {cumulative_sum(lists)}") 722 | ``` 723 | Q86. Write a Python program to check if a string is palindrome or not. 724 | 725 | Ans86. 726 | ```python 727 | def isPalindrome(s): 728 | if s == s[::-1]: 729 | return f"{s} is palindrome" 730 | return f"{s} is not palindrome" 731 | 732 | s = "dad" 733 | isPalindrome(s) 734 | ``` 735 | Q87. Write a Python program to remove i'th element from a string. 736 | 737 | Ans87. 738 | ```python 739 | def remove_ith_element(i): 740 | str1 = "Big Data Bootcamp" 741 | str2 = "" 742 | 743 | for n in range(len(str1)): 744 | if n == i: 745 | continue 746 | else: 747 | str2 = str2 + str1[n] 748 | 749 | return str2 750 | 751 | remove_ith_element(5) 752 | ``` 753 | Q88. Write a Python program to check if a substring is present in a given string. 754 | 755 | Ans88. 756 | ```python 757 | def check_substring(s2, s1): 758 | if (s2.count(s1) > 0): 759 | print(f'"{s1}" is a substring of "{s2}"') 760 | else: 761 | print(f'"{s1}" is not a substring of "{s2}"') 762 | 763 | 764 | s2 = "Welcome to iNeuron Big Data Bootcamp" 765 | s1 = "iNeuron" 766 | check_substring(s2, s1) 767 | ``` 768 | Q89. Write a Python program to find words which are greater than given length k. 769 | 770 | Ans89. 771 | ```python 772 | def string_greater_than_k(k, str): 773 | 774 | string = [] 775 | 776 | text = str.split(" ") 777 | 778 | for x in text: 779 | 780 | if len(x) > k: 781 | 782 | string.append(x) 783 | 784 | return string 785 | 786 | k = 3 787 | str ="Big Data Bootcamp" 788 | print(string_greater_than_k(k, str)) 789 | ``` 790 | Q90. Write a Python program to extract unquire dictionary values. 791 | 792 | Ans90. 793 | ```python 794 | test_dict = {'iNeuron': [5, 6, 7, 8], 795 | 'is': [10, 11, 7, 5], 796 | 'best': [6, 12, 10, 8], 797 | 'for': [1, 2, 5], 798 | 'big data': [2, 7, 12, 9] 799 | } 800 | 801 | print("The original dictionary is : " + str(test_dict)) 802 | 803 | res = list(sorted({ele for val in test_dict.values() for ele in val})) 804 | 805 | print("The unique values list is : " + str(res)) 806 | ``` 807 | Q91. Write a Python program to merge two dictionary. 808 | 809 | Ans91. 810 | ```python 811 | def Merge(dict1, dict2): 812 | return(dict2.update(dict1)) 813 | 814 | dict1 = {'a': 10, 'b': 8} 815 | dict2 = {'d': 6, 'c': 4} 816 | 817 | print(Merge(dict1, dict2)) 818 | 819 | print(dict2) 820 | 821 | ``` 822 | Q92. Write a Python program to convert a list of tuples into dictionary. 823 | ```python 824 | Input : [('Sachin', 10), ('MSD', 7), ('Kohli', 18), ('Rohit', 45)] 825 | Output : {'Sachin': 10, 'MSD': 7, 'Kohli': 18, 'Rohit': 45} 826 | ``` 827 | Ans92. 828 | ```python 829 | print (dict([('Sachin', 10), ('MSD', 7), ('Kohli', 18), ('Rohit', 45)])) 830 | ``` 831 | 832 | Q93. Write a Python program to create a list of tuples from given list having number and its cube in each tuple. 833 | ```python 834 | Input: list = [9, 5, 6] 835 | Output: [(9, 729), (5, 125), (6, 216)] 836 | ``` 837 | Ans93. 838 | ```python 839 | 840 | list1 = [9, 5, 6] 841 | 842 | res = [(val, pow(val, 3)) for val in list1] 843 | 844 | print(res) 845 | 846 | ``` 847 | Q94. Write a Python program to get all combinations of 2 tuples. 848 | ```python 849 | Input : test_tuple1 = (7, 2), test_tuple2 = (7, 8) 850 | Output : [(7, 7), (7, 8), (2, 7), (2, 8), (7, 7), (7, 2), (8, 7), (8, 2)] 851 | ``` 852 | Ans94. 853 | ```python 854 | test_tuple1 = (7, 2) 855 | test_tuple2 = (7, 8) 856 | 857 | res = [(a, b) for a in test_tuple1 for b in test_tuple2] 858 | res = res + [(a, b) for a in test_tuple2 for b in test_tuple1] 859 | 860 | print("The filtered tuple : ", str(res)) 861 | ``` 862 | Q95. Write a Python program to sort a list of tuples by second item. 863 | ```python 864 | Input : [('452', 10), ('256', 5), ('100', 20), ('135', 15)] 865 | Output : [('256', 5), ('452', 10), ('135', 15), ('100', 20)] 866 | ``` 867 | Ans95. 868 | ```python 869 | def Sort_Tuple(tup): 870 | 871 | lst = len(tup) 872 | for i in range(0, lst): 873 | 874 | for j in range(0, lst-i-1): 875 | if (tup[j][1] > tup[j + 1][1]): 876 | temp = tup[j] 877 | tup[j]= tup[j + 1] 878 | tup[j + 1]= temp 879 | return tup 880 | 881 | tup =[('452', 10), ('256', 5), ('100', 20), ('135', 15)] 882 | 883 | print(Sort_Tuple(tup)) 884 | ``` 885 | Q96. Write a python program to print below pattern. 886 | ``` 887 | * 888 | * * 889 | * * * 890 | * * * * 891 | * * * * * 892 | ``` 893 | Ans96. 894 | ```python 895 | def pypart(n): 896 | 897 | for i in range(0, n): 898 | 899 | for j in range(0, i+1): 900 | 901 | print("* ",end="") 902 | 903 | print("\r") 904 | 905 | n = 5 906 | pypart(n) 907 | 908 | ``` 909 | Q97. Write a python program to print below pattern. 910 | ``` 911 | * 912 | ** 913 | *** 914 | **** 915 | ***** 916 | ``` 917 | Ans97. 918 | ```python 919 | def inverse_pattern(): 920 | n=5;i=0 921 | while(i<=n): 922 | print(" " * (n - i) +"*" * i) 923 | i+=1 924 | 925 | inverse_pattern() 926 | ``` 927 | Q98. Write a python program to print below pattern. 928 | ``` 929 | * 930 | * * 931 | * * * 932 | * * * * 933 | * * * * * 934 | ``` 935 | Ans98. 936 | ```python 937 | 938 | def triangle(n): 939 | 940 | k = n - 1 941 | 942 | for i in range(0, n): 943 | 944 | for j in range(0, k): 945 | print(end=" ") 946 | 947 | k = k - 1 948 | 949 | for j in range(0, i+1): 950 | 951 | print("* ", end="") 952 | 953 | print("\r") 954 | 955 | n = 5 956 | triangle(n) 957 | 958 | ``` 959 | Q99. Write a python program to print below pattern. 960 | ``` 961 | 1 962 | 1 2 963 | 1 2 3 964 | 1 2 3 4 965 | 1 2 3 4 5 966 | ``` 967 | Ans99. 968 | ```python 969 | 970 | def numpat(n): 971 | 972 | num = 1 973 | 974 | for i in range(0, n): 975 | 976 | num = 1 977 | 978 | for j in range(0, i+1): 979 | 980 | print(num, end=" ") 981 | 982 | num = num + 1 983 | 984 | print("\r") 985 | 986 | n = 5 987 | numpat(n) 988 | 989 | ``` 990 | Q100. Write a python program to print below pattern. 991 | ``` 992 | A 993 | B B 994 | C C C 995 | D D D D 996 | E E E E E 997 | ``` 998 | Ans100. 999 | ```python 1000 | 1001 | def alphapat(n): 1002 | 1003 | num = 65 1004 | 1005 | for i in range(0, n): 1006 | 1007 | for j in range(0, i+1): 1008 | 1009 | ch = chr(num) 1010 | 1011 | print(ch, end=" ") 1012 | 1013 | num = num + 1 1014 | 1015 | print("\r") 1016 | 1017 | n = 5 1018 | alphapat(n) 1019 | 1020 | ``` 1021 | -------------------------------------------------------------------------------- /Python_OOP_Assignment.md: -------------------------------------------------------------------------------- 1 | ## Python OOP Assignment 2 | Q1. What is the purpose of Python's OOP? 3 | 4 | Q2. Where does an inheritance search look for an attribute? 5 | 6 | Q3. How do you distinguish between a class object and an instance object? 7 | 8 | Q4. What makes the first argument in a class’s method function special? 9 | 10 | Q5. What is the purpose of the init method? 11 | 12 | Q6. What is the process for creating a class instance? 13 | 14 | Q7. What is the process for creating a class? 15 | 16 | Q8. How would you define the superclasses of a class? 17 | 18 | Q9. What is the relationship between classes and modules? 19 | 20 | Q10. How do you make instances and classes? 21 | 22 | Q11. Where and how should be class attributes created? 23 | 24 | Q12. Where and how are instance attributes created? 25 | 26 | Q13. What does the term "self" in a Python class mean? 27 | 28 | Q14. How does a Python class handle operator overloading? 29 | 30 | Q15. When do you consider allowing operator overloading of your classes? 31 | 32 | Q16. What is the most popular form of operator overloading? 33 | 34 | Q17. What are the two most important concepts to grasp in order to comprehend Python OOP code? 35 | 36 | Q18. Describe three applications for exception processing. 37 | 38 | Q19. What happens if you don't do something extra to treat an exception? 39 | 40 | Q20. What are your options for recovering from an exception in your script? 41 | 42 | Q21. Describe two methods for triggering exceptions in your script. 43 | 44 | Q22. Identify two methods for specifying actions to be executed at termination time, regardless of 45 | whether or not an exception exists. 46 | 47 | Q23. What is the purpose of the try statement? 48 | 49 | Q24. What are the two most popular try statement variations? 50 | 51 | Q25. What is the purpose of the raise statement? 52 | 53 | Q26. What does the assert statement do, and what other statement is it like? 54 | 55 | Q27. What is the purpose of the with/as argument, and what other statement is it like? 56 | 57 | Q28. What are *args, **kwargs? 58 | 59 | Q29. How can I pass optional or keyword parameters from one function to another? 60 | 61 | Q30. What are Lambda Functions? 62 | 63 | Q31. Explain Inheritance in Python with an example? 64 | 65 | Q32. Suppose class C inherits from classes A and B as class C(A,B).Classes A and B both have their own versions of method func(). If we call func() from an object of 66 | class C, which version gets invoked? 67 | 68 | Q33. Which methods/functions do we use to determine the type of instance and inheritance? 69 | 70 | Q34.Explain the use of the 'nonlocal' keyword in Python. 71 | 72 | Q35. What is the global keyword? 73 | --------------------------------------------------------------------------------