Docsity

Python Programming first lecture, Exercises of Programming Languages

Toggle sidebar
bg1
Homework 3
Exercise 1. Concatenate two lists index-wise
a) list1 = ["M", "na", "i", "Ke"]; list2 = ["y", "me", "s", "lly"]
Expected output: ['My', 'name', 'is', 'Kelly']
b) list1 = ["Hello ", "take "]; list2 = ["Dear", "Sir"]
Expected output: ['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir']
Exercise 2. Given two Python lists. Iterate both lists simultaneously such that list1
should display the item in original order and list2 in reverse order.
list1 = [10, 20, 30, 40]; list2 = [100, 200, 300, 400]
Expected output:
10 400
20 300
30 200
40 100
Exercise 3. Add item 7000 after 6000 in the following Python List
list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]
Expected output: [10, 20, [300, 400, [5000, 6000, 7000], 500], 30, 40]
Exercise 4. Given a nested list, extend it by adding sub list ["h", "i", "j"] in such a way
that it will look like the following
Given List:
pf2

Partial preview of the text

Download Python Programming first lecture and more Exercises Programming Languages in PDF only on Docsity!

Homework 3

Exercise 1. Concatenate two lists index-wise

a) list1 = ["M", "na", "i", "Ke"]; list2 = ["y", "me", "s", "lly"] Expected output: ['My', 'name', 'is', 'Kelly'] b) list1 = ["Hello ", "take "]; list2 = ["Dear", "Sir"] Expected output: ['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir']

Exercise 2. Given two Python lists. Iterate both lists simultaneously such that list

should display the item in original order and list2 in reverse order. list1 = [10, 20, 30, 40]; list2 = [100, 200, 300, 400] Expected output: 10 400 20 300 30 200 40 100

Exercise 3. Add item 7000 after 6000 in the following Python List

list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40] Expected output: [10, 20, [300, 400, [5000, 6000, 7000], 500], 30, 40]

Exercise 4. Given a nested list, extend it by adding sub list ["h", "i", "j"] in such a way

that it will look like the following Given List:

list1 = ["a", "b", ["c", ["d", "e", ["f", "g"], "k"], "l"], "m", "n"] Sub List to be added = ["h", "i", "j"] Expected output: ['a', 'b', ['c', ['d', 'e', ['f', 'g', 'h', 'i', 'j' ], 'k'], 'l'], 'm', 'n']

Exercise 5. Given a Python list, remove all occurrence of 20 from the list

list1 = [5, 20, 15, 20, 25, 50, 20] Expected output: [5, 15, 25, 50]

Exercise 6. Approximate ๐…

The value of ๐œ‹ can be approximated by the following infinite series: ๐œ‹ โ‰ˆ 3 +

2 ร— 3 ร— 4

4 ร— 5 ร— 6

6 ร— 7 ร— 8

8 ร— 9 ร— 10

10 ร— 11 ร— 12

Write a program that displays 15 approximations of ๐œ‹. The first approximation should make use of only the first term from the infinite series. Each additional approximation displayed by your program should include one more term in the series, making it a better approximation of ๐œ‹ than any of the appr viously

Exercise 7. Approximating the following integral:

2 1

a) By The Trapezoidal Rule (see https://en.wikipedia.org/wiki/Trapezoidal_rule). b) By Simpson's rule (see https://en.wikipedia.org/wiki/Simpson%27s_rule).