Python Session3 Strings Lists
Python Session3 Strings Lists
The population
of the town is increasing at the rate of 10% per year. You have to write
a program to find out the population at the end of each of the last 10
years.
# Code here
curr_pop = 10000
for i in range(10,0,-1):
print(i,curr_pop)
curr_pop = curr_pop/1.1
10 10000
9 9090.90909090909
8 8264.462809917353
7 7513.148009015775
6 6830.134553650703
5 6209.213230591548
4 5644.739300537771
3 5131.5811823070635
2 4665.07380209733
1 4240.976183724845
Sequence sum
1/1! + 2/2! + 3/3! + ...
# Code here
n = int(input('enter n'))
result = 0
fact = 1
for i in range(1,n+1):
fact = fact * i
result = result + i/fact
print(result)
enter n2
2.0
Nested Loops
# Examples -> unique pairs
for i in range(1,5):
for j in range(1,5):
print(i,j)
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
3 4
4 1
4 2
4 3
4 4
Pattern 1
*** **** ***
# code here
for i in range(1,rows+1):
for j in range(1,i+1):
print('*',end='')
print()
# Code here
rows = int(input('enter number of rows'))
for i in range(1,rows+1):
for j in range(1,i+1):
print(j,end='')
for k in range(i-1,0,-1):
print(k,end='')
print()
1
2
3
4
for i in range(lower,upper+1):
for j in range(2,i):
if i%j == 0:
break
else:
print(i)
# Continue
for i in range(1,10):
if i == 5:
continue
print(i)
1
2
3
4
6
7
8
9
for i in range(1,10):
pass
• Creating Strings
• Accessing Strings
• Adding Chars to Strings
• Editing Strings
• Deleting Strings
• Operations on Strings
• String Functions
Creating Stings
s = 'hello'
s = "hello"
# multiline strings
s = '''hello'''
s = """hello"""
s = str('hello')
print(s)
hello
{"type":"string"}
----------------------------------------------------------------------
-----
IndexError Traceback (most recent call
last)
<ipython-input-61-633ba99ed6e5> in <module>
1 # Positive Indexing
2 s = 'hello world'
----> 3 print(s[41])
# Negative Indexing
s = 'hello world'
print(s[-3])
# Slicing
s = 'hello world'
print(s[6:0:-2])
wol
print(s[::-1])
dlrow olleh
s = 'hello world'
print(s[-1:-6:-1])
dlrow
----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
<ipython-input-80-0c8a824e3b73> in <module>
1 s = 'hello world'
----> 2 s[0] = 'H'
s = 'hello world'
del s
print(s)
----------------------------------------------------------------------
-----
NameError Traceback (most recent call
last)
<ipython-input-81-9ae37fbf1c6c> in <module>
1 s = 'hello world'
2 del s
----> 3 print(s)
s = 'hello world'
del s[-1:-5:2]
print(s)
----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
<ipython-input-82-d0d823eafb6b> in <module>
1 s = 'hello world'
----> 2 del s[-1:-5:2]
3 print(s)
delhi mumbai
print('delhi'*5)
delhidelhidelhidelhidelhi
print("*"*50)
**************************************************
'delhi' != 'delhi'
False
False
False
{"type":"string"}
'hello' or 'world'
{"type":"string"}
{"type":"string"}
'' or 'world'
{"type":"string"}
'hello' or 'world'
{"type":"string"}
not 'hello'
False
for i in 'hello':
print(i)
h
e
l
l
o
for i in 'delhi':
print('pune')
pune
pune
pune
pune
pune
'D' in 'delhi'
False
Common Functions
• len
• max
• min
• sorted
len('hello world')
11
max('hello world')
{"type":"string"}
min('hello world')
{"type":"string"}
sorted('hello world',reverse=True)
['w', 'r', 'o', 'o', 'l', 'l', 'l', 'h', 'e', 'd', ' ']
Capitalize/Title/Upper/Lower/Swapcase
s = 'hello world'
print(s.capitalize())
print(s)
Hello world
hello world
s.title()
{"type":"string"}
s.upper()
{"type":"string"}
'Hello Wolrd'.lower()
{"type":"string"}
'HeLlO WorLD'.swapcase()
{"type":"string"}
Count/Find/Index
'my name is nitish'.count('i')
-1
----------------------------------------------------------------------
-----
ValueError Traceback (most recent call
last)
<ipython-input-121-12e2ad5b75e9> in <module>
----> 1 'my name is nitish'.index('x')
False
False
format
name = 'nitish'
gender = 'male'
{"type":"string"}
False
'nitish'.isalpha()
True
'123abc'.isdigit()
False
'first-name'.isidentifier()
False
Split/Join
'hi my name is nitish'.split()
{"type":"string"}
Replace
'hi my name is nitish'.replace('nitisrgewrhgh','campusx')
{"type":"string"}
Strip
'nitish '.strip()
{"type":"string"}
Example Programs
# Find the length of a given string without using the len() function
counter = 0
for i in s:
counter += 1
pos = s.index('@')
print(s[0:pos])
counter = 0
for i in s:
if i == term:
counter += 1
print('frequency',counter)
enter the emailhi how are you
what would like to search foro
frequency 2
result = ''
for i in s:
if i != term:
result = result + i
print(result)
if flag:
print('Palindrome')
if i != ' ':
temp = temp + i
else:
L.append(temp)
temp = ''
L.append(temp)
print(L)
L = []
for i in s.split():
L.append(i[0].upper() + i[1:].lower())
print(" ".join(L))
digits = '0123456789'
result = ''
while number != 0:
result = digits[number % 10] + result
number = number//10
print(result)
print(type(result))