Learning Day-05

** Next Hands-on Session will be on 27-June-2020 Stay Tuned....

Let's Learn Python

# Day-04

python collection examples

 list/tuple/dictionary.

# list
a = [] 
type(a)

# here a is empty list object

# creating list with values

x = [10,20,30,40,50,60]
print(x)  

#how to access list elements. 
# using index numbers and slicing. 

x[0] # first element

# check below output
x[2]

x[-1]

x[-2]

x[1:4]

x[3:]

x[-3:]

x[:3]

# modifying list values.

x[2] = 300
x[-2] =  5
x

# appending values to list

x.append(500)
print(x)

x.append(450)


# merging list objects
a = [1,2,3]
b =  [9,10,11,5]
a+b

# appending multiple values to a list

x = x + [100,200,700]
or
x += [100,200,700]

a = [10,20,30,40]
a += [9,7,0]
print(a)

# aggregate functions on list object

len(x)
sum(x)
max(x)
min(x)
avg = sum(x)/len(x)

# transformation : perform some action on each element of a list.

a = [10,20,30,40,50]
# add 100 to each element.(transformation. )
#way1
b = []
for  i in a:
   v = i + 100
   b.append(v)
print(a)
print(b)

# way2. 

c = [i+100 for i in a ]
print(c)


# inserting a value in middle of list.
 
a = a[:2]+[90]+a[2:]

name = ["amar",'amala','ankit','ankita']
s = 'appple'
s.upper()

# convert each name into uppercase

newname = [s.upper()  for s in name]
newname

# convert first character into uppercase and remaining into lowercase

name = [' aMar ','kiraN','manOJ','venKat ']
# way1.

nn = []
for n in name:
    n=n.strip()
    fc = n[0].upper()
    rc = n[1:].lower()
    nn.append(fc+rc)
print(name)
print(nn)
   

# create a function , 
# to convert first letter into uppercase 
# and remaining into lower case  after removing whitespaces(strip)

def  fupper(s):
   s=s.strip().lower()
   fc = s[0].upper()
   rc = s[1:]
   return fc + rc
   
# way2(use above function for transformation)

un = [ fupper(n) for  n in name]
print(un)


Task:
add 1 to 10, 2 to 20, 3 to 30, 4 to 40, 5 to 50 of below list
a = [ 10,20,30,40,50]
b = [ i+1 for i in range(5)] 
ix = 0
res = []
for  x in a:
     res.append(x+b[ix])
     ix +=1 
print(a)
print(b)
print(res)

To Download This Material
please click on the below link:



Thank you so much for your Valuable time

Comments