0% found this document useful (0 votes)
10 views

list_tuple_string

The document provides an overview of lists, tuples, and strings in Python, including their creation, manipulation, and various operations such as appending, inserting, and sorting. It includes examples of how to access elements, search for values, and perform string operations like concatenation and slicing. Additionally, it discusses membership and relational operators in the context of strings.

Uploaded by

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

list_tuple_string

The document provides an overview of lists, tuples, and strings in Python, including their creation, manipulation, and various operations such as appending, inserting, and sorting. It includes examples of how to access elements, search for values, and perform string operations like concatenation and slicing. Additionally, it discusses membership and relational operators in the context of strings.

Uploaded by

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

list_tuple_string

January 2, 2025

1 Lists
[23]: list1 = []
list2 = [56]
friends = ["Saisha", "Advika", "Arshia"]
marks = [92, 91, 89, 95, 93, 45, 91, 48]
students = [21, "Gunjan" ,94.5 , 34, "Rohit", 91.3]
section = ["A", 25, "English", ["B", 34], ["c", 37], 56.7]
l1 = [10] * 5
aplha = list("hello")
# l1 = list(input("Enter numbers:"))

[6]: l1 = ['P', 'Y', 'T', 'H', 'O', 'N']


print(l1[0])
print(l1[3])
print(l1[-3])
print(l1[-1])
print(l1)
print(l1[0], l1[4])

P
H
H
N
['P', 'Y', 'T', 'H', 'O', 'N']
P O

[3]: word = ['E', 'D', 'U', 'C', 'A', 'T', 'I', 'O', 'N']
print(word[:1:-2])
print(word[-1:])
print(word[:-1])

['N', 'I', 'A', 'U']


['N']
['E', 'D', 'U', 'C', 'A', 'T', 'I', 'O']

[25]: word[4:-2:-2]

[25]: []

1
2 List Operations
2.1 append()
[10]: fruits = ["apple", "banana", "cherries"]
fruits.append("melon")
print(fruits)

['apple', 'banana', 'cherries', 'melon']

[11]: cars = ["Maruthi", "Ford"]


cars.append("Honda")
cars.append("Hyundai")
print(cars)

['Maruthi', 'Ford', 'Honda', 'Hyundai']

[12]: friends = []
for i in range(5):
nm = input("Enter Friends Name:")
friends.append(nm)
print(friends)

Enter Friends Name:naimu


Enter Friends Name:heb
Enter Friends Name:he
Enter Friends Name:hbt
Enter Friends Name:bhwr
['naimu', 'heb', 'he', 'hbt', 'bhwr']

[13]: pets = ['cat', 'dog', 'rabbit']


wild = ['tiger', 'lion']
pets.append(wild)
print(pets)

['cat', 'dog', 'rabbit', ['tiger', 'lion']]

2.2 insert()
[17]: l = [10, 20, 30]

[18]: l.insert(1,40)
print(l)

[10, 40, 20, 30]

[19]: l.insert(0, "abc")


print(l)

['abc', 10, 40, 20, 30]

2
[20]: a = "xyz"
l.insert(3, a)
print(l)

['abc', 10, 40, 'xyz', 20, 30]

[21]: l.insert(5, ["a", "b"])


print(l)

['abc', 10, 40, 'xyz', 20, ['a', 'b'], 30]

2.3 extend()
[23]: n = [1,2,3,4]
m = [ 5 ];
n.extend(m)
print(n)

[1, 2, 3, 4, 5]

[24]: pets = ['cat', 'dog', 'rabbit',]


wild = ['tiger', 'lion']
pets.extend(wild)
print(pets)

['cat', 'dog', 'rabbit', 'tiger', 'lion']

2.4 sort()
[2]: city = ["Delhi", "Mumbai", "Kolkata", "Chennai"]
city.sort()
print(city)

['Chennai', 'Delhi', 'Kolkata', 'Mumbai']

[5]: city.sort(reverse=True)
print(city)

['Mumbai', 'Kolkata', 'Delhi', 'Chennai']

2.5 Searching a value in a list


[17]: # Searching a specific value and displaying its number of occurences in a list
A = [10, 23, 45, 10, 12, 45, 10, 56]
s = int(input('Enter the value you want to search:'))
count = 0
for i in A:
if i == s:
count+=1
print(s," occurs ", count, " times")

3
Enter the value you want to search:87
87 occurs 0 times

[38]: # Input a value and search whether it exists in a list or not. if it exists␣
↪then dispaly its index number.

flowers = ["Rose", "Lily", "Lotus", "Sunflower"]


s = input("Input a name of a flower")
for i in range(len(flowers)):
if s == flowers[i]:
print(s, "found at index number", i)
break
else:
print("Sorry, Not found")

Input a name of a flowerSunflower


Sunflower found at index number 3

3 Tuples
[15]: tup1 = ('sst', 'chemistry', 2010, 2022)
tup2 = (1, 2, 3, 4, 5)
tup3 = (1, 2.5, 3.6, 4, 5)
tup4 = (3, "Orange", 45.5)
tup5 = (162, [7, "orange", 15.5], (2, "Orange Education", 70.5))

[4]: tup = tuple('orange')


tup

[4]: ('o', 'r', 'a', 'n', 'g', 'e')

3.1 Accessing Tuples


[6]: print(tup1[1])
print(tup2[3])

chemistry
4

3.2 Deleting a Tuple


[16]: tup5

[16]: (162, [7, 'orange', 15.5], (2, 'Orange Education', 70.5))

[17]: del tup5

[19]: tup = (2,5,7,8,2,1)


print(tup.count(2))

4
print(tup.index(7))

2
2

[20]: lst1 = list(tup)


lst1[-1] = 11
tup = tuple(lst1)
print("Modified tuple:", tup)

Modified tuple: (2, 5, 7, 8, 2, 11)

[21]: print(type(tup))
print(type(lst1))

<class 'tuple'>
<class 'list'>

4 Strings
[1]: s1 = 'Hello World'
s2 = "Good Morning"

[3]: print(s1)
print(s2)

Hello World
Good Morning

[43]: s3 = """python is an intresting


programming language"""

[45]: print(s3)

python is an intresting
programming language

[47]: s4 = '''It is a free annd open source.


Happy Learning!'''

[49]: print(s4)

It is a free annd open source.


Happy Learning!

[51]: s5 = """python is an intresting \


programming language"""

[53]: print(s5)

5
python is an intresting programming language

[55]: nm = input('Enter your name: ')


print('Hello', nm, 'Happy Learning!')

Enter your name: Ram


Hello Ram Happy Learning!

4.1 Accessing elements of the String


[2]: s1 = 'PYTHON'

print(s1[0])
print(s1[3])
print(s1[-3])
print(s1[-1])
print(s1)
print(s1[0], s1[4])
print(s1[1+4])
# print(s1[10])
# print(s1[5.5])

P
H
H
N
PYTHON
P O
N

4.2 Traversing a String


[57]: txt = "WONDERFUL"
for indx in txt:
print(indx)

W
O
N
D
E
R
F
U
L

[33]: txt = "WONDERFUL"


for indx in txt:

6
print(indx, end="*")

W*O*N*D*E*R*F*U*L*

[6]: txt = "WONDERFUL"


for i in range(len(txt)):
print(txt[i], end="!")

W!O!N!D!E!R!F!U!L!

[7]: txt = "NATURE"


for i in range(len(txt)):
print(i, end="@")

0@1@2@3@4@5@

4.3 Multiline Strings


[9]: multiline_str = """
This is my favorite book.
It is very intresting to read.
"""
print(multiline_str)

This is my favorite book.


It is very intresting to read.

[17]: multiline_str = """


This is my favorite book.\
It is very intresting to read.\
"""
print(multiline_str)

This is my favorite book.It is very intresting to read.

4.4 Concatenating Strings


[24]: str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result)

Hello World

[30]: str_list = ["Hello", "World"]


concatenated_str = " ".join(str_list)
print(concatenated_str)

7
Hello World

[59]: str_list = ["Hello", "World"]


concatenated_str = "$".join(str_list)
print(concatenated_str)

Hello$World

4.5 String Operations


4.5.1 ‘+’ and ’*’ operator

[33]: S = "LIFE"
T = "IS"
U = "BEAUTIFUL"
S + " " + T +" " + U

[33]: 'LIFE IS BEAUTIFUL'

[34]: 'hi' * 3

[34]: 'hihihi'

[35]: 3 * 'hello' * 2

[35]: 'hellohellohellohellohellohello'

4.6 Membership Operators


[37]: # in operator
'el' in 'Delhi'

[37]: True

[38]: # not in operator


'is' not in 'Life is beautiful'

[38]: False

4.7 Relational Operators


[39]: 'a' == 'A'

[39]: False

[40]: 'abc' > 'abC'

[40]: True

8
[43]: 'hi' != "Hi"

[43]: True

[44]: # To print the ASCII values


ord('a')
ord('8')

[44]: 56

4.8 String Slicing


“ “ ” For forward (positive) indexing of a string s, default values are start_index = 0 end_index =
len(s) ——>
For backward (negative) indexing of a string s, default values are
start_index = -1
end_index = -len(s) - 1
<-------
““”

[17]: txt = "PROGRAMMING IS AMAZING"


txt[::]

[17]: 'PROGRAMMING IS AMAZING'

[19]: print(len(txt))

22

[25]: txt[22::]

[25]: ''

[27]: print(txt[22::])

[65]: print(txt[::1])
print(txt[::-1])
print(txt[:7:1])
print(txt[4::1])
print(txt[3:7])
print(txt[15:])
print(txt[-1:-10:-1])

PROGRAMMING IS AMAZING
GNIZAMA SI GNIMMARGORP
PROGRAM
RAMMING IS AMAZING

9
GRAM
AMAZING
GNIZAMA S

[67]: s = "EDUCATION"
print(s[-9:-6])
print(s[3:-3])
print(s[7:-5:-1])
print(s[:1:-2])

EDU
CAT
OIT
NIAU

[9]: string = 'PROGRAMMING IN PYTHON'

[39]: # String Functions


print(len(string))
print(string.capitalize())
print(string.title())
print(string.startswith('PRO'))
print(string.endswith('PYTHON'))
print(string.isalpha())
print(string.isdigit())
print(string.isspace())
print(string.isalnum())
print(string.lower())
print(string.upper())
print(string.islower())
print(string.isupper())
print(string.find('IN'))
print(string.find('in'))
print(string.find('IN',10,15))
print(string.replace('M','_'))
print(string.count('O'))
print(string.count('O', 1, 15))

21
Programming in python
Programming In Python
True
True
False
False
False
False
programming in python
PROGRAMMING IN PYTHON

10
False
True
8
-1
12
PROGRA__ING IN PYTHON
2
1

[22]: city = 'DELHI'


s = '@'
s.join(city)

[22]: 'D@E@L@H@I'

[26]: a= ["orange"]
b = ["education"]
c = a + b
print(c)

['orange', 'education']

[ ]:

11

You might also like