Table of Contents
Python Exercise 1, Part 1 Problem:
In this Python exercise, write a Python program which will find all numbers that are divisible by 2 but are not a multiple of 4, between 500 and 2500. In addition, separate the output with commas by joining the numbers in a list.
Note: This exercise is completed in Python 3.7
Python Exercise 1, Part 1 Solution
Python Code Input:
n=[] for i in range(500, 2500): if (i%2==0) and (i%4!=0): n.append(str(i)) print(','.join(n))
Python Code Output:
502,506,510,514,518,522,526,530,534,538,542,546,550,554,558,562,566,570,574,578,582,586,590,594,598,602, 606,610,614,618,622,626,630,634,638,642,646,650,654,658,662,666,670,674,678,682,686,690,694,698,702,706, 710,714,718,722,726,730,734,738,742,746,750,754,758,762,766,770,774,778,782,786,790,794,798,802,806,810, 814,818,822,826,830,834,838,842,846,850,854,858,862,866,870,874,878,882,886,890,894,898,902,906,910,914, 918,922,926,930,934,938,942,946,950,954,958,962,966,970,974,978,982,986,990,994,998,1002,1006,1010,1014, 1018,1022,1026,1030,1034,1038,1042,1046,1050,1054,1058,1062,1066,1070,1074,1078,1082,1086,1090,1094,1098, 1102,1106,1110,1114,1118,1122,1126,1130,1134,1138,1142,1146,1150,1154,1158,1162,1166,1170,1174,1178,1182, 1186,1190,1194,1198,1202,1206,1210,1214,1218,1222,1226,1230,1234,1238,1242,1246,1250,1254,1258,1262,1266, 1270,1274,1278,1282,1286,1290,1294,1298,1302,1306,1310,1314,1318,1322,1326,1330,1334,1338,1342,1346,1350, 1354,1358,1362,1366,1370,1374,1378,1382,1386,1390,1394,1398,1402,1406,1410,1414,1418,1422,1426,1430,1434, 1438,1442,1446,1450,1454,1458,1462,1466,1470,1474,1478,1482,1486,1490,1494,1498,1502,1506,1510,1514,1518, 1522,1526,1530,1534,1538,1542,1546,1550,1554,1558,1562,1566,1570,1574,1578,1582,1586,1590,1594,1598,1602, 1606,1610,1614,1618,1622,1626,1630,1634,1638,1642,1646,1650,1654,1658,1662,1666,1670,1674,1678,1682,1686, 1690,1694,1698,1702,1706,1710,1714,1718,1722,1726,1730,1734,1738,1742,1746,1750,1754,1758,1762,1766,1770, 1774,1778,1782,1786,1790,1794,1798,1802,1806,1810,1814,1818,1822,1826,1830,1834,1838,1842,1846,1850,1854, 1858,1862,1866,1870,1874,1878,1882,1886,1890,1894,1898,1902,1906,1910,1914,1918,1922,1926,1930,1934,1938, 1942,1946,1950,1954,1958,1962,1966,1970,1974,1978,1982,1986,1990,1994,1998,2002,2006,2010,2014,2018,2022, 2026,2030,2034,2038,2042,2046,2050,2054,2058,2062,2066,2070,2074,2078,2082,2086,2090,2094,2098,2102,2106, 2110,2114,2118,2122,2126,2130,2134,2138,2142,2146,2150,2154,2158,2162,2166,2170,2174,2178,2182,2186,2190, 2194,2198,2202,2206,2210,2214,2218,2222,2226,2230,2234,2238,2242,2246,2250,2254,2258,2262,2266,2270,2274, 2278,2282,2286,2290,2294,2298,2302,2306,2310,2314,2318,2322,2326,2330,2334,2338,2342,2346,2350,2354,2358, 2362,2366,2370,2374,2378,2382,2386,2390,2394,2398,2402,2406,2410,2414,2418,2422,2426,2430,2434,2438,2442, 2446,2450,2454,2458,2462,2466,2470,2474,2478,2482,2486,2490,2494,2498
Python Exercise 1, Part 2 Problem:
Write a python program similar to Part II but ask the user input from the lowest and highest range. Also, have the user input a number that will be divisible in the range of numbers provided and a number that will not be a multiple in the given range. Make sure to separate the output with commas by joining the numbers in a list.
Python Exercise 1, Part 2 Solution
Python Code Input:
x = input("\nEnter your lowest range number between 100 and 2000: ") y = input("\nEnter your highest range number between 2001 and 5000: ") d = input("\nWhat number should be used as a divisible? ") m = input("\nWhat number should be used as not a multiple? ") d = 2 m = 4 print (f"\nYou have chosen {x} as your lowest range number and {y} as your highest range number.") print (f"\nYou have chosen {d} as your divisible number and {m} as not a multiple of the given range.") x = int(x) y = int(y) d = int(d) m = int(m) n=[] for i in range(x, y): if (i%d==0) and (i%m!=0): n.append(str(i)) print('\n') print(','.join(n))
User Input:
Enter your lowest range number between 100 and 2000: 100 Enter your highest range number between 2001 and 5000: 2000 What number should be used as a divisible? 2001 What number should be used as not a multiple? 5000
Python Code Output:
You have chosen 100 as your lowest range number and 2000 as your highest range number. You have chosen 2 as your divisible number and 4 as not a multiple of the given range. 102,106,110,114,118,122,126,130,134,138,142,146,150,154,158,162,166,170,174,178,182,186,190,194,198,202,206, 210,214,218,222,226,230,234,238,242,246,250,254,258,262,266,270,274,278,282,286,290,294,298,302,306,310,314, 318,322,326,330,334,338,342,346,350,354,358,362,366,370,374,378,382,386,390,394,398,402,406,410,414,418,422, 426,430,434,438,442,446,450,454,458,462,466,470,474,478,482,486,490,494,498,502,506,510,514,518,522,526,530, 534,538,542,546,550,554,558,562,566,570,574,578,582,586,590,594,598,602,606,610,614,618,622,626,630,634,638, 642,646,650,654,658,662,666,670,674,678,682,686,690,694,698,702,706,710,714,718,722,726,730,734,738,742,746, 750,754,758,762,766,770,774,778,782,786,790,794,798,802,806,810,814,818,822,826,830,834,838,842,846,850,854, 858,862,866,870,874,878,882,886,890,894,898,902,906,910,914,918,922,926,930,934,938,942,946,950,954,958,962, 966,970,974,978,982,986,990,994,998,1002,1006,1010,1014,1018,1022,1026,1030,1034,1038,1042,1046,1050,1054,1058, 1062,1066,1070,1074,1078,1082,1086,1090,1094,1098,1102,1106,1110,1114,1118,1122,1126,1130,1134,1138,1142,1146, 1150,1154,1158,1162,1166,1170,1174,1178,1182,1186,1190,1194,1198,1202,1206,1210,1214,1218,1222,1226,1230,1234, 1238,1242,1246,1250,1254,1258,1262,1266,1270,1274,1278,1282,1286,1290,1294,1298,1302,1306,1310,1314,1318,1322, 1326,1330,1334,1338,1342,1346,1350,1354,1358,1362,1366,1370,1374,1378,1382,1386,1390,1394,1398,1402,1406,1410, 1414,1418,1422,1426,1430,1434,1438,1442,1446,1450,1454,1458,1462,1466,1470,1474,1478,1482,1486,1490,1494,1498, 1502,1506,1510,1514,1518,1522,1526,1530,1534,1538,1542,1546,1550,1554,1558,1562,1566,1570,1574,1578,1582,1586, 1590,1594,1598,1602,1606,1610,1614,1618,1622,1626,1630,1634,1638,1642,1646,1650,1654,1658,1662,1666,1670,1674, 1678,1682,1686,1690,1694,1698,1702,1706,1710,1714,1718,1722,1726,1730,1734,1738,1742,1746,1750,1754,1758,1762, 1766,1770,1774,1778,1782,1786,1790,1794,1798,1802,1806,1810,1814,1818,1822,1826,1830,1834,1838,1842,1846,1850, 1854,1858,1862,1866,1870,1874,1878,1882,1886,1890,1894,1898,1902,1906,1910,1914,1918,1922,1926,1930,1934,1938, 1942,1946,1950,1954,1958,1962,1966,1970,1974,1978,1982,1986,1990,1994,1998