Showing posts with label python len(). Show all posts
Showing posts with label python len(). Show all posts

python list heterogeneous items and python len() function

 The list data structure is very flexible It has many unique inbuilt functionalities

Different methods of creating a list, adding, modifying, and deleting elements in the list. Also, learn how to iterate the list and access the elements in the list in detail.

The following are the properties of a list.

Mutable: The elements of the list can be modified. We can add or remove items to the list after it has been created.

Ordered: The items in the lists are ordered.

Heterogeneous: The list can contain different kinds of elements.i.e; they can contain elements of string, integer, boolean, or any type.

Duplicates: The list can contain duplicates i.e., lists can have two items with the same values.

Ex:

mylist=[89,44,55,22,90]

print(mylist)

print(type(mylist))

output:89,44,55,22,90

#Using list() constructor: In general, the constructor of a class has its class name.

mylist=list([34,55,66,77,23])

print(mylist)

ouput:34,55,66,77,23

# list heterogeneous items

mylist = [1.0, 'ram', 30,50,77,'krishna']

print(mylist)

output:1.0, 'ram', 30,50,77,'krishna'

#list functions

#list length function-In order to find the number of items present in a list, we can use the len() function.

mylist = [31, 42, 23,98]

print(len(mylist))

output:31,42,23,98

Iterating a List Object Values in Python

 # Iterating a List Object Values in Python :

# The objects in the list can be iterated over one by one, by using a for loop

Ex:

courses=["python","Django","Flask","Pyspark"]

for x in courses:

    print(x)


# Iterate along with an index number:

Ex:

mylist = [5, 8, 'ram', 7.50, 'ravi']

for i in range(2, len(mylist)):

     print(mylist[i])


Latest Post

Don’t Sweat It – Chill with Crompton Air Cooler Deals

 ðŸŒž Don’t Sweat It – Chill with Crompton Air Cooler Deals  More Details : https://www.amazon.in/s?k=laptops+under+70000&crid=29QIFPXQYB3...