0% found this document useful (0 votes)
3 views

complex

Uploaded by

Satyam Amrutkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

complex

Uploaded by

Satyam Amrutkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

In [2]: #23

s = input("Enter a string: ")

print('Positive')
for i in range(0, len(s), 1):
print(f"{i} -> {s[i]}")
print('Negative')
for i in range(-1, -(len(s)+1), -1):
print(f"{i} -> {s[i]}")

Positive
0 -> s
1 -> t
2 -> r
3 -> i
4 -> n
5 -> g
Negative
-1 -> g
-2 -> n
-3 -> i
-4 -> r
-5 -> t
-6 -> s

In [1]: #24 - Create a seperate csv file in the folder of this python file and execute
import csv
with open("./sample.csv", mode="r") as file:
csv_file = csv.reader(file)
for lines in csv_file:
print(lines)

['name', ' age']


['aaa', ' 18']
['bbb', ' 17']
['ccc', ' 19']

In [7]: #25
s = input("Enter a string")
print('Forward')
i = 0
while i < len(s):
print(f"{i} -> {s[i]}")
i += 1
print('Backwards')
i = len(s)-1
while i > -1:
print(f"{i} -> {s[i]}")
i -=1
Forward
0 -> s
1 -> t
2 -> r
3 -> i
4 -> n
5 -> g
Backwards
5 -> g
4 -> n
3 -> i
2 -> r
1 -> t
0 -> s

In [27]: #26 - kinda confusing - question not clear maybe? - similar to 29 - but 29 is a cle

In [28]: #27
s = input("Enter a string ")
vow = ['a', 'e', 'i', 'o', 'u']
s = s.lower()
found_vow = []
dup_vow = []
for i in range(len(s)):
if s[i] in vow:
if s[i] not in s[i+1:] and s[i] not in dup_vow:
found_vow.append(s[i])
else:
dup_vow.append(s[i])
print(found_vow)

['u', 'o', 'i']

In [ ]: #28 - put in two different files - the modules in a seperate folder - that is the p
#__init__.py
#--fibo.py--
def fibo(n):
result = []
a,b = 0,1
while b<=n:
result.append(b)
a,b = b, a+b
return result
#--main.py--
import fibo
n = int(input("Enter"))
fib_series = fibo.fibo(n)
print(fib_series)

In [6]: #29
n = int(input("Enter number of data in dictionary: "))
d = {}
for i in range(n):
name = input("Enter name:")
marks = int(input("Enter Marks"))
d[name] = marks
while(True):
inp = input("Enter name (or) press -1 to break")
if inp == "-1":
break
print(d[inp])

10
20

In [7]: #30
a = set(map(int, input("Enter elements of set A seperated by spaces").split
b = set(map(int, input("Enter elements of set B seperated by spaces").split
print("Union ", a|b)
print("Intersection ", a&b)
print("Difference ", a-b)
print("Symmetric Difference ", a^b)

Union {0, 1, 2, 3, 4, 5, 6, 8}
Intersection {2, 4}
Difference {1, 3, 5}
Symmetric Difference {0, 1, 3, 5, 6, 8}

In [5]: #31 - if no image on lab system, take a screenshot and save - it can be used
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
img = mpimg.imread('sid.png')
#plt.axis('off') - not necessary but yea
plt.imshow(img)

Out[5]: <matplotlib.image.AxesImage at 0x7fecfe014510>

In [6]: #32
tuples_list = [(1,2), (2,3), (3,4)]
sums = [sum(t) for t in tuples_list]
print(sums)

[3, 5, 7]

In [7]: #33
import json
inp = input("Enter a dictionary: ")
inp = dict(json.loads(inp))
s = sum(inp.values())
print(s)

600

In [11]: #34
try:
num_lines=0
num_words=0
num_char=0
with open("./data.txt", "r") as file:
for line in file:
num_lines +=1
num_char += len(line)
num_words += len(line.split())
print(f"Lines: {num_lines}")
print(f"Characters: {num_char}")
print(f"Words: {num_words}")
except FileNotFoundError:
print("File not found ")
except Exception as e:
print("An error occured: ", e)

Lines: 3
Characters: 48
Words: 8

In [12]: #35
lst = list(map(int, input("Enter nums").split()))
evens = list(e for e in lst if e%2==0)
odds = list(e for e in lst if e%2!=0)
print(evens)
print(odds)

[2, 4, 6]
[3, 5, 7]

In [13]: #36
lst = list(map(int, input("Enter nums seperated by spaces ").split()))
st = set(lst)
print(st)

{1, 2, 3}

In [18]: #37
s = input("Enter a string: ")
print("Reversed String")
print(s[::-1])
print("Vowels in the string")
vow = ['a', 'e', 'i', 'o', 'u']
s = s.lower()
present_v = {ch for ch in s if ch in vow}
print(present_v)

Reversed String
cinoci
Vowels in the string
{'o', 'i'}
In [21]: #38
import os

file_name = 'data.txt'

if os.path.exists(file_name):
with open(file_name, 'r') as file:
content = file.read()
print(content)
else:
print("File does not exist.")

First Line.
Second Line.
There are eight words.

In [22]: #39
t1=(1,2,3)
t2=(4,5,6)
lst = []
lst.append(t1)
lst.append(t2)
print(lst)
l1 = list(t1)
l2 = list(t2)
tpl = ()
tpl+=(l1, )
tpl+=(l2, )
print(tpl)

[(1, 2, 3), (4, 5, 6)]


([1, 2, 3], [4, 5, 6])

In [ ]: #40 - put in two different files - the modules in a seperate folder - that is the p
#__init__.py
#--fibo.py--
def fibo(n):
result = []
a,b = 0,1
while b<=n:
result.append(b)
a,b = b, a+b
return result
#--square.py--
def square(n):
return n**2
#--main.py-- assuming your folder name is mypackage
import mypackage.fibo as fibo
import mypackage.square as square
n = int(input("Enter Number"))
fib_series = fibo.fibo(n)
square = square.square(n)
print(fib_series)
print(square)

In [24]: #41
nums = (1,2,3,4,5)
res = [nums[i]*nums[i+1] for i in range(len(nums)-1)]
print(res)
[2, 6, 12, 20]

In [ ]: #42 - same as 40

In [ ]: #43 same as 40 - seperate files


import mypackage.fibo as f
for attr in dir(f):
print(attr)
#sample output will include fibo, __builtins__, __cached__, __doc__, __name__

In [26]: #44
nums = [1,2,3,4,5]
cumulative = []
total = 0
for n in nums:
total+=n
cumulative.append(total)
print(cumulative)

[1, 3, 6, 10, 15]

In [ ]:

You might also like