-->

Wednesday, July 10, 2019

Iterating in Python Sampler


for i in [1, 2, 3]:
print (i)

for ch in "Yay!":
print(ch)

for num in range(5):
print('David is good at this!')

help(range)


phrase="Don't Panic!"

All the Letters

phrase

Every Third Letter up to index location 10

phrase[0:10:3]

All letters up to but not including the 10th
phrase[:10]

Only the first three letters
phrase[3:]

Every Second Letter
phrase[::2]

How to count backwards 1
backwords = phrase[::-1}
''.join(backwords)

>>> phrase[::-1]
"!cinap t'noD"

How to count backwards 2
>>> phrase = "The Sky is Blue"
>>> words = phrase.split(' ')
>>> words
['The', 'Sky', 'is', 'Blue']
>>> words[::-1]
['Blue', 'is', 'Sky', 'The']

No comments:

Post a Comment