For Loop¶

In [2]:
s = "Hello, Raj"
In [3]:
for c in s:
    print(c)
H
e
l
l
o
,
 
R
a
j
In [5]:
nvowels = 0
for c in s:
    if c == 'a' or c == 'e' or c == 'i' or \
       c == 'o' or c == 'u':
        nvowels = nvowels + 1
print(nvowels)
3
In [7]:
nvowels = 0
for c in s:
    if c in 'aeiouAEIOU':
        nvowels = nvowels + 1
print(nvowels)
3
In [8]:
sum([1,2,3,4,5])
Out[8]:
15
In [9]:
def mysum(xs):
    sum = 0
    for x in xs:
        sum = sum + x
    return sum
In [10]:
mysum([10,40,90])
Out[10]:
140
In [11]:
mysum([-1,2,3,5])
Out[11]:
9
In [12]:
mysum(["a","b"])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [12], in <cell line: 1>()
----> 1 mysum(["a","b"])

Input In [9], in mysum(xs)
      2 sum = 0
      3 for x in xs:
----> 4     sum = sum + x
      5 return sum

TypeError: unsupported operand type(s) for +: 'int' and 'str'
In [13]:
s = "hello"

answer = ""
for c in s:
    d = c.upper()
    answer = answer + d
print(answer)
HELLO
In [ ]:
def encrypt(s):
    shift = int(s[0:2])
    message = s[6:]