Python练习

目录

1. 找出10000以内能被5或6整除,但不能被两者同时整除的数(函数)

测试结果:

2. 写一个方法,计算列表所有偶数下标元素的和(注意返回值)

测试结果:

3. 根据完整的路径从路径中分离文件路径、文件名及扩展名 

测试结果:

4. 根据标点符号对字符串进行分行

测试结果:

5. 去掉字符串数组中每个字符串的空格

测试结果:

6. 两个学员输入各自最喜欢的游戏名称,判断是否一致,如      果相等,则输出你们俩喜欢相同的游戏;如果不相同,则输      出你们俩喜欢不相同的游戏。

测试结果:

7. 上题中两位同学输入 lol和 LOL代表同一游戏,怎么办?

测试结果;

8. 让用户输入一个日期格式如“2008/08/08”,将 输入的日          期格式转换为“2008年-8月-8日”。

测试结果:

9. 接收用户输入的字符串,将其中的字符进行排序(升      序),并以逆序的顺序输出,“cabed”→"abcde"→“edcba”

10. 接收用户输入的一句英文,将其中的单词以反序输      出,“hello c java python”→“python java c hello”。

测试结果: 

11. 从请求地址中提取出用户名和域名       http://www.163.com?userName=admin&pwd=123456

 测试结果:

12. 有个字符串数组,存储了10个书名,书名有长有短,现       在将他们统一处理,若书名长度大于10,则截取长度8的       子串并且最后添加“...”,加一个竖线后输出作者的名字。

 测试结果:

13. 让用户输入一句话,找出所有"呵"的位置。

测试结果:

14. 让用户输入一句话,判断这句话中有没有邪恶,如果有邪       恶就替换成这种形式然后输出,如:“老牛很邪恶”,输出后变       成”老牛很**”;

测试结果;

15. 判断一个字符是否是回文字符串           "1234567654321"           "上海自来水来自海上"

 测试结果:

16. 过滤某个文件夹下的所有"xx.py"python文件


1. 找出10000以内能被5或6整除,但不能被两者同时整除的数(函数)

def find_numbers():  
    result = []  
    for i in range(1, 10001):  
        if (i % 5 == 0 or i % 6 == 0) and not (i % 5 == 0 and i % 6 == 0):  
            result.append(i)  
    return result  
  
numbers = find_numbers()  
print(numbers)

测试结果:

[5, 6, 10, 12, 15, 18, 20, 24, 25, 35, 36, 40, 42, 45, 48, 50, 54, 55, 65, 66, 70, 72, 75, 78, 80, 84, 85, 95, 96, 100, 102, 105, 108, 110, 114, 115, 125, 126, 130, 132, 135, 138, 140, 144, 145, 155, 156, 160, 162, 165, 168, 170, 174, 175, 185, 186, 190, 192, 195, 198, 200, 204, 205, 215, 216, 220, 222, 225, 228, 230, 234, 235, 245, 246, 250, 252, 255, 258, 260, 264, 265, 275, 276, 280, 282, 285, 288, 290, 294, 295, 305, 306, 310, 312, 315, 318, 320, 324, 325, 335, 336, 340, 342, 345, 348, 350, 354, 355, 365, 366, 370, 372, 375, 378, 380, 384, 385, 395, 396, 400, 402, 405, 408, 410, 414, 415, 425, 426, 430, 432, 435, 438, 440, 444, 445, 455, 456, 460, 462, 465, 468, 470, 474, 475, 485, 486, 490, 492, 495, 498, 500, 504, 505, 515, 516, 520, 522, 525, 528, 530, 534, 535, 545, 546, 550, 552, 555, 558, 560, 564, 565, 575, 576, 580, 582, 585, 588, 590, 594, 595, 605, 606, 610, 612, 615, 618, 620, 624, 625, 635, 636, 640, 642, 645, 648, 650, 654, 655, 665, 666, 670, 672, 675, 678, 680, 684, 685, 695, 696, 700, 702, 705, 708, 710, 714, 715, 725, 726, 730, 732, 735, 738, 740, 744, 745, 755, 756, 760, 762, 765, 768, 770, 774, 775, 785, 786, 790, 792, 795, 798, 800, 804, 805, 815, 816, 820, 822, 825, 828, 830, 834, 835, 845, 846, 850, 852, 855, 858, 860, 864, 865, 875, 876, 880, 882, 885, 888, 890, 894, 895, 905, 906, 910, 912, 915, 918, 920, 924, 925, 935, 936, 940, 942, 945, 948, 950, 954, 955, 965, 966, 970, 972, 975, 978, 980, 984, 985, 995, 996, 1000, 1002, 1005, 1008, 1010, 1014, 1015, 1025, 1026, 1030, 1032, 1035, 1038, 1040, 1044, 1045, 1055, 1056, 1060, 1062, 1065, 1068, 1070, 1074, 1075, 1085, 1086, 1090, 1092, 1095, 1098, 1100, 1104, 1105, 1115, 1116, 1120, 1122, 1125, 1128, 1130, 1134, 1135, 1145, 1146, 1150, 1152, 1155, 1158, 1160, 1164, 1165, 1175, 1176, 1180, 1182, 1185, 1188, 1190, 1194, 1195, 1205, 1206, 1210, 1212, 1215, 1218, 1220, 1224, 1225, 1235, 1236, 1240, 1242, 1245, 1248, 1250, 1254, 1255, 1265, 1266, 1270, 1272, 1275, 1278, 1280, 1284, 1285, 1295, 1296, 1300, 1302, 1305, 1308, 1310, 1314, 1315, 1325, 1326, 1330, 1332, 1335, 1338, 1340, 1344, 1345, 1355, 1356, 1360, 1362, 1365, 1368, 1370, 1374, 1375, 1385, 1386, 1390, 1392, 1395, 1398, 1400, 1404, 1405, 1415, 1416, 1420, 1422, 1425, 1428, 1430, 1434, 1435, 1445, 1446, 1450, 1452, 1455, 1458, 1460, 1464, 1465, 1475, 1476, 1480, 1482, 1485, 1488, 1490, 1494, 1495, 1505, 1506, 1510, 1512, 1515, 1518, 1520, 1524, 1525, 1535, 1536, 1540, 1542, 1545, 1548, 1550, 1554, 1555, 1565, 1566, 1570, 1572, 1575, 1578, 1580, 1584, 1585, 1595, 1596, 1600, 1602, 1605, 1608, 1610, 1614, 1615, 1625, 1626, 1630, 1632, 1635, 1638, 1640, 1644, 1645, 1655, 1656, 1660, 1662, 1665, 1668, 1670, 1674, 1675, 1685, 1686, 1690, 1692, 1695, 1698, 1700, 1704, 1705, 1715, 1716, 1720, 1722, 1725, 1728, 1730, 1734, 1735, 1745, 1746, 1750, 1752, 1755, 1758, 1760, 1764, 1765, 1775, 1776, 1780, 1782, 1785, 1788, 1790, 1794, 1795, 1805, 1806, 1810, 1812, 1815, 1818, 1820, 1824, 1825, 1835, 1836, 1840, 1842, 1845, 1848, 1850, 1854, 1855, 1865, 1866, 1870, 1872, 1875, 1878, 1880, 1884, 1885, 1895, 1896, 1900, 1902, 1905, 1908, 1910, 1914, 1915, 1925, 1926, 1930, 1932, 1935, 1938, 1940, 1944, 1945, 1955, 1956, 1960, 1962, 1965, 1968, 1970, 1974, 1975, 1985, 1986, 1990, 1992, 1995, 1998, 2000, 2004, 2005, 2015, 2016, 2020, 2022, 2025, 2028, 2030, 2034, 2035, 2045, 2046, 2050, 2052, 2055, 2058, 2060, 2064, 2065, 2075, 2076, 2080, 2082, 2085, 2088, 2090, 2094, 2095, 2105, 2106, 2110, 2112, 2115, 2118, 2120, 2124, 2125, 2135, 2136, 2140, 2142, 2145, 2148, 2150, 2154, 2155, 2165, 2166, 2170, 2172, 2175, 2178, 2180, 2184, 2185, 2195, 2196, 2200, 2202, 2205, 2208, 2210, 2214, 2215, 2225, 2226, 2230, 2232, 2235, 2238, 2240, 2244, 2245, 2255, 2256, 2260, 2262, 2265, 2268, 2270, 2274, 2275, 2285, 2286, 2290, 2292, 2295, 2298, 2300, 2304, 2305, 2315, 2316, 2320, 2322, 2325, 2328, 2330, 2334, 2335, 2345, 2346, 2350, 2352, 2355, 2358, 2360, 2364, 2365, 2375, 2376, 2380, 2382, 2385, 2388, 2390, 2394, 2395, 2405, 2406, 2410, 2412, 2415, 2418, 2420, 2424, 2425, 2435, 2436, 2440, 2442, 2445, 2448, 2450, 2454, 2455, 2465, 2466, 2470, 2472, 2475, 2478, 2480, 2484, 2485, 2495, 2496, 2500, 2502, 2505, 2508, 2510, 2514, 2515, 2525, 2526, 2530, 2532, 2535, 2538, 2540, 2544, 2545, 2555, 2556, 2560, 2562, 2565, 2568, 2570, 2574, 2575, 2585, 2586, 2590, 2592, 2595, 2598, 2600, 2604, 2605, 2615, 2616, 2620, 2622, 2625, 2628, 2630, 2634, 2635, 2645, 2646, 2650, 2652, 2655, 2658, 2660, 2664, 2665, 2675, 2676, 2680, 2682, 2685, 2688, 2690, 2694, 2695, 2705, 2706, 2710, 2712, 2715, 2718, 2720, 2724, 2725, 2735, 2736, 2740, 2742, 2745, 2748, 2750, 2754, 2755, 2765, 2766, 2770, 2772, 2775, 2778, 2780, 2784, 2785, 2795, 2796, 2800, 2802, 2805, 2808, 2810, 2814, 2815, 2825, 2826, 2830, 2832, 2835, 2838, 2840, 2844, 2845, 2855, 2856, 2860, 2862, 2865, 2868, 2870, 2874, 2875, 2885, 2886, 2890, 2892, 2895, 2898, 2900, 2904, 2905, 2915, 2916, 2920, 2922, 2925, 2928, 2930, 2934, 2935, 2945, 2946, 2950, 2952, 2955, 2958, 2960, 2964, 2965, 2975, 2976, 2980, 2982, 2985, 2988, 2990, 2994, 2995, 3005, 3006, 3010, 3012, 3015, 3018, 3020, 3024, 3025, 3035, 3036, 3040, 3042, 3045, 3048, 3050, 3054, 3055, 3065, 3066, 3070, 3072, 3075, 3078, 3080, 3084, 3085, 3095, 3096, 3100, 3102, 3105, 3108, 3110, 3114, 3115, 3125, 3126, 3130, 3132, 3135, 3138, 3140, 3144, 3145, 3155, 3156, 3160, 3162, 3165, 3168, 3170, 3174, 3175, 3185, 3186, 3190, 3192, 3195, 3198, 3200, 3204, 3205, 3215, 3216, 3220, 3222, 3225, 3228, 3230, 3234, 3235, 3245, 3246, 3250, 3252, 3255, 3258, 3260, 3264, 3265, 3275, 3276, 3280, 3282, 3285, 3288, 3290, 3294, 3295, 3305, 3306, 3310, 3312, 3315, 3318, 3320, 3324, 3325, 3335, 3336, 3340, 3342, 3345, 3348, 3350, 3354, 3355, 3365, 3366, 3370, 3372, 3375, 3378, 3380, 3384, 3385, 3395, 3396, 3400, 3402, 3405, 3408, 3410, 3414, 3415, 3425, 3426, 3430, 3432, 3435, 3438, 3440, 3444, 3445, 3455, 3456, 3460, 3462, 3465, 3468, 3470, 3474, 3475, 3485, 3486, 3490, 3492, 3495, 3498, 3500, 3504, 3505, 3515, 3516, 3520, 3522, 3525, 3528, 3530, 3534, 3535, 3545, 3546, 3550, 3552, 3555, 3558, 3560, 3564, 3565, 3575, 3576, 3580, 3582, 3585, 3588, 3590, 3594, 3595, 3605, 3606, 3610, 3612, 3615, 3618, 3620, 3624, 3625, 3635, 3636, 3640, 3642, 3645, 3648, 3650, 3654, 3655, 3665, 3666, 3670, 3672, 3675, 3678, 3680, 3684, 3685, 3695, 3696, 3700, 3702, 3705, 3708, 3710, 3714, 3715, 3725, 3726, 3730, 3732, 3735, 3738, 3740, 3744, 3745, 3755, 3756, 3760, 3762, 3765, 3768, 3770, 3774, 3775, 3785, 3786, 3790, 3792, 3795, 3798, 3800, 3804, 3805, 3815, 3816, 3820, 3822, 3825, 3828, 3830, 3834, 3835, 3845, 3846, 3850, 3852, 3855, 3858, 3860, 3864, 3865, 3875, 3876, 3880, 3882, 3885, 3888, 3890, 3894, 3895, 3905, 3906, 3910, 3912, 3915, 3918, 3920, 3924, 3925, 3935, 3936, 3940, 3942, 3945, 3948, 3950, 3954, 3955, 3965, 3966, 3970, 3972, 3975, 3978, 3980, 3984, 3985, 3995, 3996, 4000, 4002, 4005, 4008, 4010, 4014, 4015, 4025, 4026, 4030, 4032, 4035, 4038, 4040, 4044, 4045, 4055, 4056, 4060, 4062, 4065, 4068, 4070, 4074, 4075, 4085, 4086, 4090, 4092, 4095, 4098, 4100, 4104, 4105, 4115, 4116, 4120, 4122, 4125, 4128, 4130, 4134, 4135, 4145, 4146, 4150, 4152, 4155, 4158, 4160, 4164, 4165, 4175, 4176, 4180, 4182, 4185, 4188, 4190, 4194, 4195, 4205, 4206, 4210, 4212, 4215, 4218, 4220, 4224, 4225, 4235, 4236, 4240, 4242, 4245, 4248, 4250, 4254, 4255, 4265, 4266, 4270, 4272, 4275, 4278, 4280, 4284, 4285, 4295, 4296, 4300, 4302, 4305, 4308, 4310, 4314, 4315, 4325, 4326, 4330, 4332, 4335, 4338, 4340, 4344, 4345, 4355, 4356, 4360, 4362, 4365, 4368, 4370, 4374, 4375, 4385, 4386, 4390, 4392, 4395, 4398, 4400, 4404, 4405, 4415, 4416, 4420, 4422, 4425, 4428, 4430, 4434, 4435, 4445, 4446, 4450, 4452, 4455, 4458, 4460, 4464, 4465, 4475, 4476, 4480, 4482, 4485, 4488, 4490, 4494, 4495, 4505, 4506, 4510, 4512, 4515, 4518, 4520, 4524, 4525, 4535, 4536, 4540, 4542, 4545, 4548, 4550, 4554, 4555, 4565, 4566, 4570, 4572, 4575, 4578, 4580, 4584, 4585, 4595, 4596, 4600, 4602, 4605, 4608, 4610, 4614, 4615, 4625, 4626, 4630, 4632, 4635, 4638, 4640, 4644, 4645, 4655, 4656, 4660, 4662, 4665, 4668, 4670, 4674, 4675, 4685, 4686, 4690, 4692, 4695, 4698, 4700, 4704, 4705, 4715, 4716, 4720, 4722, 4725, 4728, 4730, 4734, 4735, 4745, 4746, 4750, 4752, 4755, 4758, 4760, 4764, 4765, 4775, 4776, 4780, 4782, 4785, 4788, 4790, 4794, 4795, 4805, 4806, 4810, 4812, 4815, 4818, 4820, 4824, 4825, 4835, 4836, 4840, 4842, 4845, 4848, 4850, 4854, 4855, 4865, 4866, 4870, 4872, 4875, 4878, 4880, 4884, 4885, 4895, 4896, 4900, 4902, 4905, 4908, 4910, 4914, 4915, 4925, 4926, 4930, 4932, 4935, 4938, 4940, 4944, 4945, 4955, 4956, 4960, 4962, 4965, 4968, 4970, 4974, 4975, 4985, 4986, 4990, 4992, 4995, 4998, 5000, 5004, 5005, 5015, 5016, 5020, 5022, 5025, 5028, 5030, 5034, 5035, 5045, 5046, 5050, 5052, 5055, 5058, 5060, 5064, 5065, 5075, 5076, 5080, 5082, 5085, 5088, 5090, 5094, 5095, 5105, 5106, 5110, 5112, 5115, 5118, 5120, 5124, 5125, 5135, 5136, 5140, 5142, 5145, 5148, 5150, 5154, 5155, 5165, 5166, 5170, 5172, 5175, 5178, 5180, 5184, 5185, 5195, 5196, 5200, 5202, 5205, 5208, 5210, 5214, 5215, 5225, 5226, 5230, 5232, 5235, 5238, 5240, 5244, 5245, 5255, 5256, 5260, 5262, 5265, 5268, 5270, 5274, 5275, 5285, 5286, 5290, 5292, 5295, 5298, 5300, 5304, 5305, 5315, 5316, 5320, 5322, 5325, 5328, 5330, 5334, 5335, 5345, 5346, 5350, 5352, 5355, 5358, 5360, 5364, 5365, 5375, 5376, 5380, 5382, 5385, 5388, 5390, 5394, 5395, 5405, 5406, 5410, 5412, 5415, 5418, 5420, 5424, 5425, 5435, 5436, 5440, 5442, 5445, 5448, 5450, 5454, 5455, 5465, 5466, 5470, 5472, 5475, 5478, 5480, 5484, 5485, 5495, 5496, 5500, 5502, 5505, 5508, 5510, 5514, 5515, 5525, 5526, 5530, 5532, 5535, 5538, 5540, 5544, 5545, 5555, 5556, 5560, 5562, 5565, 5568, 5570, 5574, 5575, 5585, 5586, 5590, 5592, 5595, 5598, 5600, 5604, 5605, 5615, 5616, 5620, 5622, 5625, 5628, 5630, 5634, 5635, 5645, 5646, 5650, 5652, 5655, 5658, 5660, 5664, 5665, 5675, 5676, 5680, 5682, 5685, 5688, 5690, 5694, 5695, 5705, 5706, 5710, 5712, 5715, 5718, 5720, 5724, 5725, 5735, 5736, 5740, 5742, 5745, 5748, 5750, 5754, 5755, 5765, 5766, 5770, 5772, 5775, 5778, 5780, 5784, 5785, 5795, 5796, 5800, 5802, 5805, 5808, 5810, 5814, 5815, 5825, 5826, 5830, 5832, 5835, 5838, 5840, 5844, 5845, 5855, 5856, 5860, 5862, 5865, 5868, 5870, 5874, 5875, 5885, 5886, 5890, 5892, 5895, 5898, 5900, 5904, 5905, 5915, 5916, 5920, 5922, 5925, 5928, 5930, 5934, 5935, 5945, 5946, 5950, 5952, 5955, 5958, 5960, 5964, 5965, 5975, 5976, 5980, 5982, 5985, 5988, 5990, 5994, 5995, 6005, 6006, 6010, 6012, 6015, 6018, 6020, 6024, 6025, 6035, 6036, 6040, 6042, 6045, 6048, 6050, 6054, 6055, 6065, 6066, 6070, 6072, 6075, 6078, 6080, 6084, 6085, 6095, 6096, 6100, 6102, 6105, 6108, 6110, 6114, 6115, 6125, 6126, 6130, 6132, 6135, 6138, 6140, 6144, 6145, 6155, 6156, 6160, 6162, 6165, 6168, 6170, 6174, 6175, 6185, 6186, 6190, 6192, 6195, 6198, 6200, 6204, 6205, 6215, 6216, 6220, 6222, 6225, 6228, 6230, 6234, 6235, 6245, 6246, 6250, 6252, 6255, 6258, 6260, 6264, 6265, 6275, 6276, 6280, 6282, 6285, 6288, 6290, 6294, 6295, 6305, 6306, 6310, 6312, 6315, 6318, 6320, 6324, 6325, 6335, 6336, 6340, 6342, 6345, 6348, 6350, 6354, 6355, 6365, 6366, 6370, 6372, 6375, 6378, 6380, 6384, 6385, 6395, 6396, 6400, 6402, 6405, 6408, 6410, 6414, 6415, 6425, 6426, 6430, 6432, 6435, 6438, 6440, 6444, 6445, 6455, 6456, 6460, 6462, 6465, 6468, 6470, 6474, 6475, 6485, 6486, 6490, 6492, 6495, 6498, 6500, 6504, 6505, 6515, 6516, 6520, 6522, 6525, 6528, 6530, 6534, 6535, 6545, 6546, 6550, 6552, 6555, 6558, 6560, 6564, 6565, 6575, 6576, 6580, 6582, 6585, 6588, 6590, 6594, 6595, 6605, 6606, 6610, 6612, 6615, 6618, 6620, 6624, 6625, 6635, 6636, 6640, 6642, 6645, 6648, 6650, 6654, 6655, 6665, 6666, 6670, 6672, 6675, 6678, 6680, 6684, 6685, 6695, 6696, 6700, 6702, 6705, 6708, 6710, 6714, 6715, 6725, 6726, 6730, 6732, 6735, 6738, 6740, 6744, 6745, 6755, 6756, 6760, 6762, 6765, 6768, 6770, 6774, 6775, 6785, 6786, 6790, 6792, 6795, 6798, 6800, 6804, 6805, 6815, 6816, 6820, 6822, 6825, 6828, 6830, 6834, 6835, 6845, 6846, 6850, 6852, 6855, 6858, 6860, 6864, 6865, 6875, 6876, 6880, 6882, 6885, 6888, 6890, 6894, 6895, 6905, 6906, 6910, 6912, 6915, 6918, 6920, 6924, 6925, 6935, 6936, 6940, 6942, 6945, 6948, 6950, 6954, 6955, 6965, 6966, 6970, 6972, 6975, 6978, 6980, 6984, 6985, 6995, 6996, 7000, 7002, 7005, 7008, 7010, 7014, 7015, 7025, 7026, 7030, 7032, 7035, 7038, 7040, 7044, 7045, 7055, 7056, 7060, 7062, 7065, 7068, 7070, 7074, 7075, 7085, 7086, 7090, 7092, 7095, 7098, 7100, 7104, 7105, 7115, 7116, 7120, 7122, 7125, 7128, 7130, 7134, 7135, 7145, 7146, 7150, 7152, 7155, 7158, 7160, 7164, 7165, 7175, 7176, 7180, 7182, 7185, 7188, 7190, 7194, 7195, 7205, 7206, 7210, 7212, 7215, 7218, 7220, 7224, 7225, 7235, 7236, 7240, 7242, 7245, 7248, 7250, 7254, 7255, 7265, 7266, 7270, 7272, 7275, 7278, 7280, 7284, 7285, 7295, 7296, 7300, 7302, 7305, 7308, 7310, 7314, 7315, 7325, 7326, 7330, 7332, 7335, 7338, 7340, 7344, 7345, 7355, 7356, 7360, 7362, 7365, 7368, 7370, 7374, 7375, 7385, 7386, 7390, 7392, 7395, 7398, 7400, 7404, 7405, 7415, 7416, 7420, 7422, 7425, 7428, 7430, 7434, 7435, 7445, 7446, 7450, 7452, 7455, 7458, 7460, 7464, 7465, 7475, 7476, 7480, 7482, 7485, 7488, 7490, 7494, 7495, 7505, 7506, 7510, 7512, 7515, 7518, 7520, 7524, 7525, 7535, 7536, 7540, 7542, 7545, 7548, 7550, 7554, 7555, 7565, 7566, 7570, 7572, 7575, 7578, 7580, 7584, 7585, 7595, 7596, 7600, 7602, 7605, 7608, 7610, 7614, 7615, 7625, 7626, 7630, 7632, 7635, 7638, 7640, 7644, 7645, 7655, 7656, 7660, 7662, 7665, 7668, 7670, 7674, 7675, 7685, 7686, 7690, 7692, 7695, 7698, 7700, 7704, 7705, 7715, 7716, 7720, 7722, 7725, 7728, 7730, 7734, 7735, 7745, 7746, 7750, 7752, 7755, 7758, 7760, 7764, 7765, 7775, 7776, 7780, 7782, 7785, 7788, 7790, 7794, 7795, 7805, 7806, 7810, 7812, 7815, 7818, 7820, 7824, 7825, 7835, 7836, 7840, 7842, 7845, 7848, 7850, 7854, 7855, 7865, 7866, 7870, 7872, 7875, 7878, 7880, 7884, 7885, 7895, 7896, 7900, 7902, 7905, 7908, 7910, 7914, 7915, 7925, 7926, 7930, 7932, 7935, 7938, 7940, 7944, 7945, 7955, 7956, 7960, 7962, 7965, 7968, 7970, 7974, 7975, 7985, 7986, 7990, 7992, 7995, 7998, 8000, 8004, 8005, 8015, 8016, 8020, 8022, 8025, 8028, 8030, 8034, 8035, 8045, 8046, 8050, 8052, 8055, 8058, 8060, 8064, 8065, 8075, 8076, 8080, 8082, 8085, 8088, 8090, 8094, 8095, 8105, 8106, 8110, 8112, 8115, 8118, 8120, 8124, 8125, 8135, 8136, 8140, 8142, 8145, 8148, 8150, 8154, 8155, 8165, 8166, 8170, 8172, 8175, 8178, 8180, 8184, 8185, 8195, 8196, 8200, 8202, 8205, 8208, 8210, 8214, 8215, 8225, 8226, 8230, 8232, 8235, 8238, 8240, 8244, 8245, 8255, 8256, 8260, 8262, 8265, 8268, 8270, 8274, 8275, 8285, 8286, 8290, 8292, 8295, 8298, 8300, 8304, 8305, 8315, 8316, 8320, 8322, 8325, 8328, 8330, 8334, 8335, 8345, 8346, 8350, 8352, 8355, 8358, 8360, 8364, 8365, 8375, 8376, 8380, 8382, 8385, 8388, 8390, 8394, 8395, 8405, 8406, 8410, 8412, 8415, 8418, 8420, 8424, 8425, 8435, 8436, 8440, 8442, 8445, 8448, 8450, 8454, 8455, 8465, 8466, 8470, 8472, 8475, 8478, 8480, 8484, 8485, 8495, 8496, 8500, 8502, 8505, 8508, 8510, 8514, 8515, 8525, 8526, 8530, 8532, 8535, 8538, 8540, 8544, 8545, 8555, 8556, 8560, 8562, 8565, 8568, 8570, 8574, 8575, 8585, 8586, 8590, 8592, 8595, 8598, 8600, 8604, 8605, 8615, 8616, 8620, 8622, 8625, 8628, 8630, 8634, 8635, 8645, 8646, 8650, 8652, 8655, 8658, 8660, 8664, 8665, 8675, 8676, 8680, 8682, 8685, 8688, 8690, 8694, 8695, 8705, 8706, 8710, 8712, 8715, 8718, 8720, 8724, 8725, 8735, 8736, 8740, 8742, 8745, 8748, 8750, 8754, 8755, 8765, 8766, 8770, 8772, 8775, 8778, 8780, 8784, 8785, 8795, 8796, 8800, 8802, 8805, 8808, 8810, 8814, 8815, 8825, 8826, 8830, 8832, 8835, 8838, 8840, 8844, 8845, 8855, 8856, 8860, 8862, 8865, 8868, 8870, 8874, 8875, 8885, 8886, 8890, 8892, 8895, 8898, 8900, 8904, 8905, 8915, 8916, 8920, 8922, 8925, 8928, 8930, 8934, 8935, 8945, 8946, 8950, 8952, 8955, 8958, 8960, 8964, 8965, 8975, 8976, 8980, 8982, 8985, 8988, 8990, 8994, 8995, 9005, 9006, 9010, 9012, 9015, 9018, 9020, 9024, 9025, 9035, 9036, 9040, 9042, 9045, 9048, 9050, 9054, 9055, 9065, 9066, 9070, 9072, 9075, 9078, 9080, 9084, 9085, 9095, 9096, 9100, 9102, 9105, 9108, 9110, 9114, 9115, 9125, 9126, 9130, 9132, 9135, 9138, 9140, 9144, 9145, 9155, 9156, 9160, 9162, 9165, 9168, 9170, 9174, 9175, 9185, 9186, 9190, 9192, 9195, 9198, 9200, 9204, 9205, 9215, 9216, 9220, 9222, 9225, 9228, 9230, 9234, 9235, 9245, 9246, 9250, 9252, 9255, 9258, 9260, 9264, 9265, 9275, 9276, 9280, 9282, 9285, 9288, 9290, 9294, 9295, 9305, 9306, 9310, 9312, 9315, 9318, 9320, 9324, 9325, 9335, 9336, 9340, 9342, 9345, 9348, 9350, 9354, 9355, 9365, 9366, 9370, 9372, 9375, 9378, 9380, 9384, 9385, 9395, 9396, 9400, 9402, 9405, 9408, 9410, 9414, 9415, 9425, 9426, 9430, 9432, 9435, 9438, 9440, 9444, 9445, 9455, 9456, 9460, 9462, 9465, 9468, 9470, 9474, 9475, 9485, 9486, 9490, 9492, 9495, 9498, 9500, 9504, 9505, 9515, 9516, 9520, 9522, 9525, 9528, 9530, 9534, 9535, 9545, 9546, 9550, 9552, 9555, 9558, 9560, 9564, 9565, 9575, 9576, 9580, 9582, 9585, 9588, 9590, 9594, 9595, 9605, 9606, 9610, 9612, 9615, 9618, 9620, 9624, 9625, 9635, 9636, 9640, 9642, 9645, 9648, 9650, 9654, 9655, 9665, 9666, 9670, 9672, 9675, 9678, 9680, 9684, 9685, 9695, 9696, 9700, 9702, 9705, 9708, 9710, 9714, 9715, 9725, 9726, 9730, 9732, 9735, 9738, 9740, 9744, 9745, 9755, 9756, 9760, 9762, 9765, 9768, 9770, 9774, 9775, 9785, 9786, 9790, 9792, 9795, 9798, 9800, 9804, 9805, 9815, 9816, 9820, 9822, 9825, 9828, 9830, 9834, 9835, 9845, 9846, 9850, 9852, 9855, 9858, 9860, 9864, 9865, 9875, 9876, 9880, 9882, 9885, 9888, 9890, 9894, 9895, 9905, 9906, 9910, 9912, 9915, 9918, 9920, 9924, 9925, 9935, 9936, 9940, 9942, 9945, 9948, 9950, 9954, 9955, 9965, 9966, 9970, 9972, 9975, 9978, 9980, 9984, 9985, 9995, 9996, 10000]

2. 写一个方法,计算列表所有偶数下标元素的和(注意返回值)

def sum_even_indexed_elements(lst):  
    total = 0  
    for i in range(0, len(lst), 2):  # 步长为2,只遍历偶数下标的元素  
        total += lst[i]  
    return total  
  
# 示例列表  
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
  
# 计算偶数下标元素的和  
result = sum_even_indexed_elements(my_list)  
print(result)

测试结果:

25

3. 根据完整的路径从路径中分离文件路径、文件名及扩展名 

import os  
  
def split_filepath(filepath):  
    # 使用os.path.dirname获取文件路径  
    file_path = os.path.dirname(filepath)  
    # 使用os.path.basename获取文件名+扩展名  
    filename_with_extension = os.path.basename(filepath)  
    # 使用os.path.splitext分离文件名和扩展名  
    filename, extension = os.path.splitext(filename_with_extension)  
    return file_path, filename, extension  
  
# 示例使用  
full_path = "/home/user/documents/example.txt"  
path, filename, extension = split_filepath(full_path)  
print("文件路径:", path)  
print("文件名:", filename)  
print("扩展名:", extension)

测试结果:

文件路径: /home/user/documents  
文件名: example  
扩展名: .txt

4. 根据标点符号对字符串进行分行

def split_string_by_punctuation(text, punctuation):  
    # 使用生成器表达式分割字符串,在标点符号处插入换行符  
    segments = (segment + '\n' if segment in punctuation else segment for segment in text)  
    # 移除字符串末尾可能多余的换行符,并连接所有片段  
    return ''.join(segments).rstrip('\n')  
  
# 示例使用  
text = "Hello, how are you? I'm fine. Thank you!"  
punctuation = [',', '?', '.']  
  
# 在标点符号处分割字符串并插入换行符  
result = split_string_by_punctuation(text, punctuation)  
print(result)

测试结果:

 

5. 去掉字符串数组中每个字符串的空格

def remove_all_spaces_from_strings(string_list):  
    # 使用列表推导式去除每个字符串中的所有空格  
    return [s.replace(" ", "") for s in string_list]  
  
# 示例使用  
string_array = ["  Hello World  ", "   Python  Programming  ", "   Learn   "]  
result = remove_all_spaces_from_strings(string_array)  
print(result)

测试结果:

['HelloWorld', 'PythonProgramming', 'Learn']

6. 两个学员输入各自最喜欢的游戏名称,判断是否一致,如
      果相等,则输出你们俩喜欢相同的游戏;如果不相同,则输
      出你们俩喜欢不相同的游戏。

def compare_favorite_games(game1, game2):  
    if game1 == game2:  
        print("你们俩喜欢相同的游戏")  
    else:  
        print("你们俩喜欢不相同的游戏")  
  
# 示例使用  
favorite_game_student1 = input("请输入你最喜欢的游戏名称: ")  
favorite_game_student2 = input("请输入你最喜欢的游戏名称: ")  
  
compare_favorite_games(favorite_game_student1, favorite_game_student2)

测试结果:

 

7. 上题中两位同学输入 lol和 LOL代表同一游戏,怎么办?

def compare_favorite_games(game1, game2):  
    # 将两个游戏名称转换为小写  
    game1_lower = game1.lower()  
    game2_lower = game2.lower()  
      
    # 比较转换后的小写游戏名称  
    if game1_lower == game2_lower:  
        print("你们俩喜欢相同的游戏")  
    else:  
        print("你们俩喜欢不相同的游戏")  
  
# 示例使用  
favorite_game_student1 = input("请输入你最喜欢的游戏名称: ")  
favorite_game_student2 = input("请输入你最喜欢的游戏名称: ")  
  
compare_favorite_games(favorite_game_student1, favorite_game_student2)

测试结果;

8. 让用户输入一个日期格式如“2008/08/08”,将 输入的日
          期格式转换为“2008年-8月-8日”。

def format_date(input_date):  
    # 使用正斜杠分割输入的日期字符串  
    year, month, day = input_date.split('/')  
      
    # 格式化输出字符串,将各个部分连接起来  
    formatted_date = f"{year}年-{month}月-{day}日"  
      
    return formatted_date  
  
# 示例使用  
input_date_str = input("请输入一个日期格式如“2008/08/08”: ")  
formatted_date_str = format_date(input_date_str)  
print("转换后的日期格式为:", formatted_date_str)

测试结果:

9. 接收用户输入的字符串,将其中的字符进行排序(升
      序),并以逆序的顺序输出,“cabed”→"abcde"→“edcba”

def sort_and_reverse_string(input_string):  
    # 对字符串中的字符进行排序,然后逆序排序结果  
    reversed_sorted_string = ''.join(sorted(input_string)[::-1])  
    # 返回逆序排序后的字符串  
    return reversed_sorted_string  
  
# 示例使用  
user_input = input("请输入一个字符串: ")  
sorted_and_reversed = sort_and_reverse_string(user_input)  
print(f"排序并逆序后的字符串是: {sorted_and_reversed}")

10. 接收用户输入的一句英文,将其中的单词以反序输
      出,“hello c java python”→“python java c hello”。

def reverse_words_in_sentence(sentence):  
    # 将句子按空格分割成单词列表  
    words = sentence.split()  
    # 反转单词列表  
    reversed_words = words[::-1]  
    # 将反转后的单词列表用空格连接起来形成新的句子  
    reversed_sentence = ' '.join(reversed_words)  
    # 返回反转后的句子  
    return reversed_sentence  
  
# 示例使用  
user_input = input("请输入一句英文: ")  
reversed_sentence = reverse_words_in_sentence(user_input)  
print(f"单词反序后的句子是: {reversed_sentence}")

测试结果: 

请输入一句英文: Year after year, people look the same.
单词反序后的句子是: same. the look people year, after Year

11. 从请求地址中提取出用户名和域名
       http://www.163.com?userName=admin&pwd=123456

from urllib.parse import urlparse, parse_qs  
  
def extract_username_and_domain(url):  
    # 解析URL  
    parsed_url = urlparse(url)  
      
    # 提取域名(netloc)  
    domain = parsed_url.netloc  
      
    # 解析查询参数  
    query_params = parse_qs(parsed_url.query)  
      
    # 提取用户名(从查询参数中)  
    username = query_params.get('userName', [None])[0]  # 获取userName的值,如果不存在则返回None  
      
    # 返回用户名和域名  
    return username, domain  
  
# 示例使用  
url = "http://www.163.com?userName=admin&pwd=123456"  
username, domain = extract_username_and_domain(url)  
print(f"用户名: {username}")  
print(f"域名: {domain}")

 测试结果:

用户名: admin
域名: www.163.com

12. 有个字符串数组,存储了10个书名,书名有长有短,现
       在将他们统一处理,若书名长度大于10,则截取长度8的
       子串并且最后添加“...”,加一个竖线后输出作者的名字。

def format_book_titles(books_with_authors):  
    for book, author in books_with_authors:  
        if len(book) > 10:  
            formatted_title = book[:8] + '...'  
        else:  
            formatted_title = book  
        print(f"{formatted_title}|{author}")  

 测试结果:

红星照耀中国|Edgar Snow
乡土中国|费孝通
红岩|罗广斌、杨益言
可爱的中国|方志敏
青春之歌|杨沫
太阳照在桑干河上|丁玲
中国共产党百年历...|曲青山
剑来|烽火戏诸侯
Clean Code|Isabella
Database...|Jack

13. 让用户输入一句话,找出所有"呵"的位置。

def find_all_occurrences(sentence, target):  
    positions = []  
    start_pos = 0  
    while True:  
        pos = sentence.find(target, start_pos)  
        if pos == -1:  
            break  
        positions.append(pos)  
        start_pos = pos + len(target)  # 更新起始位置为当前位置之后  
    return positions  
  
# 示例使用  
sentence = input("请输入一句话: ")  
target = "呵"  
occurrences = find_all_occurrences(sentence, target)  
  
if occurrences:  
    print(f"找到 '{target}' 的位置: {occurrences}")  
else:  
    print(f"'{target}' 没有在句子中出现。")

测试结果:

14. 让用户输入一句话,判断这句话中有没有邪恶,如果有邪
       恶就替换成这种形式然后输出,如:“老牛很邪恶”,输出后变
       成”老牛很**”;

def censor_evil(sentence):  
    # 检查句子中是否包含"邪恶"  
    if "邪恶" in sentence:  
        # 如果包含,则将"邪恶"替换为"**"  
        censored_sentence = sentence.replace("邪恶", "**")  
        return censored_sentence  
    else:  
        # 如果不包含,则直接返回原句  
        return sentence  
  
# 示例使用  
user_input = input("请输入一句话: ")  
censored_text = censor_evil(user_input)  
print(f"处理后的句子: {censored_text}")

测试结果;

15. 判断一个字符是否是回文字符串
           "1234567654321"
           "上海自来水来自海上"

def is_palindrome(s):  
    # 将字符串转换为小写(如果需要忽略大小写)  
    s = s.lower()  
    # 去除字符串中的非字母数字字符(如果需要)  
    s = ''.join(c for c in s if c.isalnum())  
    # 判断字符串是否等于其反转  
    return s == s[::-1]  
  
# 测试字符串  
string1 = "1234567654321"  
string2 = "上海自来水来自海上"  
  
# 调用函数并打印结果  
print(is_palindrome(string1))  # 应该输出 True  
print(is_palindrome(string2))  # 应该输出 True

 测试结果:

True
True

16. 过滤某个文件夹下的所有"xx.py"python文件

import os  
import glob  
  
def find_python_files(directory_path):
  
    # 确保路径是字符串类型  
    if not isinstance(directory_path, str):  
        raise ValueError("The directory_path must be a string.")  
      
    # 确保路径存在  
    if not os.path.exists(directory_path):  
        raise FileNotFoundError(f"The directory {directory_path} does not exist.")  
    if not os.path.isdir(directory_path):  
        raise NotADirectoryError(f"{directory_path} is not a directory.")  
      
    # 使用glob模块搜索所有.py文件  
    python_files = glob.glob(os.path.join(directory_path, '**', '*.py'), recursive=True)  
    return python_files  
  
# 示例使用  
directory_to_search = '/path/to/your/folder'  # 替换为你要搜索的文件夹路径  
try:  
    python_files = find_python_files(directory_to_search)  
    for file_path in python_files:  
        print(file_path)  
except (ValueError, FileNotFoundError, NotADirectoryError) as e:  
    print(f"An error occurred: {e}")

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值