Python
Lambda and Arrays
Hello guys, we will here discuss about, Lambda and
arrays.
Python Lambda
Actually lambda is a small anonymous function which
can take any number of arguments, but can only have one expression.
Syntax is,
lambda arguments: expression
Output
14
Let’s look into multiple arguments thing as mentioned
above,
You must be thinking, “that that’s it?”. Well actual
power of lambda is when this anonymous function is used under a function.
Say you have a function definition that takes one
argument, and that argument will be multiplied with an unknown number:
Note: Use lambda function when an
anonymous function is required for a short period of time.
Arrays
Arrays are used to store
variables of the same type under one single variable.
An array of numeric values is supported in Python by
the array module.
Output
Arrays can hold any number of values under a single
name.
Accessing elements of an array
Accessing elements of an array is quite easy as you
only have to refer to its index number.
Check out the output and practice more.
Slicing an array: It is
simple as we have to be in the index range of the array, example
print(array_1[1:2])
Length of an array
Well we
can easily tell the length just by counting the number of items 😉
But for
larger numbers, we do have to check the out.
Arrays
Methods
Python
doesn’t have a buit-in support for arrays, though few of the list’s methods can
be used same as we used it in lists.
If you
haven’t yet, been through Python list, click here
Built-in
functions supporting arrays are:
a)
append()
b)
clear()
c)
copy()
d)
count()
e)
extend()
f)
insert()
g)
index()
h)
pop()
i)
remove()
j)
reverse()
k)
sort()
The
important fact is that lists are much more flexible and are in high demand in
python as it can store variables of different types. Arrays are much more
called in C/C++ or Java or JavaScript, etc.
Unless
you don’t need any array module, don’t use array.
No comments:
Post a Comment