Py (MODULE 2) (Chapter1)
Py (MODULE 2) (Chapter1)
ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
CHAPTER 1
[1.1THE LIST DATA TYPE]
• A list is a value that contains multiple values in an ordered sequence.
• A list begins with an opening square bracket and ends with a closing square bracket,
[].
• Values inside the list are also called items.
• Items are separated with commas (that is, they are comma-delimited).
• The value [ ] is an empty list that contains no values, similar to ‘ ’, the empty string.
•it is considered as Mutable datatype because list supoort all kind of modification.
•Syntax—
var=[val1,val2,val3,…………….,valn]
The len() function will return the number of values that are in a list value passed to
it. Example:
>>> s = ['cat', 'dog', 'moose']
>>> len(s)
3
1.1.6 Changing Values in a List with Indexes
Use an index of a list to change the value at that index.
For example—
s[1] = ‘TIGER’ means “Assign the value at index 1 in the list s to the string ' TIGER
'.”
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
Examples:
>>> s = ['cat', 'bat', 'rat', 'elephant']
>>> s[1] = ‘TIGER'
>>> s ['cat', ' TIGER ', 'rat', 'elephant']
>>> s[2] = s[1]
>>> s ['cat', ' TIGER ', ' TIGER ', 'elephant']
>>> s[-1] = 12345
>>> s ['cat', ' TIGER ', ' TIGER ', 12345]
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
1.1.7 LIST CONCATENATION AND LIST REPLICATION
The + operator can combine two lists to create a new list value in the same
way it combines two strings into a new string value.
The * operator can also be used with a list and an integer value to replicate
the list.
Examples –
>>> [1, 2, 3] + ['A', 'B', 'C']
[1, 2, 3, 'A', 'B', 'C']
>>> ['X', 'Y', 'Z'] * 3
['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z']
PREPARED BY- MIS.ITISHREE BARIK
>>> s = [1, 2, 3] ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
1.3 USING FOR LOOPS WITH LISTS
• A for loop repeats the code block once for each value in a list or list-like value.
Example 1:
for i in [0, 1, 2, 3]:
print(i)
Output: 0
1
2
3
The multiple assignment trick is a shortcut that lets you assign multiple variables with the
values in a list in one line of code.
Examples:
>>> cat = ['fat', 'black', ‘ZooZoo']
>>> size = cat[0]
>>> color = cat[1]
>>> name = cat[2]
you could type this line of code:
>>> cat = ['fat', 'black', 'ZooZoo']
>>> size, color, name = cat
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
1.6 AUGMENTED ASSIGNMENT OPERATORS
Examples:
>>> spam = 42
>>> spam = spam + 1
>>> spam
43
Replace With >>> spam = 42
>>> spam += 1
>>> spam
43
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
The augmented assignment operators for the +, -, *, /, and % operators
• index
• append
• insert
• remove
• sort
1.7.1 index() - Finding a Value in a List with the index() Method
Example: --
>>> spam = ['hello', 'hi', 'howdy', 'heyas']
>>> spam.index('hello')
0
>>> spam.index('heyas')
3
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
1.7.2 append() - Adding Values to Lists with the append() Method.
Example:
>>> spam = ['cat', 'dog', 'bat']
>>> spam.append(‘rat')
>>> spam ['cat', 'dog', 'bat', ‘rat']
1.7.3 insert() - Adding Values to Lists with the insert() Method.
Example:
>>> spam = ['cat', 'dog', 'bat']
>>> spam.insert(1, 'rat')
>>> spam ['cat', 'rat', 'dog', 'bat']
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
Note:
Methods belong to a single data type.
The append() and insert() methods are list methods and can be called only on list values, not on
other values such as strings or integers.
Example—
>>> eggs = 'hello‘
>>> eggs.append('world')
AttributeError: 'str' object has no attribute 'append‘
>>> bacon = 42
>>> bacon.insert(1, 'world')
AttributeError: 'int' object has no attribute 'insert'
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
1.7.4 remove() - Removing Values from Lists with remove()
Examples—
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam.remove('bat')
>>> spam ['cat', 'rat', 'elephant']
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam.remove('chicken')
ValueError: list.remove(x): x not in list
>>> spam = ['cat', 'bat', 'rat', 'cat', 'hat', 'cat']
>>> spam.remove('cat')
>>> spam ['bat', 'rat', 'cat', 'hat', 'cat']
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
1.7.5 SORT()
• The sort() method is one of the ways you can sort a list in Python.
• When using sort(), you sort a list in-place.
• This means that the original list is directly modified. Specifially, the original order of elements is altered
Example— Output--
# a list of numbers
my_numbers = [10, 8, 3, 22, 33, 7, 11, 100, 54]
• You can also pass True for the reverse keyword argument to have sort() sort the values in reverse
order.
Example— Output—
# a list of numbers
my_numbers = [10, 8, 3, 22, 33, 7, 11, 100, 54]
[100, 54, 33, 22, 11, 10, 8, 7, 3]
#sort list in-place in descending order
my_numbers.sort(reverse=True)
import random
def getAnswer(answerNumber):
if answerNumber == 1:
return 'It is certain'
elif answerNumber == 2:
return 'It is decidedly so'
elif answerNumber == 3:
return 'Yes'
elif answerNumber == 4:
return 'Reply hazy try again'
elif answerNumber == 5:
return 'Ask again later' PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
elif answerNumber == 6:
return 'Concentrate and ask again'
elif answerNumber == 7:
return 'My reply is no' what do you ask of me?4
elif answerNumber == 8: Outlook not so good
return 'Outlook not so good'
elif answerNumber == 9:
return 'Very doubtful'
r=random.randint(1,9)
x=getAnswer(r)
int(input("what do you ask of me?"))
print(x)
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
MAGIC 8 BALL WITH A LIST
import random
messages = ['It is certain','It is decidedly so','Yes definitely','Reply hazy try again',
'Ask again later','Concentrate and ask again','My reply is no','Outlook not so good',
'Very doubtful']
question=int(input("enter to shake the magic 8 ball "))
enter to shake the magic 8 ball
while question:
Very doubtful
print(random.choice(messages)) press enter to shake the magic ba
question=int(input("press enter to shake the magic ball:")) It is decidedly so
press enter to shake the magic ba
print("") It is certain
press enter to shake the magic ba
Outlook not so good
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
press enter to shake the magic b
1.9 LIST-LIKE TYPES: STRINGS AND TUPLES
• Lists aren’t the only data types that represent ordered sequences of values.
• For example, strings and lists are actually similar, if you consider a string to be a “list” of single text characters.
• Many of the things you can do with lists can also be done with strings: indexing; slicing; and using them with for loops, with
len(), and with the in and not in operators.
Example—
>>> name = 'Zophie'
>>> name[0]
'Z'
>>> name[-2]
'i'
The tuple data type is almost identical to the list data type, except in two ways.
First, tuples are typed with parentheses, ( and ), instead of square brackets, [ and ].
Tuples are different from lists is that tuples, like strings, are immutable (Tuples
cannot have their values modified, appended, or removed).
Example—
>>> eggs = ('hello', 42, 0.5)
>>> eggs[1] = 99
TypeError: 'tuple' object does not support item assignment
Example 1:
>>> spam = 42
>>> cheese = spam
>>> spam = 100
>>> spam
100
>>> cheese
100
Copy:
a = [[1, 2, 3], [4, 5, 6]]
>>> b = list(a)
>>> a [[1, 2, 3], [4, 5, 6]]
>>> b [[1, 2, 3], [4, 5, 6]]
>>> a[0][1] = 10
>>> a [[1, 10, 3], [4, 5, 6]]
>>> b # b changes too -> Not a deepcopy. [[1, 10, 3], [4, 5, 6]]