0% found this document useful (0 votes)
5 views5 pages

Lab - Week 7 (Yeni)

This document covers the concepts of lists and tuples in Python, including their properties, methods, and operations such as indexing, slicing, and modifying elements. It also discusses string manipulation techniques, including string methods, slicing, and testing string properties. Various examples demonstrate the usage of these concepts through code snippets.

Uploaded by

ghostofmidnights
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)
5 views5 pages

Lab - Week 7 (Yeni)

This document covers the concepts of lists and tuples in Python, including their properties, methods, and operations such as indexing, slicing, and modifying elements. It also discusses string manipulation techniques, including string methods, slicing, and testing string properties. Various examples demonstrate the usage of these concepts through code snippets.

Uploaded by

ghostofmidnights
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/ 5

Lists and Tuples

In this lab session, you are going to learn;


o Lists, including:
o Repetition and concatenation operators
o Indexing
o Techniques for processing lists
o Slicing and copying lists
o List methods and built-in functions for lists
o Two-dimensional lists
o Tuples, including:
o Immutability
o Difference from and advantages over lists

1) Consider following statements and see the results.

#lists
print(list('cat'))
lst1 = ['physics', 'chemistry', 2017, 2023]
print("Value available at index 2 : ", lst1[2])
#tuples
a_tuple = ('ready', 'fire', 'aim')
print(list(a_tuple))
tup1 = ('physics', 'chemistry', 2017, 2023)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print("tup1[3]: ", tup1[3])
print("tup2[2:6]: ", tup2[2:6])

2) Consider following statements about lists and tuples. See the results.

#deletion
mylist = ['python', 'java', 2017, 2023]
print(mylist)
del mylist[1]
print("After deleting value at index 1 : ", mylist)
#updating
lst2 = ['python', 'geometry', 2017, 2023]
print("Value available at index 3 : ", lst2[3])
lst2[3] = 5000
print("New value available at index 3 : ", lst2[3])
3) Consider following example and see the results.

#split
txt = 'but soft what light in yonder window breaks'
words = txt.split()
print(words)
#Two expressions
pow2 = [2 ** x for x in range(10) if x > 5]
print(pow2)
pow2 = []
for x in range(10):
pow2.append(2 ** x)
print(pow2)

4) Consider following statements by using functions for different goals. See the
results.

# append, insert, extend


langs = []
langs.append("Python")
langs.append("Perl")
print(langs)
langs.insert(0, "PHP")
langs.insert(2, "Lua")
print(langs)
langs.extend(("JavaScript", "ActionScript"))
print(langs)
#removing all elements
langs = ["Python", "Ruby", "Perl", "Lua", "JavaScript"]
print(langs)
del langs[1]
print(langs)
del langs[:]
print(langs)
More About Strings

In this lab session, you are going to learn;


➢ Methods for iterating over strings
➢ Repetition and concatenation operators
➢ Strings as immutable objects
➢ Slicing strings and testing strings
➢ String methods
➢ Splitting a string

5) Consider following statements and see the results.

#accessing
L = ['a', 'b', 'c']
for index, item in enumerate(L):
print(index, item)

#concatenation
hello = "hello"
python = "python"

print (hello + " " + python)


print ("%s %s" % (hello, python))
print ("{} {}".format(hello, python))
print (' '.join([hello, python]))

#slicing
ss = "Shark attacks fisherman!"
print(ss[:5])
print(ss[7:])
print(ss[-4:-1])
print(ss[0:12:2])

6) Consider following statements by using functions for different goals. See the
results.

#isalpha()
mystr1 = "this" #No space and digit in this string
print(mystr1.isalpha())

mystr2 = "this is string example....wow!!!"


print(mystr2.isalpha())
#islower()

mystr3 = "THIS is string example....wow!!!"


print(mystr3.islower())

mystr4 = "this is string example....wow!!!"


print(mystr4.islower())

#isspace()
mystr5 = " "
print(mystr5.isspace())

mystr6 = "This is string example....wow!!!"


print(mystr6.isspace())

7) Consider following statements by using functions for different goals. See the
results.

#strip()

mystr7 = "0000000this is string example....wow!!!0000000"

print(mystr7.strip( '0' ))

#lstrip()
mystr8 = " this is string example....wow!!! "
print(mystr8.lstrip())
mystr9 = "88888888this is string example....wow!!!8888888"
print(mystr9.lstrip('8'))

#rstrip()

mystr10 = " this is string example....wow!!! "

print(mystr10.rstrip())

mystr11 = "88888888this is string example....wow!!!8888888"

print(mystr11.rstrip('8'))
8) Consider following statements by using functions for different goals. See the
results.

# endswith()
mystr12 = "this is string example....wow!!!"

suffix = "wow!!!"
print(mystr12.endswith(suffix))
print(mystr12.endswith(suffix,20))

suffix = "is"
print(mystr12.endswith(suffix, 2, 4))
print(mystr12.endswith(suffix, 2, 6))

#startswith()
mystr13 = "this is string example....wow!!!"

print(mystr13.startswith( 'this' ))
print(mystr13.startswith( 'is', 2, 4 ))
print(mystr13.startswith( 'this', 2, 4 ))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy