__author__ = "Narendra Boyina"
# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Technologies PVT LTD
#-----------------------------------------------------------------------------"""===================================================================================
PART -1===================================================================================
Topics to be covered in today's class:
--> Introduction of list
--> Characteristics of List
--> Elements accessing
--> Single element accessing
--> range of elements accessing
--> step element accessing
--> Built_in_list_methods
--> Practice programs
"""
"""
################# Introduction of list #####################
--> Until now we have initialised the variable with single value
a = 10
--> Here we can't store more than one value in a variable like
a = 10
a = 20
--> when you have printed value of a, 20 will be printed,
i.e we can't store more than one value in a single variable in genarice way
--> But by using List, we can assign multiple values to a single variable inside []
Definiton: list is a heterogeneous & mutable sequence of elements
• Lists are super dynamic, and heterogeneous mutable sequence of elements delimited by square brackets [ ]
• We can access list elements by using Index, "Index always starts with 0 and ends with -1"
--> Syntax: Defined using square brackets [], elements are separated by comma operator.
Ex: x = [12, 14, 18, 19, 85, 2] ==> list index starts with "Zero" x[0] is 12
Characteristics of List:
--> Dynamic Size: Lists can dynamically resize. You can add or remove items, and the list will automatically adjust its size.
--> Heterogeneous(different types) Elements: Lists can store elements of different types (integers, float values, strings, objects, etc.).
Different ways to create a list
Creating homogeneous lists x = [1, 2, 3, 4, 5]
Creating heterogeneous lists y = ["hello", 472, 22.5, "good morning", 732, "hello", 74.5]
"""
varun = [2, 6.6, "abc"] # initialization + object
"""split function on string returns a list"""
# input_string = "Narendra Mahalakshmi Raj Surendra Aruna"
# print(input_string.split()) # split function on string returns a list
# output: ['Narendra', 'Mahalakshmi', 'Raj', 'Surendra', 'Aruna']
"""
############## Elements accessing ###################
Syntax: variable = instance[SA : EA : step]
index always starts with Zero ends with -1
We can access elements in 3 ways
1. Single element accessing
2. range of elements accessing
3. step element accessing
"""
""" @@@@@ Single element accessing @@@@@@
Syntax: variable = instance/object[index]
"""
data = [444, 1.5, 7.5, "Narendra", 472, 'Raj', 'Surendra', 'Aruna', 'Lakshyasree', "Satwika"]
# print(data[5]) # 'Raj'
# print(data[-5]) # "Raj" (Last element is at index -1))
# print(data[5], data[-5])
###################################################################################
"""
@@@@@ range of elements accessing @@@@@@
variable = instance[SI : EI]
"""
data = [444, 2.5, 7.5, "Narendra", 472, 'Raj', 'Surendra', 'Aruna','Lakshya sree', "Satwika"]
# print(data[1:5])
# print(data[2:5])
# print(data[1:-1])
"""accessing works with negative indexes
Required o/p is as a list, except 1st element and last element"""
data = ["Udaya bhaskarao","Samrajya lakshmi",'Mahalakshmi', 'Narendra', 'Raj']
# print(data)
# print(data[1:-1])
""""Omitting the stop index slices to the end"""
# print(data[2:])
"""Omitting the start index slices from the beginning."""
# print(data[:4])
"""Omitting the start & stop indexes slices from the beginning to the end ==>a full slice
deep copy creates separate memory"""
# print(data[:])
# print(data)
###################################################################################
"""@@@@@ step element accessing @@@@@"""
input_list = ['Rajyalakshmi', 'Narendra', 1.5, 'Manasa', 70, "Priyanka", "suresh", 333, "lalitha"]
# print(input_list[0:5:2])
# print(input_list[1:5:2])
# print(input_list[ :6:3])
# print(input_list[ : : 2])
# print(input_list[ : :-1], "<===") # Reverse the lst
# print(input_list[ : :-2]) # ['lalitha', 'suresh', 70, 1.5, 'Rajyalakshmi']
# print(input_list[ : :-3])
x = "suresh"
y = "madam"
# print(x)
# print(x[::-1])
# print(x == x[::-1]) # False
# print(y == y[::-1]) # True
##=============================================================================
lst1 = [5, 12, 14, 18, 19, 85, 25]
lst2 = ["Narendra","Surendra","n"]
# print("list before modification : ",lst1)
# lst1[3] = 41.5 # 18 is replaced by 41.5 dynamically (replace with +ve index)
# print("Modified list is : ", lst1)
# lst2[-1] = "python data science web frameworks flask restapi pandas" # replace with -ve index
# print(lst2)
""" "in" and "not in "operators test for membership( in keyword, used for check availability)"""
input_Data = ["SaLMA", 234, "Anand", "Sri", "Ashok", "Srikanth", 22.2, "Giri", "Swathi",
"srinivas", [78, 85.2, "Srinivas"],"Ashok", "Tejaswi", ["Haritha", "Radha", "Lakshmi"], "Durga Narendra"]
print(len(input_Data))
# print("Swathi" in input_Data) # membership operator
# print(22.5 in input_Data)
# print("Mani" not in input_Data) # membership operator
# print([78, 85.2,"Srinivas"] in input_Data)
# print(78 in input_Data) # can't search data in nested list
# print(input_Data[10][-1][4:])
""" ################# Practice programs ############### """
a = ["SaLMA",234, "Anand", "Sri", "Giri","Swathi",1,78,85.2,"Srinivas","Ashok", "Tejaswi","Haritha","Srinivas"]
# print(a)
# b = a[: :2]
# print(b)
# b = a[: :-1]
# print(b)
# print(a[3:7])
# c = a[: :-2]
# print(c)
# del a[5]
# print(a)
#
# a.remove("Anand")
# print(a)
# #
# a.remove("Anand")
# print("***********",a)
#
# a.remove("Swathi")
# print("#####################",a)
# ##'''Accessing Values in Lists ,Updating Lists & Delete List Elements'''
# a="I am taking guidance from narendra in understanding Python"
# b=a.split()
# print(b)
# print("Value available at index 3 : ",b[3]) # Accessing Values in Lists
# b[3] = "support" # Updating Lists
# s="-" #s='space'
# c=s.join(b) #coverting list to string using string.join(list)
# print("Value available at index 3 after updating \n: ",c)
l = ["Hi",444,"hello",472,"good morning",732,"hello",74.5,"hello","yugesh", 34,56.3,"n",89, "Kiran"] # l is a object for the list
# print("No of times element available in list :",l.count("yugesh"))
# print(l.count("hello"))===================================================================================
PART -2===================================================================================__author__ = "Narendra Boyina"
# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Technologies PVT LTD
#-----------------------------------------------------------------------------
"""====Built-in List Methods================
Common Builtin Methods:
insert(), count(), index(), append(), extend(), insert(), remove(), pop(), clear(), index(), count(), sort(), reverse(),"""
""" Insert element in the required position
Syntax: obj.insert(index,value)"""
u = ['tell', 'me', 'index', 'Narasimha', 'Swathi','into', 'sequence','DURGA']
# print(u, type(u))
# u.insert(3, "Narendra") # User Can insert string
# print(u) # ['tell', 'me', 'index', 'Narendra', 'Narasimha', 'Swathi', 'into', 'sequence', 'Swathi']
# u.insert(4, 74.5) # User Can insert float value
# print(u) # ['tell', 'me', 'index', 74.5, 'Narendra', 'Narasimha', 'Swathi', 'into', 'sequence', 'Swathi']
# u.insert(-1, [1,2,3,4])
# print(u)
""" count no of repeated elements in a list.
Syntax: seq.count(Element)
Syntax: list.count(obj)
where "obj" to be counted in the given list."""
b = ['Iam', 'taking', 'guidance', 'from', 'Narendra', 'in',
'understanding', 'Python', "thanks", "Narendra", 22, 2, 1, 10, 22, 22, 43.56]
# f = b.count("Narendra")
# print(f)
# print(b.count("guidance"))
# f = b.count("narendra") # case sensitive so it assumes that no string, So count is 0
# print(f)
# f = b.count(22) # given string is not available in the list So count is 0
# print(f)
"""We can find the index for required element in the list
Syntax:list.index(obj) ==>obj -- This is the object to be find out."""
x = ["SaLMA", "Anand","Raahi", "Ashok", "Srikanth", 22.2, "Giri", "Swathi", ["srinivas", 78, 85.2], "Srinivas",
"Ashok", "Tejaswi", "Haritha", "Radha", "Lakshmi", "Durga Narendra"]
# ii = x.index("Ashok") # it will give 1st occurence's index
# print(ii)
# print(x[5: ])
# print(x[x.index("Srikanth"): ]) # omitting ending address
"""===================We can reverse the list ============="""
x = ['SaLMA', 'Anand', 'Sri', 'Srikanth', 'Giri', 'Swathi','Srinivas', 'Ashok',
'Tejaswi', 'Haritha', 'Srinivas', "jai", 'Radha', 'Lakshmi', "pankaj",
"narendra"]
# x.reverse()
# print(x)
# y = x[ : :-1]
# print(y) # Modified value
# print(x) # Original value
#
#
# y = x.reverse() # reverse() method will return None
# print(y) # we can't store the reverse value
"""
Note : difference b/w reverse() & accessing
x.reverse() return None. i.e we can't store result of x.reverse() in another variable.
But result of x[ : :-1] can be stored in other variable.
"""
""" difference B/W append and extend methods"""
a = ["Nanna"]
# print(a)
# a.append("Amma")
# print(a)
# a.append(7.5)
# print(a)
# print(len(a))
# b = ["Hi",444,"hello","hello", 3.4]
# a.append(b)
# print(a)
# # print(len(a))
# b = ["Hi",444,"hello","hello", 3.4]
# a.extend(b)
# print(a)
# print(len(a))
# print(b)
# We can extend the list of /tuple of/set of elements to the list
# Ex:
# a = ['Nanna', 'Amma', 7.5] # Mutable
# b = ("Hi",444,"hello","hello", 3.4) #Immutable
# a.extend(b)
# print(a)
# print(b)
"""
Can we add tuple of elements to the List ? ---> Yes #Reason list is mutable
Can we add list of elements to the tuple ? ---> No #Reason tuple is immutable
"""
''''
we can add 2 lists by using extend() method.
the difference b/w concatenation and extend is
(if we use concatenate operator "+" we need to store the result in another variable)
but extend method will combine both lists and stored in 1st list variable '''
l1 = [1, 2, 3, "Narendra"]
l2 = ["Bhagyasree", "Raj", 4, 5, 6]
# l1.extend(l2)
# print(l1)
""" concatenate lists with + operator """
l3 = l1 + l2 # [1, 2, 3, 'Narendra', 'Bhagyasree', 'Raj', 4, 5, 6]
# print(l3)
# print(l1)
# print(l2)
#==============================================================================
"""syntax: del instance[index] to remove by index"""
s = ['show', 'how', 5.5, 7, "Jaya",8.5,'manasa', "Raahi",'Narendra', 'into', 'sequence',"Surendra"]
# del s[2]
# print(s)
## or
# a = s.index("Narendra")
# print(a)
# del s[a]
# print(s)
# del s[s.index("Narendra")]
# print(s)
""" syntax: instance.remove(element) to remove by value; raises ValueError if not present
# remove() equivalent to "del seq[seq.index(item)]"""""
s = ['show', 'how', 'to', 1.4, 'Narendra', 12,'into', 'sequence',"into", "Narendra"]
# s.remove("into") # it will remove the specified element which is present at the 1st occurence
# print(s)
# a = ['narenra', 1, 'Manju', 2162, 'siva', '415']
# a.clear()
# print(a)
"""returns last element and remove it"""
l = ["hello", 472, "good morning", 732, "hello", 74.5, "suresh", "Indhusree","Sai Srinivas","Mani"]
# r = l.pop()
# print(r) # This print statement is for display the removed element
# print(l) # displays the list after deletion of last element
# r = ["hello",472,"good morning",732,"1111hello","shubhashini", "rindha","priyanka"].pop()
# print(r) # displays deleted element
# x = 4.5 # float value
# print(int(x)) # converted in to integer
# y = 5
# print(float(y)) # converted in to float
#list() constructor
# input_data = (2, 4, 6.5, "narendra", 77.7, "priyanka") # tuple
# output = list(input_data)
# print(output)
# output.insert(1,"Raahi")
# print(output)
# print(tuple(output))
import copy
"""
In python we are having 2 copy technics.
1. deep copy
2. shallow copy
deep copy creates separate memory location for the 2nd list so that if you change element in the 2nd list,
then 1st list will not be changed """
# lst_1 = ['a', 'b', ['ab', 'ba']]
# lst_2 = copy.deepcopy(lst1) # Also known as deep copy (creates separate memory location)
# print(lst_1, id(lst_1))
# print(lst_2, id(lst_2))
# lst2[0] = 'c'
# print(lst1)
# print(lst2)
"""Shallow copy uses same memory location for the 2nd list
# so that if you change element in the 2nd list , then 1st list values also will change """
# lst_1 = ['a', 'b', ['ab', 'ba']]
# lst_2 = lst_1 # shallow copy (it will use the same memory location)
# print(lst_1, id(lst_1))
# print(lst_2, id(lst_2))
# lst_2[0] = "c"
# lst_2[2][1] = "d"
# # # # I have done 2 changes in the 2nd list, but i haven't done any changes in 1 st list
# print(lst_1) # ['c', 'b', ['ab', 'd']]
# print(lst_2) # ['c', 'b', ['ab', 'd']]
"""
Repeat lists using the * operator
(also known as shallow copy, does not create separate memory location,
if one inner list change means all the outer lists will be modified)"""
# a = [1, 2, 3] * 3 # here * creates the elements in the list
# print(a)
# c = [[1, 2, 3]]
# d = c*3 # Repeat inner list
# print(d) # [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
# d[2].append(9) # doing modification only for 3rd inner list
# print(d) # [[1, 2, 3, 9], [1, 2, 3, 9], [1, 2, 3, 9]]
""" Element accessing from nested list"""
Narendra = [1, "venkat", [44.5, 55, "ganesh"], "narendra"]
# print(Narendra[2])
# print(len(Narendra))
# print(Narendra[2][2])
# print(Narendra[1][-1])
# print(Narendra[2][2][2:])
# print(Narendra[2][1])
# print(Narendra[2][2][:3])
# print(Narendra[3][2:])
# print(Narendra[2][1][1])
# print(Narendra[2][2][0])
# print(Narendra[2][2][:4])
data = ["narendra", "surendra", "23432", 3, 4]
# print(data[2][2]) #TypeError: 'int' object is not subscriptable
"""
#####################################################
################# Practice programs ###############
#####################################################"""
# a = [1, 2, 3, 4, 7, 'Harshitha']
# b =[11, 22,33,44]
# c = a + b
# print(c)
# Real word based examples
#1. shopping_list list
# shopping_list = []
# # Add items
# shopping_list.append("Milk")
# shopping_list.append("Eggs")
# shopping_list.append("Bread")
# # Remove an item
# shopping_list.remove("Eggs")
# # Display the list
# print("Shopping List:", shopping_list)
# Servey response calculator
# List of survey responses
# responses = ["Yes", "No", "Yes", "Maybe", "No", "Yes"]
# # Count the number of "Yes" responses
# yes_count = responses.count("Yes")
# # Make a copy of the responses list
# responses_copy = responses.copy()
# # Clear the original list
# responses.clear()
# print(f"Number of 'Yes' responses: {yes_count}")
# print("Original responses cleared.")
# print("Copy of responses:", responses_copy)Author: Boyina Narendra
Supporting Author: M. Meera Sindhu
Request: If you find this information useful, please provide your valuable comments
No comments:
Post a Comment