0% found this document useful (0 votes)
9 views4 pages

List and Tuple - Examples and Explanation

Uploaded by

stefanymca12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

List and Tuple - Examples and Explanation

Uploaded by

stefanymca12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Data Structure:

List:
- We can store different type of elements. (String, integer, float)
- We can store multiple elements into single variable.
- We can store elements in specific order.
- Indexes start from 0.
- We can change value of element into the list.
- We can store duplicate values.
1. Ordered
2. Changeable
3. Allows duplicate values

syntax:
variablename = [element1,element2,element3]

Access elements:
variablename[0]

#list declaration
students = ["sahil","Tanveer","Veronica","Stefany","Vanya"]

#Access specific element


print(students[2])

#Print entire list


print(students)

#list declaration with different types of value


studentInfo = [12345,"Gabi","Montreal","1234567890","[email protected]"]

print(studentInfo[4])

#Display length of the list:


print("Length of student info list:",len(studentInfo))

#Check the element is exist or not:


if "Tanveer" in students:
print("Yes, student is available")
else:
print("Student is not available")

print(students[:3])

#list declaration
students = ["sahil","Tanveer","Veronica","Stefany","Vanya"]

#Change element:
students[2] = "Sandra"

print(students)

#insert element at specific index


students.insert(3,"Erick")

print(students)

#insert element at end of the list


students.append("Ahmed")
print(students)

grades = [88,89,90,95,98,99]

#merge two list


students.extend(grades)

print(students)

#remove specific element


students.remove(90)

print(students)

#remove element from specific index


students.pop(3)

print(students)

#two ways to display elements using for loop


#first way to display elements using for loop
counter = 0
for element in students:
print(element)
counter +=1

print("length of list:",counter)

#second way to display elements using for loop


for index in range(len(students)):
print(students[index])

#Display elements using while loop


i=0
while i < len(students):
print(students[i])
i+=1

#list declaration
students = ["Sahil","Tanveer","Veronica","Stefany","Vanya"]

#arrange elements into ascending order


students.sort()

print(students)

#arrange elements into descending order


students.sort(reverse=True)

print(students)

#copy elements from student list into programmingStudents list


programingStudents = students.copy()

print(programingStudents)

#copy elements from programmingStudents into databaseStudents list


databaseStudents = list(programingStudents)
print(databaseStudents)
#copy elements from one list to another list
itStudents = students
print(itStudents)

Tuple:
- We can store different type of elements into single variable.
- We can't change the value of element. can't add, change or remove elements.
- Ordered
- allows duplicate values

#tuple declaration
students = ("veronica","erick","tanveer","ahmed","simarpreet","sandra","sebastian")

print(students[3])

print(students)

#length of tuple
print(len(students))

#Check the element is exist or not:


if "krunal" in students:
print("Yes, student is available")
else:
print("Student is not available")

print(students[1:3])
print(students[-4:-2])
print(students[2:])
print(students[:5])

#two ways to display elements using for loop


#first way to display elements using for loop
counter = 0
for element in students:
print(element)
counter +=1

print("length of tuple:",counter)

#second way to display elements using for loop


for index in range(len(students)):
print(students[index])

#Display elements using while loop


i=0
while i < len(students):
print(students[i])
i+=1

teachers = ("Naresh","Krunal","Joseph","Aouatif")

#merge two tuples into another tuple


members = students + teachers
print(members)

You might also like