Skip to content

Commit 56f0cdd

Browse files
committed
new files added
1 parent d80f183 commit 56f0cdd

File tree

11 files changed

+292
-1
lines changed

11 files changed

+292
-1
lines changed

data.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
this is a practice question
2+
line 1
3+
line 2
4+
line 3
5+
line 4

datastructures/dict.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# collection of key-value pairs , mutable, unordered, no duplicate keys
2+
student = {
3+
"name": "rohit",
4+
"age": 20,
5+
"college": "chandigarh university"
6+
}
7+
print(student)
8+
# accessing:
9+
print(student["name"])
10+
# .get(), preferable as it will not throw error if key is not present
11+
print(student.get("age"))
12+
13+
# adding and updating
14+
student["course"] = "C.S.E"
15+
student["age"] = "21"
16+
print(student)
17+
18+
# removing
19+
student.pop("college")
20+
del student["course"]
21+
print(student)
22+
23+
# looping
24+
for key in student:
25+
print(key, end=" ")
26+
print()
27+
for value in student.values():
28+
print(value, end=" ")
29+
print()
30+
for key, value in student.items():
31+
print(f"{key}:{value}")
32+
33+
# dictionary methods
34+
student1 = {
35+
"name": "rohit",
36+
"age": 20,
37+
"college": "chandigarh university"
38+
}
39+
40+
print()
41+
print(student1.keys())
42+
print(student1.values())
43+
print(student1.items())
44+
45+
# Create a dictionary with 5 student names as keys and their marks as values.
46+
student_marks = {
47+
"a": 91,
48+
"b": 93,
49+
"x": 88,
50+
"y": 90,
51+
"z": 92
52+
}
53+
print(student_marks)
54+
55+
print("practice questions")
56+
57+
# Find the student with the highest marks.
58+
scholar = max(student_marks, key=student_marks.get)
59+
# max(dictionary, key=dictionary.get) → Finds key with max value.
60+
print(f"topper student : {scholar} with {student_marks[scholar]} marks")

datastructures/lists.py

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
print(fruits)
3535
fruits.pop() # remove last element
3636
print(fruits)
37+
print("practice questions")
38+
3739

3840
# Create a list of 5 numbers and print their total_sum.
3941
num = [1, 2, 3, 4, 5]

datastructures/sets.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# unordered, mutable, unique collection of elements
2+
numbers = {1, 2, 3, 4, 5, 6}
3+
info = {"rohit", 21, "B.E-S.C.S.E"}
4+
empty_set = set() # {} creates empty dict not set
5+
6+
# no indexing as unordered
7+
print(numbers)
8+
for i in numbers:
9+
print(i, end=" ")
10+
print()
11+
12+
# adding element to set
13+
numbers.add(4.3)
14+
print(numbers)
15+
16+
# removing element from a set
17+
18+
numbers.remove(3) # gives error if 3 is not present in the set
19+
print(numbers)
20+
21+
numbers.discard(4) # does not give error if 3 is not present in the set
22+
print(numbers)
23+
24+
# mathematical operations
25+
a = {1, 2, 3, 4}
26+
b = {3, 4, 5, 6}
27+
print(a)
28+
print(b)
29+
print("a U b: ", a | b) # union
30+
print("a intersec b: ", a & b) # intersection
31+
print("a - b: ", a - b) # difference
32+
print("b - a: ", b - a) # difference
33+
print("a ^ b: ", a ^ b) # symmetric difference
34+
35+
print("practice questions")
36+
37+
# Create a set with 5 numbers and remove the largest number.
38+
39+
num = {1, 2, 3, 4, 5}
40+
max_num = max(num)
41+
print(f"largest num is {max_num}")
42+
num.remove(max_num)
43+
# Find the common elements between two sets {10, 20, 30, 40} and {30, 40, 50, 60}.
44+
x = {10, 20, 30, 40}
45+
y = {30, 40, 50, 60}
46+
print("common elements:", x & y, end=" ")

datastructures/tuples.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# similar to list but immutable
2+
3+
numbers = (10, 20, 40)
4+
diff_dataatype = ("rohit", 3, 3.5, True)
5+
6+
single_elem = (10,) # without comma it is just an int
7+
8+
for i in numbers:
9+
print(i, end=" ")
10+
print()
11+
# packing and unpacking
12+
13+
person = ("rohit", 21, "B.E-C.S.E") # packing
14+
name, age, stream = person # unpacking
15+
print(name, end=" ")
16+
print(age, end=" ")
17+
print(stream)
18+
19+
# convert tuple to list and vice-versa
20+
21+
# tuple to list
22+
tup_num = (10, 20, 30)
23+
list_num = list(tup_num)
24+
for i in list_num:
25+
print(i, end=" ")
26+
print()
27+
list_num.append(40)
28+
for i in list_num:
29+
print(i, end=" ")
30+
print()
31+
32+
# list to tuple
33+
list_num = tuple(tup_num)
34+
print("this is a tuple")
35+
for i in list_num:
36+
print(i, end=" ")
37+
print()
38+
print("practice questions")
39+
40+
41+
# create a tuple of 5 numbers and find their sum.
42+
tup_sum = 0
43+
for i in tup_num:
44+
tup_sum += i
45+
print(f"the sum is {tup_sum}")
46+
47+
# Swap two numbers using a tuple(without using a third variable).
48+
49+
tuple_num = (5, 6)
50+
print(tuple_num)
51+
tuple_num = (tuple_num[1], tuple_num[0])
52+
print(tuple_num)

example.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
this is file handling
2+
-w mode is used to write inside a file
3+
-a is used for appending in the same file

filehandling/filehandling.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -w is used to write inside a file
2+
# if file doesnt exist, it creates one
3+
# if file exists, it overwrites the file
4+
with open("example.txt", "w") as file:
5+
file.write("this is file handling")
6+
file.write("\n-w mode is used to write inside a file")
7+
print("file written successfully")
8+
9+
# -r is used to read the contents of a file
10+
# if file doesnt exist, it throws an err
11+
with open("example.txt", "r") as file:
12+
content = file.read()
13+
print(content)
14+
15+
# 'a' mode: Keeps the existing content and adds new data
16+
with open("example.txt", "a") as file:
17+
file.write("\n-a is used for appending in the same file")
18+
print("appended")
19+
20+
# to read file line by line
21+
with open("example.txt", "r") as file:
22+
for line in file:
23+
print(line.strip()) # strip() remove the newline character
24+
25+
# Write a Python script to create a file named "data.txt" and write 5 lines of text.
26+
with open("data.txt", "w") as file:
27+
file.write("this is a practice question\nline 1\nline 2\nline 3\nline 4")
28+
print("data.txt created and written")
29+
# optimised code:
30+
# with open("data.txt", "w") as file:
31+
# lines = ["this is a practice question\n",
32+
# "line 1\n",
33+
# "line 2\n,"
34+
# "line 3\n,"
35+
# "line 4\n"]
36+
# file.writelines(lines)
37+
38+
39+
# Read the file and print each line one by one.
40+
41+
with open("data.txt", "r") as file:
42+
content = file.readlines()
43+
total_words = 0
44+
total_char = 0
45+
for line in content:
46+
print(line.strip())
47+
total_words += len(line.split())
48+
total_char += len(line)
49+
# Count the number of lines in "data.txt" and print:
50+
# "Total number of lines in data.txt: X"
51+
print(f"Total number of lines in data.txt: {len(content)}")
52+
# Modify your script to count the total number of words in "data.txt" and print:
53+
# "Total number of words in data.txt: X"
54+
print(f"Total number of words in data.txt: {total_words}")
55+
# Count the total number of characters in "data.txt" and print:
56+
# "Total number of characters in data.txt: X"
57+
print(f"Total number of characters in data.txt: {total_char}")

functions/LambdaFunctions.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# with arbitrary arguments: when we dont know the no. of arguments beforehand
2+
3+
from functools import reduce
4+
print("with arbitrary arguments")
5+
6+
7+
def add(*nums):
8+
return sum(nums)
9+
10+
11+
print(add(1, 2, 3, 4))
12+
13+
# to accept any number of named arguments **kwargs
14+
15+
16+
def student_info(**details):
17+
for key, value in details.items():
18+
print(f'{key}: {value}')
19+
20+
21+
student_info(name="rohit", age=21, city="cgd")
22+
23+
# lambda functions: one line functions, use of map(),filter(),reduce()
24+
25+
nums = [1, 2, 3, 4, 5]
26+
square = list(map(lambda x: x*x, nums))
27+
print(square)
28+
29+
even = list(filter(lambda x: x % 2 == 0, nums))
30+
print(even)
31+
32+
# from functools import reduce
33+
product = reduce(lambda x, y: x*y, nums)
34+
print(product)
35+
36+
37+
print("practice questions")
38+
# Write a function multiply_list(numbers) that takes a list and returns the product of all elements.
39+
40+
41+
def multiply_list(numbers):
42+
product = 1
43+
for num in numbers:
44+
product *= num
45+
return product
46+
47+
48+
print(multiply_list([1, 2, 3, 4, 5]))
49+
50+
51+
# Use a lambda function to filter out even numbers from [3, 6, 9, 12, 15]
52+
num_list = [3, 6, 9, 12, 15]
53+
even_num = list(filter(lambda x: x % 2 == 0, num_list))
54+
odd_num = list(filter(lambda x: x % 2 != 0, num_list))
55+
print(even_num)
56+
print(odd_num)

functions.py renamed to functions/functions.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
print("without argument")
2+
3+
14
def greet():
25
print("hello")
36

47

58
greet()
69

710
# with argument
11+
print("with argument")
812

913

1014
def Name(name):
@@ -24,6 +28,8 @@ def add(a, b):
2428

2529
# default parameter
2630

31+
print("default parameter")
32+
2733

2834
def greet(name="Guest"):
2935
print(f"Hello, {name}!")
@@ -34,13 +40,15 @@ def greet(name="Guest"):
3440

3541

3642
# lambda function
43+
print("lambda fxn")
3744
def square(x): return x*x
3845

3946

4047
print(square(4))
4148
# cube = lambda y: y*y*y
4249
# print(cube(4))
4350

51+
print("practice question")
4452
# Write a function multiply(a, b) that returns the product of two numbers.
4553

4654

loops.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
print(i, end=" ")
4646
print()
4747

48+
print("practice questions")
49+
4850
# Write a Python program to print all even numbers from 1 to 20 using a for loop.
4951
for i in range(1, 10):
5052
if i % 2 == 0:
@@ -56,4 +58,3 @@
5658
print(i, end=" ")
5759
i -= 1
5860
print()
59-

tempCodeRunnerFile.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
multiply_list([1, 2, 3, 4, 5])

0 commit comments

Comments
 (0)