2 +3
5
3.6 + 4
7.6
2**100
1267650600228229401496703205376
items = [1, 2, 3, 4, 5]
items
[1, 2, 3, 4, 5]
items[0] # index starts with zero
1
items[4] # index ends with one less than length of list
5
items[-1] # give me last item
5
words = ["one", "two", "three", "four", "five"]
words[1:3] # subset of list which starts at index 1 but ends at 3(excluded)
['two', 'three']
words[2:] # drop 2 items
['three', 'four', 'five']
words[:2] # take first 2 items
['one', 'two']
words[:] # take everything
['one', 'two', 'three', 'four', 'five']
words[::2] # give everything from start to end ...but alternate item
['one', 'three', 'five']
list[start:end:step]
words[::-1] # reverse the list
['five', 'four', 'three', 'two', 'one']
scores = {
"Prathamesh" : 20,
"Priya": 25,
"Prafulla": 24,
"Priyanka":22
}
scores
{'Prathamesh': 20, 'Priya': 25, 'Prafulla': 24, 'Priyanka': 22}
scores['Priya']
25
nums = {1:"one",
2:"two",
3:"three"
}
nums
{1: 'one', 2: 'two', 3: 'three'}
nums[1]
'one'
d = {
[1, 1, 1]: "value1",
[1]:["value2"]
}
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-25-83cc877e91b1> in <module> ----> 1 d = { 2 [1, 1, 1]: "value1", 3 [1]:["value2"] 4 } TypeError: unhashable type: 'list'
What can be stored in dictionary
classroom = ["Prathamesh", "Hema", "Pranav", "Sanika", "Preetee", "Sidhhu"]
i = 0
while i < len(classroom):
i = i + 1
print(classroom[i])
Hema Pranav Sanika Preetee Sidhhu
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-28-63aba0778bd3> in <module> 2 while i < len(classroom): 3 i = i + 1 ----> 4 print(classroom[i]) IndexError: list index out of range
i = 0
while i < len(classroom):
print(classroom[i])## <--- this is where errors are if any!
i = i + 1
Prathamesh Hema Pranav Sanika Preetee Sidhhu
for student in classroom:
print(student) # this loop will never fail!
Prathamesh Hema Pranav Sanika Preetee Sidhhu
for member in scores: # this will go over the keys
print(member)
Prathamesh Priya Prafulla Priyanka
for member in scores: # this will go over the keys
print(member, scores[member])
Prathamesh 20 Priya 25 Prafulla 24 Priyanka 22
for member, score in scores.items():
print(member, score)
Prathamesh 20 Priya 25 Prafulla 24 Priyanka 22
x, y = 3, 4
print(x, y)
3 4
list(scores.items())
[('Prathamesh', 20), ('Priya', 25), ('Prafulla', 24), ('Priyanka', 22)]
for student in classroom:
print(student)
Prathamesh Hema Pranav Sanika Preetee Sidhhu
for student in reversed(classroom):
print(student)
Sidhhu Preetee Sanika Pranav Hema Prathamesh
for rollno, student in enumerate(classroom):
print(rollno, student)
0 Prathamesh 1 Hema 2 Pranav 3 Sanika 4 Preetee 5 Sidhhu
for rollno, student in enumerate(classroom, start=1):
print(rollno, student)
1 Prathamesh 2 Hema 3 Pranav 4 Sanika 5 Preetee 6 Sidhhu
testmarks = [20, 19, 18, 18, 20, 19]
classroom
['Prathamesh', 'Hema', 'Pranav', 'Sanika', 'Preetee', 'Sidhhu']
testmarks
[20, 19, 18, 18, 20, 19]
for student, marks in zip(classroom, testmarks):
print(student, marks)
Prathamesh 20 Hema 19 Pranav 18 Sanika 18 Preetee 20 Sidhhu 19
import os
all_files = os.listdir()
all_files
['index.html', 'Lecture6-Comprehensions-1.ipynb', 'presenty5.txt', 'sample.html', 'presenty3.txt', 'Welcome.html', 'push', 'Lecture4-Working_With_Data.html', 'joy.py', 'presenty6.txt', 'Lecture2-Quick_Tour.ipynb', 'Lecture7-Recap.html', 'sample.ipynb', '#presenty4.txt#', '__pycache__', 'Untitled.html', 'Lecture4-Working_With_Data.ipynb', 'Lecture5-Comprehensions.html', 'Lecture1-Fundamental_ideas_of_programming.ipynb.ipynb', 'index.ipynb', 'Lecture7-Recap.ipynb', 'presenty5.txt~', 'Lecture6-Comprehensions-1.html', 'test.html', 'Lecture5-Comprehensions.ipynb', 'Lecture6-Recap.html', 'Lecture3-Quick_Tour.ipynb', 'Lecture1-Fundamental_ideas_of_programming.ipynb.html', 'Makefile', 'Lecture2-Quick_Tour.html', '.ipynb_checkpoints', 'Lecture3-Quick_Tour.html']
def read_files(filenames):
filedata = {}
for file in filenames:
if os.path.isfile(file):
with open(file) as f:
print("Reading file:", file)
filedata[file] = f.read()
return filedata
data = read_files(all_files)
Reading file: index.html Reading file: Lecture6-Comprehensions-1.ipynb Reading file: presenty5.txt Reading file: sample.html Reading file: presenty3.txt Reading file: Welcome.html Reading file: push Reading file: Lecture4-Working_With_Data.html Reading file: joy.py Reading file: presenty6.txt Reading file: Lecture2-Quick_Tour.ipynb Reading file: Lecture7-Recap.html Reading file: sample.ipynb Reading file: #presenty4.txt# Reading file: Untitled.html Reading file: Lecture4-Working_With_Data.ipynb Reading file: Lecture5-Comprehensions.html Reading file: Lecture1-Fundamental_ideas_of_programming.ipynb.ipynb Reading file: index.ipynb Reading file: Lecture7-Recap.ipynb Reading file: presenty5.txt~ Reading file: Lecture6-Comprehensions-1.html Reading file: test.html Reading file: Lecture5-Comprehensions.ipynb Reading file: Lecture6-Recap.html Reading file: Lecture3-Quick_Tour.ipynb Reading file: Lecture1-Fundamental_ideas_of_programming.ipynb.html Reading file: Makefile Reading file: Lecture2-Quick_Tour.html Reading file: Lecture3-Quick_Tour.html
data.keys()
dict_keys(['index.html', 'Lecture6-Comprehensions-1.ipynb', 'presenty5.txt', 'sample.html', 'presenty3.txt', 'Welcome.html', 'push', 'Lecture4-Working_With_Data.html', 'joy.py', 'presenty6.txt', 'Lecture2-Quick_Tour.ipynb', 'Lecture7-Recap.html', 'sample.ipynb', '#presenty4.txt#', 'Untitled.html', 'Lecture4-Working_With_Data.ipynb', 'Lecture5-Comprehensions.html', 'Lecture1-Fundamental_ideas_of_programming.ipynb.ipynb', 'index.ipynb', 'Lecture7-Recap.ipynb', 'presenty5.txt~', 'Lecture6-Comprehensions-1.html', 'test.html', 'Lecture5-Comprehensions.ipynb', 'Lecture6-Recap.html', 'Lecture3-Quick_Tour.ipynb', 'Lecture1-Fundamental_ideas_of_programming.ipynb.html', 'Makefile', 'Lecture2-Quick_Tour.html', 'Lecture3-Quick_Tour.html'])
testmarks
[20, 19, 18, 18, 20, 19]
def square_list(numbers):
sqrs = []
for n in numbers:
sqrs.append(n*n)
return sqrs
def filtertext_files(files):
textfiles = []
for file in files:
if file.endswith(".txt"):
textfiles.append(file)
return textfiles
square_list(testmarks)
[400, 361, 324, 324, 400, 361]
filtertext_files(all_files)
['presenty5.txt', 'presenty3.txt', 'presenty6.txt']
textfiles = filtertext_files(all_files)
textfiles
['presenty5.txt', 'presenty3.txt', 'presenty6.txt']
read_files(textfiles)
Reading file: presenty5.txt Reading file: presenty3.txt Reading file: presenty6.txt
{'presenty5.txt': 'Varsha Bendre18:37\nok\nYou18:40\nhttps://notes.pipal.in/2021/pccoe-python-ml/\nlink for live notes\nTEETC322 Nisha Hiremath18:43\nno sir \nVarsha Bendre18:43\nno sir\nTEETC322 Nisha Hiremath18:43\nwill try it today\nTEETA145 INDRARAJ19:28\nTEETA145\nTEETC322 Nisha Hiremath19:29\nTEETC322 Nisha Hiremath\n',
'presenty3.txt': 'TEETC340 Sameer Pathak20:08\nSameer Pathak\nTEETC340\nBEETC316 Vedant Kharche20:08\nBEETC316 Vedant kharche \nTEETC323 Renuka Jadhav20:08\nRenuka Jadhav TEETC323\nTEETA108_ Kartikkumar Athani20:08\nKartikkumar Athani TEETA108 \nNisha Hiremath20:08\nTEETC322 Nisha Hiremath\n',
'presenty6.txt': 'TEETC322 Nisha Hiremath18:57\nOk sir\nTEETC323 Renuka Jadhav19:01\nsir can you give some hint\nTEETC340 Sameer Pathak19:03\nZip\nTEETC340 Sameer Pathak19:05\nYes sir\nTEETC322 Nisha Hiremath19:58\nTEETC322 Nisha Hiremath\nTEETC340 Sameer Pathak19:58\nTEETC340 Sameer Pathak\nTEETC323 Renuka Jadhav19:58\nTEETC323 Renuka Jadhav\n'}
def square_list(numbers):
sqrs = []
for n in numbers:
sqrs.append(n*n)
return sqrs
numbers = [1, 2, 3, 4, 5,6 ,7 ]
[n*n for n in numbers] # list compehsnions
[1, 4, 9, 16, 25, 36, 49]
square_list(numbers)
[1, 4, 9, 16, 25, 36, 49]
Geeral gudelines while coding loops
[student.upper() for student in classroom]
['PRATHAMESH', 'HEMA', 'PRANAV', 'SANIKA', 'PREETEE', 'SIDHHU']
scores
{'Prathamesh': 20, 'Priya': 25, 'Prafulla': 24, 'Priyanka': 22}
scaled_score = {}
for student, score in scores.items():
scaled_score[student] = score/25
print(scaled_score)
{'Prathamesh': 0.8, 'Priya': 1.0, 'Prafulla': 0.96, 'Priyanka': 0.88}
{student:score/25 for student, score in scores.items()}
{'Prathamesh': 0.8, 'Priya': 1.0, 'Prafulla': 0.96, 'Priyanka': 0.88}
[file for file in os.listdir() if file.endswith(".txt")]
['presenty5.txt', 'presenty3.txt', 'presenty6.txt']
[f for f in os.listdir() if f.endswith(".html")]
['index.html', 'sample.html', 'Welcome.html', 'Lecture4-Working_With_Data.html', 'Lecture7-Recap.html', 'Untitled.html', 'Lecture5-Comprehensions.html', 'Lecture6-Comprehensions-1.html', 'test.html', 'Lecture6-Recap.html', 'Lecture1-Fundamental_ideas_of_programming.ipynb.html', 'Lecture2-Quick_Tour.html', 'Lecture3-Quick_Tour.html']
[f for f in os.listdir() if f.endswith(".ipynb") and f.startswith("Lecture")]
['Lecture6-Comprehensions-1.ipynb', 'Lecture2-Quick_Tour.ipynb', 'Lecture4-Working_With_Data.ipynb', 'Lecture1-Fundamental_ideas_of_programming.ipynb.ipynb', 'Lecture7-Recap.ipynb', 'Lecture5-Comprehensions.ipynb', 'Lecture3-Quick_Tour.ipynb']
[n*n for n in numbers if n%2==0]
[4, 16, 36]
sum(numbers)
28