#LIST --List one of the data type in python
#A List is a collection of items stored in a single variable.
#list can store multiple values with comma speciation
#list we can identify begins with an opening square bracket and
#ends with a closing square bracket, []
#A list value looks like this: ['cat', 'bat', 'rat', 'elephant'].
# list is Ordered,Mutable (changeable),Allow duplicates ,Can store different data types
#
animals=["cat","rat","Cow","Hen","horse","dog"]
print("Name of the animals Is",animals)
#in List data type we can store String values ,Int values ,flote values anything
animals_digits=["cat","0","rat","1","Cow","2","Hen","3","horse","4","dog","5"]
print("Name of the animals Is",animals_digits)
#Just as an index can get a single value from a list,
#List value Index --> it will Index first value from list
print(animals_digits[0])
print(animals_digits[1]) #it will Index 2 value from list
print(animals_digits[2]) #it will Index 3 value from list
print(animals_digits[-1]) #it will Negative Index Last value from list
print(animals_digits[-2])
#a slice can get several values from a list, in the form of a new list
print(animals_digits[0:4]) # Get 0,1,2,3 values from List # means 0-cat,1-0,2-rat,3-1
text = "HELLO"
print(text[1]) # E
print(text[1:2]) # E (but type is different!)
text = "PYTHON"
print(text[::2]) # Result is PTO Means Step = 2 (skip one)
Blak = "Mahen"
print(Blak[1:10])
#-------------------------------------------------
text = "HELLO"
print(text[1:2] == text[1]) # Result should be True
# Both are 'E', but types conceptually differ (still equal)
#---------------------------------------------------
nums = [10, 20, 30, 40]
print(nums[-3:-1])
#While Slicing start included, stop excluded
#Changing the Values in List
nums[0]=200 # Old Nums [10, 20, 30, 40]
print(nums) #Result should be [200, 20, 30, 40]
#List Concatenation(+) and List Replication(*)
value=[1, 2, 3] + ['A', 'B', 'C']
print(value) # Result is:-[1, 2, 3, 'A', 'B', 'C']
value=[1,2,3]*2
print(value) # Result Is:[1, 2, 3, 1, 2, 3]
#Removing Values from Lists with del Statements
del value[1] # Result Is:[1, 2, 3, 1, 2, 3]
print(value) #Result [1, 3, 1, 2, 3]
#Adding Values to Lists with the append() and insert() Methods
# sy : string.append(value)
s1=[1,2,3,'python','java']
s1.append('C++')
print(s1) #[1, 2, 3, 'python', 'java', 'C++']
#Insert Method SY: Insert(index,object)
str=[1,2,3,'python','java']
str.insert(2,'C') #[1, 2, 'C', 3, 'python', 'java']
print(str)
#Removing Values from Lists with remove()
str1=[1,2,3,'python','java']
str1.remove(2)
print(str1) # result [1, 3, 'python', 'java']
#Sorting the Values in a List with the sort() Method
str2=[0,5,13,9,6,20,15]
str2.sort() # result [0, 5, 6, 9, 13, 15, 20]
print(str2)
str2.sort(reverse=True) # result [20, 15, 13, 9, 6, 5, 0]
print(str2)
str3=[1, 3, 2, 4, 'Alice', 'Bob']
#str3.sort()
print(str3) #TypeError: '<' not supported between instances of 'str' and 'int'
#A list value is a mutable data type: It can have values added, removed, or changed.
#List Copy
l1=str1.copy() #str1 copy into l1
print(l1)
#========== real Time Interview question ===============
#Find the largest Number in given LIST
Numbers=[10,20,30,50,60] # Largest Number is 60
print(max(Numbers)) #->60
#Find the second-largest number in List
Numbers=[10,20,70,50,90,15,20,80,20,25,100]
Numbers.sort(reverse=False)
print(Numbers[-2]) # result 90
# Remove Duplicates in Given List
Number1=[10,20,70,50,90,15,20,80,20,25,100]
unique=list(set(Number1))
print(unique) #-->[100, 70, 10, 15, 80, 50, 20, 25, 90]
# List Unpacking
a, b, c = [10, 20, 30]
print(a, b, c) #
print(a+b)
print(a)
#Reveses list Using Reverse keyword without using reverse key word
nums = [1,2,3,4]
nums.sort(reverse=True)
print(nums)
print(nums[::1])#-Result [4, 3, 2, 1]
No comments:
Post a Comment