num1 =1_000_000_000num2 =1_000_000total = num1 + num2print(f'{total:,}')# Output would be:1,001,000,000
Using context managers
# This will automatically open and close the filewithopen('file.txt, 'r') as f:for lines in f.readlines():print(lines)
Using enumerate
words = ['hello','world','hi','stuff']for num, word inenumerate(words, start=1):print(f'{num}{word}')
Using zip to loop over multiple lists
name = ['mike','steve','james']job = ['officer','teacher','hacker']for name, job inzip(name, job):print(f'{name}{job}')
Unpacking
a, b,*c = (1,2,3,4,5)print(a)1print(b)2print(c)[3,4,5]# If you want to ignore values 3, 4 and 5, you can use *_a, b,*_ = (1,2,3,4,5)print (a)1print (b)2# Another examplea, b,*c, d = (1,2,3,4,5)print (a)1print (b)2print (c)[3,4]print (d)5