Friday, May 28, 2021

Python - Lists and Tuples

Python Lists:

Lists are containers to store a set of values of any data type.

Example, friends = [‘Apple’, ‘Sri’, ‘Sanddy’, 7, False]

The list can contain different types of elements such as int, float, string, Boolean, etc. Above list is a collection of different types of elements.

List Indexing:

A list can be indexed just like a string.

L1 = [7, 9, ‘sri’]

L1[0] – 7

L1[1] – 9

L1[70] – Error

L1[0:2] – [7,9]         (This is known as List Slicing)

List Methods:

Consider the following list:

L1 = [1, 8, 7, 2, 21, 15]

sort() – updates the list to [1,2,7,8,15,21]

reverse() – updates the list to [15,21,2,7,8,1]

append(8) – adds 8 at the end of the list

insert(3,8) – This will add 8 at 3 index

pop(2) – It will delete element at index 2 and return its value

remove(21) – It will remove 21 from the last


Tuples in Python:

A tuple is an immutable (can’t change or modified) data type in Python.

a = ()              #It is an example of empty tuple

a = (1,)           #Tuple with only one element needs a comma

a = (1, 7, 2)   #Tuple with more than one element

Once defined, tuple elements can’t be manipulated or altered.

Accessing Values in Tuples:

To access values in tuple, use the square brackets for slicing along with the index or indices to obtain value available at that index. For example −

tup1 = ('physics', 'chemistry', 1997, 2000);

tup2 = (1, 2, 3, 4, 5, 6, 7 );

print(tup1[0])

print("tup2[1:5])

Output:

physics

[2, 3, 4, 5]

Tuple methods:

Consider the following tuple,

a = (1, 7, 2)

count(1) – It will return number of times 1 occurs in a.

index(1) – It will return the index of first occurrence of 1 in a.

 

Practice:

Write a program to store seven fruits in a list entered by the user.

n1 = input("Enter fruit name 1:")
n2 = input("Enter fruit name 2:")
n3 = input("Enter fruit name 3:")
n4 = input("Enter fruit name 4:")
n5 = input("Enter fruit name 5:")
n6 = input("Enter fruit name 6:")
n7 = input("Enter fruit name 7:")

fruits = [n1,n2,n3,n4,n5,n6,n7]
print(fruits)

Write a program to accept marks of 6 students and display them in a sorted manner.

m1 = input("Enter score 1:")
m2 = input("Enter score 2:")
m3 = input("Enter score 3:")
m4 = input("Enter score 4:")
m5 = input("Enter score 5:")
m6 = input("Enter score 6:")
m7 = input("Enter score 7:")

marks = [m1,m2,m3,m4,m5,m6,m7]
marks.sort()
print(marks)

Check that a tuple cannot be changed in Python.

myTuple =(1,4,7)
myTuple[1]=5
print(myTuple)
TypeError: 'tuple' object does not support item assignment

Write a program to sum a list with 4 numbers.

num=int(input("how many numbers you want to sum:\n"))
i=1
myList=[]
while i <=num:
    value=int(input("Enter number:\n"))
    myList.append(value)
    i+=1
print("your list is:",myList)
sum=0
for x in myList:
    sum+=x
print("sum of" ,num , "numbers:" , sum)

Write a program to count the number of zeros in the following tuple:

a = (7, 0, 8, 0, 0, 9)

a = (708009)
print(a.count(0))

No comments:

Post a Comment

Featured Post

11g to 12c OSB projects migration points

1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper:   File⇾Import⇾Service Bus Resources⇾ Se...