xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Python Coding Interview All In One

Python Coding Interview All In One

Python Advanced

Question

Use enumerate() to iterate over both indices and values
Debug problematic code with breakpoint()
Format strings effectively with f-strings
Sort lists with custom arguments
Use generators instead of list comprehensions to conserve memory
Define default values when looking up dictionary keys
Count hashable objects with the collections.Counter class
Use the standard library to get lists of permutations and combinations

enumerate()

enumerate() is a built-in function to iterate through a sequence and keep track of both the index and the number.

>>> list(range(11))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


>>> list(enumerate([1, 2, 3]))
[(0, 1), (1, 2), (2, 3)]

>>> list(enumerate([1, 2, 3], start=10))
[(10, 1), (11, 2), (12, 3)]

 list = [45, 22, 14, 65, 97, 72].

for i, num in enumerate(numbers):
    if num % 3 == 0:
        numbers[i] = "fizz"
    if num % 5 == 0:
        numbers[i] = "buzz"
    if num % 5 == 0 and num % 3 == 0:
        numbers[i] = "fizzbuzz"


for i, num in enumerate(numbers):
    if num % 5 == 0 and num % 3 == 0:
        numbers[i] = "fizzbuzz"
    elif num % 3 == 0:
        numbers[i] = "fizz"
    elif num % 5 == 0:
        numbers[i] = "buzz"

demos

python for loop with index

#!/usr/bin/python3

Blue = 17
GREEN = 27
RED = 22

LEDs = list([RED, GREEN, Blue])

for index, led in enumerate(LEDs):
  print('led = ', LEDs[index])
  # 22, 27, 17

# 等价于,start default 0
for index, led in enumerate(LEDs, start=0):
  print('led = ', LEDs[index])
  # 22, 27, 17


image

https://www.runoob.com/try/runcode.php?filename=HelloWorld&type=python3

Jupyter

Jupyter Notebook Interface

https://ipython.org/index.html

Jupyter Notebook

https://jupyter.readthedocs.io/en/latest/install.html

refs

https://realpython.com/lessons/python-coding-interview-tips-overview/

https://realpython.com/courses/python-range-function/

https://realpython.com/lessons/use-enumerate-keep-running-index/



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(254)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2019-09-02 PWA & Service Worker
2016-09-02 web 存储方式汇总:Cookies,Session, Web SQL; Web Storage(LocalStorage ,SessionStorage),IndexedDB,Application Cache,Cache Storage
2016-09-02 jQuery 使用注意事项 与 小技巧(tips)
2016-09-02 CSS 定位 relative && absolute 问题?
2016-09-02 FireFox native cookie operation
点击右上角即可分享
微信分享提示