Python Tutorial 5---"Python Lists And List Functions"
Python Tutorial 5---
"Python Lists And List Functions"
This PROGRAM or TUTORIAL about Python's "Python Lists And List Functions"
DOC
Lists :
Python lists are containers used to store a list of values of any data type. In simple words, we can say that a list is a collection of elements from any data type E.g.
The above list contains strings, an integer, and even an element of type float. A list can contain any kind of data i.e. it’s not mandatory to form a list of only one data type. The list can contain any kind of data in it.
Remember we saw indexing in strings? List elements can also be accessed by using Indices i.e. first element of the list has 0 index and the second element has 1 as its index and so on.
Note: If you put an index which isn’t in the list i.e. that index is not there in list then you will get an error. i.e. if a list named list1 contains 4 elements, list1[4] will throw an error because the list index starts from 0.
Have a look at the examples below:
List Methods :
Here is the list of list methods in Python. These methods can be used in any python list to produce the desired output.
List Slicing :
List slices, like string slices, returns a part of a list extracted out of it. Let me explain, you can use indices to get elements and create list slices as per the following format :
seq = list1[start:stop]
Just like we saw in strings, slicing will go from a start index to stop_index-1. It means the seq list which is a slice of list1 contains elements from the specified start index to specified stop_index – 1.
List Methods:
There are a lot of list methods that make our life easy while using lists in python. Lets have a look at few of them below:
Tuples in Python:
A tuple is an immutable data type in Python. A tuple in python is a collection of elements enclosed in () (parentheses). Tuple once defined can’t be changed i.e. its elements or values can’t be altered or manipulated.
Note: To create a tuple of one element it is necessary to put a comma ‘,’ after that one element like this tup=(1,) because if we have only 1 element inside the parenthesis, the python interpreter will interpret it as a single entity which is why it’s important to use a ‘,’ after the element while creating tuples of a single element.
Swapping of two numbers
Python provides a very handy way of swapping two numbers like below:
CODE
#Tut 5---Python Lists And List Functions
vegies = ("Potato", "Onion", "Carrots", "Cucumbers", "Beans")
print(vegies[3]) #Output =Cucumbers
numbers = [3, 9, 7, 5, 13, 11]
numbers.sort() #Organize #Output =[3, 5, 7, 9, 11, 13]
numbers.reverse() #Gives output in reverse #Output =[13, 11, 9, 7, 5, 3]
print(numbers)
print(numbers[:2]) #Slicing in nums #Output =[13, 11]
print(numbers[::2]) #It simply skip 2 numbers as we did it in strings; Output=[13, 9, 5]
"""[_:_:_] here tha last place called extended index, make sure you dont put value <-1
if it in it's default value it'll work just fine, but if put other than default, it'll show an error;
<Example--->"""
print(numbers[::-1], numbers[::-3], numbers[1:5:-3])
#output = [3, 5, 7, 9, 11, 13] [3, 9], []
print(len(numbers), max(numbers), min(numbers)) #Output=6, 13, 3
#len for length, max for maximum value, min for minimum value
numbers.append(90) #append means it just simply add a number in the end of your output.
print(numbers) #[13, 11, 9, 7, 5, 3, 90]
#we can also create e blan list and put numbers there by 'append' function
num = []
num.append(80)
num.append(56)
num.append(18)
num.append(95)
print(num) #Output=[80, 56, 18, 95]
#We can out numbers not just at the end but anywhere in middle by 'insert' function
num1 = [2, 6, 10, 12, 8, 1]
num1.insert(3, 21) #3 is index num, 21 is object number
num1.remove(8) #'remove' function removes the selected index. #Output=[2, 6, 10, 21, 12, 1]
num1.pop() #'pop' fuunction removes the last index. #Output=[2, 6, 10, 21, 12]
num1[0] = 3 #Output=[3, 6, 10, 21, 12]
#this is happening because num1 is a mutable(CAN CHANGE) function
print(num1) #Output = [2, 6, 10, 21, 12, 8, 1]
#If we don't want to change values we use tupple function
#tupple(tp) function is an immutable(CANNOT CHANGE) function
tp = (1, 2, 3, 8)
#if we write; tp1 = 10 then it'll give us an error, because tp is immutable
"""we can verify which output is tp and which is list just by looking at the brackets
'tp' gives outputs in () PARANTHESIS OR FIRST BRACKETS;
'list' gives outputs in [] SQUARE BRACKETS"""
print(tp) #Output=(1, 2, 3, 8)
#for one element tupples we have to use comma; Example-----
tp1 = (1,)
print(tp1) #Output=(1,)
#If don't use a comma then it'll not act as a tupple; Example----
tp2= (10)
print(tp2) #Output = 10
#for swaping values use 'temp' function(TRADITIONAL METHOD); Example----
a = 7
b = 9
temp = a
a = b
b = temp
print(a, b) #Output = 9, 7
#PYTHON METHOD for swaping values
c = 10
d = 20
c, d = d, c
print(c, d) #Output = 20, 10
Comments
Post a Comment