├── Ceaser Cipher ├── decrypt.py └── encrypt.py ├── File Handling ├── dummy.txt ├── product.csv ├── q10.py ├── q11.py ├── q12.py ├── q13.py ├── q14.py ├── q15.py ├── q16.py ├── q17.py ├── q18.py ├── q2 │ ├── binary.py │ └── linear.py ├── q20.py ├── q21.py ├── q22.py ├── q23.py ├── q3.py ├── q4.py ├── q5.py ├── q6.py ├── q7.py ├── q8.py ├── q9.py └── question15 │ ├── q1 │ ├── gcd.py │ ├── list.py │ ├── palindrome_string.py │ ├── prime.py │ └── reverse_string.py │ └── student.dat ├── Functions ├── Factorial_Of_Number.py ├── Fibonacci_Series.py ├── Leap Year Checker ├── LinuxTUI.py ├── checkLuck.py ├── guessingGame.py ├── leap.py ├── palindrome.py ├── q1.py └── weeklySalaryCalculator.py ├── Lists ├── q1.py ├── q10.py ├── q11.py ├── q12.py ├── q13.py ├── q14.py ├── q15.py ├── q16.py ├── q17.py ├── q18.py ├── q19.py ├── q2.py ├── q20.py ├── q21.py ├── q3.py ├── q4.py ├── q5.py ├── q6.py ├── q7.py ├── q8.py └── q9.py ├── Patterns ├── Pattern Numbers & Stars - 1.py ├── Pattern Numbers & Stars - 2.py └── ReverseNumberPyramid.py ├── Prime Numbers ├── Check Prime.py └── Prime Generator.py ├── Python Dictionary ├── q1.py ├── q10.py ├── q11.py ├── q12.py ├── q13.py ├── q14.py ├── q15.py ├── q16.py ├── q17.py ├── q18.py ├── q19.py ├── q2.py ├── q20.py ├── q3.py ├── q4.py ├── q5.py ├── q6.py ├── q7.py ├── q8.py └── q9.py ├── README.md ├── Strings ├── bulletadd.bat ├── bulletadd.py ├── pw.bat ├── pw.py ├── q1.py ├── q10.py ├── q11.py ├── q12.py ├── q13.py ├── q14.py ├── q15.py ├── q16.py ├── q17.py ├── q18.py ├── q19.py ├── q2.py ├── q20.py ├── q21.py ├── q3.py ├── q4.py ├── q5.py ├── q6.py └── q9.py └── pythonlogo.jpg /Ceaser Cipher/decrypt.py: -------------------------------------------------------------------------------- 1 | # Program to decrypt ceaser cipher 2 | 3 | text = input("Message > ").upper() 4 | 5 | abc = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ" 6 | 7 | for i in range(28): 8 | decrypt = "" 9 | for l in text: 10 | if l in abc: 11 | pos_let = abc.index(l) 12 | new_pos = (pos_let - i) % len(abc) 13 | decrypt += abc[new_pos] 14 | else: 15 | descrypt+= l 16 | msj = (f"ROT-{i}:", decrypt) 17 | print(msj) -------------------------------------------------------------------------------- /Ceaser Cipher/encrypt.py: -------------------------------------------------------------------------------- 1 | # Program to encrypt ceaser cipher 2 | 3 | text = input("Message > ").upper() 4 | n = int(input("Displacement > ")) 5 | abc = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ" 6 | cipher = "" 7 | 8 | for l in text: 9 | if l in abc: 10 | pos_let = abc.index(l) 11 | new_pos = (pos_let + n) % len(abc) 12 | cipher+= abc[new_pos] 13 | else: 14 | cipher+= l 15 | 16 | print(cipher) -------------------------------------------------------------------------------- /File Handling/dummy.txt: -------------------------------------------------------------------------------- 1 | this 2 | is a line 3 | which is 4 | aweomse 5 | -------------------------------------------------------------------------------- /File Handling/product.csv: -------------------------------------------------------------------------------- 1 | 132,hello,14,41 2 | 3151,gsj,15,135 3 | -------------------------------------------------------------------------------- /File Handling/q10.py: -------------------------------------------------------------------------------- 1 | # Create a function showEmployee() in such a way that it should accept employee 2 | # name, and it’s salary and display both, and if the salary is missing in function call it 3 | # should show it as 30000 4 | 5 | def show_employee(name, salary=30000): 6 | employee = { 7 | "Name": name, 8 | "Salary":salary 9 | } 10 | return employee -------------------------------------------------------------------------------- /File Handling/q11.py: -------------------------------------------------------------------------------- 1 | with open("dummy.txt","r+") as file: 2 | read = file.readlines() 3 | for i in read: 4 | print("#" + i) -------------------------------------------------------------------------------- /File Handling/q12.py: -------------------------------------------------------------------------------- 1 | # Read a text file and display the number of vowels/ consonants/ 2 | # uppercase/ lowercase characters present in the file. 3 | 4 | vowels ={'a','i','o','e','u'} 5 | with open("dummy.txt","r+") as file: 6 | lines = file.readlines() 7 | letters = file.read() 8 | 9 | def show_vowels(): 10 | for word in lines: 11 | for letter in word: 12 | if letter.lower() in vowels: 13 | print("Found: " + letter) 14 | def show_consonants(): 15 | for word in lines: 16 | for letter in word: 17 | if letter.lower() in vowels: 18 | print("Found: " + letter) 19 | def show_uppercase(): 20 | count = 0 21 | for word in lines: 22 | for letter in word: 23 | if letter != letter.lower(): 24 | count += 1 25 | print(f"there are {count} upper letters") 26 | def show_lowercase(): 27 | count = 0 28 | for word in lines: 29 | for letter in word: 30 | if letter == letter.lower(): 31 | count += 1 32 | print(f"there are {count} lower letters") 33 | 34 | show_vowels() -------------------------------------------------------------------------------- /File Handling/q13.py: -------------------------------------------------------------------------------- 1 | with open("dummy.txt","r+") as file: 2 | lines = file.readlines() 3 | freq = {} 4 | for i in lines: 5 | if i not in freq: 6 | freq[i] = 1 7 | else: 8 | freq[i] += 1 9 | 10 | 11 | vals = freq.values() 12 | Keys = freq.keys() 13 | max_val = 0 14 | max_idx = 0 15 | for i in range(len(vals)): 16 | if vals[i] > max_val: 17 | max_val = vals[i] 18 | max_idx = i 19 | max_key = Keys[max_idx] 20 | print("The most common word is " + max_key) 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /File Handling/q14.py: -------------------------------------------------------------------------------- 1 | # 14.Remove all the lines that start with character `I' or ‘i’ in a file and write it 2 | # to another file. 3 | 4 | with open("dummy.txt","r+") as file: 5 | lines = file.readlines() 6 | with open("newfile.txt","r+") as file_2: 7 | writer = [] 8 | for line in lines: 9 | if line[0].lower() != "i": 10 | writer.append(line) 11 | 12 | file_2.write(writer) 13 | 14 | -------------------------------------------------------------------------------- /File Handling/q15.py: -------------------------------------------------------------------------------- 1 | """ 2 | Create a binary file with name and roll number and marks of students(represented 3 | using a dictionary).Write a complete Menu Driven program to perform following 4 | operations(define a function for each operation): 5 | ● Inserting/Appending records to a binary file 6 | ● Reading records from a binary file 7 | ● Searching a record in a binary file 8 | ● Updating a record in a binary file 9 | ● Deleting a record from a binary file 10 | """ 11 | 12 | import pickle as p 13 | 14 | def get_records_to_add(): 15 | data_to_be_added = [] 16 | while True: 17 | name = input("Enter student's name: ") 18 | roll_number = int(input(f"Enter {name}'s roll number: ")) 19 | marks = int(input(f"Please enter {name}'s marks: ")) 20 | new_student = { 21 | "Name":name, 22 | "Roll Number":roll_number, 23 | "Marks":marks, 24 | } 25 | data_to_be_added.append(new_student) 26 | choice = input("Want to add another student?: ").lower() 27 | if 'n' in choice: 28 | break 29 | return data_to_be_added 30 | 31 | def add_record_to_binaryfile(list_of_data): 32 | with open("student.dat","wb") as file: 33 | p.dump(list_of_data,file) 34 | print("Succesfully added data to file!") 35 | 36 | def read_binary_file(): 37 | with open("student.dat","rb") as f: 38 | student_data_to_read = p.load(f) 39 | for student in student_data_to_read: 40 | name = student["Name"] 41 | roll_number = student["Roll Number"] 42 | marks = student["Marks"] 43 | print(f"Name of the Student is: {name}.\n {name}'s' roll number is {roll_number} & he scored {marks} marks") 44 | 45 | def search_binary_file(): 46 | with open("student.dat","rb+") as file: 47 | data = p.load(file) 48 | flag = True 49 | to_search = int(input("Enter Roll Number to Search For: ")) 50 | for info in data: 51 | if info["Roll Number"] == to_search: 52 | student_name = info["Name"] 53 | print(f"Data match found for {student_name}") 54 | flag = False 55 | break 56 | if flag == True: 57 | print("Couldnt find that roll number") 58 | 59 | def update_binary_file(): 60 | with open("student.dat","rb+") as file: 61 | students_info = p.load(file) 62 | flag = True 63 | roll_number = int(input("Enter roll number for student to update their name: ")) 64 | for student in students_info: 65 | if student["Roll Number"] == roll_number: 66 | name = student["Name"] 67 | print(f"Orignal name is: {name}") 68 | new_name = input("Enter new name to be updated: ") 69 | student["Name"] = new_name 70 | flag = False 71 | break 72 | if flag == False: 73 | file.seek(0) 74 | p.dump(students_info,file) 75 | print("Done Bro!") 76 | 77 | def delete_binary_file(): 78 | deleted_name = input("Enter name for which info is to be deleted: ") 79 | with open("student.dat","rb+") as file: 80 | entire_file_data = p.load(file) 81 | with open("student.dat","rb+") as file: 82 | for student in entire_file_data: 83 | if student["Name"] == deleted_name: 84 | continue 85 | p.dump(student,file) 86 | 87 | if __name__ == "__main__": 88 | while True: 89 | print("Press 1 for Inserting a Record") 90 | print("Press 2 for Reading a Record") 91 | print("Press 3 for Searching a Record") 92 | print("Press 4 for Updating a Record") 93 | print("Press 5 for Deleting a Record") 94 | option = int(input()) 95 | if option == 1: 96 | list_of_data = get_records_to_add() 97 | add_record_to_binaryfile(list_of_data) 98 | elif option == 2: 99 | read_binary_file() 100 | elif option == 3: 101 | search_binary_file() 102 | elif option == 4: 103 | update_binary_file() 104 | else: 105 | delete_binary_file() 106 | 107 | 108 | ch = input("Do you want to continue?: ").lower() 109 | if "n" in ch: 110 | break -------------------------------------------------------------------------------- /File Handling/q16.py: -------------------------------------------------------------------------------- 1 | import pickle as p 2 | 3 | 4 | def get_records_to_add(): 5 | data_to_be_added = [] 6 | while True: 7 | book_title = input("Enter student's name: ") 8 | book_number = int(input(f"Enter {book_title}'s number: ")) 9 | book_price = int(input(f"Please enter {book_title}'s price: ")) 10 | new_book = { 11 | "Title":book_title, 12 | "Number":book_number, 13 | "Cost":book_price 14 | } 15 | data_to_be_added.append(new_book) 16 | choice = input("Want to add another book?: ").lower() 17 | if 'n' in choice: 18 | break 19 | return data_to_be_added 20 | 21 | def add_record_to_binaryfile(list_of_data): 22 | with open("book.dat","wb") as file: 23 | p.dump(list_of_data,file) 24 | print("Succesfully added data to file!") 25 | 26 | def update_binary_file(): 27 | with open("book.dat","rb+") as file: 28 | book_info = p.load(file) 29 | flag = True 30 | book_title = int(input("Enter roll number for student to update their name: ")) 31 | for book in book_info: 32 | if book["Title"] == book_title: 33 | name = book["Title"] 34 | print(f"Orignal name is: {name}") 35 | new_name = input("Enter new name to be updated: ") 36 | book["Title"] = new_name 37 | flag = False 38 | break 39 | if flag == False: 40 | file.seek(0) 41 | p.dump(book_info,file) 42 | print("Done Bro!") 43 | -------------------------------------------------------------------------------- /File Handling/q17.py: -------------------------------------------------------------------------------- 1 | # Creating the file 2 | import pickle 3 | result = [] 4 | with open("flights.dat",'wb') as file: 5 | while True: 6 | flight_number = int(input("enter flight number please: ")) 7 | airline_name = input("enter airline name please: ") 8 | departure = (input("enter departure time: ")) 9 | arrival = (input("enter time of arrival")) 10 | data = [flight_number, airline_name, departure, arrival] 11 | result.append(data) 12 | choice = input("continue? ").upper() 13 | if "N" in choice: 14 | break 15 | 16 | with open("flights.dat", "wb") as f: 17 | pickle.dump(result, f) 18 | print("record addded :))") 19 | 20 | 21 | #Checking for dubai 22 | with open("flights.dat",'rb') as f: 23 | details = pickle.load(f) 24 | for i in details: 25 | if i[2] == "Dubai": 26 | print(i) -------------------------------------------------------------------------------- /File Handling/q18.py: -------------------------------------------------------------------------------- 1 | import csv 2 | def write_to_file(): 3 | result = [] 4 | 5 | while True: 6 | prod_id = int(input("enter prod id please: ")) 7 | prod_name = input("enter prod name please: ") 8 | prod_price = int(input("enter price : ")) 9 | prod_qty = int(input("enter quantity ")) 10 | data = [prod_id,prod_name,prod_qty,prod_price] 11 | result.append(data) 12 | choice = input("continue? ").upper() 13 | if "N" in choice: 14 | break 15 | 16 | with open("product.csv",'w') as f: 17 | write = csv.writer(f,delimiter =',') 18 | for each in result: 19 | write.writerow(each) 20 | 21 | 22 | #to display 23 | def show_file(): 24 | with open("product.csv",'r') as f: 25 | read = csv.reader(f) 26 | for line in read: 27 | print(line) 28 | 29 | write_to_file() 30 | show_file() -------------------------------------------------------------------------------- /File Handling/q2/binary.py: -------------------------------------------------------------------------------- 1 | target = int(input("Enter number to search for: ")) 2 | 3 | the_list = [1,2,3,4,5,6,7,8,9] 4 | 5 | low = 0 6 | high = len(the_list) 7 | def binary_search(the_list,target,low,high): 8 | mid = (low + high)//2 + low 9 | if(the_list[mid] == target): 10 | return mid 11 | if the_list[mid] > target: 12 | return binary_search(the_list,target,low,mid-1) 13 | else: 14 | return binary_search(the_list,target,mid+1,high) 15 | 16 | answer = binary_search(the_list,target,low,high) 17 | print(answer) -------------------------------------------------------------------------------- /File Handling/q2/linear.py: -------------------------------------------------------------------------------- 1 | target = int(input("Enter number to search for: ")) 2 | 3 | the_list = [1,1,15,15,632,13,2,1,62,3,6,1] 4 | 5 | def linear_search(list,target): 6 | for i in range(len(the_list)): 7 | if the_list[i] == target: 8 | return i 9 | linear_search(the_list,target) -------------------------------------------------------------------------------- /File Handling/q20.py: -------------------------------------------------------------------------------- 1 | import csv 2 | #to display comma seperated 3 | 4 | def show_file(): 5 | with open("product.csv",'r') as f: 6 | read = csv.reader(f) 7 | for line in read: 8 | print(','.join(line)) 9 | -------------------------------------------------------------------------------- /File Handling/q21.py: -------------------------------------------------------------------------------- 1 | # 21. Write a random number generator that generates random numbers 2 | # between 1 and 6 (simulates a dice). 3 | 4 | 5 | import random 6 | print(random.randint(1, 6)) -------------------------------------------------------------------------------- /File Handling/q22.py: -------------------------------------------------------------------------------- 1 | # Write a menu drivenPython program to implement a Stack of integers,floating point 2 | # numbers and strings using a list data-structure.(Push ,Pop n Display) 3 | 4 | stack = [] 5 | def push(item): 6 | stack.append(item) 7 | 8 | 9 | def top(): 10 | if len(stack) != 0 : 11 | print(stack[-1]) 12 | else: 13 | print("Stack is empty") 14 | 15 | def pop_top(): 16 | if len(stack) != 0: 17 | print("Now Popping...") 18 | stack.pop() 19 | else: 20 | print("Not possible on Empty Stack") 21 | 22 | def clear_stack(): 23 | stack.clear() 24 | 25 | def display_stack(): 26 | for element in range(len(stack)-1,-1,-1): 27 | print(stack[element]) 28 | 29 | 30 | while True: 31 | print("Press 1 to Push to Stack") 32 | print("Press 2 to Pop from Stack") 33 | print("Press 3 to show Stack") 34 | print("Press 4 to get Top of Stack") 35 | print("Press 5 to clear a Stack") 36 | 37 | first_choice = int(input()) 38 | if first_choice == 1: 39 | item= input("Enter Item to add: ") 40 | push(item) 41 | elif first_choice == 2: 42 | pop_top() 43 | elif first_choice == 3: 44 | display_stack() 45 | elif first_choice == 4: 46 | top() 47 | elif first_choice == 5: 48 | clear_stack() 49 | else: 50 | print("invalid input\n try again?") 51 | 52 | second_choice = input("Another Operation? (y/n)").lower() 53 | if 'n' in second_choice : 54 | break -------------------------------------------------------------------------------- /File Handling/q23.py: -------------------------------------------------------------------------------- 1 | # 23. Write a complete menu driven Python program to implement a Stack of Employees 2 | # containing ENo,Ename n Esalary.(Push ,Pop n Display) 3 | 4 | stack_of_employees = [] 5 | 6 | 7 | def add_employee(): 8 | name = input("Enter student's name: ") 9 | emp_number = int(input(f"Enter {name}'s employee number: ")) 10 | emp_salary = int(input(f"Please enter {name}'s salary: ")) 11 | data = { 12 | "Employee Name" : name, 13 | "Emp Number" : emp_number, 14 | "Emp Salary" : emp_salary 15 | } 16 | stack_of_employees.append(data) 17 | 18 | 19 | def latest_employee(): 20 | if len(stack_of_employees) != 0 : 21 | print(stack_of_employees[-1]) 22 | else: 23 | print("Stack is empty") 24 | 25 | def pop_top(): 26 | if len(stack_of_employees) != 0: 27 | print("Now Popping...") 28 | stack_of_employees.pop() 29 | else: 30 | print("Not possible on Empty Stack") 31 | 32 | def clear_stack(): 33 | stack_of_employees.clear() 34 | 35 | def display_stack(): 36 | for element in range(len(stack_of_employees)-1,-1,-1): 37 | print(stack_of_employees[element]) 38 | 39 | def top(): 40 | print(stack_of_employees[-1]) 41 | 42 | while True: 43 | print("Press 1 to Add Emp") 44 | print("Press 2 to Remove Latest Emp") 45 | print("Press 3 to Show All Emp") 46 | print("Press 4 to Show Latest Emp") 47 | print("Press 5 to fire Eveyone") 48 | 49 | first_choice = int(input()) 50 | if first_choice == 1: 51 | add_employee() 52 | elif first_choice == 2: 53 | pop_top() 54 | elif first_choice == 3: 55 | display_stack() 56 | elif first_choice == 4: 57 | top() 58 | elif first_choice == 5: 59 | clear_stack() 60 | else: 61 | print("invalid input\n try again?") 62 | 63 | second_choice = input("Another Operation? (y/n)").lower() 64 | if 'n' in second_choice : 65 | break -------------------------------------------------------------------------------- /File Handling/q3.py: -------------------------------------------------------------------------------- 1 | # 3. Define a function SIMINTEREST( ) in Python to demonstrate the use of : 2 | # ● Positional/Required arguments 3 | # ● Default Arguments 4 | # ● Keyword/Named Arguments 5 | # ● Multiple Arguments 6 | 7 | def interest_required(principal,rate,time): 8 | answer = (principal * rate * time)/100 9 | print(f"the interst is: {answer}") 10 | 11 | def interst_default(principal,rate=4,time=2): 12 | answer = (principal * rate * time)/100 13 | print(f"the interst is: {answer}") 14 | 15 | def interest_named(principal,rate,time): 16 | answer = (principal * rate * time)/100 17 | print(f"the interst is: {answer}") 18 | 19 | interest_named(rate=3,time=1,principal=1000) 20 | 21 | def interst_multiple(*details): 22 | principal = details[0] 23 | rate = details[1] 24 | time = details[2] 25 | answer = (principal * rate * time)/100 26 | print(f"the interst is: {answer}") 27 | -------------------------------------------------------------------------------- /File Handling/q4.py: -------------------------------------------------------------------------------- 1 | def insertion_asscending(arr): 2 | for i in range(1, len(arr)): 3 | key = arr[i] 4 | j = i-1 5 | while j >= 0 and key < arr[j] : 6 | arr[j + 1] = arr[j] 7 | j -= 1 8 | arr[j + 1] = key 9 | 10 | 11 | 12 | arr = [14,15,9,1,7,10] 13 | insertion_asscending(arr) 14 | print(arr) 15 | -------------------------------------------------------------------------------- /File Handling/q5.py: -------------------------------------------------------------------------------- 1 | def insertion_desc(arr): 2 | for i in range(1, len(arr)): 3 | key = arr[i] 4 | j = i-1 5 | while j >= 0 and key > arr[j] : 6 | arr[j + 1] = arr[j] 7 | j -= 1 8 | arr[j + 1] = key 9 | 10 | 11 | 12 | arr = [14,15,9,1,7,10] 13 | insertion_desc(arr) 14 | print(arr) 15 | -------------------------------------------------------------------------------- /File Handling/q6.py: -------------------------------------------------------------------------------- 1 | def count(string): 2 | lower_count = 0 3 | upper = 0 4 | for i in string: 5 | if i == i.lower(): 6 | lower_count+=1 7 | else: 8 | upper+=1 9 | 10 | print(f"there are {lower_count} lower case alphabets and {upper} uppercase") -------------------------------------------------------------------------------- /File Handling/q7.py: -------------------------------------------------------------------------------- 1 | def add_string(string1,string2): 2 | print(string1+string2) 3 | 4 | add_string("Jivansh","Sharma") -------------------------------------------------------------------------------- /File Handling/q8.py: -------------------------------------------------------------------------------- 1 | # Define a function which can generate and print a list where the values are square of 2 | 3 | # numbers between 1 and 20 (both included). 4 | 5 | def generate(): 6 | out = [] 7 | for i in range(21): 8 | out.append(i**2) 9 | return out -------------------------------------------------------------------------------- /File Handling/q9.py: -------------------------------------------------------------------------------- 1 | # With a given tuple (1,2,3,4,5,6,7,8,9,10), define a function to print the first half values 2 | # in one line and the second half values in another line. 3 | 4 | tup = (1,2,3,4,5,6,7,8,9,10) 5 | def seperate(tup): 6 | mid = len(tup)//2 7 | for i in range(mid): 8 | print(tup[i], end=' ') 9 | print() 10 | for j in range(mid,len(tup)): 11 | print(tup[j], end=' ') 12 | 13 | 14 | seperate(tup) -------------------------------------------------------------------------------- /File Handling/question15/q1/gcd.py: -------------------------------------------------------------------------------- 1 | def find_the_hcf_using_recursion(first, second): 2 | if first == second: 3 | return first 4 | if first == 0 or second == 0: 5 | return 0 6 | 7 | if first > second: 8 | return find_the_hcf_using_recursion(first-second,second) 9 | return find_the_hcf_using_recursion(first, second-first) 10 | 11 | def find_the_hcf(first, second): 12 | if first < 0: 13 | first = -first 14 | if second<0: 15 | second = -second 16 | while(first!=second): 17 | if first>second: 18 | first-=second 19 | else: 20 | second-=first 21 | return first 22 | 23 | 24 | 25 | print(find_the_hcf_using_recursion(81,153)) -------------------------------------------------------------------------------- /File Handling/question15/q1/list.py: -------------------------------------------------------------------------------- 1 | def unique(a_list): 2 | new_list = [] 3 | for i in a_list: 4 | if i not in new_list: 5 | new_list.append(i) 6 | 7 | return new_list 8 | 9 | my_list = [1,12,41,1,5,15,156,156,153,1,135,9] 10 | print(unique(my_list)) 11 | -------------------------------------------------------------------------------- /File Handling/question15/q1/palindrome_string.py: -------------------------------------------------------------------------------- 1 | def reverse_string(string): 2 | output = "" 3 | for i in range(len(string)-1,-1,-1): 4 | output += string[i] 5 | return output 6 | 7 | def check_palindrome(string): 8 | reversed_version = reverse_string(string) 9 | if string == reversed_version: 10 | print("Palindrome") 11 | return 12 | else: 13 | print("Nope") 14 | return 15 | 16 | 17 | 18 | check_palindrome("lalal") -------------------------------------------------------------------------------- /File Handling/question15/q1/prime.py: -------------------------------------------------------------------------------- 1 | def is_prime(number): 2 | for i in range(2,number): 3 | if number % i == 0 : 4 | print("not prime") 5 | return 6 | print("prime") 7 | 8 | is_prime(24) -------------------------------------------------------------------------------- /File Handling/question15/q1/reverse_string.py: -------------------------------------------------------------------------------- 1 | def reverse_string(string): 2 | output = "" 3 | for i in range(len(string)-1,-1,-1): 4 | output += string[i] 5 | return output 6 | 7 | 8 | to_be_reversed = "Jivansh" 9 | answer = reverse_string(to_be_reversed) 10 | print(answer) -------------------------------------------------------------------------------- /File Handling/question15/student.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Easyvipin/Practice-Python/b76d7db03e626248437fd31f4ff4ef37c4fc1b26/File Handling/question15/student.dat -------------------------------------------------------------------------------- /Functions/Factorial_Of_Number.py: -------------------------------------------------------------------------------- 1 | # Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument. 2 | def factorial(n): 3 | if n == 0: 4 | return 1 5 | else: 6 | return n * factorial(n-1) 7 | n=int(input("Input a number to compute the factiorial : ")) 8 | print(factorial(n)) -------------------------------------------------------------------------------- /Functions/Fibonacci_Series.py: -------------------------------------------------------------------------------- 1 | n=int(input("Enter the number of terms ")) 2 | a,b = 0, 1 3 | count=0 4 | 5 | # check if the number of terms is valid 6 | if n<= 0: 7 | print("Please enter a positive integer") 8 | elif n == 1: 9 | print("Fibonacci sequence :") 10 | print(a) 11 | else: 12 | print("Fibonacci sequence:") 13 | while count < n: 14 | print(a) 15 | c=a + b 16 | a = b 17 | b = c 18 | count += 1 19 | -------------------------------------------------------------------------------- /Functions/Leap Year Checker: -------------------------------------------------------------------------------- 1 | def leapYearChecker(): 2 | year = int(input("Enter a year: ")) 3 | 4 | if year % 4 == 0: 5 | if year % 100 == 0: 6 | if year % 400 == 0: 7 | print(f"{year} is a leap year") 8 | else: 9 | print(f"{year} is not a leap year") 10 | else: 11 | print(f"{year} is a leap year") 12 | else: 13 | print(f"{year} is not a leap year") 14 | 15 | print(leapYearChecker()) 16 | -------------------------------------------------------------------------------- /Functions/LinuxTUI.py: -------------------------------------------------------------------------------- 1 | import os 2 | import getpass 3 | os.system("tput setaf 2") 4 | 5 | print("""\t\t\tWelcome To My TUI which makes work easy 6 | \t\t\t----------------------------------------""") 7 | 8 | os.system("tput setaf 7") 9 | 10 | while True: 11 | pwd = getpass.getpass("enter your password: ") 12 | crct_pwd = "root" 13 | if pwd != crct_pwd: 14 | print("Incorrect password,Try again") 15 | continue 16 | else: 17 | break 18 | 19 | 20 | location = input("Where do you want to run your Program (local/remote)") 21 | 22 | if location == "remote": 23 | print(location) 24 | remote_ip = input("Enter host IP: ") 25 | os.system("ssh-copy-id root@{}".format(remote_ip )) 26 | elif location == "local": 27 | print(location) 28 | else: 29 | print("option not valid") 30 | quit() 31 | 32 | 33 | while True: 34 | input("Press Enter to Continue") #to make the input stay on screen 35 | os.system("clear") 36 | print("""press 1 to see date 37 | press 2 to see calendar 38 | press 3 to create web server 39 | Press 4 to give a Custom command 40 | Press 7 : to exit""") 41 | 42 | x=int(input("enter your choice: ")) 43 | if location == "local": 44 | if x==1: 45 | os.system("date") 46 | elif x==2: 47 | os.system("cal") 48 | elif x==3: 49 | os.system("systemctl start httpd") 50 | print("Go to http://10.0.2.15 to access the server") 51 | elif x==4: 52 | cmd = input("Enter your Command : ") 53 | os.system("{}".format(cmd)) 54 | elif x==7: 55 | exit() 56 | else: 57 | print("Invalid Option") 58 | else: 59 | 60 | if x==1: 61 | os.system("ssh {} date".format(remote_ip)) 62 | elif x==2: 63 | os.system("ssh {} cal".format(remote_ip)) 64 | elif x==4: 65 | cmd= input("Enter your Command : ") 66 | os.system("ssh {} {}".format(remote_ip,cmd)) 67 | elif x==7: 68 | exit() 69 | else: 70 | print("Invalid Option") 71 | -------------------------------------------------------------------------------- /Functions/checkLuck.py: -------------------------------------------------------------------------------- 1 | # Whats on your luck 2 | print("Type your Lucky no") 3 | yourNumber = int(input()) 4 | 5 | # function 6 | 7 | 8 | def checkLuck(yourNumber): 9 | if yourNumber == 1: 10 | print("happy person") 11 | elif yourNumber == 2: 12 | print("lucky person") 13 | elif yourNumber == 3: 14 | print("strong person") 15 | elif yourNumber == 4: 16 | print("RESPECTFUL") 17 | elif yourNumber == 5: 18 | print("sports person") 19 | 20 | 21 | if(yourNumber > 0 and yourNumber <= 5): 22 | checkLuck(yourNumber) 23 | else: 24 | print("Enter no between 1 and 5") 25 | -------------------------------------------------------------------------------- /Functions/guessingGame.py: -------------------------------------------------------------------------------- 1 | import random # random is a python module used to generate random numbers, and choose a random value from lists, etc. 2 | 3 | def start_game(tries): 4 | """This function takes a `tries` argument. This is the amount of attempts until the game is over""" 5 | user_tries = 0 6 | 7 | random_number = random.randint(0, 10) 8 | 9 | while user_tries < tries: 10 | user_tries += 1 # Add 1 to the try counter. 11 | 12 | guess = int(input("Guess: ")) # Get the user's guess. 13 | 14 | if guess == random_number: 15 | # The user guessed correctly 16 | print("Correct! The random number was", random_number) 17 | 18 | return 19 | 20 | print("Incorrect!") 21 | 22 | print("You did not guess within the correct amount of tries! The number was", random_number) 23 | 24 | start_game(10) 25 | -------------------------------------------------------------------------------- /Functions/leap.py: -------------------------------------------------------------------------------- 1 | def leap_year(year): 2 | if year < 1900: 3 | return "Please Enter a year after 1900 ONLY" 4 | leap = False 5 | if year % 4 == 0: 6 | leap = True 7 | if year % 100 == 0: 8 | leap = False 9 | if year % 400 ==0: 10 | leap = True 11 | return leap 12 | -------------------------------------------------------------------------------- /Functions/palindrome.py: -------------------------------------------------------------------------------- 1 | # A palindrome is nothing but any number or a string which remains unaltered when reversed. 2 | def palindrome(): 3 | # check if user input is palindrome or not 4 | Input = input("Enter a number or string\t") 5 | if Input.isdigit(): 6 | Input = int(Input) 7 | rev = 0 # Reverse of number 8 | temp = Input 9 | while Input > 0: #Loop through number backwards 10 | rem = Input % 10 11 | rev = rev * 10 + rem 12 | Input = Input // 10 13 | if temp == rev: 14 | print(str(temp) + " is a Palindrome number") 15 | else: 16 | print(str(temp) + " is NOT a palindrome number") 17 | else: 18 | if Input == Input[::-1]: 19 | print(Input + " is a palindrome") 20 | else: 21 | print(Input + " is NOT a palindrome") 22 | 23 | 24 | palindrome() 25 | -------------------------------------------------------------------------------- /Functions/q1.py: -------------------------------------------------------------------------------- 1 | #Write a function to check if a string is palindrome or not 2 | def palstr (sarg): 3 | '''takes a string argument''' 4 | if len(sarg) % 2 == 0: 5 | mid = int(len(sarg)/2) 6 | else: 7 | mid = int(len(sarg)/2 + 1) 8 | flag = 0 9 | for i in range (0, mid): 10 | if sarg [i] != sarg[len(sarg)-1-i] : 11 | flag = 0 12 | break 13 | else: 14 | flag = 1 15 | 16 | 17 | if flag == 0: 18 | return False 19 | else: 20 | return True 21 | inp = input("enter to check") 22 | palstr(inp) 23 | -------------------------------------------------------------------------------- /Functions/weeklySalaryCalculator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Input name, hours worked per week, and hourly salary. 3 | If hours worked > 40, multiply the following hours' salary by 1.5 4 | """ 5 | print(__doc__) 6 | 7 | employeeName = (input("Enter your name: ")) 8 | hoursWorked = float(input("Enter how many hours you work per week:")) 9 | hourlyRate = float(input("Enter your hourly rate: ")) 10 | maxHoursBeforeOvertime = 40 11 | 12 | 13 | if hoursWorked > 40: 14 | totalPay = (float)(((hoursWorked-40)*(hourlyRate*1.5)) + (maxHoursBeforeOvertime * hourlyRate)) 15 | 16 | else: 17 | totalPay = (float)(hoursWorked * hourlyRate) 18 | print(totalPay) 19 | 20 | print( employeeName, "should be paid ${:,.2f} per week".format(totalPay)) 21 | 22 | 23 | -------------------------------------------------------------------------------- /Lists/q1.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to sum all the items in a list. 2 | def findSum(numberList): 3 | sum = 0 4 | for count in numberList: 5 | sum += count 6 | return sum 7 | 8 | 9 | number = [1, 2, 3, 4, 5] 10 | # CALL 11 | totalSum = findSum(number) 12 | print(totalSum) 13 | 14 | # 15 15 | -------------------------------------------------------------------------------- /Lists/q10.py: -------------------------------------------------------------------------------- 1 | # Write a Python function that takes two lists and returns True if they have at least one common member. 2 | 3 | list1 = ["vipin", "nitin", "akash", "john"] 4 | list2 = ["lorem", "ipsum", "john", "nitin"] 5 | 6 | # function 7 | 8 | 9 | def checkCommon(list1, list2): 10 | for check in list1: 11 | if check in list2: 12 | return True 13 | break 14 | return False 15 | 16 | 17 | istrue = checkCommon(list1, list2) 18 | 19 | # True 20 | -------------------------------------------------------------------------------- /Lists/q11.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Easyvipin/Practice-Python/b76d7db03e626248437fd31f4ff4ef37c4fc1b26/Lists/q11.py -------------------------------------------------------------------------------- /Lists/q12.py: -------------------------------------------------------------------------------- 1 | # . Write a Python program to convert a list of characters into a string. 2 | 3 | 4 | def makeString(givenList): 5 | newWord = "" 6 | return newWord.join(givenList) 7 | 8 | 9 | # joins is method takes all item in iterable and joins them in one string 10 | charList = ['v', 'i', 'p', 'i', 'n'] 11 | newString = makeString(charList) 12 | print(newString) 13 | 14 | # vipin 15 | -------------------------------------------------------------------------------- /Lists/q13.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to append a list to the second list. 2 | 3 | list2 = ["vipin", "21", "123 street"] 4 | list1 = ["india", "1234001", "hindu"] 5 | 6 | for check in list2: 7 | list1.append(check) 8 | 9 | print(list1) 10 | 11 | #['india', '1234001', 'hindu', 'vipin', '21', '123 street'] 12 | -------------------------------------------------------------------------------- /Lists/q14.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to select an item randomly from a list. 2 | 3 | import random 4 | 5 | list1 = ["vipin", "123 street", "india"] 6 | 7 | randItem = list1[random.randint(0, len(list1)-1)] 8 | print(randItem) 9 | -------------------------------------------------------------------------------- /Lists/q15.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to change the position of every n-th value with the (n+1)th in a list 2 | # Sample list: [0,1,2,3,4,5] 3 | # Expected Output: [1, 0, 3, 2, 5, 4] 4 | 5 | 6 | list1 = [6, 1, 2, 3, 4, 5] 7 | totalLength = len(list1) - 1 8 | 9 | for i in range(0, len(list1), 2): 10 | item = list1[i] 11 | indexItem = list1.index(item) 12 | if indexItem < totalLength: 13 | prevItem = list1[indexItem] 14 | nextItem = list1[indexItem + 1] 15 | list1[indexItem + 1] = prevItem 16 | list1[indexItem] = nextItem 17 | 18 | print(list1) 19 | 20 | 21 | #[1, 2, 3, 4, 5, 6] 22 | -------------------------------------------------------------------------------- /Lists/q16.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to find missing and additional values in two lists. Go to the editor 2 | # Sample data : Missing values in second list: b,a,c 3 | # Additional values in second list: g,h 4 | 5 | mainList = ['a', 'b', 'c', 'd', 'g', 'h', 'j'] 6 | list1 = ['a', 'b', 'e', 'f'] 7 | missingValues = [] 8 | addValues = [] 9 | 10 | for item in list1: 11 | if item not in mainList: 12 | missingValues.append(item) 13 | for item in mainList: 14 | if item not in list1: 15 | addValues.append(item) 16 | 17 | print(missingValues) 18 | print(addValues) 19 | -------------------------------------------------------------------------------- /Lists/q17.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to split a list into different variables. 2 | 3 | cssValues = ["black", "grid", "20px"] 4 | color, display, fontSize = cssValues 5 | 6 | print(color) 7 | print(display) 8 | print(fontSize) 9 | -------------------------------------------------------------------------------- /Lists/q18.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to generate groups of five consecutive numbers in a list. 2 | 3 | list1 = [1, 2, 3, 4, 5, 7, 6, 9, 10] 4 | consList = [] 5 | 6 | for item in list1: 7 | indexItem = list1.index(item) 8 | if item + 1 == list1[indexItem+1]: 9 | consList.append(item) 10 | else: 11 | consList.append(item) 12 | break 13 | print(consList) 14 | #[1, 2, 3, 4, 5] 15 | -------------------------------------------------------------------------------- /Lists/q19.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to insert an element at a specified position into a given list. 2 | 3 | list1 = [1, 2, 5, 6, 7] 4 | list1.insert(1, 5) 5 | print(list1) 6 | -------------------------------------------------------------------------------- /Lists/q2.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to multiplies all the items in a list. 2 | 3 | 4 | def findProduct(numberList): 5 | product = 1 6 | for count in numberList: 7 | product *= count 8 | return product 9 | 10 | 11 | number = [1, 2, 3, 4, 5] 12 | # CALL 13 | totalProduct = findProduct(number) 14 | print(totalProduct) 15 | 16 | #120 17 | -------------------------------------------------------------------------------- /Lists/q20.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to print a list of space-separated elements. 2 | 3 | list1 = ["vipin", "john doe", "india", "123 street"] 4 | sepList = [] 5 | 6 | for item in list1: 7 | if " " in item: 8 | sepList.append(item) 9 | 10 | print(sepList) 11 | -------------------------------------------------------------------------------- /Lists/q21.py: -------------------------------------------------------------------------------- 1 | #Write a program to find all the prime numbers in a range using list comprehension 2 | 3 | lower=int(input("Enter the lower bound of the range:")) 4 | upper=int(input("Enter the upper bound of the range:")) 5 | 6 | prime=[] 7 | if lower >0 and upper >0 and lower max: 10 | max = eachNo 11 | return max 12 | 13 | 14 | number = [110.1, 110.2, 2, 3, 4, 5] 15 | largestNo = largeNo(number) 16 | print(largestNo) 17 | 18 | # 110.2 19 | -------------------------------------------------------------------------------- /Lists/q4.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to get the smallest number from a list. 2 | 3 | 4 | def smallerNo(numberList): 5 | min = numberList[0] 6 | for eachNo in numberList: 7 | if eachNo < min: 8 | min = eachNo 9 | return min 10 | 11 | 12 | number = [110.1, 110.2, 2, 3, 4, 5] 13 | smallestNo = smallerNo(number) 14 | print(smallestNo) 15 | -------------------------------------------------------------------------------- /Lists/q5.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings. 2 | 3 | 4 | def counterString(stringList): 5 | ctr = 0 6 | for word in stringList: 7 | if len(word) > 1 and word[0] == word[-1]: 8 | ctr += 1 9 | return ctr 10 | 11 | 12 | words = ["vipin", "nitin", "3323", "2342"] 13 | find = counterString(words) 14 | print(find) 15 | 16 | # 3 17 | -------------------------------------------------------------------------------- /Lists/q6.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to remove duplicates from a list. 2 | 3 | 4 | def removeDuplicates(givenList): 5 | for item in givenList: 6 | count = 0 7 | for item2 in givenList: 8 | if item == item2: 9 | count += 1 10 | if count >= 2: 11 | givenList.remove(item2) 12 | return givenList 13 | 14 | 15 | nameList = ["vipin", "nitin", "vipin", "akash", "nitin"] 16 | 17 | removeDuplicates(nameList) 18 | print(nameList) 19 | 20 | # ['vipin', 'akash', 'nitin'] -------------------------------------------------------------------------------- /Lists/q7.py: -------------------------------------------------------------------------------- 1 | # Python program to check a list is empty or not. 2 | 3 | givenList = ["one", "2"] 4 | givenList2 = [] 5 | 6 | # check 7 | 8 | 9 | def checkList(listOne): 10 | if len(listOne) == 0: 11 | print("EMPTY") 12 | else: 13 | print("Not Empty") 14 | 15 | 16 | checkList(givenList) 17 | checkList(givenList2) 18 | 19 | # Not Empty 20 | # EMPTY 21 | -------------------------------------------------------------------------------- /Lists/q8.py: -------------------------------------------------------------------------------- 1 | 2 | # Write a Python program to clone or copy a list 3 | 4 | list1 = ["one", "two", "three"] 5 | list2 = list1.copy() 6 | 7 | 8 | print(list2) 9 | -------------------------------------------------------------------------------- /Lists/q9.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to find the list of words that are longer than n from a given list of words. 2 | 3 | totalLength = 5 4 | list = ["vipin", "akashta", "nitin", "subham"] 5 | count = 0 6 | for check in list: 7 | if len(check) > totalLength: 8 | count += 1 9 | 10 | # 2 11 | -------------------------------------------------------------------------------- /Patterns/Pattern Numbers & Stars - 1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | 7 | num = int(input()) 8 | n = 1 9 | k = 1 10 | for i in range(0, num): 11 | for j in range(num, i, -1): 12 | print(n, end=" ") 13 | n = n + 1 14 | while k < 2*i: 15 | print("*",end=" ") 16 | k = k+1 17 | print() 18 | n = 1 19 | k = 1 20 | 21 | 22 | # In[ ]: 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Patterns/Pattern Numbers & Stars - 2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # All test cases passed 5 | 6 | # In[1]: 7 | 8 | 9 | num = int(input()) 10 | n = 1 11 | k = num 12 | for i in range(0, num): 13 | for j in range(0, i+1): 14 | print(n, end="") 15 | n = n + 1 16 | while k > i+1: 17 | print("*",end = "") 18 | k = k-1 19 | print() 20 | n = 1 21 | k = num 22 | 23 | 24 | # In[ ]: 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Patterns/ReverseNumberPyramid.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 1 3 | 4 | 2 1 5 | 6 | 3 2 1 7 | 8 | 4 3 2 1 9 | 10 | 5 4 3 2 1 11 | ''' 12 | 13 | #CODE 14 | rows = 6 15 | 16 | for row in range(1, rows): 17 | 18 | for column in range(row, 0, -1): 19 | 20 | print(column, end=’ ‘) 21 | 22 | print(“”) 23 | -------------------------------------------------------------------------------- /Prime Numbers/Check Prime.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[ ]: 5 | 6 | num = int(input("Enter the number\n")) 7 | if num > 1: 8 | for i in range(2,num): 9 | if (num % i) ==0: 10 | print(num,"is not a prime number") 11 | break 12 | else: 13 | print(num,"is a prime number") 14 | else: 15 | print(num,"is not a prime number") 16 | -------------------------------------------------------------------------------- /Prime Numbers/Prime Generator.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[ ]: 5 | 6 | 7 | from math import sqrt 8 | for _ in range(int(input())): 9 | a,b = map(int,input().split()) 10 | num_prime = {} 11 | 12 | for i in range(2,int(sqrt(b)+1)): 13 | for j in range(max(a//i,2),(b//i)+1): 14 | num_prime[i*j] = 1 15 | 16 | for i in range(max(a,2),b+1): 17 | if i not in num_prime: 18 | print(i,end=" ") 19 | print() 20 | 21 | -------------------------------------------------------------------------------- /Python Dictionary/q1.py: -------------------------------------------------------------------------------- 1 | # create a birth database system 2 | birthDatabase = {"vipin": 'November 26', 3 | "mohit": 'July 13', 'akash': 'August 20'} 4 | 5 | 6 | while True: 7 | print("Enter your name ****___****** ") 8 | yourName = input() 9 | if yourName == " ": 10 | break 11 | if yourName in birthDatabase: 12 | print(f"{yourName} Birthday is on {birthDatabase[yourName]}") 13 | else: 14 | print("Name does't exists") 15 | print("Add a birth month and date to add a new data ::") 16 | birthday = input() 17 | if birthday == " ": 18 | print("invalid details") 19 | break 20 | birthDatabase[yourName] = birthday 21 | print("New data added successfully ****************") 22 | -------------------------------------------------------------------------------- /Python Dictionary/q10.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to iterate over dictionaries using for loops. 2 | 3 | dic1 = {1: 10, 2: 20, 3: 30} 4 | for key, values in dic1.items(): 5 | print(f"{key} -> {values} ") 6 | 7 | # 1 -> 10 8 | # 2 -> 20 9 | # 3 -> 30 10 | -------------------------------------------------------------------------------- /Python Dictionary/q11.py: -------------------------------------------------------------------------------- 1 | # Write a Python script to generate and print a dictionary that contains a number (between 1 and n) in the form (x, x*x). 2 | 3 | dic = {} 4 | n = 5 5 | for item in range(1, 5+1): 6 | dic[item] = item * item 7 | 8 | print(dic) 9 | 10 | #{1: 1, 2: 4, 3: 9, 4: 16, 5: 25} 11 | -------------------------------------------------------------------------------- /Python Dictionary/q12.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to remove a key from a dictionary. 2 | spam = {'rahul': 23, 'vipin': 21, 'prince': 25, 'kas': 17} 3 | del spam['kas'] 4 | print(spam) 5 | 6 | #{'rahul': 23, 'vipin': 21, 'prince': 25} 7 | -------------------------------------------------------------------------------- /Python Dictionary/q13.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to get the maximum and minimum value in a dictionary. 2 | 3 | spam = {'rahul': 25, 'vipin': 21, 'prince': 29, 'kas': 17} 4 | allValues = list(spam.values()) 5 | maxvalue = allValues[0] 6 | for value in allValues: 7 | if maxvalue < value: 8 | maxvalue = value 9 | 10 | print(maxvalue) 11 | -------------------------------------------------------------------------------- /Python Dictionary/q14.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to check a dictionary is empty or not. 2 | dic1 = {} 3 | spam = {'rahul': 25, 'vipin': 21, 'prince': 29, 'kas': 17} 4 | 5 | if len(dic1.keys()) == 0: 6 | print("dic is empty") 7 | else: 8 | print("dic is not empty") 9 | 10 | #dic is empty 11 | -------------------------------------------------------------------------------- /Python Dictionary/q15.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to combine two dictionary adding values for common keys. 2 | 3 | d1 = {'a': 100, 'b': 200, 'c': 300} 4 | d2 = {'a': 300, 'b': 200, 'd': 400} 5 | commonDic = {} 6 | for key in list(d1.keys()): 7 | if key in d2: 8 | commonDic[key] = d1[key] + d2[key] 9 | 10 | print(commonDic) 11 | 12 | # {'a': 400, 'b': 400} 13 | -------------------------------------------------------------------------------- /Python Dictionary/q16.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to print all unique values in a dictionary. 2 | d1 = [{"V": "S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, 3 | {"VII": "S005"}, {"V": "S009"}, {"VIII": "S007"}] 4 | uniqueList = [] 5 | for i in range(0, len(d1)): 6 | for value in list(d1[i].values()): 7 | if value not in uniqueList: 8 | uniqueList.append(value) 9 | 10 | uniqueDic = set(uniqueList) 11 | print(uniqueDic) 12 | 13 | # {'S002', 'S001', 'S009', 'S007', 'S005'} 14 | -------------------------------------------------------------------------------- /Python Dictionary/q17.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to create and display all combinations of letters, selecting each letter from a different key in a dictionary. 2 | 3 | dic = {'1': ['a', 'b'], '2': ['c', 'd']} 4 | valuesList = list(dic.values()) 5 | 6 | for eachValue1 in valuesList[0]: 7 | for eachValue2 in valuesList[1]: 8 | print(eachValue1 + eachValue2) 9 | 10 | # ac ad bc bd 11 | -------------------------------------------------------------------------------- /Python Dictionary/q18.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to find the highest 3 values in a dictionary 2 | 3 | d1 = {'a': 100, 'b': 200, 'c': 300, 'd': 600, 'e': 700, 'f': 400, 'g': 900} 4 | list1 = list(d1.values()) 5 | list1.sort() 6 | print(list1[len(list1)-3:]) 7 | 8 | #[600, 700, 900] 9 | -------------------------------------------------------------------------------- /Python Dictionary/q19.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to combine values in python list of dictionaries. 2 | 3 | itemList = [{'item': 'item1', 'amount': 400}, { 4 | 'item': 'item2', 'amount': 300}, {'item': 'item1', 'amount': 750}] 5 | commonDic = {} 6 | 7 | for i in itemList: 8 | if i['item'] in commonDic.keys(): 9 | commonDic[i['item']] += i['amount'] 10 | else: 11 | commonDic[i['item']] = i['amount'] 12 | 13 | print(commonDic) 14 | 15 | #{'item1': 1150, 'item2': 300} 16 | -------------------------------------------------------------------------------- /Python Dictionary/q2.py: -------------------------------------------------------------------------------- 1 | # program based on keys(), values(), and items() Methods 2 | 3 | spam = {'color': 'red', 'age': 42} 4 | print(list(spam.keys())) 5 | 6 | #['color', 'age'] 7 | 8 | for eachKey in spam.keys(): 9 | print(eachKey) 10 | # color 11 | # age 12 | 13 | # items 14 | for eachKey, eachValue in spam.items(): 15 | print(eachKey, eachValue) 16 | 17 | # color red 18 | # age 42 19 | 20 | # values 21 | for eachValue in spam.values(): 22 | print(eachKey) 23 | # red 24 | # 42 25 | -------------------------------------------------------------------------------- /Python Dictionary/q20.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to count the values associated with key in a dictionary. Go to the editor 2 | Sampledata = [{'id': 1, 'success': True, 'name': 'Lary'}, { 3 | 'id': 2, 'success': False, 'name': 'Rabi'}, {'id': 3, 'success': True, 'name': 'Alex'}] 4 | count = 0 5 | for i in Sampledata: 6 | if 'success' in i.keys(): 7 | if i['success'] == True: 8 | count += 1 9 | 10 | print(count) 11 | -------------------------------------------------------------------------------- /Python Dictionary/q3.py: -------------------------------------------------------------------------------- 1 | # to check keys , value available or not 2 | 3 | spam = {'color': 'red', 'age': 42} 4 | 5 | print('city' in spam.keys()) 6 | # False 7 | print('zophie' in spam.values()) 8 | # false 9 | 10 | -------------------------------------------------------------------------------- /Python Dictionary/q4.py: -------------------------------------------------------------------------------- 1 | # get method 2 | 3 | spam = {'name': 'vipin', 'age': 42} 4 | print(f"{spam.get('name','the person')} age is {spam.get('age','not available')}") 5 | -------------------------------------------------------------------------------- /Python Dictionary/q5.py: -------------------------------------------------------------------------------- 1 | # wap for charachter count. 2 | 3 | import pprint 4 | # module for printing dic look more understandable 5 | message = 'It was a bright cold day in April, and the clocks were striking thirteen.' 6 | count = {} 7 | 8 | for charachter in message: 9 | count.setdefault(charachter, 0) 10 | count[charachter] = count[charachter] + 1 11 | 12 | pprint.pprint(count) 13 | 14 | """ count: -{ 15 | : 13, 16 | ,: 1, 17 | .: 1, 18 | A: 1, 19 | I: 1, 20 | a: 4, 21 | b: 1, 22 | c: 3, 23 | d: 3, 24 | e: 5, 25 | g: 2, 26 | h: 3, 27 | i: 6, 28 | k: 2, 29 | l: 3, 30 | n: 4, 31 | o: 2, 32 | p: 1, 33 | r: 5, 34 | s: 3, 35 | t: 6, 36 | w: 2, 37 | y: 1 38 | }, """ 39 | -------------------------------------------------------------------------------- /Python Dictionary/q6.py: -------------------------------------------------------------------------------- 1 | # built a tic tac toe game 2 | 3 | space_keys = list('123456789') 4 | X, O, BLANK = 'X', 'O', ' ' 5 | 6 | 7 | def main(): 8 | print("********** Welcome To Tic-Tac-toe Game") 9 | gameBoard = getBlankBoard() 10 | currentPlayer, nextPlayer = X, O 11 | 12 | while True: 13 | print(getBoardStr(gameBoard)) 14 | 15 | move = None 16 | 17 | while not isValidSpace(gameBoard, move): 18 | print('What is {}\'s move? (1-9)'.format(currentPlayer)) 19 | move = input() 20 | updateBoardGame(gameBoard, move, currentPlayer) 21 | 22 | if isWinner(gameBoard, currentPlayer): 23 | print(getBoardStr(gameBoard)) 24 | print(currentPlayer + ' has won the game!') 25 | break 26 | elif isGameTie(gameBoard): 27 | print(getBoardStr(gameBoard)) 28 | print('The game is a tie!') 29 | break 30 | # Swap turns. 31 | currentPlayer, nextPlayer = nextPlayer, currentPlayer 32 | print('Thanks for playing!') 33 | 34 | 35 | def getBlankBoard(): 36 | board = {} 37 | for space in space_keys: 38 | board[space] = BLANK 39 | return board 40 | 41 | 42 | def getBoardStr(board): 43 | return ''' 44 | {}|{}|{} 1 2 3 45 | -+-+- 46 | {}|{}|{} 4 5 6 47 | -+-+- 48 | {}|{}|{} 7 8 9'''.format(board['1'], board['2'], board['3'], board['4'], board['5'], board['6'], board['7'], board['8'], board['9']) 49 | 50 | 51 | def isValidSpace(board, space): 52 | return space in space_keys and board[space] == BLANK 53 | 54 | 55 | def updateBoardGame(board, move, mark): 56 | board[move] = mark 57 | 58 | 59 | def isWinner(b, p): 60 | return((b['1'] == b['2'] == b['3'] == p) or # Across the top 61 | (b['4'] == b['5'] == b['6'] == p) or # Across the middle 62 | (b['7'] == b['8'] == b['9'] == p) or # Across the bottom 63 | (b['1'] == b['4'] == b['7'] == p) or # Down the left 64 | (b['2'] == b['5'] == b['8'] == p) or # Down the middle 65 | (b['3'] == b['6'] == b['9'] == p) or # Down the right 66 | (b['3'] == b['5'] == b['7'] == p) or # Diagonal 67 | (b['1'] == b['5'] == b['9'] == p)) # Diagonal 68 | 69 | 70 | def isGameTie(board): 71 | for space in space_keys: 72 | if board[space] == BLANK: 73 | return False 74 | return True 75 | 76 | 77 | if __name__ == '__main__': 78 | main() # Call main() if this module is run, but not when imported. 79 | -------------------------------------------------------------------------------- /Python Dictionary/q7.py: -------------------------------------------------------------------------------- 1 | # Write a Python script to sort (ascending and descending) a dictionary by value. 2 | 3 | spam = {'rahul': 23, 'vipin': 21, 'prince': 25, 'kas': 17} 4 | valueList = list(spam.items()) 5 | valueList.sort() 6 | sortedSpam = dict(valueList) 7 | print(sortedSpam) 8 | -------------------------------------------------------------------------------- /Python Dictionary/q8.py: -------------------------------------------------------------------------------- 1 | # Write a Python script to concatenate following dictionaries to create a new one. 2 | dic1 = {1: 10, 2: 20} 3 | dic2 = {3: 30, 4: 40} 4 | dic3 = {5: 50, 6: 60} 5 | 6 | concatDic = list(dic1.items()) + list(dic2.items()) + list(dic3.items()) 7 | print(concatDic) 8 | allDic = dict(concatDic) 9 | print(allDic) 10 | -------------------------------------------------------------------------------- /Python Dictionary/q9.py: -------------------------------------------------------------------------------- 1 | # Write a Python script to check whether a given key already exists in a dictionary. 2 | 3 | dic1 = {1: 10, 2: 20} 4 | 5 | 6 | def checkKey(key): 7 | if key in dic1.keys(): # this will return a list of keys 8 | print("key exists") 9 | else: 10 | print("key does't exists") 11 | 12 | 13 | checkKey(5) 14 | 15 | # key does't exists 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Practice-Python 2 | 20 Programs based on basic concepts of python with solution. 3 | 4 | # Question based on following concepts 5 | concepts : 6 | - [x] functions 7 | - [x] lists 8 | - [x] dictionary 9 | - [x] strings 10 | - [x] oops 11 | - [x] patterns 12 | - [x] file handling 13 | 14 | 15 | ![Python](pythonlogo.jpg) 16 | ### Contribution 17 | You can easily make contribute by adding more question on different topics. 18 | -------------------------------------------------------------------------------- /Strings/bulletadd.bat: -------------------------------------------------------------------------------- 1 | @py.exe C:\Users\om\Documents\Exercise_Sandbox\strings\bulletadd.py %* 2 | 3 | -------------------------------------------------------------------------------- /Strings/bulletadd.py: -------------------------------------------------------------------------------- 1 | #! python3 2 | # add bullet in front of list copied from web or somewhere else 3 | 4 | import pyperclip 5 | 6 | text = pyperclip.paste() 7 | 8 | lines = text.split('\n') 9 | """ to split the copied string by new line character """ 10 | 11 | for i in range(len(lines)): 12 | lines[i] = '* ' + lines[i] 13 | 14 | text = '\n'.join(lines) 15 | 16 | pyperclip.copy(text) 17 | 18 | # this file has bat file 19 | -------------------------------------------------------------------------------- /Strings/pw.bat: -------------------------------------------------------------------------------- 1 | @py.exe C:strings\pw.py %* 2 | @pause 3 | -------------------------------------------------------------------------------- /Strings/pw.py: -------------------------------------------------------------------------------- 1 | #! python3 2 | # password locker 3 | 4 | import sys 5 | import pyperclip 6 | 7 | 8 | passwords = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6', 9 | 'blog': 'VmALvQyKAxiVH5G8v01if1MLZF3sdt', 10 | 'luggage': '12345' 11 | } 12 | if len(sys.argv) < 2: 13 | print(' Usage : pw.py [account] -- to copy the password') 14 | sys.exit() 15 | 16 | account = sys.argv[1] 17 | if account in passwords: 18 | pyperclip.copy(passwords[account]) 19 | print(f"Password is copied to the clipboard of {account}") 20 | else: 21 | print(f"{account} not found ") 22 | 23 | # >> py pw.py email 24 | # Password is copied to the clipboard of email 25 | -------------------------------------------------------------------------------- /Strings/q1.py: -------------------------------------------------------------------------------- 1 | # use of escape charaters 2 | 3 | # wap to print this 'Say hi to Bob's mother.' 4 | 5 | print('Say hi to Bob\'s mother') 6 | 7 | # wap to print 'Bob is a type "nostalgic" person' 8 | 9 | print('Bob is a type \"nostalgic\" person.') 10 | 11 | # wap to print 12 | # Hello there! 13 | # How are you? 14 | # I'm doing fine. 15 | 16 | print('Hello there!\n How are you ? \n I\'m doing fine.') 17 | -------------------------------------------------------------------------------- /Strings/q10.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to get a string made of the first 2 and the last 2 chars from a given a string. If the string length is less than 2, return instead of the empty string. \ 2 | 3 | print("**** Enter a String *********") 4 | 5 | userString = input() 6 | # vipin 7 | 8 | 9 | # first 2 and last 2 char 10 | # first check its length is not < 2 11 | 12 | if len(userString) < 2: 13 | print("Empty string ") 14 | 15 | else: 16 | print(userString[:2], userString[len(userString)-2:]) 17 | 18 | 19 | # vi in 20 | -------------------------------------------------------------------------------- /Strings/q11.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to get a string from a given string where all occurrences of its first char have been changed to '$', except the first char itself. 2 | 3 | """ Sample String : 'restart' 4 | Expected Result : 'resta$$' """ 5 | 6 | def change_char(str1): 7 | char = str1[0] 8 | str1 = str1.replace(char, '$') 9 | str1 = char + str1[1:] 10 | 11 | return str1 12 | 13 | print(change_char('restart')) 14 | 15 | print(matchString) 16 | print(randomString) 17 | -------------------------------------------------------------------------------- /Strings/q12.py: -------------------------------------------------------------------------------- 1 | """ Write a Python program to get a single string from two given strings, separated by a space and swap the first two characters of each string. Go to the editor 2 | Sample String : 'abc', 'xyz' 3 | Expected Result : 'xyc abz' """ 4 | 5 | string1 = 'abc' 6 | string2 = 'xyz' 7 | 8 | char = string1[0] + string1[1] 9 | string1 = string1.replace(char, string2[0] + string2[1]) 10 | print(string1) 11 | string2 = string2.replace(string2[0] + string2[1], char) 12 | print(string2) 13 | fullString = string1 + " " + string2 14 | print(fullString) 15 | -------------------------------------------------------------------------------- /Strings/q13.py: -------------------------------------------------------------------------------- 1 | """ Write a Python program to add 'ing' at the end of a given string (length should be at least 3). If the given string already ends with 'ing' then add 'ly' instead. If the string length of the given string is less than 3, leave it unchanged. Go to the editor 2 | Sample String : 'abc' 3 | Expected Result : 'abcing' 4 | Sample String : 'string' 5 | Expected Result : 'stringly' """ 6 | 7 | 8 | string = "abcing" 9 | if len(string) >= 3: 10 | if string.endswith("ing"): 11 | string = string+"ly" 12 | else: 13 | string = string + "ing" 14 | print(string) 15 | 16 | # abcingly 17 | -------------------------------------------------------------------------------- /Strings/q14.py: -------------------------------------------------------------------------------- 1 | """ Write a Python program to find the first appearance of the substring 'not' and 'poor' from a given string, if 'not' follows the 'poor', replace the whole 'not'...'poor' substring with 'good'. Return the resulting string. Go to the editor 2 | Sample String : 'The lyrics is not that poor!' 3 | 'The lyrics is poor!' 4 | Expected Result : 'The lyrics is good!' 5 | 'The lyrics is poor!' """ 6 | 7 | string = 'The lyrics is not that poor!' 8 | notPost = string.find("not") 9 | poorPost = string.find('poor') 10 | 11 | if poorPost > notPost and notPost > 0 and poorPost > 0: 12 | string = string.replace(string[notPost:poorPost+4], "good") 13 | 14 | print(string) 15 | -------------------------------------------------------------------------------- /Strings/q15.py: -------------------------------------------------------------------------------- 1 | # Write a Python function that takes a list of words and returns the length of the longest one 2 | listWords = ["vipin", "akash", "abhishek", "john doel"] 3 | 4 | 5 | def longestLength(listWords): 6 | length = 0 7 | for i in listWords: 8 | if length < len(i): 9 | length = len(i) 10 | return length 11 | 12 | 13 | value = longestLength(listWords) 14 | print(value) 15 | -------------------------------------------------------------------------------- /Strings/q16.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to remove the nth index character from a nonempty string. 2 | 3 | 4 | def removeChar(givenString, pos): 5 | newString = "" 6 | if givenString == "": 7 | print("empty string") 8 | else: 9 | list1 = list(givenString) 10 | for i in range(len(list1)): 11 | if pos == i: 12 | del list1[i] 13 | for char in list1: 14 | print(char) 15 | newString += char 16 | return newString 17 | 18 | 19 | value = removeChar("vipin", 2) 20 | print(value) 21 | 22 | #viin -------------------------------------------------------------------------------- /Strings/q17.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to get the last part of a string before a specified character. 2 | 3 | 4 | def getLastPart(givenString, char): 5 | pos = givenString.find(char) 6 | return givenString[pos+1:] 7 | 8 | 9 | value = getLastPart("the strongest man is called-teminator", "-") 10 | print(value) 11 | 12 | # teminator 13 | -------------------------------------------------------------------------------- /Strings/q18.py: -------------------------------------------------------------------------------- 1 | # Write a Python function to reverses a string if it's length is a multiple of 4. 2 | 3 | 4 | def reverseString(givenString): 5 | newString = "" 6 | if len(givenString) % 4 == 0: 7 | print("hello") 8 | for i in range(len(givenString)-1, -1, -1): 9 | newString += givenString[i] 10 | return newString 11 | 12 | 13 | value = reverseString("vipi") 14 | print(value) 15 | 16 | # ipiv 17 | -------------------------------------------------------------------------------- /Strings/q19.py: -------------------------------------------------------------------------------- 1 | """ Write a Python function to convert a given string to all uppercase if it contains at least 2 uppercase characters in the first 4 characters """ 2 | 3 | 4 | def changeUppercase(givenString): 5 | num_counter = 0 6 | for letter in givenString[:4]: 7 | if letter.upper() == letter: 8 | num_counter += 1 9 | if num_counter >= 2: 10 | return givenString.upper() 11 | return givenString 12 | 13 | 14 | value = changeUppercase("viPIn") 15 | print(value) 16 | 17 | #VIPIN -------------------------------------------------------------------------------- /Strings/q2.py: -------------------------------------------------------------------------------- 1 | # the following program repeatedly asks users for their age and a password until they provide valid input. 2 | while True: 3 | print("Enter your age") 4 | age = input() 5 | if age.isdecimal(): 6 | break 7 | print("Enter a valid age (numbers)") 8 | 9 | while True: 10 | print("Enter your Password(letters and numbers only)") 11 | password = input() 12 | if password.isalnum(): 13 | print("Age and password is set") 14 | break 15 | print("Enter a valid password") 16 | 17 | 18 | # Enter your age 19 | # 12 20 | # Enter your Password(letters and numbers only) 21 | # 34am 22 | #Age and password is set 23 | 24 | """ details 25 | 26 | isdecimal() method returns true when the string contains numbers only. 27 | isalnum() method returns true when the string contains numbers and letters only. 28 | istitle() method return true if string starts with an uppercase letter . 29 | isalpha() method return true if string contain only letters. 30 | 31 | """ 32 | -------------------------------------------------------------------------------- /Strings/q20.py: -------------------------------------------------------------------------------- 1 | """ Write a Python function to get a string made of its first three characters of a specified string. If the length of the string is less than 3 then return the original string. """ 2 | 3 | 4 | def first_three(str): 5 | return str[:3] if len(str) > 3 else str 6 | 7 | 8 | print(first_three('ipy')) 9 | print(first_three('python')) 10 | print(first_three('py')) 11 | 12 | # ipy 13 | # pyt 14 | # py 15 | -------------------------------------------------------------------------------- /Strings/q21.py: -------------------------------------------------------------------------------- 1 | #Write a program to find and replace a particular pattern in a string with a given pattern. 2 | #For example: 3 | ''' 4 | Enter a sentence: My favourite programming language is Python. This language is very flexible and will not age. 5 | Enter a pattern to find: age 6 | Enter a pattern to replace: xyz 7 | Original Sentence: My favourite programming language is Python. This language is very flexible and will not age. 8 | Replaced Sentence: My favourite programming languxyz is Python. This languxyz is very flexible and will not xyz. 9 | ''' 10 | #User input 11 | sentence=input("Enter a sentence: ") 12 | pattern=input("Enter a pattern to find: ") 13 | replace=input("Enter a pattern to replace: ") 14 | #pattern length 15 | pLen = len(pattern) 16 | s=sentence2="" 17 | i=0 18 | #replacing the sentence 19 | while i < len(sentence): 20 | s=sentence[i:pLen+i] 21 | if s==pattern: 22 | s="" 23 | sentence2+=replace 24 | i+=pLen 25 | else: 26 | sentence2+=sentence[i] 27 | i+=1 28 | print("Original Sentence: ",sentence) 29 | print("Replaced Sentence: ",sentence2) 30 | -------------------------------------------------------------------------------- /Strings/q3.py: -------------------------------------------------------------------------------- 1 | # startswith() and endswith() String Methods 2 | # wap check the tags are closed 3 | 4 | String = '''

hello world

''' 5 | value = String.endswith('') 6 | 7 | 8 | if String.startswith('
') == String.endswith('
'): 9 | print("Tags are closed") 10 | else: 11 | print("Please close the tags") 12 | 13 | #Tags are closed -------------------------------------------------------------------------------- /Strings/q4.py: -------------------------------------------------------------------------------- 1 | # split() and join () 2 | # wap to add fullstops 3 | intialString = "my name is vipin,i live in 123 street" 4 | string = intialString.split(',') 5 | print(string) 6 | correctString = '.'.join(string) 7 | print(correctString) 8 | 9 | # my name is vipin.I live in 123 street 10 | -------------------------------------------------------------------------------- /Strings/q5.py: -------------------------------------------------------------------------------- 1 | # wap picnic table through ljust, rjust, center\ 2 | 3 | 4 | def picnicTable(picnicItems, leftWidth, rightWidth): 5 | print('Picnic Items'.center(leftWidth + rightWidth, "-")) 6 | for key, value in picnicItems.items(): 7 | print(key.ljust(leftWidth, ".") + str(value).rjust(rightWidth)) 8 | 9 | 10 | picnicItems = {'sandwiches': 4, 'apples': 12, 'cups': 4, 'cookies': 8000} 11 | picnicTable(picnicItems, 20, 5) 12 | 13 | """ -------PICNIC ITEMS------- 14 | sandwiches.......... 4 15 | apples.............. 12 16 | cups................ 4 17 | cookies............. 8000 """ 18 | -------------------------------------------------------------------------------- /Strings/q6.py: -------------------------------------------------------------------------------- 1 | # Removing Whitespace with strip(), rstrip(), and lstrip() 2 | 3 | string = " this is a string " 4 | print(string.strip()) 5 | print(string.lstrip()) 6 | print(string.rstrip()) 7 | 8 | """ this is a string 9 | this is a string 10 | this is a string """ 11 | -------------------------------------------------------------------------------- /Strings/q9.py: -------------------------------------------------------------------------------- 1 | # Write a Python program to count the number of characters (character frequency) in a string. 2 | randomString = "google.com" 3 | list1 = list(randomString) 4 | count = 0 5 | frequencyDic = {} 6 | for i in list1: 7 | if i in frequencyDic: 8 | frequencyDic[i] = frequencyDic[i] + 1 9 | else: 10 | frequencyDic[i] = 1 11 | print(frequencyDic) 12 | 13 | #{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1} 14 | -------------------------------------------------------------------------------- /pythonlogo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Easyvipin/Practice-Python/b76d7db03e626248437fd31f4ff4ef37c4fc1b26/pythonlogo.jpg --------------------------------------------------------------------------------