how to use list in Python?Python has a great built-in list type named "list". List literals are written within square brackets [ ]. Lists work similarly to strings -- use the len() function and square brackets [ ] to access data, with the first element at index 0.example:
companies = ['apple', 'google', 'ford']
Print companies[0]
result: apple
Print companies[2]
result: ford
Print len(compnaies)
result: 3
Assign multiple values to the variable:
if you can see one array which numbers. we have the second derivative names. which are of string we can get a different list of different types. now can I get a list of different types? I will simply call it values equal to and I want to have a double value. here example the first one I would say is 9.5. which is a float value then I want to go forward a string go with my name and then let's say another one. which is 25 which is number right. so we have a float number we have a string and integer number.
example:
values =[9.5, 'Navin', 25]
Print(values)
result: 9.5, Navin, 25
Python list functions:
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the
current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
use key "ctrl+space" to get list function(by pop-up) suggestions.
append() function:
in append function, you can add elements at the end of the list.
example:
marks = [88, 76, 45, 34]
marks.append(69)
result: [88, 76, 45, 34, 69]
2nd
number = [67, 56, 42, 19]
number.append(96)
result: [67, 56, 42, 19, 96]
extend() function:
in this function, you can Add the elements of a list (or any iterable), to the end of the current list.
example:
marks = [88, 76, 45, 34]
marks.extend([59, 62, 69, 90])
result: [88, 76, 45, 34, 59, 62, 69, 90]
more examples for Python list function:
number = [99, 67, 52, 19]
min(number)
result: 19
max(number)
result: 99
sum(number)
result: 237
<<-What is variables in Python?
Conclusion:
faefa
ReplyDelete