Module 3 4 PLC
Module 3 4 PLC
print(os.getcwd())
os.chdir("D:\\")
print(os.getcwd())
# # os.makedirs("./Arun")
# print(os.listdir("."))
os.chdir("./Arun")
print(os.getcwd())
# os.makedirs("./A.txt")
print(os.listdir("."))
D:\Arun
D:\
D:\Arun
['a.txt']
a = {1:2, 2:4}
b = a.items()
print(b)
c = a.keys()
print(c)
d = a.values()
print(d)
a.update({2:34})
print(a)
dict_items([(1, 2), (2, 4)])
dict_keys([1, 2])
dict_values([2, 4])
{1: 2, 2: 34}
import os
print(os.getcwd())
print(os.path.abspath("arun"))
print(os.path.isabs("."))
D:\Arun
D:\Arun\arun
False
import os
print(os.getcwd())
print(os.path.relpath("C:\\User\\56", "C:\\User"))
D:\Arun
56
import os
print(os.getcwd())
print(os.path.getsize("./a.txt"))
D:\Arun
0
import os
import pprint
print(os.getcwd())
pprint.pprint(os.listdir("."))
D:\
['$RECYCLE.BIN',
'advanced-note-uploader',
'Arun',
'C',
'colleges_cutoff.xlsx',
'Dra 1.d ',
'IITG resumes',
'Photoshop Temp154531856',
'Photoshop Temp242832832',
'Photoshop Temp27818645420',
'Photoshop Temp3419565896',
'Photoshop Temp8206072564',
'PSAutoRecover',
'System Volume Informa on']
import os
print(os.path.basename("D:\\Arun\\a.txt"))
print(os.path.dirname("D:\\Arun\\a.txt"))
a.txt
D:\Arun
import os
print(("D:\\Arun\\a.txt").split(os.path.sep))
print(os.path.exists("D:\\Arun\\a.txt"))
print(os.path.isdir("D:\\Arun"))
['D:', 'Arun', 'a.txt']
True
True
import os
print(os.getcwd())
a = open("./a.txt", "a")
b = a.write(" shdhsdshds dhsdhshArun Kumar S B")
a.close()
D:\Arun
ep1={111: 23, 112:33, 113:54}
ep2={115: 87, 119:73, 118:98}
for v in ep1.items():
print(v)
# (111, 23)
# (112, 33)
# (113, 54)
for v in ep1.keys():
print(v)
# 111
# 112
# 113
for v in ep1.values():
print(v)
# 23
# 33
# 54
print()
# in and no n operator
ep1={111: 23, 112:33, 113:54}
a = 232 in ep1.keys()
print(a)
# False
# get(key, fallback)
ep1={111: 23, 112:33, 113:54}
a = ep1.get(1123)
print(a)
# None
print()
print()
# set default
ep1={111: 23, 112:33, 113:54}
ep1.setdefault(0)
print(ep1)
# {111: 23, 112: 33, 113: 54, 0: None}
print()
print()
print()
print()
# Important ques on
message = 'It was a bright cold day in April, and the clocks
were striking thirteen.'
count = {}
for character in message:
count.setdefault(character, 0)
count[character] = count[character] + 1
print(count)
# pre y print
import pprint
message = 'It was a bright cold day in April, and the clocks
were striking thirteen.'
count = {}
for character in message:
count.setdefault(character, 0)
count[character] = count[character] + 1
pprint.pprint(count)
# 1. What does the code for an empty dic onary look like?
# An empty dic onary is defined using curly braces {}:
# python
# Copy code
# empty_dict = {}
# 2. What does a dic onary value with a key 'foo' and a value
42 look like?
# A dic onary with the key 'foo' and value 42 looks like:
# python
# Copy code
# {'foo': 42}
# 3. What is the main difference between a dic onary and a
list?
# Dic onary:
# python
# Copy code
# spam.setdefault('color', 'black')
# setdefault() checks if the key 'color' exists:
# If it does, it returns the exis ng value.
# If it doesn't, it sets 'color' to 'black'.
# 8. What module and func on can be used to "pre y print"
dic onary values?
# The pprint module and its pprint() func on can be used to
"pre y print" dic onary values:
# python
# Copy code
# from pprint import pprint