2025/5/3 21:28 M1_01_Intro_Python
Contents
1 Functions
2 Strings
CS664 - Artificial Intelligence
2.1 Strings - Indexing and slic
2.2 Strings - Skip slicing Module1 - Python Essentials - Review
2.3 Strings are Immutable
2.4 Strings - Concatenation O
2.5 Strings - membership 1 Functions
2.6 Strings - Iterating
2.7 Strings - other methods In [1]: def greet(name, className):
2.8 Strings - formatting print("Hello " + name + ", Welcome to " + className)
3 Lists
3.1 Lists - Indexing
3.2 Lists - Slicing
In [2]: greet('John', 'CS664')
3.3 Lists - Skip slicing Hello John, Welcome to CS664
3.4 Modifying a list
3.5 Concatenation of Lists wi In [3]: greet('Jane', 'CS521')
3.6 Removing items from list
Hello Jane, Welcome to CS521
3.7 Other List methods
3.8 Copying Lists
In [4]: print(greet('Jane', 'CS521'))
3.9 membership in a list
3.10 range Hello Jane, Welcome to CS521
3.11 Iterating over lists None
4 Tuples
4.1 Tuple Indexing and Slicing In [5]: greet('Jane', 'CS521') == None
4.2 Concatenation and repeti Hello Jane, Welcome to CS521
4.3 count and index Out[5]:True
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w To explicitly return a value
5 Lists versus Tuples
6 Lists and List Comprehension In [6]: def greet(name, className):
6.1 Nested Lists return ("Hello " + name + ", Welcome to " + className)
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
In [7]: print(greet('Jane', 'CS521'))
7.3 Deleting items from a dict Hello Jane, Welcome to CS521
7.4 Membership in a dictiona
7.5 Other dictionary methods In [8]: greet('Jane', 'CS521') == "Hello Jane, Welcome to CS521"
7.6 Dictionary comprehension
Out[8]:True
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
2 Strings
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection In [9]: s1 = 'programming'
8.6 Set Difference s2 = "programming"
8.7 Set Comparison s3 = '''program
8.8 Iterating over sets ming'''
9 Functions ... continued
print(s1)
9.1 Functions with default arg
print(s2)
print(s3)
9.2 Functions with variable ar
9.3 Functions with keyword a programming
9.4 Anonymous (Lambda) Fu programming
9.5 Indirect references program
ming
In [10]: print(len(s1))
print(len(s2))
print(len(s3))
11
11
16
In [11]: s1 == s2
Out[11]:True
In [12]: s1 == s3
Out[12]:False
In [13]: s3[6], s3[7], s3[8], s3[9], s3[10], s3[11], s3[12]
Out[13]:('m', '\n', ' ', ' ', ' ', ' ', 'm')
[Link] 1/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 2.1 Strings - Indexing and slicing
1 Functions
2 Strings In [14]: s1 = 'programming'
2.1 Strings - Indexing and slic print(s1)
2.2 Strings - Skip slicing
2.3 Strings are Immutable print(s1[0])
2.4 Strings - Concatenation O print(s1[0:1])
2.5 Strings - membership
2.6 Strings - Iterating print(s1[-1])
2.7 Strings - other methods
2.8 Strings - formatting print(s1[0:7])
3 Lists print(s1[0:-4])
3.1 Lists - Indexing
print(s1[:7])
3.2 Lists - Slicing
print(s1[3:-4])
3.3 Lists - Skip slicing
print(s1[3:7])
3.4 Modifying a list
3.5 Concatenation of Lists wi
print(s1[3:])
3.6 Removing items from list
3.7 Other List methods programming
3.8 Copying Lists p
3.9 membership in a list p
3.10 range g
3.11 Iterating over lists
program
4 Tuples
program
program
4.1 Tuple Indexing and Slicing
gram
4.2 Concatenation and repeti
gram
4.3 count and index
gramming
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 2/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 2.2 Strings - Skip slicing
1 Functions
2 Strings In [15]: s1 = 'abcdefghijk'
2.1 Strings - Indexing and slic print(s1)
2.2 Strings - Skip slicing
2.3 Strings are Immutable print(s1[::2])
2.4 Strings - Concatenation O
2.5 Strings - membership print(s1[:7:2])
2.6 Strings - Iterating print(s1[4::2])
2.7 Strings - other methods
2.8 Strings - formatting print(s1[::-1])
3 Lists print(s1[::-2])
3.1 Lists - Indexing abcdefghijk
3.2 Lists - Slicing acegik
3.3 Lists - Skip slicing aceg
3.4 Modifying a list egik
3.5 Concatenation of Lists wi kjihgfedcba
3.6 Removing items from list kigeca
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 3/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 2.3 Strings are Immutable
1 Functions
2 Strings In [16]: print(s1)
2.1 Strings - Indexing and slic s1[0] = 'A'
2.2 Strings - Skip slicing
abcdefghijk
2.3 Strings are Immutable
-------------------------------------------------------------------
2.4 Strings - Concatenation O
TypeError Traceback (most recent ca
2.5 Strings - membership
<ipython-input-16-9177bd4911ec> in <module>
2.6 Strings - Iterating 1 print(s1)
2.7 Strings - other methods ----> 2 s1[0] = 'A'
2.8 Strings - formatting
3 Lists TypeError: 'str' object does not support item assignment
3.1 Lists - Indexing
3.2 Lists - Slicing
3.3 Lists - Skip slicing
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 4/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 2.4 Strings - Concatenation Operators + and *
1 Functions
2 Strings In [17]: s1 = 'Data'
2.1 Strings - Indexing and slic s2 = 'Science'
2.2 Strings - Skip slicing print(s1 + s2)
2.3 Strings are Immutable print(s1 + ' ' + s2)
2.4 Strings - Concatenation O
2.5 Strings - membership print(2 * s1)
2.6 Strings - Iterating print(s2 * 3)
2.7 Strings - other methods
2.8 Strings - formatting print(2 * (s1 + s2))
3 Lists DataScience
3.1 Lists - Indexing Data Science
3.2 Lists - Slicing DataData
3.3 Lists - Skip slicing ScienceScienceScience
3.4 Modifying a list DataScienceDataScience
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 5/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 2.5 Strings - membership
1 Functions
2 Strings In [18]: vowels = 'aeiou'
2.1 Strings - Indexing and slic
2.2 Strings - Skip slicing print('p' in vowels)
2.3 Strings are Immutable print('e' in vowels)
2.4 Strings - Concatenation O print('t' not in vowels)
2.5 Strings - membership False
2.6 Strings - Iterating True
2.7 Strings - other methods True
2.8 Strings - formatting
3 Lists In [19]: print('log' in 'hologram')
3.1 Lists - Indexing print('hal' in 'hologram')
3.2 Lists - Slicing
True
3.3 Lists - Skip slicing
False
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 6/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 2.6 Strings - Iterating
1 Functions
2 Strings In [20]: s = 'Python'
2.1 Strings - Indexing and slic
2.2 Strings - Skip slicing for letter in s:
2.3 Strings are Immutable print(letter, ord(letter))
2.4 Strings - Concatenation O P 80
2.5 Strings - membership y 121
2.6 Strings - Iterating t 116
2.7 Strings - other methods h 104
2.8 Strings - formatting o 111
3 Lists n 110
3.1 Lists - Indexing
3.2 Lists - Slicing
3.3 Lists - Skip slicing
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 7/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 2.7 Strings - other methods
1 Functions
2 Strings In [21]: s = 'PyThon is Fun'
2.1 Strings - Indexing and slic print(s)
2.2 Strings - Skip slicing print("capitalize:", [Link]())
2.3 Strings are Immutable print("lower:", [Link]())
2.4 Strings - Concatenation O print("upper:", [Link]())
2.5 Strings - membership print("title:", [Link]())
2.6 Strings - Iterating print("swapcase:", [Link]())
2.7 Strings - other methods print("split:", [Link]())
2.8 Strings - formatting print("list:", list(s))
3 Lists print("find:", [Link]('is'))
3.1 Lists - Indexing
print("find:", [Link]('n'))
3.2 Lists - Slicing
print("find:", [Link]('n', 6))
print("find:", [Link]('n', -1))
3.3 Lists - Skip slicing
print("find:", [Link]('a'))
3.4 Modifying a list
3.5 Concatenation of Lists wi PyThon is Fun
3.6 Removing items from list capitalize: Python is fun
3.7 Other List methods lower: python is fun
3.8 Copying Lists upper: PYTHON IS FUN
3.9 membership in a list title: Python Is Fun
3.10 range swapcase: pYtHON IS fUN
3.11 Iterating over lists
split: ['PyThon', 'is', 'Fun']
4 Tuples
list: ['P', 'y', 'T', 'h', 'o', 'n', ' ', 'i', 's', ' ', 'F', 'u',
find: 7
4.1 Tuple Indexing and Slicing
find: 5
4.2 Concatenation and repeti
find: 12
4.3 count and index
find: 12
4.4 membership test
find: -1
4.5 Iterating over a tuple
4.6 Other built-in functions w
In [22]: s = "John,CS664,A"
5 Lists versus Tuples
[Link](',')
6 Lists and List Comprehension
6.1 Nested Lists Out[22]:['John', 'CS664', 'A']
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 8/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 2.8 Strings - formatting
1 Functions
2 Strings In [23]: s = "Hello {}, your grade is {}.".format("John", 85)
2.1 Strings - Indexing and slic print(s)
2.2 Strings - Skip slicing
2.3 Strings are Immutable s = "Hello {:10s}, your grade is {:10.2f}.".format("John", 85)
2.4 Strings - Concatenation O print(s)
2.5 Strings - membership
2.6 Strings - Iterating s = "Hello {:>10s}, your grade is {:<10.2f}.".format("John", 85)
2.7 Strings - other methods print(s)
2.8 Strings - formatting
3 Lists s = "Hello {:^10s}, your grade is {:^10.2f}.".format("John", 85)
3.1 Lists - Indexing
print(s)
3.2 Lists - Slicing Hello John, your grade is 85.
3.3 Lists - Skip slicing Hello John , your grade is 85.00.
3.4 Modifying a list Hello John, your grade is 85.00 .
3.5 Concatenation of Lists wi Hello John , your grade is 85.00 .
3.6 Removing items from list
3.7 Other List methods
Out of range index
3.8 Copying Lists
3.9 membership in a list
In [24]: s = 'Hello'
3.10 range
s[6]
3.11 Iterating over lists
4 Tuples -------------------------------------------------------------------
4.1 Tuple Indexing and Slicing IndexError Traceback (most recent ca
4.2 Concatenation and repeti <ipython-input-24-4cfe130d0688> in <module>
4.3 count and index 1 s = 'Hello'
4.4 membership test ----> 2 s[6]
4.5 Iterating over a tuple
IndexError: string index out of range
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
3 Lists
7 Dictionary like arrays in other languages
7.1 Alternative ways of creatin access to elements is O(1)
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona In [25]: l1 = []
7.5 Other dictionary methods l2 = [10, 20, 30]
7.6 Dictionary comprehension l3 = ['Python', 75, 'Java', 60]
8 Sets l4 = [[10, 20], [30, 40, 50]]
8.1 Adding elements to a set
8.2 Removing items from a se print(l1)
8.3 Membership in a set print(l2)
8.4 Set Union
print(l3)
8.5 Set Intersection
print(l4)
8.6 Set Difference
print(len(l1))
8.7 Set Comparison
print(len(l2))
8.8 Iterating over sets
print(len(l3))
9 Functions ... continued
print(len(l4))
9.1 Functions with default arg
9.2 Functions with variable ar []
9.3 Functions with keyword a [10, 20, 30]
9.4 Anonymous (Lambda) Fu ['Python', 75, 'Java', 60]
9.5 Indirect references
[[10, 20], [30, 40, 50]]
0
3
4
2
[Link] 9/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 3.1 Lists - Indexing
1 Functions
2 Strings In [26]: data = ['Python', 75, 'Java', 60]
2.1 Strings - Indexing and slic
2.2 Strings - Skip slicing print(data[0])
2.3 Strings are Immutable print(data[3])
2.4 Strings - Concatenation O
2.5 Strings - membership print(data[-1])
2.6 Strings - Iterating print(data[-4])
2.7 Strings - other methods Python
2.8 Strings - formatting 60
3 Lists 60
3.1 Lists - Indexing Python
3.2 Lists - Slicing
3.3 Lists - Skip slicing
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 10/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 3.2 Lists - Slicing
1 Functions
2 Strings In [27]: data = ['Python', 75, 'Java', 60, 'R', 90]
2.1 Strings - Indexing and slic
2.2 Strings - Skip slicing print(data)
2.3 Strings are Immutable print(data[0:4])
2.4 Strings - Concatenation O print(data[2:])
2.5 Strings - membership print(data[:4])
2.6 Strings - Iterating print(data[-2:])
2.7 Strings - other methods print(data[-4:-2])
2.8 Strings - formatting ['Python', 75, 'Java', 60, 'R', 90]
3 Lists ['Python', 75, 'Java', 60]
3.1 Lists - Indexing ['Java', 60, 'R', 90]
3.2 Lists - Slicing ['Python', 75, 'Java', 60]
3.3 Lists - Skip slicing ['R', 90]
3.4 Modifying a list ['Java', 60]
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 11/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 3.3 Lists - Skip slicing
1 Functions
2 Strings In [28]: data = ['Python', 75, 'Java', 60, 'R', 90]
2.1 Strings - Indexing and slic
2.2 Strings - Skip slicing print(data)
2.3 Strings are Immutable print(data[::2])
2.4 Strings - Concatenation O print(data[1::2])
2.5 Strings - membership print(data[:4:2])
2.6 Strings - Iterating
2.7 Strings - other methods print(data[::-1])
2.8 Strings - formatting print(data[::-2])
3 Lists ['Python', 75, 'Java', 60, 'R', 90]
3.1 Lists - Indexing ['Python', 'Java', 'R']
3.2 Lists - Slicing [75, 60, 90]
3.3 Lists - Skip slicing ['Python', 'Java']
3.4 Modifying a list [90, 'R', 60, 'Java', 75, 'Python']
3.5 Concatenation of Lists wi [90, 60, 75]
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 12/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 3.4 Modifying a list
1 Functions
2 Strings In [29]: data = ['Python', 75, 'Java', 60, 'R', 90]
2.1 Strings - Indexing and slic print(data)
2.2 Strings - Skip slicing
2.3 Strings are Immutable data[1] = 99
2.4 Strings - Concatenation O print(data)
2.5 Strings - membership
2.6 Strings - Iterating data[1::2] = [66,77,88]
2.7 Strings - other methods print(data)
2.8 Strings - formatting
3 Lists [Link](['NumPy', 55])
3.1 Lists - Indexing
print(data)
3.2 Lists - Slicing
[Link]('Pandas')
3.3 Lists - Skip slicing
print(data)
3.4 Modifying a list
3.5 Concatenation of Lists wi
[Link](-1, 'TensorFlow')
3.6 Removing items from list
print(data)
3.7 Other List methods
3.8 Copying Lists [Link](-2, 'Keras')
3.9 membership in a list print(data)
3.10 range
3.11 Iterating over lists
['Python', 75, 'Java', 60, 'R', 90]
4 Tuples
['Python', 99, 'Java', 60, 'R', 90]
['Python', 66, 'Java', 77, 'R', 88]
4.1 Tuple Indexing and Slicing
['Python', 66, 'Java', 77, 'R', 88, 'NumPy', 55]
4.2 Concatenation and repeti
['Python', 66, 'Java', 77, 'R', 88, 'NumPy', 55, 'Pandas']
4.3 count and index
['Python', 66, 'Java', 77, 'R', 88, 'NumPy', 55, 'TensorFlow', 'Pan
4.4 membership test
['Python', 66, 'Java', 77, 'R', 88, 'NumPy', 55, 'Keras', 'TensorFl
4.5 Iterating over a tuple
4.6 Other built-in functions w
In [30]: data = ['Python', 75]
5 Lists versus Tuples
6 Lists and List Comprehension [Link]('Java')
6.1 Nested Lists print(data)
7 Dictionary
7.1 Alternative ways of creatin # With indexing
7.2 Modifying and adding item data[len(data):] = [60]
7.3 Deleting items from a dict print(data)
7.4 Membership in a dictiona
7.5 Other dictionary methods data[len(data):] = ['R', 90]
7.6 Dictionary comprehension print(data)
8 Sets
['Python', 75, 'Java']
8.1 Adding elements to a set
['Python', 75, 'Java', 60]
8.2 Removing items from a se ['Python', 75, 'Java', 60, 'R', 90]
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 13/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 3.5 Concatenation of Lists with operators + and *
1 Functions
2 Strings In [31]: skills = ['Python', 'Java', 'R']
2.1 Strings - Indexing and slic levels = [75, 85, 60]
2.2 Strings - Skip slicing
2.3 Strings are Immutable result = skills + levels
2.4 Strings - Concatenation O print(result)
2.5 Strings - membership
2.6 Strings - Iterating print(levels + skills)
2.7 Strings - other methods
2.8 Strings - formatting print(skills + ['Numpy'])
3 Lists
3.1 Lists - Indexing
print(2 * skills)
3.2 Lists - Slicing
print(2 * levels)
3.3 Lists - Skip slicing
3.4 Modifying a list
print(3 * levels)
3.5 Concatenation of Lists wi
3.6 Removing items from list ['Python', 'Java', 'R', 75, 85, 60]
3.7 Other List methods [75, 85, 60, 'Python', 'Java', 'R']
3.8 Copying Lists ['Python', 'Java', 'R', 'Numpy']
3.9 membership in a list ['Python', 'Java', 'R', 'Python', 'Java', 'R']
3.10 range [75, 85, 60, 75, 85, 60]
3.11 Iterating over lists
[75, 85, 60, 75, 85, 60, 75, 85, 60]
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 14/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 3.6 Removing items from list
1 Functions
2 Strings In [32]: data = ['Python', 75, 'Java', 60, 'R', 90, 'Pandas', 100]
2.1 Strings - Indexing and slic print(data)
2.2 Strings - Skip slicing
2.3 Strings are Immutable del data[1::2]
2.4 Strings - Concatenation O print(data)
2.5 Strings - membership ['Python', 75, 'Java', 60, 'R', 90, 'Pandas', 100]
2.6 Strings - Iterating ['Python', 'Java', 'R', 'Pandas']
2.7 Strings - other methods
2.8 Strings - formatting In [33]: data = ['Python', 75, 'Java', 60, 'R', 90, 'Pandas', 100]
3 Lists print(data)
3.1 Lists - Indexing
3.2 Lists - Slicing [Link]('Java')
3.3 Lists - Skip slicing print(data)
3.4 Modifying a list
3.5 Concatenation of Lists wi print([Link]()) # retrieves and removes
3.6 Removing items from list print(data)
3.7 Other List methods
3.8 Copying Lists print([Link](2)) # retrieves and removes at specified index
3.9 membership in a list print(data)
3.10 range
3.11 Iterating over lists [Link]()
4 Tuples print(data)
4.1 Tuple Indexing and Slicing ['Python', 75, 'Java', 60, 'R', 90, 'Pandas', 100]
4.2 Concatenation and repeti ['Python', 75, 60, 'R', 90, 'Pandas', 100]
4.3 count and index 100
4.4 membership test ['Python', 75, 60, 'R', 90, 'Pandas']
4.5 Iterating over a tuple 60
4.6 Other built-in functions w ['Python', 75, 'R', 90, 'Pandas']
5 Lists versus Tuples []
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 15/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 3.7 Other List methods
1 Functions
2 Strings In [34]: data = [10, 15, 20, 15, 7, 15, 40, 15]
2.1 Strings - Indexing and slic print(data)
2.2 Strings - Skip slicing
2.3 Strings are Immutable print([Link](15))
2.4 Strings - Concatenation O print([Link](15))
2.5 Strings - membership print([Link](15, 2))
2.6 Strings - Iterating [10, 15, 20, 15, 7, 15, 40, 15]
2.7 Strings - other methods 4
2.8 Strings - formatting 1
3 Lists 3
3.1 Lists - Indexing
3.2 Lists - Slicing In [35]: data = [10, 20, 15, 7, 40, 15]
3.3 Lists - Skip slicing print(data)
3.4 Modifying a list
3.5 Concatenation of Lists wi [Link]()
3.6 Removing items from list print(data)
3.7 Other List methods
3.8 Copying Lists [Link]()
3.9 membership in a list print(data)
3.10 range
3.11 Iterating over lists data = [10, 20, 50, 100]
4 Tuples [Link](reverse = True)
4.1 Tuple Indexing and Slicing print(data)
4.2 Concatenation and repeti [10, 20, 15, 7, 40, 15]
4.3 count and index [7, 10, 15, 15, 20, 40]
4.4 membership test [40, 20, 15, 15, 10, 7]
4.5 Iterating over a tuple [100, 50, 20, 10]
4.6 Other built-in functions w
5 Lists versus Tuples In [36]: data = [10, 20, 15, 7, 40, 15]
6 Lists and List Comprehension print(data)
6.1 Nested Lists
7 Dictionary print(max(data))
7.1 Alternative ways of creatin print(min(data))
7.2 Modifying and adding item print(sum(data))
7.3 Deleting items from a dict [10, 20, 15, 7, 40, 15]
7.4 Membership in a dictiona 40
7.5 Other dictionary methods 7
7.6 Dictionary comprehension 107
8 Sets
8.1 Adding elements to a set In [37]: data = [10, 20, 15, 7, 40, 15]
8.2 Removing items from a se
8.3 Membership in a set print(data)
8.4 Set Union print(sorted(data))
8.5 Set Intersection
8.6 Set Difference print(data)
8.7 Set Comparison
8.8 Iterating over sets print([Link]()) # in place sort
9 Functions ... continued
9.1 Functions with default arg print(data)
9.2 Functions with variable ar [10, 20, 15, 7, 40, 15]
9.3 Functions with keyword a [7, 10, 15, 15, 20, 40]
9.4 Anonymous (Lambda) Fu [10, 20, 15, 7, 40, 15]
9.5 Indirect references None
[7, 10, 15, 15, 20, 40]
In [38]: data = ['Python', 'Java', 'R', 'Cobol']
[Link]()
print(data)
['Cobol', 'Java', 'Python', 'R']
[Link] 16/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 3.8 Copying Lists
1 Functions
2 Strings In [39]: data1 = [10, 20, 30]
2.1 Strings - Indexing and slic data2 = data1
2.2 Strings - Skip slicing
2.3 Strings are Immutable print(data1)
2.4 Strings - Concatenation O print(data2)
2.5 Strings - membership
2.6 Strings - Iterating data2[0] = [100,111]
2.7 Strings - other methods
2.8 Strings - formatting print(data1)
3 Lists print(data2)
3.1 Lists - Indexing [10, 20, 30]
3.2 Lists - Slicing [10, 20, 30]
3.3 Lists - Skip slicing [[100, 111], 20, 30]
3.4 Modifying a list [[100, 111], 20, 30]
3.5 Concatenation of Lists wi
3.6 Removing items from list In [40]: data1 = [10, 20, 30]
3.7 Other List methods data2 = [Link]() # Shallow copy
3.8 Copying Lists
3.9 membership in a list print(data1)
3.10 range print(data2)
3.11 Iterating over lists
4 Tuples data2[0] = [100,111]
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
print(data1)
print(data2)
4.3 count and index
4.4 membership test [10, 20, 30]
4.5 Iterating over a tuple [10, 20, 30]
4.6 Other built-in functions w [10, 20, 30]
5 Lists versus Tuples [[100, 111], 20, 30]
6 Lists and List Comprehension
6.1 Nested Lists In [41]: data1 = [10, 20, 30]
7 Dictionary data2 = data1[:] # Shallow copy
7.1 Alternative ways of creatin
7.2 Modifying and adding item print(data1)
7.3 Deleting items from a dict print(data2)
7.4 Membership in a dictiona
7.5 Other dictionary methods
data2[0] = [100,111]
7.6 Dictionary comprehension
print(data1)
8 Sets
print(data2)
8.1 Adding elements to a set
8.2 Removing items from a se [10, 20, 30]
8.3 Membership in a set [10, 20, 30]
8.4 Set Union [10, 20, 30]
8.5 Set Intersection [[100, 111], 20, 30]
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 17/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 3.9 membership in a list
1 Functions
2 Strings In [42]: data = ['Python', 75, 'Java', 60, 'R', 90]
2.1 Strings - Indexing and slic print(data)
2.2 Strings - Skip slicing
2.3 Strings are Immutable print('Python' in data)
2.4 Strings - Concatenation O print('NumPy' in data)
2.5 Strings - membership print(60 in data)
2.6 Strings - Iterating print(70 not in data)
2.7 Strings - other methods ['Python', 75, 'Java', 60, 'R', 90]
2.8 Strings - formatting True
3 Lists False
3.1 Lists - Indexing True
3.2 Lists - Slicing True
3.3 Lists - Skip slicing
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 18/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 3.10 range
1 Functions return an immutable sequence of numbers
2 Strings
2.1 Strings - Indexing and slic
2.2 Strings - Skip slicing
2.3 Strings are Immutable
range(stop) - range of numbers from 0 to 𝑠𝑡𝑜𝑝 −1
2.4 Strings - Concatenation O
In [43]: print(range(10))
2.5 Strings - membership
2.6 Strings - Iterating range(0, 10)
2.7 Strings - other methods
2.8 Strings - formatting In [44]: print(list(range(10)))
3 Lists [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3.1 Lists - Indexing
3.2 Lists - Slicing
3.3 Lists - Skip slicing range(start, stop) - range of numbers from start to 𝑠𝑡𝑜𝑝 −1
3.4 Modifying a list
3.5 Concatenation of Lists wi In [45]: print(list(range(0, 10)))
3.6 Removing items from list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3.7 Other List methods
3.8 Copying Lists In [46]: print(list(range(10, 20)))
3.9 membership in a list
3.10 range
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
3.11 Iterating over lists
4 Tuples In [47]: print(list(range(20, 10)))
4.1 Tuple Indexing and Slicing []
4.2 Concatenation and repeti
4.3 count and index
range(start, stop, step)
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w In [48]: print(list(range(10, 20, 2)))
5 Lists versus Tuples [10, 12, 14, 16, 18]
6 Lists and List Comprehension
6.1 Nested Lists In [49]: print(list(range(10, 20, -2)))
7 Dictionary
[]
7.1 Alternative ways of creatin
7.2 Modifying and adding item
In [50]: print(list(range(20, 10, -2)))
7.3 Deleting items from a dict
7.4 Membership in a dictiona [20, 18, 16, 14, 12]
7.5 Other dictionary methods
7.6 Dictionary comprehension In [51]: print(list(range(20, 10, -1)))
8 Sets [20, 19, 18, 17, 16, 15, 14, 13, 12, 11]
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set Iterating using range
8.4 Set Union
8.5 Set Intersection In [52]: for i in range(10, 20):
8.6 Set Difference print('Square of', i, 'is', i*i)
8.7 Set Comparison Square of 10 is 100
8.8 Iterating over sets Square of 11 is 121
9 Functions ... continued Square of 12 is 144
9.1 Functions with default arg Square of 13 is 169
9.2 Functions with variable ar Square of 14 is 196
9.3 Functions with keyword a Square of 15 is 225
9.4 Anonymous (Lambda) Fu Square of 16 is 256
9.5 Indirect references Square of 17 is 289
Square of 18 is 324
Square of 19 is 361
In [53]: skills = ['Python', 'Java', 'R']
for i in range(len(skills)):
print('Skill#{} is {}'.format(i+1, skills[i]))
Skill#1 is Python
Skill#2 is Java
Skill#3 is R
[Link] 19/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 3.11 Iterating over lists
1 Functions
2 Strings In [54]: skills = ['Python', 'Java', 'R']
2.1 Strings - Indexing and slic levels = [75, 85, 60]
2.2 Strings - Skip slicing
2.3 Strings are Immutable for skill in skills:
2.4 Strings - Concatenation O print(skill)
2.5 Strings - membership Python
2.6 Strings - Iterating Java
2.7 Strings - other methods R
2.8 Strings - formatting
3 Lists In [55]: for index, skill in enumerate(skills):
3.1 Lists - Indexing print(skill, '-->', levels[index])
3.2 Lists - Slicing
Python --> 75
3.3 Lists - Skip slicing
Java --> 85
3.4 Modifying a list
R --> 60
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods zip - returns an iterator of tuples
3.8 Copying Lists
3.9 membership in a list In [56]: print(list(zip(skills, levels)))
3.10 range
[('Python', 75), ('Java', 85), ('R', 60)]
3.11 Iterating over lists
4 Tuples
In [57]: for skill, level in zip(skills, levels):
4.1 Tuple Indexing and Slicing
print(skill, '-->', level)
4.2 Concatenation and repeti
4.3 count and index Python --> 75
4.4 membership test Java --> 85
4.5 Iterating over a tuple R --> 60
4.6 Other built-in functions w
5 Lists versus Tuples unzip
6 Lists and List Comprehension
6.1 Nested Lists In [58]: data = [('Python', 75), ('Java', 85), ('R', 60)]
7 Dictionary
7.1 Alternative ways of creatin skills, levels = zip(*data)
7.2 Modifying and adding item
7.3 Deleting items from a dict print(skills)
7.4 Membership in a dictiona print(levels)
7.5 Other dictionary methods
('Python', 'Java', 'R')
7.6 Dictionary comprehension
(75, 85, 60)
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se out of range index
8.3 Membership in a set
8.4 Set Union In [59]: skills = ['Python', 'Java', 'R']
8.5 Set Intersection skills[3]
8.6 Set Difference -------------------------------------------------------------------
8.7 Set Comparison IndexError Traceback (most recent ca
8.8 Iterating over sets <ipython-input-59-85142c8d05c3> in <module>
9 Functions ... continued 1 skills = ['Python', 'Java', 'R']
9.1 Functions with default arg ----> 2 skills[3]
9.2 Functions with variable ar
9.3 Functions with keyword a IndexError: list index out of range
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
4 Tuples
a sequence of values separated by a comma
In [60]: x = (10, 20, 30)
print(x)
print(len(x))
y = ('Alice', (10, 20), [30, 40], (50, 60))
print(y)
print(len(y))
z = 100, 'Python', 80, 'R'
print(z)
(10, 20, 30)
3
('Alice', (10, 20), [30, 40], (50, 60))
4
(100, 'Python', 80, 'R')
[Link] 20/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 4.1 Tuple Indexing and Slicing - similar to list
1 Functions
2 Strings In [61]: x = (10, 20, 30)
2.1 Strings - Indexing and slic y = ('Alice', (10, 20), [30, 40], (50, 60))
2.2 Strings - Skip slicing
2.3 Strings are Immutable print(x)
2.4 Strings - Concatenation O print(x[0], x[-1])
2.5 Strings - membership
2.6 Strings - Iterating print(y)
2.7 Strings - other methods print(y[1:3])
2.8 Strings - formatting print(y[::-1])
3 Lists (10, 20, 30)
3.1 Lists - Indexing 10 30
3.2 Lists - Slicing ('Alice', (10, 20), [30, 40], (50, 60))
3.3 Lists - Skip slicing ((10, 20), [30, 40])
3.4 Modifying a list ((50, 60), [30, 40], (10, 20), 'Alice')
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 21/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 4.2 Concatenation and repetition
1 Functions
2 Strings In [62]: x = (10, 20, 30)
2.1 Strings - Indexing and slic y = ('Alice', (10, 20), [30, 40], (50, 60))
2.2 Strings - Skip slicing
2.3 Strings are Immutable print(x + y)
2.4 Strings - Concatenation O (10, 20, 30, 'Alice', (10, 20), [30, 40], (50, 60))
2.5 Strings - membership
2.6 Strings - Iterating In [63]: x = (10, 20, 30)
2.7 Strings - other methods y = ('Alice', (10, 20), [30, 40], (50, 60))
2.8 Strings - formatting
3 Lists print(x * 3)
3.1 Lists - Indexing print(2 * y)
3.2 Lists - Slicing
(10, 20, 30, 10, 20, 30, 10, 20, 30)
3.3 Lists - Skip slicing
('Alice', (10, 20), [30, 40], (50, 60), 'Alice', (10, 20), [30, 40]
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 22/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 4.3 count and index
1 Functions
2 Strings In [64]: x = (10, 20, 30)
2.1 Strings - Indexing and slic z = x * 3
2.2 Strings - Skip slicing
2.3 Strings are Immutable print(z)
2.4 Strings - Concatenation O (10, 20, 30, 10, 20, 30, 10, 20, 30)
2.5 Strings - membership
2.6 Strings - Iterating In [65]: [Link](30)
2.7 Strings - other methods
2.8 Strings - formatting
Out[65]:3
3 Lists
3.1 Lists - Indexing In [66]: [Link](40)
3.2 Lists - Slicing Out[66]:0
3.3 Lists - Skip slicing
3.4 Modifying a list In [67]: [Link](30)
3.5 Concatenation of Lists wi
Out[67]:2
3.6 Removing items from list
3.7 Other List methods
In [68]: [Link](30, 3)
3.8 Copying Lists
3.9 membership in a list Out[68]:5
3.10 range
3.11 Iterating over lists In [69]: [Link](40)
4 Tuples
-------------------------------------------------------------------
4.1 Tuple Indexing and Slicing ValueError Traceback (most recent ca
4.2 Concatenation and repeti <ipython-input-69-0c24aa2375fa> in <module>
4.3 count and index ----> 1 [Link](40)
4.4 membership test
4.5 Iterating over a tuple ValueError: [Link](x): x not in tuple
4.6 Other built-in functions w
5 Lists versus Tuples In [ ]:
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 23/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 4.4 membership test
1 Functions
2 Strings In [70]: z = 100, 'Python', 80, 'R'
2.1 Strings - Indexing and slic print(z)
2.2 Strings - Skip slicing
2.3 Strings are Immutable print(80 in z)
2.4 Strings - Concatenation O print('Python' in z)
2.5 Strings - membership print('Java' not in z)
2.6 Strings - Iterating (100, 'Python', 80, 'R')
2.7 Strings - other methods True
2.8 Strings - formatting True
3 Lists True
3.1 Lists - Indexing
3.2 Lists - Slicing
3.3 Lists - Skip slicing
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 24/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 4.5 Iterating over a tuple
1 Functions
2 Strings In [71]: x = (10, 20, 30)
2.1 Strings - Indexing and slic
2.2 Strings - Skip slicing for value in x:
2.3 Strings are Immutable print(value, value*value)
2.4 Strings - Concatenation O 10 100
2.5 Strings - membership 20 400
2.6 Strings - Iterating 30 900
2.7 Strings - other methods
2.8 Strings - formatting In [72]: for index, value in enumerate(x):
3 Lists print(index, value)
3.1 Lists - Indexing
3.2 Lists - Slicing
0 10
1 20
3.3 Lists - Skip slicing
2 30
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 25/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 4.6 Other built-in functions with tuple
1 Functions
2 Strings In [73]: x = (10, 20, 30)
2.1 Strings - Indexing and slic y = ('R', 'Java', 'Python')
2.2 Strings - Skip slicing
2.3 Strings are Immutable In [74]: (min(x), max(x), sum(x))
2.4 Strings - Concatenation O
2.5 Strings - membership Out[74]:(10, 30, 60)
2.6 Strings - Iterating
2.7 Strings - other methods In [75]: min(y), max(y)
2.8 Strings - formatting Out[75]:('Java', 'R')
3 Lists
3.1 Lists - Indexing In [76]: sorted(x, reverse = True)
3.2 Lists - Slicing
Out[76]:[30, 20, 10]
3.3 Lists - Skip slicing
3.4 Modifying a list
In [77]: sorted(y)
3.5 Concatenation of Lists wi
3.6 Removing items from list Out[77]:['Java', 'Python', 'R']
3.7 Other List methods
3.8 Copying Lists In [78]: all(x)
3.9 membership in a list Out[78]:True
3.10 range
3.11 Iterating over lists
In [79]: y = (True, False, True)
4 Tuples
4.1 Tuple Indexing and Slicing print(all(y))
4.2 Concatenation and repeti print(any(y))
4.3 count and index
4.4 membership test False
4.5 Iterating over a tuple
True
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
5 Lists versus Tuples
6.1 Nested Lists
7 Dictionary In [80]: list_values = [10, 20, 30, 40]
7.1 Alternative ways of creatin tuple_values = (10, 20, 30, 40)
7.2 Modifying and adding item
7.3 Deleting items from a dict print(list_values)
7.4 Membership in a dictiona print(tuple_values)
7.5 Other dictionary methods [10, 20, 30, 40]
7.6 Dictionary comprehension (10, 20, 30, 40)
8 Sets
8.1 Adding elements to a set In [81]: print(len(list_values))
8.2 Removing items from a se print(len(tuple_values))
8.3 Membership in a set
4
8.4 Set Union
4
8.5 Set Intersection
8.6 Set Difference
In [82]: print(list_values.__sizeof__())
8.7 Set Comparison
print(tuple_values.__sizeof__())
8.8 Iterating over sets
9 Functions ... continued 72
9.1 Functions with default arg 56
9.2 Functions with variable ar
9.3 Functions with keyword a In [83]: # Mutable versus Immutable
9.4 Anonymous (Lambda) Fu
9.5 Indirect references list_values[-1] = 400
print(list_values)
[10, 20, 30, 400]
In [84]: tuple_values[-1] = 400
-------------------------------------------------------------------
TypeError Traceback (most recent ca
<ipython-input-84-31d12efbfa96> in <module>
----> 1 tuple_values[-1] = 400
TypeError: 'tuple' object does not support item assignment
In [85]: list_values.append(50)
print(list_values)
[10, 20, 30, 400, 50]
In [86]: tuple_values.append(50)
-------------------------------------------------------------------
AttributeError Traceback (most recent ca
<ipython-input-86-821316f2950c> in <module>
----> 1 tuple_values.append(50)
AttributeError: 'tuple' object has no attribute 'append'
[Link] 26/50
2025/5/3 21:28 M1_01_Intro_Python
Contents In [87]: y = ('Alice', (10, 20), [30, 40], (50, 60))
1 Functions y[2][0] = 300
2 Strings print(y)
2.1 Strings - Indexing and slic ('Alice', (10, 20), [300, 40], (50, 60))
2.2 Strings - Skip slicing
2.3 Strings are Immutable
2.4 Strings - Concatenation O 6 Lists and List Comprehensions
2.5 Strings - membership
2.6 Strings - Iterating
In [88]: word = 'Data Science'
2.7 Strings - other methods
2.8 Strings - formatting
letters = list(word)
3 Lists
3.1 Lists - Indexing print(letters)
3.2 Lists - Slicing
3.3 Lists - Skip slicing ['D', 'a', 't', 'a', ' ', 'S', 'c', 'i', 'e', 'n', 'c', 'e']
3.4 Modifying a list
3.5 Concatenation of Lists wi In [89]: # Using loop
3.6 Removing items from list
3.7 Other List methods
word = 'Data Science'
letters = []
3.8 Copying Lists
3.9 membership in a list
for letter in word:
3.10 range
[Link](letter)
3.11 Iterating over lists
4 Tuples
print(letters)
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti ['D', 'a', 't', 'a', ' ', 'S', 'c', 'i', 'e', 'n', 'c', 'e']
4.3 count and index
4.4 membership test In [90]: # Using list comprehension
4.5 Iterating over a tuple
4.6 Other built-in functions w word = 'Data Science'
5 Lists versus Tuples
6 Lists and List Comprehension
letters = [letter for letter in word]
6.1 Nested Lists
print(letters)
7 Dictionary
7.1 Alternative ways of creatin ['D', 'a', 't', 'a', ' ', 'S', 'c', 'i', 'e', 'n', 'c', 'e']
7.2 Modifying and adding item
7.3 Deleting items from a dict In [91]: # Using Lambda function
7.4 Membership in a dictiona
7.5 Other dictionary methods word = 'Data Science'
7.6 Dictionary comprehension
8 Sets letters = list(map(lambda x: x, word))
8.1 Adding elements to a set
8.2 Removing items from a se print(letters)
8.3 Membership in a set ['D', 'a', 't', 'a', ' ', 'S', 'c', 'i', 'e', 'n', 'c', 'e']
8.4 Set Union
8.5 Set Intersection In [92]: # Conditions in List Comprehension
8.6 Set Difference
8.7 Set Comparison word = 'Data Science'
8.8 Iterating over sets
9 Functions ... continued letters = [letter for letter in word if letter not in 'aeiou']
9.1 Functions with default arg
9.2 Functions with variable ar print(letters)
9.3 Functions with keyword a ['D', 't', ' ', 'S', 'c', 'n', 'c']
9.4 Anonymous (Lambda) Fu
9.5 Indirect references In [93]: data = [70, 30, 50, 80, 40, 90]
result = [score for score in data if score > 50]
print(result)
[70, 80, 90]
In [94]: data = [70, 30, 50, 80, 40, 90]
result = [1.1*score for score in data if score > 50]
print(result)
[77.0, 88.0, 99.00000000000001]
In [95]: data = [70, 30, 50, 80, 40, 90]
result = [round(1.1*score) for score in data if score > 50]
print(result)
[77, 88, 99]
[Link] 27/50
2025/5/3 21:28 M1_01_Intro_Python
Contents In [96]: # if-else
1 Functions
2 Strings word = 'Data Science'
2.1 Strings - Indexing and slic
2.2 Strings - Skip slicing
vowel_or_consonant = ['V' if letter in 'aeiou' else 'C' for letter
2.3 Strings are Immutable
print(vowel_or_consonant)
2.4 Strings - Concatenation O
2.5 Strings - membership ['C', 'V', 'C', 'V', 'C', 'C', 'C', 'V', 'V', 'C', 'C', 'V']
2.6 Strings - Iterating
2.7 Strings - other methods
2.8 Strings - formatting
3 Lists
3.1 Lists - Indexing
3.2 Lists - Slicing
3.3 Lists - Skip slicing
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 28/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 6.1 Nested Lists
1 Functions
2 Strings In [97]: # Using loops
2.1 Strings - Indexing and slic
2.2 Strings - Skip slicing result = []
2.3 Strings are Immutable for i in range(1,6):
2.4 Strings - Concatenation O new_row = []
2.5 Strings - membership for j in range(1,6):
2.6 Strings - Iterating new_row.append((i,j))
2.7 Strings - other methods [Link](new_row)
2.8 Strings - formatting
3 Lists result
3.1 Lists - Indexing Out[97]:[[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5)],
3.2 Lists - Slicing [(2, 1), (2, 2), (2, 3), (2, 4), (2, 5)],
3.3 Lists - Skip slicing [(3, 1), (3, 2), (3, 3), (3, 4), (3, 5)],
3.4 Modifying a list [(4, 1), (4, 2), (4, 3), (4, 4), (4, 5)],
3.5 Concatenation of Lists wi [(5, 1), (5, 2), (5, 3), (5, 4), (5, 5)]]
3.6 Removing items from list
3.7 Other List methods In [98]: # Using list comprehension
3.8 Copying Lists
3.9 membership in a list result = [[(i,j) for j in range(1, 6)] for i in range(1, 6)]
3.10 range
3.11 Iterating over lists result
4 Tuples Out[98]:[[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5)],
4.1 Tuple Indexing and Slicing [(2, 1), (2, 2), (2, 3), (2, 4), (2, 5)],
4.2 Concatenation and repeti [(3, 1), (3, 2), (3, 3), (3, 4), (3, 5)],
4.3 count and index [(4, 1), (4, 2), (4, 3), (4, 4), (4, 5)],
4.4 membership test [(5, 1), (5, 2), (5, 3), (5, 4), (5, 5)]]
4.5 Iterating over a tuple
4.6 Other built-in functions w In [99]: [[(10*i,j) for j in range(1, i+1)] for i in range(1, 6)]
5 Lists versus Tuples Out[99]:[[(10, 1)],
6 Lists and List Comprehension [(20, 1), (20, 2)],
6.1 Nested Lists [(30, 1), (30, 2), (30, 3)],
7 Dictionary [(40, 1), (40, 2), (40, 3), (40, 4)],
7.1 Alternative ways of creatin [(50, 1), (50, 2), (50, 3), (50, 4), (50, 5)]]
7.2 Modifying and adding item
7.3 Deleting items from a dict In [100]: # flatten list
7.4 Membership in a dictiona
7.5 Other dictionary methods data = [[1,2,3], [4], [5,6]]
7.6 Dictionary comprehension [val for row in data for val in row]
8 Sets
Out[100]:[1, 2, 3, 4, 5, 6]
8.1 Adding elements to a set
8.2 Removing items from a se
In [ ]:
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection In [101]: # print 2D lists
8.6 Set Difference
8.7 Set Comparison def print2D(x):
8.8 Iterating over sets for row in x:
9 Functions ... continued for value in row:
9.1 Functions with default arg print('{:4}'.format(value), end = '')
9.2 Functions with variable ar
print()
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu In [102]: # Nested loops - matrix transpose using loops
9.5 Indirect references
data = [[1,2,3], [4,5,6]]
print2D(data)
result = []
for col in range(len(data[0])):
new_row = []
for row in data:
new_row.append(row[col])
[Link](new_row)
print("Transpose is", result)
print2D(result)
1 2 3
4 5 6
Transpose is [[1, 4], [2, 5], [3, 6]]
1 4
2 5
3 6
[Link] 29/50
2025/5/3 21:28 M1_01_Intro_Python
Contents In [103]: # Using list comprehension
1 Functions
2 Strings data = [[1,2,3], [4,5,6]]
2.1 Strings - Indexing and slic
print2D(data)
2.2 Strings - Skip slicing
result = [ [row[col] for row in data] for col in range(len(data[0]
2.3 Strings are Immutable
2.4 Strings - Concatenation O
print("Transpose is", result)
2.5 Strings - membership
print2D(result)
2.6 Strings - Iterating
2.7 Strings - other methods 1 2 3
2.8 Strings - formatting 4 5 6
3 Lists Transpose is [[1, 4], [2, 5], [3, 6]]
3.1 Lists - Indexing 1 4
3.2 Lists - Slicing 2 5
3.3 Lists - Skip slicing
3 6
3.4 Modifying a list
3.5 Concatenation of Lists wi In [104]: # Using list comprehension
3.6 Removing items from list
def print2D(x):
3.7 Other List methods
print('\n'.join([''.join(['{:4}'.format(item) for item in row]
3.8 Copying Lists
for row in x]))
3.9 membership in a list
3.10 range
print2D(data)
3.11 Iterating over lists
4 Tuples print("Transpose is")
4.1 Tuple Indexing and Slicing print2D(result)
4.2 Concatenation and repeti
4.3 count and index 1 2 3
4.4 membership test 4 5 6
Transpose is
4.5 Iterating over a tuple
1 4
4.6 Other built-in functions w
2 5
5 Lists versus Tuples
3 6
6 Lists and List Comprehension
6.1 Nested Lists
In [105]: print2D([[i*j for j in range(1, 11)] for i in range(1, 11)])
7 Dictionary
7.1 Alternative ways of creatin 1 2 3 4 5 6 7 8 9 10
7.2 Modifying and adding item 2 4 6 8 10 12 14 16 18 20
7.3 Deleting items from a dict 3 6 9 12 15 18 21 24 27 30
7.4 Membership in a dictiona 4 8 12 16 20 24 28 32 36 40
7.5 Other dictionary methods 5 10 15 20 25 30 35 40 45 50
7.6 Dictionary comprehension 6 12 18 24 30 36 42 48 54 60
8 Sets
7 14 21 28 35 42 49 56 63 70
8.1 Adding elements to a set
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
8.2 Removing items from a se
10 20 30 40 50 60 70 80 90 100
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
7 Dictionary
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets unordered collection of key-value pairs
9 Functions ... continued
indexed by keys
9.1 Functions with default arg
can be any immutable type - strings, numbers, tuples with only strings and numbers
9.2 Functions with variable ar
empty dictionary - {}
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references In [106]: d1 = {}
d2 = {1: 'Python', 2: 'Java'}
d3 = {'Java': ['CS520', 'CS526'], 'Python': ['CS521', 'CS677', 'CS
print(d1)
print(d2)
print(d3)
{}
{1: 'Python', 2: 'Java'}
{'Java': ['CS520', 'CS526'], 'Python': ['CS521', 'CS677', 'CS767']}
The dict() constructor builds dictionaries directly from sequences of key-value pairs
In [107]: d2 = dict({1: 'Python', 2: 'Java'})
d3 = dict([ ('Java', ['CS520', 'CS526']), ('Python', ['CS521', 'CS
print(d2)
print(d3)
{1: 'Python', 2: 'Java'}
{'Java': ['CS520', 'CS526'], 'Python': ['CS521', 'CS677', 'CS767']}
[Link] 30/50
2025/5/3 21:28 M1_01_Intro_Python
Contents In [108]: print(d2[1])
1 Functions print(d3['Python'])
2 Strings print(d3[d2[1]])
2.1 Strings - Indexing and slic Python
2.2 Strings - Skip slicing ['CS521', 'CS677', 'CS767']
2.3 Strings are Immutable ['CS521', 'CS677', 'CS767']
2.4 Strings - Concatenation O
2.5 Strings - membership In [109]: d3['R']
2.6 Strings - Iterating
-------------------------------------------------------------------
2.7 Strings - other methods
KeyError Traceback (most recent ca
2.8 Strings - formatting <ipython-input-109-1860c03d820e> in <module>
3 Lists ----> 1 d3['R']
3.1 Lists - Indexing
3.2 Lists - Slicing KeyError: 'R'
3.3 Lists - Skip slicing
3.4 Modifying a list In [110]: print(d2)
3.5 Concatenation of Lists wi print(d3)
3.6 Removing items from list print()
3.7 Other List methods
3.8 Copying Lists print([Link](1))
3.9 membership in a list print([Link]('Python'))
3.10 range print([Link]([Link](1)))
3.11 Iterating over lists {1: 'Python', 2: 'Java'}
4 Tuples {'Java': ['CS520', 'CS526'], 'Python': ['CS521', 'CS677', 'CS767']}
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti Python
4.3 count and index ['CS521', 'CS677', 'CS767']
4.4 membership test ['CS521', 'CS677', 'CS767']
4.5 Iterating over a tuple
4.6 Other built-in functions w In [111]: print([Link]('R'))
5 Lists versus Tuples
None
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 31/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 7.1 Alternative ways of creating dictionaries
1 Functions
2 Strings In [112]: a = dict(one=1, two=2, three=3)
2.1 Strings - Indexing and slic b = {'one': 1, 'two': 2, 'three': 3}
2.2 Strings - Skip slicing c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
2.3 Strings are Immutable d = dict([('two', 2), ('one', 1), ('three', 3)])
2.4 Strings - Concatenation O e = dict({'three': 3, 'one': 1, 'two': 2})
2.5 Strings - membership
2.6 Strings - Iterating print(a)
2.7 Strings - other methods
2.8 Strings - formatting print(a == b == c == d == e)
3 Lists {'one': 1, 'two': 2, 'three': 3}
3.1 Lists - Indexing True
3.2 Lists - Slicing
3.3 Lists - Skip slicing
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 32/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 7.2 Modifying and adding items in a dictionary
1 Functions
2 Strings In [113]: d2 = dict({1: 'Python', 2: 'Java'})
2.1 Strings - Indexing and slic print(d2)
2.2 Strings - Skip slicing
2.3 Strings are Immutable d2[2] = 'C++'
2.4 Strings - Concatenation O d2[5] = 'R'
2.5 Strings - membership d2['a'] = 'Go'
2.6 Strings - Iterating
2.7 Strings - other methods print(d2)
2.8 Strings - formatting {1: 'Python', 2: 'Java'}
3 Lists {1: 'Python', 2: 'C++', 5: 'R', 'a': 'Go'}
3.1 Lists - Indexing
3.2 Lists - Slicing
3.3 Lists - Skip slicing
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 33/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 7.3 Deleting items from a dictionary
1 Functions
2 Strings In [114]: print(d2)
2.1 Strings - Indexing and slic
2.2 Strings - Skip slicing print([Link](5))
2.3 Strings are Immutable print(d2)
2.4 Strings - Concatenation O
2.5 Strings - membership del d2['a']
2.6 Strings - Iterating print(d2)
2.7 Strings - other methods {1: 'Python', 2: 'C++', 5: 'R', 'a': 'Go'}
2.8 Strings - formatting R
3 Lists {1: 'Python', 2: 'C++', 'a': 'Go'}
3.1 Lists - Indexing {1: 'Python', 2: 'C++'}
3.2 Lists - Slicing
3.3 Lists - Skip slicing
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 34/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 7.4 Membership in a dictionary
1 Functions
2 Strings In [115]: d3 = dict([ ('Java', ['CS520', 'CS526']), ('Python', ['CS521', 'CS
2.1 Strings - Indexing and slic print(d3)
2.2 Strings - Skip slicing
2.3 Strings are Immutable print('Java' in d3)
2.4 Strings - Concatenation O print('R' not in d3)
2.5 Strings - membership print('C++' in d3)
2.6 Strings - Iterating {'Java': ['CS520', 'CS526'], 'Python': ['CS521', 'CS677', 'CS767']}
2.7 Strings - other methods True
2.8 Strings - formatting True
3 Lists False
3.1 Lists - Indexing
3.2 Lists - Slicing
3.3 Lists - Skip slicing
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 35/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 7.5 Other dictionary methods
1 Functions keys
2 Strings
2.1 Strings - Indexing and slic In [116]: print(d3)
2.2 Strings - Skip slicing print([Link]())
2.3 Strings are Immutable
{'Java': ['CS520', 'CS526'], 'Python': ['CS521', 'CS677', 'CS767']}
2.4 Strings - Concatenation O
dict_keys(['Java', 'Python'])
2.5 Strings - membership
2.6 Strings - Iterating
In [117]: for item in d3:
2.7 Strings - other methods
print(item)
2.8 Strings - formatting
3 Lists Java
3.1 Lists - Indexing Python
3.2 Lists - Slicing
3.3 Lists - Skip slicing In [118]: for item in [Link]():
3.4 Modifying a list print(item)
3.5 Concatenation of Lists wi Java
3.6 Removing items from list Python
3.7 Other List methods
3.8 Copying Lists In [119]: for index, item in enumerate(d3):
3.9 membership in a list print(index, item)
3.10 range
0 Java
3.11 Iterating over lists
1 Python
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti values
4.3 count and index
4.4 membership test In [120]: print([Link]())
4.5 Iterating over a tuple dict_values([['CS520', 'CS526'], ['CS521', 'CS677', 'CS767']])
4.6 Other built-in functions w
5 Lists versus Tuples
In [121]: for item in [Link]():
6 Lists and List Comprehension print(item)
6.1 Nested Lists
7 Dictionary ['CS520', 'CS526']
7.1 Alternative ways of creatin ['CS521', 'CS677', 'CS767']
7.2 Modifying and adding item
7.3 Deleting items from a dict items
7.4 Membership in a dictiona
7.5 Other dictionary methods In [122]: print([Link]())
7.6 Dictionary comprehension
8 Sets
dict_items([('Java', ['CS520', 'CS526']), ('Python', ['CS521', 'CS6
8.1 Adding elements to a set
8.2 Removing items from a se In [123]: for key, value in [Link]():
8.3 Membership in a set
print('{:>8} --> {}'.format(key, value))
8.4 Set Union Java --> ['CS520', 'CS526']
8.5 Set Intersection Python --> ['CS521', 'CS677', 'CS767']
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 36/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 7.6 Dictionary comprehension
1 Functions
2 Strings In [124]: powers = {i: 2**i for i in range(10)}
2.1 Strings - Indexing and slic print(powers)
2.2 Strings - Skip slicing print(powers[8])
2.3 Strings are Immutable {0: 1, 1: 2, 2: 4, 3: 8, 4: 16, 5: 32, 6: 64, 7: 128, 8: 256, 9: 51
2.4 Strings - Concatenation O 256
2.5 Strings - membership
2.6 Strings - Iterating In [125]: # Without comprehension
2.7 Strings - other methods
2.8 Strings - formatting powers = {}
3 Lists for i in range(10):
3.1 Lists - Indexing powers[i] = 2**i
3.2 Lists - Slicing
3.3 Lists - Skip slicing print(powers)
3.4 Modifying a list
{0: 1, 1: 2, 2: 4, 3: 8, 4: 16, 5: 32, 6: 64, 7: 128, 8: 256, 9: 51
3.5 Concatenation of Lists wi
3.6 Removing items from list
In [126]: # filtering with if
3.7 Other List methods
3.8 Copying Lists
odd_powers = {i: 2**i for i in range(10) if i%2 == 1}
3.9 membership in a list
print(odd_powers)
3.10 range
3.11 Iterating over lists {1: 2, 3: 8, 5: 32, 7: 128, 9: 512}
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti 8 Sets
4.3 count and index
unordered collection of items
4.4 membership test
every item is unique
4.5 Iterating over a tuple
4.6 Other built-in functions w
every item is immutable
5 Lists versus Tuples
6 Lists and List Comprehension In [127]: s1 = set() # {} is an empty dictionary
6.1 Nested Lists s2 = {10, 20, 10, 30}
7 Dictionary s3 = set([10, 20, 30, 10, 20])
7.1 Alternative ways of creatin print(s1)
7.2 Modifying and adding item print(s2)
7.3 Deleting items from a dict print(s3)
7.4 Membership in a dictiona set()
7.5 Other dictionary methods {10, 20, 30}
7.6 Dictionary comprehension {10, 20, 30}
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se no mutable items in a set
8.3 Membership in a set
8.4 Set Union In [128]: s3 = {10, 20, [10, 30]}
8.5 Set Intersection -------------------------------------------------------------------
8.6 Set Difference TypeError Traceback (most recent ca
8.7 Set Comparison <ipython-input-128-139e283bd3ac> in <module>
8.8 Iterating over sets ----> 1 s3 = {10, 20, [10, 30]}
9 Functions ... continued
9.1 Functions with default arg TypeError: unhashable type: 'list'
9.2 Functions with variable ar
9.3 Functions with keyword a
no indexing support
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
In [129]: print(s2)
s2[0]
{10, 20, 30}
-------------------------------------------------------------------
TypeError Traceback (most recent ca
<ipython-input-129-68ce638a5d92> in <module>
1 print(s2)
----> 2 s2[0]
TypeError: 'set' object is not subscriptable
[Link] 37/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 8.1 Adding elements to a set
1 Functions
2 Strings In [130]: s2 = {10, 20, 10, 30}
2.1 Strings - Indexing and slic print(s2)
2.2 Strings - Skip slicing
2.3 Strings are Immutable [Link](40)
2.4 Strings - Concatenation O print(s2)
2.5 Strings - membership
2.6 Strings - Iterating [Link]((40, 45))
2.7 Strings - other methods print(s2)
2.8 Strings - formatting {10, 20, 30}
3 Lists {40, 10, 20, 30}
3.1 Lists - Indexing {40, 10, (40, 45), 20, 30}
3.2 Lists - Slicing
3.3 Lists - Skip slicing In [131]: s2 = {10, 20, 10, 30}
3.4 Modifying a list print(s2)
3.5 Concatenation of Lists wi
3.6 Removing items from list [Link]([20,30,40,50])
3.7 Other List methods print(s2)
3.8 Copying Lists {10, 20, 30}
3.9 membership in a list {40, 10, 50, 20, 30}
3.10 range
3.11 Iterating over lists In [132]: s2 = {10, 20, 10, 30}
4 Tuples print(s2)
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti [Link]({10, 15, 25}, [20,30,40,50])
4.3 count and index print(s2)
4.4 membership test
{10, 20, 30}
4.5 Iterating over a tuple
{50, 20, 40, 25, 10, 30, 15}
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 38/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 8.2 Removing items from a set
1 Functions
2 Strings In [133]: s2 = {10, 20, 10, 30}
2.1 Strings - Indexing and slic print(s2)
2.2 Strings - Skip slicing
2.3 Strings are Immutable [Link](10)
2.4 Strings - Concatenation O print(s2)
2.5 Strings - membership {10, 20, 30}
2.6 Strings - Iterating {20, 30}
2.7 Strings - other methods
2.8 Strings - formatting
3 Lists
3.1 Lists - Indexing
3.2 Lists - Slicing
3.3 Lists - Skip slicing
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 39/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 8.3 Membership in a set
1 Functions
2 Strings In [134]: s2 = {10, 20, 10, 30}
2.1 Strings - Indexing and slic print(s2)
2.2 Strings - Skip slicing
2.3 Strings are Immutable print(10 in s2)
2.4 Strings - Concatenation O print(40 in s2)
2.5 Strings - membership print(50 not in s2)
2.6 Strings - Iterating {10, 20, 30}
2.7 Strings - other methods True
2.8 Strings - formatting False
3 Lists True
3.1 Lists - Indexing
3.2 Lists - Slicing
3.3 Lists - Skip slicing
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 40/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 8.4 Set Union
1 Functions
2 Strings In [135]: a = {10, 20, 30, 40}
2.1 Strings - Indexing and slic b = {30, 40, 50}
2.2 Strings - Skip slicing
2.3 Strings are Immutable # Union
2.4 Strings - Concatenation O
2.5 Strings - membership print(a | b)
2.6 Strings - Iterating
2.7 Strings - other methods print([Link](b))
2.8 Strings - formatting
3 Lists print([Link](a))
3.1 Lists - Indexing {50, 20, 40, 10, 30}
3.2 Lists - Slicing {50, 20, 40, 10, 30}
3.3 Lists - Skip slicing {50, 20, 40, 10, 30}
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 41/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 8.5 Set Intersection
1 Functions
2 Strings In [136]: a = {10, 20, 30, 40}
2.1 Strings - Indexing and slic b = {30, 40, 50}
2.2 Strings - Skip slicing
2.3 Strings are Immutable # Intersection
2.4 Strings - Concatenation O
2.5 Strings - membership print(a & b)
2.6 Strings - Iterating
2.7 Strings - other methods print([Link](b))
2.8 Strings - formatting
3 Lists print([Link](a))
3.1 Lists - Indexing {40, 30}
3.2 Lists - Slicing {40, 30}
3.3 Lists - Skip slicing {40, 30}
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 42/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 8.6 Set Difference
1 Functions
2 Strings In [137]: a = {10, 20, 30, 40}
2.1 Strings - Indexing and slic b = {30, 40, 50}
2.2 Strings - Skip slicing
2.3 Strings are Immutable # Difference
2.4 Strings - Concatenation O
2.5 Strings - membership print(a - b)
2.6 Strings - Iterating print([Link](b))
2.7 Strings - other methods
2.8 Strings - formatting print(b - a)
3 Lists print([Link](a))
3.1 Lists - Indexing {10, 20}
3.2 Lists - Slicing {10, 20}
3.3 Lists - Skip slicing {50}
3.4 Modifying a list {50}
3.5 Concatenation of Lists wi
3.6 Removing items from list
Symmetric difference (except those that are common in both)
3.7 Other List methods
3.8 Copying Lists
In [138]: a = {10, 20, 30, 40}
3.9 membership in a list
b = {30, 40, 50}
3.10 range
3.11 Iterating over lists
# Symmetric Difference
4 Tuples
4.1 Tuple Indexing and Slicing print(a ^ b)
4.2 Concatenation and repeti print(a.symmetric_difference(b))
4.3 count and index
4.4 membership test {10, 50, 20}
{10, 50, 20}
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 43/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 8.7 Set Comparison
1 Functions
2 Strings In [139]: a = {10, 20, 30, 40}
2.1 Strings - Indexing and slic b = {30, 40, 50}
2.2 Strings - Skip slicing
2.3 Strings are Immutable print([Link](b))
2.4 Strings - Concatenation O False
2.5 Strings - membership
2.6 Strings - Iterating In [140]: a = {10, 20, 30, 40}
2.7 Strings - other methods b = {50, 60}
2.8 Strings - formatting
3 Lists print([Link](b))
3.1 Lists - Indexing
3.2 Lists - Slicing
True
3.3 Lists - Skip slicing
In [141]: a = {10, 20}
3.4 Modifying a list
b = {10, 15, 20, 25}
3.5 Concatenation of Lists wi
3.6 Removing items from list
print([Link](b))
3.7 Other List methods
3.8 Copying Lists True
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 44/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 8.8 Iterating over sets
1 Functions
2 Strings In [142]: a = {10, 20, 30, 40}
2.1 Strings - Indexing and slic b = {30, 40, 50}
2.2 Strings - Skip slicing c = a | b
2.3 Strings are Immutable print(c)
2.4 Strings - Concatenation O
2.5 Strings - membership for item in c:
2.6 Strings - Iterating print (item)
2.7 Strings - other methods {50, 20, 40, 10, 30}
2.8 Strings - formatting 50
3 Lists 20
3.1 Lists - Indexing 40
3.2 Lists - Slicing 10
3.3 Lists - Skip slicing 30
3.4 Modifying a list
3.5 Concatenation of Lists wi In [143]: for item in enumerate(c):
3.6 Removing items from list print (item)
3.7 Other List methods
(0, 50)
3.8 Copying Lists (1, 20)
3.9 membership in a list (2, 40)
3.10 range (3, 10)
3.11 Iterating over lists (4, 30)
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti 9 Functions ... continued
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 45/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 9.1 Functions with default arguments
1 Functions
2 Strings In [144]: def greet(name, className = 'CS677'):
2.1 Strings - Indexing and slic return ("Hello " + name + ", Welcome to " + className)
2.2 Strings - Skip slicing
2.3 Strings are Immutable In [145]: print(greet('John'))
2.4 Strings - Concatenation O
2.5 Strings - membership Hello John, Welcome to CS677
2.6 Strings - Iterating
2.7 Strings - other methods In [146]: print(greet('John', 'CS777'))
2.8 Strings - formatting Hello John, Welcome to CS777
3 Lists
3.1 Lists - Indexing In [147]: def greet(name, className = 'CS677', instructor):
3.2 Lists - Slicing return ("Hello " + name + ", Welcome to " + className)
3.3 Lists - Skip slicing
File "<ipython-input-147-ef53871c95a3>", line 1
3.4 Modifying a list
def greet(name, className = 'CS677', instructor):
3.5 Concatenation of Lists wi
^
3.6 Removing items from list
SyntaxError: non-default argument follows default argument
3.7 Other List methods
3.8 Copying Lists
In [148]: def greet(name, className = 'CS677', instructor = 'Suresh'):
3.9 membership in a list
return ("Hello " + name + ", Welcome to " + className + " - "
3.10 range
3.11 Iterating over lists
4 Tuples In [149]: print(greet('John'))
4.1 Tuple Indexing and Slicing Hello John, Welcome to CS677 - Suresh
4.2 Concatenation and repeti
4.3 count and index In [150]: print(greet('John', 'CS546'))
4.4 membership test
Hello John, Welcome to CS546 - Suresh
4.5 Iterating over a tuple
4.6 Other built-in functions w
In [151]: print(greet('John', 'CS546', 'Anatoly' ))
5 Lists versus Tuples
6 Lists and List Comprehension Hello John, Welcome to CS546 - Anatoly
6.1 Nested Lists
7 Dictionary In [152]: print(greet('John', instructor = 'Anatoly' ))
7.1 Alternative ways of creatin
Hello John, Welcome to CS677 - Anatoly
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 46/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 9.2 Functions with variable arguments
1 Functions
2 Strings In [153]: def my_sum(name, *args):
2.1 Strings - Indexing and slic print(name, args)
2.2 Strings - Skip slicing result = 0
2.3 Strings are Immutable for n in args:
2.4 Strings - Concatenation O result += n
2.5 Strings - membership return (result)
2.6 Strings - Iterating
2.7 Strings - other methods In [154]: my_sum('John', 1, 2, 3)
2.8 Strings - formatting
John (1, 2, 3)
3 Lists
Out[154]:6
3.1 Lists - Indexing
3.2 Lists - Slicing
3.3 Lists - Skip slicing
3.4 Modifying a list
3.5 Concatenation of Lists wi
3.6 Removing items from list
3.7 Other List methods
3.8 Copying Lists
3.9 membership in a list
3.10 range
3.11 Iterating over lists
4 Tuples
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 47/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 9.3 Functions with keyword arguments
1 Functions
2 Strings In [155]: def my_average(name, **kwargs):
2.1 Strings - Indexing and slic print(name, kwargs)
2.2 Strings - Skip slicing result = 0
2.3 Strings are Immutable
2.4 Strings - Concatenation O if (len(kwargs) == 0):
2.5 Strings - membership return result
2.6 Strings - Iterating
2.7 Strings - other methods for key, value in [Link]():
2.8 Strings - formatting print('Score in {} = {}'.format(key, value))
3 Lists result += value
3.1 Lists - Indexing
3.2 Lists - Slicing
return (result / len(kwargs))
3.3 Lists - Skip slicing
3.4 Modifying a list In [156]: my_average('John', cs1=70, cs2=80, cs3=90)
3.5 Concatenation of Lists wi John {'cs1': 70, 'cs2': 80, 'cs3': 90}
3.6 Removing items from list Score in cs1 = 70
3.7 Other List methods Score in cs2 = 80
3.8 Copying Lists Score in cs3 = 90
3.9 membership in a list Out[156]:80.0
3.10 range
3.11 Iterating over lists In [157]: my_average('abc')
4 Tuples
4.1 Tuple Indexing and Slicing abc {}
4.2 Concatenation and repeti Out[157]:0
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 48/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 9.4 Anonymous (Lambda) Functions
1 Functions
2 Strings In [158]: double = lambda x: 2 * x
2.1 Strings - Indexing and slic
2.2 Strings - Skip slicing
In [159]: print(double(5))
2.3 Strings are Immutable
2.4 Strings - Concatenation O 10
2.5 Strings - membership
2.6 Strings - Iterating In [160]: print(double([10,20]))
2.7 Strings - other methods [10, 20, 10, 20]
2.8 Strings - formatting
3 Lists In [161]: # generally used as argument for higher order functions
3.1 Lists - Indexing
3.2 Lists - Slicing data = [10, 13, 15, 18, 20, 23, 26]
3.3 Lists - Skip slicing
3.4 Modifying a list list(filter(lambda x: (x %2 == 1), data))
3.5 Concatenation of Lists wi
Out[161]:[13, 15, 23]
3.6 Removing items from list
3.7 Other List methods
In [162]: list(map(double, data))
3.8 Copying Lists
3.9 membership in a list Out[162]:[20, 26, 30, 36, 40, 46, 52]
3.10 range
3.11 Iterating over lists In [163]: list(map(lambda x: x * x, data))
4 Tuples Out[163]:[100, 169, 225, 324, 400, 529, 676]
4.1 Tuple Indexing and Slicing
4.2 Concatenation and repeti
4.3 count and index
4.4 membership test
4.5 Iterating over a tuple
4.6 Other built-in functions w
5 Lists versus Tuples
6 Lists and List Comprehension
6.1 Nested Lists
7 Dictionary
7.1 Alternative ways of creatin
7.2 Modifying and adding item
7.3 Deleting items from a dict
7.4 Membership in a dictiona
7.5 Other dictionary methods
7.6 Dictionary comprehension
8 Sets
8.1 Adding elements to a set
8.2 Removing items from a se
8.3 Membership in a set
8.4 Set Union
8.5 Set Intersection
8.6 Set Difference
8.7 Set Comparison
8.8 Iterating over sets
9 Functions ... continued
9.1 Functions with default arg
9.2 Functions with variable ar
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 49/50
2025/5/3 21:28 M1_01_Intro_Python
Contents 9.5 Indirect references
1 Functions
2 Strings In [164]: a = (10, 20, [30,40], 50)
2.1 Strings - Indexing and slic print(a)
2.2 Strings - Skip slicing
2.3 Strings are Immutable b = a[2]
2.4 Strings - Concatenation O b[0] = 300
2.5 Strings - membership
2.6 Strings - Iterating print(b)
2.7 Strings - other methods print(a)
2.8 Strings - formatting (10, 20, [30, 40], 50)
3 Lists [300, 40]
3.1 Lists - Indexing (10, 20, [300, 40], 50)
3.2 Lists - Slicing
3.3 Lists - Skip slicing In [165]: # Caution - list references
3.4 Modifying a list
3.5 Concatenation of Lists wi lists = [[]] * 3
3.6 Removing items from list print(lists)
3.7 Other List methods
3.8 Copying Lists lists[0].append(3)
3.9 membership in a list lists[1].append(5)
3.10 range lists[2].append(7)
3.11 Iterating over lists
4 Tuples print(lists)
4.1 Tuple Indexing and Slicing [[], [], []]
4.2 Concatenation and repeti [[3, 5, 7], [3, 5, 7], [3, 5, 7]]
4.3 count and index
4.4 membership test In [166]: # correct approach
4.5 Iterating over a tuple
4.6 Other built-in functions w lists = [[] for i in range(3)]
5 Lists versus Tuples print(lists)
6 Lists and List Comprehension
6.1 Nested Lists lists[0].append(3)
7 Dictionary lists[1].append(5)
7.1 Alternative ways of creatin lists[2].append(7)
7.2 Modifying and adding item
7.3 Deleting items from a dict print(lists)
7.4 Membership in a dictiona [[], [], []]
7.5 Other dictionary methods [[3], [5], [7]]
7.6 Dictionary comprehension
8 Sets In [167]: # or, verbose
8.1 Adding elements to a set
8.2 Removing items from a se lists = [[],[],[]]
8.3 Membership in a set print(lists)
8.4 Set Union
8.5 Set Intersection lists[0].append(3)
8.6 Set Difference lists[1].append(5)
8.7 Set Comparison lists[2].append(7)
8.8 Iterating over sets
9 Functions ... continued print(lists)
9.1 Functions with default arg [[], [], []]
9.2 Functions with variable ar [[3], [5], [7]]
9.3 Functions with keyword a
9.4 Anonymous (Lambda) Fu
9.5 Indirect references
[Link] 50/50