Header Ads

Responsive Ads Here

Advanced Functions

 Advanced Functions



# enumerate()
# l =["hi","hello","world"]

# x = 0
# for i in l:
#     print(f"{x} = {i}")
#     x += 1

# for pos,value in enumerate(l):
#     print(f"{pos} = {value}")



# map()
num = [1,2,3,4,5]


print(list(map(lambda a: a ** 2, num))) # ---> map()

print([i ** 2 for i in num])  # ---> list comprehension

l = []    # ---> normal method (for loop)
for i in num:
    l.append(i ** 2)
print(l)


# filter()

my_var = list(filter(lambda a: a % 2==0, num))

# for i in my_var:
#     print(i)

# for i in my_var:
#     print(i)


# zip()
user = ["user1", "user2", "user3"]
names = ["Adrika", "Anika", "Sia"]
age = [14,3,3]

print(list(zip(user,names,age)))

my_list = [(1,2),(3,4),(5,6),(7,8),(9,10)]
print(list(zip(*my_list)))


# all() & any()

# all() ---> Returns True if all values are True
print(all([True,True,False,True,True]))

even1 = [2,4,6,8,10]

# e = []
# for i in even1:
#     e.append(i % 2 == 0)
# print(all(e))

print(all([i % 2 == 0 for i in even1]))

# any() ----> Returns True if even if a single value is True

odd1 = [2,4,5,6,8,10]

# o = []
# for i in odd1:
#     o.append(i % 2 != 0)
# print(any(o))

print(any([i % 2 != 0 for i in odd1]))


# Docstrings & help()

def add(a,b):
    '''This function adds two numbers.'''
    return a + b

print(add.__doc__)

print(sum.__doc__)
print(help(sum))

Powered by Blogger.