diff --git a/Week 1/SET 11/Task-5.py b/Week 1/SET 11/Task-5.py index f3c9d99..37581c8 100644 --- a/Week 1/SET 11/Task-5.py +++ b/Week 1/SET 11/Task-5.py @@ -1,13 +1,11 @@ print("Enter the exam marks:") marks=[] -tot = 0 print("Enter Marks Obtained in different exams: ") for i in range(5): marks.insert(i, input()) #print(marks[i]) -for i in range(5): - tot = tot + int(marks[i]) +tot = sum(int(marks[i]) for i in range(5)) avg = tot/5 print("The average is :",avg) if avg >=90 : diff --git a/Week 1/SET 14/ques4.py b/Week 1/SET 14/ques4.py index 7cdcad1..e87874c 100644 --- a/Week 1/SET 14/ques4.py +++ b/Week 1/SET 14/ques4.py @@ -1,7 +1,5 @@ def sumofDigits(n): - sum = 0 - for digit in str(n): - sum += int(digit) + sum = sum(int(digit) for digit in str(n)) print(sum) n =input() diff --git a/Week 2/SET 11/q1.py b/Week 2/SET 11/q1.py index 5fd87d3..772db81 100644 --- a/Week 2/SET 11/q1.py +++ b/Week 2/SET 11/q1.py @@ -37,14 +37,14 @@ def binaryToDecimal(n): num = n; dec_value = 0; base = 1; - + temp = num; - while(temp): + while (temp): last_digit = temp % 10; temp = int(temp / 10); - + dec_value += last_digit * base; - base = base * 2; + base *= 2; return dec_value; if n == 1: diff --git a/Week 2/SET 11/q2.py b/Week 2/SET 11/q2.py index 51504a6..216efff 100644 --- a/Week 2/SET 11/q2.py +++ b/Week 2/SET 11/q2.py @@ -4,8 +4,8 @@ def pascal_triangle(num): row = [1] y = [0] - for x in range(max(num,0)): - print(row) - row=[l+r for l,r in zip(row+y, y+row)] + for _ in range(max(num,0)): + print(row) + row=[l+r for l,r in zip(row+y, y+row)] return num>=1 pascal_triangle(n) \ No newline at end of file diff --git a/Week 2/SET 11/q4.py b/Week 2/SET 11/q4.py index aa8e140..dc8aea0 100644 --- a/Week 2/SET 11/q4.py +++ b/Week 2/SET 11/q4.py @@ -2,16 +2,16 @@ # value a list from 11-20, 21-30, and 31-40 respectively. Access the fifth value of each key # from the dictionary. -m = dict() +m = {} li = ['x','y','z'] s = 11; for l in li: - val = list() - for i in range(9): + val = [] + for _ in range(9): val.append(s) s+=1 - s = s+1 + s += 1 m[l] = val print(m) -for k in m.keys(): - print(m[k][4]) \ No newline at end of file +for k, v in m.items(): + print(v[4]) \ No newline at end of file diff --git a/Week 2/SET 11/q5.py b/Week 2/SET 11/q5.py index 1a0691d..819677a 100644 --- a/Week 2/SET 11/q5.py +++ b/Week 2/SET 11/q5.py @@ -5,27 +5,25 @@ a = [1,2,3,4,5,6,4] b = [9,8,7,6,5,4,3] -m = dict() +m = {} -if(len(a) >= len(b)): - for i in range(len(a)): +if (len(a) >= len(b)): + for i in range(len(a)): if i < len(b): - if a[i] in m.keys(): + if a[i] in m: m[a[i]].append(b[i]) else: - val = list() - val.append(b[i]) + val = [b[i]] m[a[i]] = val else: m[a[i]] = None else: - for i in range(len(b)): + for i in range(len(b)): if i < len(a): - if b[i] in m.keys(): + if b[i] in m: m[b[i]].append(a[i]) else: - val = list() - val.append(a[i]) + val = [a[i]] m[b[i]] = val else: m[b[i]] = None diff --git a/Week 2/SET 14/ques1.py b/Week 2/SET 14/ques1.py index f9be90a..347f953 100644 --- a/Week 2/SET 14/ques1.py +++ b/Week 2/SET 14/ques1.py @@ -1,10 +1,6 @@ def ispangram(str): alphabet = "abcdefghijklmnopqrstuvwxyz" - for char in alphabet: - if char not in str.lower(): - return False - - return True + return all(char in str.lower() for char in alphabet) str = input("enter a sentence :\n ") if ispangram(str): diff --git a/Week 3/SET 14/ques2.py b/Week 3/SET 14/ques2.py index 5726299..d804e39 100644 --- a/Week 3/SET 14/ques2.py +++ b/Week 3/SET 14/ques2.py @@ -4,8 +4,7 @@ def __init__(self): pass def _getString(self): - s = input() - return s + return input() def _printString(self,s): print(s.upper()) diff --git a/Week 3/SET 5/Task-2.py b/Week 3/SET 5/Task-2.py index 94bf86d..27933a2 100644 --- a/Week 3/SET 5/Task-2.py +++ b/Week 3/SET 5/Task-2.py @@ -2,7 +2,7 @@ class Student: def __init__(self): self.name = input("Enter your name:") self.roll = int(input("Enter your roll number:")) - self.marks=list() + self.marks = [] for i in range (5): self.marks.insert(i,int(input("enter %d th mark :"%(i+1)))) diff --git a/Week 3/SET 5/Task-4.py b/Week 3/SET 5/Task-4.py index b72f7ee..a988c74 100644 --- a/Week 3/SET 5/Task-4.py +++ b/Week 3/SET 5/Task-4.py @@ -2,7 +2,7 @@ print("Which package do you have A, B, or C? : ") package =str(input()) -if(package == 'A'or package == 'B'or package == 'C'): +if package in {'A', 'B', 'C'}: print("How many hours were used:") hours=int(input()) if(hours > 744 or hours < 0): @@ -10,30 +10,18 @@ print("Enter hours again: ") hours=int(input()) -if(package == "A"): - +if package == "A": limit = 9.95; - - if(hours < 10): - total = limit; - - else: - total = ((hours - 10) * 2) + limit + total = limit if (hours < 10) else ((hours - 10) * 2) + limit print("The amount due is: $%.2f"%total) -if(package == 'B'): - +elif package == 'B': limit = 14.95; - if(hours < 20): - total = limit; - else: - total = ((hours - 20) * 1) + limit; - + total = limit if (hours < 20) else ((hours - 20) * 1) + limit print("The amount due is: $%.2f"%total) -if(package == 'C'): - +elif package == 'C': limit = 19.95; total = limit; print("The amount due is: $%.2f"%total) diff --git a/Week 3/SET 5/Task-5.py b/Week 3/SET 5/Task-5.py index 5a9e746..c95acbc 100644 --- a/Week 3/SET 5/Task-5.py +++ b/Week 3/SET 5/Task-5.py @@ -6,7 +6,7 @@ def main(): height = int(input('Please enter your height in inches: ')) # Calculate the BMI (BMI = weight*703/height^2) - BMI = (weight * 703.0) / (height*height) + BMI = weight * 703.0 / height**2 print ('Your BMI is %.1f'%BMI) # Determine whether the user is underweight, overweight, or optimal diff --git a/Week 4/SET1.py b/Week 4/SET1.py index 6a7fbfa..fc40769 100644 --- a/Week 4/SET1.py +++ b/Week 4/SET1.py @@ -29,23 +29,19 @@ dob=tk.Label(window, text="DATE OF BIRTH", bg='#6449C6', fg='white', pady=15) dob.place(x=13, y=75) -monthchosen = Combobox(window, width = 7) -daychosen = Combobox(window, width = 7) +monthchosen = Combobox(window, width = 7) +daychosen = Combobox(window, width = 7) yearchosen = Combobox(window, width = 7) -dayl=[] -for i in range(0 ,32): - dayl.append(str(i)) +dayl = [str(i) for i in range(32)] dayl[0]='Day:' daychosen['values'] = dayl -yearl=[] -for i in range(1968 ,2022): - yearl.append(str(i)) +yearl = [str(i) for i in range(1968 ,2022)] yearl[0]='Year:' yearchosen['values'] = yearl - + monthchosen['values'] = ('Month:', ' January', ' February', @@ -59,11 +55,11 @@ ' October', ' November', ' December') - -daychosen.current(0) + +daychosen.current(0) daychosen.place(x=150, y=88) -monthchosen.current(0) +monthchosen.current(0) monthchosen.place(x=220, y=88) yearchosen.current(0) @@ -133,10 +129,10 @@ f1=tk.Label(window, text="COUNTRY", bg='#6449C6', fg='white', padx=10, pady=15) f1.place(x=5, y=428) -countrychosen = Combobox(window, width = 7) +countrychosen = Combobox(window, width = 7) c=["India", "Malaysia", "Japan", "U.S.", "Germany"] countrychosen['values'] = c -countrychosen.current(0) +countrychosen.current(0) countrychosen.place(x=150, y=440) f1=tk.Label(window, text="HOBBIES", bg='#6449C6', fg='white', padx=10, pady=15) diff --git a/Week-5/Set4-hotelwithdatabase.py b/Week-5/Set4-hotelwithdatabase.py index bc0ada1..9036e37 100644 --- a/Week-5/Set4-hotelwithdatabase.py +++ b/Week-5/Set4-hotelwithdatabase.py @@ -56,20 +56,18 @@ def database(): def display(): db=sqlite3.connect('hotel.db') - + with db: cursor=db.cursor() my_w = tk.Tk() my_w.geometry("600x250") r_set=cursor.execute('''SELECT * from HOTEL '''); - i=0 - for HOTEL in r_set: + for i, HOTEL in enumerate(r_set): for j in range(len(HOTEL)): e = Entry(my_w, width=10, fg='blue') e.grid(row=i, column=j) e.insert(END, HOTEL[j]) - i=i+1 a = Label(window ,text = "Title ").grid(row = 0,column = 0,sticky = "NSEW") b = Label(window ,text = "First Name ").grid(row = 1,column = 0,sticky = "NSEW") c = Label(window ,text = "Last Name ").grid(row = 2,column = 0,sticky = "NSEW") @@ -119,7 +117,7 @@ def display(): line2 = Label(window, text="Total amount payable ZAR__ x_ nights = ZAR__ due to Arabella\n Hotel and Spa", bg="white") line3 = Label(window, text="Credit Card will be charged on receipt of this form and details will also be used to settle all incidentals not settle on\n departure. A copy of the final folio will be sent to you should there be any unsettled charges.", bg="white") line4 = Label(window, text="In order to qualify for the above rates, your booking needs to be made on or before 15th January 2016", bg="white") -line5 = Label(window, text="Terms and conditions can be found on the next page.", bg="white") +line5 = Label(window, text="Terms and conditions can be found on the next page.", bg="white") line6 = Label(window, text="The rate is valid for seven days before and after the conference dates. Check in time is 14:00 & check out time is 11:00", bg="white") line7 = Label(window, text="By your signature hereto, you are accepting all terms and conditions specified on this form and confirm that all information\n given is current and accurate.", bg="white") @@ -169,23 +167,21 @@ def display(): p103.grid(row = 28,column = 1,sticky = "NSEW") btn = Button(window ,text="SUBMIT",command = database,width=20).grid(row=30,column=2) -btn = Button(window,text="DISPLAY RECORD(s)",command=display,width=20).grid(row=30,column=3) +btn = Button(window,text="DISPLAY RECORD(s)",command=display,width=20).grid(row=30,column=3) window.mainloop() import sqlite3 my_conn = sqlite3.connect('hotel.db') -import tkinter as tk -from tkinter import * +import tkinter as tk +from tkinter import * my_w = tk.Tk() my_w.geometry("1300x250") r_set=my_conn.execute('''SELECT * from HOTEL '''); -i=0 # row value inside the loop -for HOTEL in r_set: +for i, HOTEL in enumerate(r_set): for j in range(len(HOTEL)): e = Entry(my_w, width=10, fg='blue') e.grid(row=i, column=j) e.insert(END, HOTEL[j]) - i=i+1 my_w.mainloop() \ No newline at end of file diff --git a/Week-5/displaySQL.py b/Week-5/displaySQL.py index c9e8e6f..509b50e 100644 --- a/Week-5/displaySQL.py +++ b/Week-5/displaySQL.py @@ -8,11 +8,9 @@ my_w.geometry("1300x250") my_w.title("Database") r_set = my_conn.execute('''SELECT * from STFORM3 ''') -i = 0 # row value inside the loop -for STFORM3 in r_set: +for i, STFORM3 in enumerate(r_set): for j in range(len(STFORM3)): e = Entry(my_w, width=10, fg='black') e.grid(row=i, column=j) e.insert(END, STFORM3[j]) - i = i+1 my_w.mainloop() diff --git a/Week-5/form.py b/Week-5/form.py index fe3bb59..e1f2ad4 100644 --- a/Week-5/form.py +++ b/Week-5/form.py @@ -53,13 +53,11 @@ def display(): window.geometry("400x250") select = cursor.execute('''SELECT * from STFORM3 ''') - i = 0 - for STFORM3 in select: + for i, STFORM3 in enumerate(select): for j in range(len(STFORM3)): e = Entry(my_w, width=10, fg='blue') e.grid(row=i, column=j) e.insert(END, STFORM3[j]) - i = i+1 an = Tk() diff --git a/Week-5/registration_form_with_sql.py b/Week-5/registration_form_with_sql.py index 1f53d25..de493db 100644 --- a/Week-5/registration_form_with_sql.py +++ b/Week-5/registration_form_with_sql.py @@ -45,8 +45,7 @@ def users_registered(): L17 = Label(window5,text="EMAIL").grid(row=0,column=5,pady=(60,0)) L17 = Label(window5,text="FULLNAME").grid(row=0,column=6,pady=(60,0)) cursor.execute("SELECT * FROM user_details") - i=1 - for users in cursor: + for i, users in enumerate(cursor, start=1): for j in range(len(users)): if(j==5): j+=1 @@ -58,7 +57,6 @@ def users_registered(): E11 = Entry(window5, width=25, fg='black') E11.grid(row=i, column=j) E11.insert(END, users[j]) - i=i+1 button_8 = Button(window5,text="QUIT",width=10,command=lambda: quit()).place(relx=0.5,rely=0.84,anchor=CENTER) diff --git a/Week-6/Mutliprogramming/Week06 Set08.py b/Week-6/Mutliprogramming/Week06 Set08.py index 0e4cfd7..4bc3c49 100644 --- a/Week-6/Mutliprogramming/Week06 Set08.py +++ b/Week-6/Mutliprogramming/Week06 Set08.py @@ -16,7 +16,7 @@ def producer_func(name): n = random.randint(10,20) if obj._value+n <= MAX: - for i in range(n): + for _ in range(n): obj.release() print(name, 'number of items: ', obj._value) sleep(0.2) @@ -28,7 +28,7 @@ def producer_func(name): def consumer_func(name): n = random.randint(1,20) if obj._value+n >= 0: - for i in range(n): + for _ in range(n): obj.acquire() print(name, 'number of items: ', obj._value) sleep(0.2) diff --git a/Week-6/Mutliprogramming/Week06 Set10.py b/Week-6/Mutliprogramming/Week06 Set10.py index 0ca898d..20e04f1 100644 --- a/Week-6/Mutliprogramming/Week06 Set10.py +++ b/Week-6/Mutliprogramming/Week06 Set10.py @@ -32,7 +32,7 @@ def Writer(): print("Writer couldn't write, as Reading or Writing is taking place.") if __name__ == '__main__': - for i in range(0, 10): + for _ in range(10): randomNumber = random.randint(0, 100) if(randomNumber > 50): threading1 = threading.Thread(target = Reader) diff --git a/Week-6/Socket/Set3/server3.py b/Week-6/Socket/Set3/server3.py index 011484d..5079fd6 100644 --- a/Week-6/Socket/Set3/server3.py +++ b/Week-6/Socket/Set3/server3.py @@ -15,6 +15,4 @@ mssg = c.recv(1024).decode() if mssg == 'PING': print('PONG') - else: - pass c.close() \ No newline at end of file diff --git a/Week-6/Socket/Set4/client4.py b/Week-6/Socket/Set4/client4.py index f1cfd3e..2f5dad8 100644 --- a/Week-6/Socket/Set4/client4.py +++ b/Week-6/Socket/Set4/client4.py @@ -76,7 +76,7 @@ # Some operating systems will indicate that using AGAIN, and some using WOULDBLOCK error code # We are going to check for both - if one of them - that's expected, means no incoming data, continue as normal # If we got different error code - something happened - if e.errno != errno.EAGAIN and e.errno != errno.EWOULDBLOCK: + if e.errno not in [errno.EAGAIN, errno.EWOULDBLOCK]: print('Reading error: {}'.format(str(e))) sys.exit() # We just did not receive anything