diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..20b944d --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +add _two _numbers.py \ No newline at end of file diff --git a/Check file size .py b/Check file size .py deleted file mode 100644 index d151722..0000000 --- a/Check file size .py +++ /dev/null @@ -1,4 +0,0 @@ -from pathlib import Path - -file = Path('my_file.txt') -print(file.stat().st_size) \ No newline at end of file diff --git a/Deleting_elements_from_a_tuple.py b/Deleting_elements_from_a_tuple.py new file mode 100644 index 0000000..ed92e8e --- /dev/null +++ b/Deleting_elements_from_a_tuple.py @@ -0,0 +1,12 @@ +#deleting an element of a tuple +num=(10,20,30,40,50) +print(num) +#accept position number of the element to delete +pos=int(input('Enter position no: ')) + +#copy from 0th to pos-2 into another tuple num1 +num1=num[0:pos-1] + +#Concatenate the remaining elements of num fromm pos till end +num=num1+num[pos:] +print(num) \ No newline at end of file diff --git a/Infinite_loop.py b/Infinite_loop.py new file mode 100644 index 0000000..855d9f3 --- /dev/null +++ b/Infinite_loop.py @@ -0,0 +1,4 @@ +i=1 +while i<=10: + print(i) + i+=1 \ No newline at end of file diff --git a/Insertion_array_element.py b/Insertion_array_element.py new file mode 100644 index 0000000..aba1ef1 --- /dev/null +++ b/Insertion_array_element.py @@ -0,0 +1,10 @@ +# 29-7-22 +# insertion array operation +from array import * + +array1 = array('i', [10, 20, 30, 40, 50, 60, 70, ]) + +array1.insert(9,99) + +for s in array1: + print(s) \ No newline at end of file diff --git a/Positive and negative number program in python.py b/Positive_and_negative_number.py similarity index 100% rename from Positive and negative number program in python.py rename to Positive_and_negative_number.py diff --git a/Remove_items_from_a_list.py b/Remove_items_from_a_list.py new file mode 100644 index 0000000..3b2bf88 --- /dev/null +++ b/Remove_items_from_a_list.py @@ -0,0 +1,10 @@ +#9-7-22 +#remove items from a list +def remove(initial_set): + while(initial_set): + initial_set.pop() + print(initial_set) + +#Driver code +initial_set = set([10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100]) +remove(initial_set) \ No newline at end of file diff --git a/Swap_function.py b/Swap_function.py new file mode 100644 index 0000000..ef409e4 --- /dev/null +++ b/Swap_function.py @@ -0,0 +1,19 @@ +# Python3 program to swap first +# and last element of a list + +# Swap function +def swapList(newList): + size = len(newList) + + # Swapping + temp = newList[0] + newList[0] = newList[size - 1] + newList[size - 1] = temp + + return newList + + +# Driver code +newList = [12, 35, 9, 56, 24] + +print(swapList(newList)) diff --git a/AddTwoNumber.py b/add_two_number.py similarity index 100% rename from AddTwoNumber.py rename to add_two_number.py diff --git a/add two numbers.py b/add_two_numbers.py similarity index 100% rename from add two numbers.py rename to add_two_numbers.py diff --git a/aliasing_the_array.py b/aliasing_the_array.py new file mode 100644 index 0000000..7d749cd --- /dev/null +++ b/aliasing_the_array.py @@ -0,0 +1,11 @@ +#aliasing an array +from numpy import * +a=arange(1,6)#create a with elements 1to5 +b=a #give another name b to a + +print('original array: ',a) +print('alias array: ',b) +b[0]=99 #modify 0th element of b +print('after modification: ') +print('original array: ',a) +print('alias array: ',b) \ No newline at end of file diff --git a/arange.py b/arange.py new file mode 100644 index 0000000..ceb6718 --- /dev/null +++ b/arange.py @@ -0,0 +1,6 @@ +#29-1-22 +#creating an array with even number up to 10 +from numpy import * +#create an array using arrange() function +a=arange(2,11,2) +print(a) \ No newline at end of file diff --git a/area of triangle.py b/area_of_triangle.py similarity index 100% rename from area of triangle.py rename to area_of_triangle.py diff --git a/array_module.py b/array_module.py new file mode 100644 index 0000000..8465c62 --- /dev/null +++ b/array_module.py @@ -0,0 +1,23 @@ +#27-1-22 +#creating an array +from import array * +a=array.array('i',[5,6,-7,8]) +print('the array elements are: ') +for element in a: + print(element) + +#creating an array with characters +from array import * +arr= array('u',['a','b','c','d','e']) +print('The array elements are:') +for ch in arr: + print(ch) + + +#creating an array with characters +from array import * +arr= array('u',['a','b','c','d','e']) +print('The array elements are:') +for ch in arr: + print(ch) + \ No newline at end of file diff --git a/array_using_index_method.py b/array_using_index_method.py new file mode 100644 index 0000000..893780c --- /dev/null +++ b/array_using_index_method.py @@ -0,0 +1,22 @@ +#28-1-22 +#searching an array for an element - v2.0 +from array import* +#create an empty array to store integers +x=array('i',[]) + +#store elements into the array x +print('how many elements? ',end=' ') +n=int(input())#accept input into n +for i in range(n): #repeat for n times + print('enter element: ',end=' ') + x.append(int(input())) #add the element to the array x +print('original array: ',x) + +print('enter element to search: ',end=' ') +s=int(input())#accept element to be searched +#index() method gives the location of the element in the array +try: + pos=x.index(s) + print('found at position=',pos+1) +except valueerror:# if element not found then valueerror will else\ + print('not found in the array') \ No newline at end of file diff --git a/array_using_numpy.py b/array_using_numpy.py new file mode 100644 index 0000000..1cdb600 --- /dev/null +++ b/array_using_numpy.py @@ -0,0 +1,11 @@ +#28-1-22 +#creating single dimensional array using numpy +import numpy as np +arr=np.array([10,20,30,40,50])#create array +print(arr) #display array + + +#creating single array dimensional array using numpy -v3.0 +from numpy import * +arr=array([10,20,30,40,50])#create array +print(arr) #display array diff --git a/assertion.py b/assertion.py new file mode 100644 index 0000000..4bf3a4b --- /dev/null +++ b/assertion.py @@ -0,0 +1,18 @@ +#26-3-22 +#understanding assert statement +x=int(input('enter a number greater than 0: ')) +assert x>0,"wrong input entered" +print('U entered: ',x) + + + + +#to handle assertion error raised by assert\ +x=int(input('enter a number greater than 0:')) +try: + assert(x>0) # exception may occur here + print('u entered:',x) +except assertionerror: + print("Wrong input entered")# this is executed in case of + #exception + diff --git a/attributes_an_array.py b/attributes_an_array.py new file mode 100644 index 0000000..33381e1 --- /dev/null +++ b/attributes_an_array.py @@ -0,0 +1,22 @@ +#31-1-22 +#the ndim attribute +from numpy import array +arr1=array([1,2,3,4,5])# 1d array +print(arr1.ndim) + +arr2=array([[1,2,3],[4,5,6]]) +print(arr2.ndim) + +#the shape attribute +arr1=array([1,2,3,4,5]) +print(arr1.shape) + +arr2=array([[1,2,3],[4,5,6]]) +print(arr2.shape) + +arr2.shape=(3,2) #change shape of arr2 to 3 rows and 2 cols +print(arr2) + +# the size attribute +arr1=array([1,2,3,4,5]) +print(arr1.size) diff --git a/Boolean operator.py b/boolean_operator.py similarity index 100% rename from Boolean operator.py rename to boolean_operator.py diff --git a/bubble_sort.py b/bubble_sort.py new file mode 100644 index 0000000..0e4ead9 --- /dev/null +++ b/bubble_sort.py @@ -0,0 +1,33 @@ +#28-1-22 +#sorting an array using bubble short technique +from array import* +#create an empty array to store integers +x=array('i',[]) + +#store elements into the array x +print('how many elements? ',end=' ') +n=int(input())#accept input into n + +#store elements into the array x +print('how many elements? ',end=' ') +n=int(input()) #accept input into n + +for i in range(n): #repeat for n times + print('enter element: ',end=' ') + x.append(int(input())) # add the element to the array x +print('original array: ',x) + +#bubblesort +flag = False # when swapping is done, flag becomes true +for i in range(n-1): # l is from 0 to n-1 + for j in range(n-1-i): # j is from 0 to one element lesser than i + if x[j] > x[j+1]: #it lst element is bigger than the 2nd one + t=x[j]#swap j and j+1 elements + x[j]=x[j+1] + x[j+1]=t + flag=true #swapping done, hence flag is true + if flag==False: #no swapping means array is in sorted order + break #come out of inner for loop + else: + flag=false #assign initial value to flag +print('sorted array= ',x) \ No newline at end of file diff --git a/ByteArray.py b/byte_array (2).py similarity index 100% rename from ByteArray.py rename to byte_array (2).py diff --git a/ByteArray,py.py b/byte_array.py similarity index 100% rename from ByteArray,py.py rename to byte_array.py diff --git a/byte types array.py b/byte_types_array.py similarity index 100% rename from byte types array.py rename to byte_types_array.py diff --git a/calender.py b/calender.py index c887331..d4cc044 100644 --- a/calender.py +++ b/calender.py @@ -1,8 +1,8 @@ # Program to display calender of the given month and year #importing calender Module import calendar -yy = 2020 #year -mm = 12 #month +yy = 2022 #year +mm = 1 #month # To take month and year input from the user # yy = int(input("Enter year:")) # mm = int(input("Enter month:")) diff --git a/caling_a_function.py b/caling_a_function.py new file mode 100644 index 0000000..195a590 --- /dev/null +++ b/caling_a_function.py @@ -0,0 +1,8 @@ +#4-2-22 +def sum(a,b): + """ This function finds sum of two numbers """ + c=a+b + print('sum= ',c) +#call the function +sum(10,15) +sum(1.5,10.75) \ No newline at end of file diff --git a/check_even_or_prime.py b/check_even_or_prime.py new file mode 100644 index 0000000..84b6fc8 --- /dev/null +++ b/check_even_or_prime.py @@ -0,0 +1,40 @@ +def prime(n): + """ to check if n is prime or not """ + x=1 + for i in range(2,n): + if n%i==0: + x=0 + break + else: + x=1 + return x +num=int(input('enter a number: ')) +result=prime(num) +if result ==1: + print(num,'is prime') +else: + print(num,'is not prime') + +def prime(n): + """ to check if n is prime or not """ + x=1 + for i in range(2,n): + if n%i == 0: + x=0 + break + else: + x=1 + return x + +num=int(input('How many primes do you want')) +i=2 +c=1 +while True: + if prime(i): + print(i) + c+=1 + i+=1 + if c>num : + break + + diff --git a/check_file_size .py b/check_file_size .py new file mode 100644 index 0000000..5f68694 --- /dev/null +++ b/check_file_size .py @@ -0,0 +1,13 @@ +#Get file size , print it , process it... +#Os.stat will provide the file size in (.st_size) property. +#The file size will be shown in bytes. + +import os + +fsize=os.stat('filepath') +print('size:' + fsize.st_size.__str__()) + +#check if the file size is less than 10 MB + +if fsize.st_size < 10000000: + processing is: \ No newline at end of file diff --git a/checking_membership.py b/checking_membership.py new file mode 100644 index 0000000..f62103e --- /dev/null +++ b/checking_membership.py @@ -0,0 +1,7 @@ +#2-2-22 +str=input('enter main string: ') +sub=input('enter main string: ') +if sub in str: + print(sub+'is found main string') +else: + print(sub+'is not found in the main activity') diff --git a/cloning_or_copying.py b/cloning_or_copying.py new file mode 100644 index 0000000..7cf6048 --- /dev/null +++ b/cloning_or_copying.py @@ -0,0 +1,11 @@ +#19-7-22 +#cloning or copying +def cloning(list1): + list_copy=[] + list_copy=list1 + return list_copy +#Drivercode +list1=[10,15,20,25,30,35,40,45,50] +list2=cloning(list1) +print("original list: ",list1) +print("After cloning: ",list2) \ No newline at end of file diff --git a/command line arguments.py b/command_line_arguments.py similarity index 86% rename from command line arguments.py rename to command_line_arguments.py index 6b47671..0f17259 100644 --- a/command line arguments.py +++ b/command_line_arguments.py @@ -1,12 +1,12 @@ #find sum of even numbers import sys #read command line arguments except the program name -args=sys.args[1:] +args=sys.argv[1:] print(args) sum=0 #find sum of even arguments for a in args: x=int(a) - ifx%2=0 + if x%2==0: sum+=x print('sum of events=',sum) \ No newline at end of file diff --git a/comparing_arrays.py b/comparing_arrays.py new file mode 100644 index 0000000..5d9ead3 --- /dev/null +++ b/comparing_arrays.py @@ -0,0 +1,63 @@ +#to know the result of comparing two arrays +from numpy import * +a=array([1,2,3,0]) +b=array([0,2,3,1]) + +c=a==b +print('result of a==b: ',c) +c=a>b +print('result of a>b: ',c) +c=a<=b +print('result of a<=b',c) + + +#using any() and all() functions +from numpy import * +a=array([1,2,3,0]) +b=array([0,2,3,1]) + +c=ab)): + print('a contains atleast one element greater than those of b') + + +#using logical_and(),logical_or(),logical_not() +from numpy import * +a=array([1,2,3],int) +b=array([0,2,3],int) + +c=logical_and(a>0,a<4) +print(c) +c=logical_or(b>=0,b==1) +print(c) + +c=logical_not(b) +print(c) + + +#using where() function +from numpy import * +a=array([10,20,30,40,50,],int) +b=array([1,21,3,40,51],int) +#if a>b then make element from a else from b +c=where(a>b,a,b) +print(c) + + + +#using nonzero() function +from numpy import * +a=array([1,2,0,-1,0,6],int) +#retrieve includes of non zero elements from a +c=nonzero(a) +#display indexes +for i in c: + print(i) +#display the elements +print(a[c]) + diff --git a/compound_interest.py b/compound_interest.py new file mode 100644 index 0000000..43d9eca --- /dev/null +++ b/compound_interest.py @@ -0,0 +1,11 @@ +#4-7-22 +#compound interest +def compound_interest(Principle,rate,time): + #calculate ci + Amount=Principle*(pow((1+rate/100),time)) + CI= Amount-Principle + print("The compound Interest is",CI) + + +#Driver code +compound_interest(15000,45.5,10) \ No newline at end of file diff --git a/continue_statement.py b/continue_statement.py new file mode 100644 index 0000000..d25f1b1 --- /dev/null +++ b/continue_statement.py @@ -0,0 +1,9 @@ +# 26-1-22 +#using continue to execute next iteration of while loop +x=0 +while x<10: + x+=1 + if x>5: # if x>5 then continue next iteration + continue + print('x= ',x) +print("out of loop") \ No newline at end of file diff --git a/convert numbers obh system.py b/convert_numbers_obh_system.py similarity index 100% rename from convert numbers obh system.py rename to convert_numbers_obh_system.py diff --git a/convert_set_into_list.py b/convert_set_into_list.py new file mode 100644 index 0000000..5939346 --- /dev/null +++ b/convert_set_into_list.py @@ -0,0 +1,7 @@ +#11-7-22 +#convert set into list + +my_set = {'Never', 'Give', 'Up'} + +s = list(my_set) +print(s) diff --git a/convert_set_to_string.py b/convert_set_to_string.py new file mode 100644 index 0000000..8a54009 --- /dev/null +++ b/convert_set_to_string.py @@ -0,0 +1,12 @@ +#13-7-22 +#convert set to string +# create a set +s = {'a', 'b', 'c', 'd'} +print("Initially") +print("The datatype of s : " + str(type(s))) +print("Contents of s : ", s) + +# convert Set to String +S = ', '.join(s) +print("The datatype of s : " + str(type(S))) +print("Contents of s : ", S) diff --git a/converting_lists_into_dictionary.py b/converting_lists_into_dictionary.py new file mode 100644 index 0000000..d553706 --- /dev/null +++ b/converting_lists_into_dictionary.py @@ -0,0 +1,13 @@ +#converting lists into a dictionary +#take two separate lists with elements +countries=["USA","India","Germany","France"] +cities=['Washington','New Delhi','Berlin','Paris'] + +#make a dictionary +z=zip(countries,cities) +d=dict(z) + +#display key- value pairs from dictionary d +print('{:15s} -- {:15s}'.format('COUNTRY','CAPITAL')) +for k in d: + print('{:15s} -- {:15s}'.format(k, d[k])) diff --git a/converting_strings_into_dictionary.py b/converting_strings_into_dictionary.py new file mode 100644 index 0000000..2372b69 --- /dev/null +++ b/converting_strings_into_dictionary.py @@ -0,0 +1,21 @@ +#converting a string into a dictionary +#take a string +str="Vijay=23,Ganesh=20,lakshmi=19,Nikhil=22" + +#brake the string at ',' and then at '=' +#store the pieces into a list lst +lst=[] +for x in str.split(','): + y= x.split('=') + lst.append(y) +# convert the list into dictionary 'd' +#but this'd' will have both name and age as as strings +d=dict(lst) + +#create a new dictionary 'd1' with name as string +#and age as integer +d1={} +for k,v in d.items(): + d1[k] = int(v) +#display the final dictionary +print(d1) \ No newline at end of file diff --git a/cosine_series.py b/cosine_series.py new file mode 100644 index 0000000..3555cd4 --- /dev/null +++ b/cosine_series.py @@ -0,0 +1,20 @@ +#program to evaluate cosine series +#accept user input +x,n=[int(i) for i in input("enter angle value, no. of iterations: ").split(',')] +#convert the angle from degrees into radians +r=(x*3.14159)/180 +#this become the first term +t=1 +#till now,find the sum +sum=1 +#display the iteration number and sum +print('Iteration=%d\tsum=%f'%(1,sum)) +#denominator for the second term +i=1 +#repeat for 2nd to nth terms +for j in range(2,n+1): + t=(-1)*t*r*r/(i*(i+1))#find the next term + sum=sum+t; #add it to sum + print('iteration= %d\tsum= %f' %(j,sum)) + i+=2 #increaase i value by 2 for denominator for next term + \ No newline at end of file diff --git a/count program.py b/count.py similarity index 100% rename from count program.py rename to count.py diff --git a/count less than.py b/count_less_than.py similarity index 100% rename from count less than.py rename to count_less_than.py diff --git a/count_occurance_of_an_element_in_list.py b/count_occurance_of_an_element_in_list.py new file mode 100644 index 0000000..ac1a563 --- /dev/null +++ b/count_occurance_of_an_element_in_list.py @@ -0,0 +1,13 @@ +# 22-7-22 +# count occurrences of an element in a list +def countX(lst, x): + return lst.count(x) + + +# driver code +lst = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 100, 10, 20, 20, 30, 40] +x = 30 +print('{} has occurred {} times'.format(x, countX(lst, x))) + + + diff --git a/creating_arrays_using_array.py b/creating_arrays_using_array.py new file mode 100644 index 0000000..c6a3f0f --- /dev/null +++ b/creating_arrays_using_array.py @@ -0,0 +1,16 @@ +#28-1-22 +#creating an array with characters +from numpy import * +arr=array(['delhi','hyderabad','mumbai','ahmedabad']) #create array +print(arr)#display array + + +#creating an array from another array +from numpy import * +a=array([1,2,3,4,5])#original array +b=array(a)#create b from a using array() function +c=a #create c by assigning a to c +#display the arrays +print("a= ",a) +print("b= ",b) +print("c= ",c) diff --git a/desecending_order.py b/desecending_order.py new file mode 100644 index 0000000..dfe50d1 --- /dev/null +++ b/desecending_order.py @@ -0,0 +1,9 @@ +#To dispaly numbers in descending order +for x in range(10): + print(x) + +# take a list of elements +list=[20,30.5,'S','Singapore'] +#display each element from the list +for element in list: + print(element) \ No newline at end of file diff --git a/dictionary.py b/dictionary.py new file mode 100644 index 0000000..b3a0750 --- /dev/null +++ b/dictionary.py @@ -0,0 +1,13 @@ +#creating dictionary with key- value pairs +""" +Create a dictionary with employee details. +Here 'Name' is a key and 'Jay' is its value. +'Id' is key and 200 is its value. +'Salary' is key and 9080.50 is its value. +""" +dict={'Name':'Jay','id':200,'salary':9080.50} + +#access value by giving key +print('Name of employee= ',dict['Name']) +print('id number= ',dict['id']) +print('salary= ',dict['salary']) diff --git a/dictionary_methods.py b/dictionary_methods.py new file mode 100644 index 0000000..f7d007f --- /dev/null +++ b/dictionary_methods.py @@ -0,0 +1,73 @@ +#dictionary methods +#create a dictionary with employee details. +dict={'Name': 'Jay', 'Id':200,'Salary':9080.50} + +#print entire dictionary +print(dict) + +#display only keys +print('keys in dict= ',dict.keys()) + +#display only values +print('Values in dict= ',dict.values()) + +#display both key and value pairs on tuples +print('Items in dict= ',dict.items()) + +#program to find sum of values in a dictionary +#enter the dictionary entries from keybord +dict=eval(input("enter elements in {}: ")) + +# find the sum of values +s=sum(dict.values()) +print('sum of values in the dictionary: ',s) # display sum + + +#creating a dictionary from the keyboard +x={} #take an empty dictionary + +print('How many elements? ',end='') +n=int(input()) #n indicates no. of key-value pairs + +for i in range(n): #repeat for n times + print('Enter key: ',end='') + k=input() #key is string + print('Enter its value: ',end='') + v=int(input()) #value is integer + x.update({k:v}) # store the key-value pair in dictionary x + +# display the dictionary +print('The dictionary is: ', x) + +#creating a dictionary with cricket players names and scores +x={} #take an empty dictionary + +print('How many players? ',end='') +n=int(input()) #n indicates no. of key-value pairs + +for i in range(n): #repeat for n times + print('Enter player name: ',end='') + k=input() #key is string + print('Enter runs: ',end='') + v=int(input()) #value is integer + x.update({k:v}) + +#display only players names + print('\nPlayers in this match: ') + for pname in x.keys(): #keys() will give only keys + print(pname) + +# accept a player name from keyboard +print('Enter player name: ',end=' ') +name=input() + +#find the runs done by the players +runs=x.get(name,-1) +if(runs== -1): + print('player not found') +else: + print('{} made runs {}.'.format(name,runs)) + + + + diff --git a/dictionary_using_lambdas.py b/dictionary_using_lambdas.py new file mode 100644 index 0000000..e604090 --- /dev/null +++ b/dictionary_using_lambdas.py @@ -0,0 +1,10 @@ +#sorting a dictionary by key or value +#take a dictionary +colors={10:"red",40: "green",15:"blue",25:"White"} +#sort the dictionary the keys , i.e. 0th element +c1=sorted(colors.items(),key=lambda t: t[0]) +print(c1) + +#sort the dictionary by values ,i.e. list element +c2=sorted(colors.items(),key=lambda t: t[1]) +print(c2) \ No newline at end of file diff --git a/different_ways_to_clear_a_list.py b/different_ways_to_clear_a_list.py new file mode 100644 index 0000000..f5768bb --- /dev/null +++ b/different_ways_to_clear_a_list.py @@ -0,0 +1,11 @@ +#19-7-22 +#different ways to clear +#creating a list +hi=[10,20,30,40] +print('hi before clear',hi) +#cleaning a list +hi.clear() +print('hi after clear',hi) + + + diff --git a/dimensions_of_arrays.py b/dimensions_of_arrays.py new file mode 100644 index 0000000..a27719f --- /dev/null +++ b/dimensions_of_arrays.py @@ -0,0 +1,15 @@ +#array with 1 row +from numpy import array +arr1=array([1,2,3,4,5]) +print(arr1) #displays [1 2 3 4 5] + +#array with 1 column +arr2=array([10,20,30,40]) +print(arr2) + + +#create a 2d array with 2 rows and 3 cols in each row +arr2 = array([[1,2,3],[4,5,6]]) +print(arr2) + + diff --git a/# Divide Another Number.py b/divide_another_number.py similarity index 100% rename from # Divide Another Number.py rename to divide_another_number.py diff --git a/doc string.py b/doc_string.py similarity index 100% rename from doc string.py rename to doc_string.py diff --git a/duplicate_integers_of_a_list.py b/duplicate_integers_of_a_list.py new file mode 100644 index 0000000..7073bc2 --- /dev/null +++ b/duplicate_integers_of_a_list.py @@ -0,0 +1,13 @@ +#5-7-22 +#print duplicates integers from a list + +lst=[1,1,5,5,7,7,20,20,80,80,15,15,7,7,9,9,40,70,40,80,99,80,80,100,100] +J=[] +S=[] +for i in lst: + if i not in J: + J.append(i) +for i in J: + if lst.count(i) > 1: + S.append(i) + print(S) diff --git a/element_exist_in_a_list.py b/element_exist_in_a_list.py new file mode 100644 index 0000000..b8fb3d6 --- /dev/null +++ b/element_exist_in_a_list.py @@ -0,0 +1,14 @@ +# 22-7-22 +# to check if element exist in a list +# creating a list +lst = [10, 15, 20, 25, 30, 35, 40, 45, 50] +# now taking variable and integers of number +i = 45 +if i in lst: + print("exist") +else: + print("not exist") + + + + diff --git a/even_length_words_of_string.py b/even_length_words_of_string.py new file mode 100644 index 0000000..7b48977 --- /dev/null +++ b/even_length_words_of_string.py @@ -0,0 +1,11 @@ +# 21-7-22 +# to print even length of a string +# Input string +n = 'One minute next minute up' +# splitting the words +j = n.split(" ") +for i in j: + # checking the length of words + if len(i)%2==0: + print(i) + diff --git a/even or odd number.py b/even_or _odd_number.py similarity index 100% rename from even or odd number.py rename to even_or _odd_number.py diff --git a/exponential_series.py b/exponential_series.py new file mode 100644 index 0000000..410067c --- /dev/null +++ b/exponential_series.py @@ -0,0 +1,16 @@ +#program to evaluates exponential seres +#accept user input +x,n=[int(i) for i in input("enter power of e, no. of iterations: ").split(',')] +#this become the first term +t=1 +#till now,find the sum +sum=t +#display the iteration number and sum +print('Iteration=%d\tsum=%f'%(1,sum)) +#repeat for 1st to n-1th terms +for j in range(1,n): + t=t* x/j #find the next term + sum=sum+t; #add it to sum + print('iteration= %d\tsum= %f' %(j+1,sum)) + + \ No newline at end of file diff --git a/Factor Number .py b/factor_number .py similarity index 100% rename from Factor Number .py rename to factor_number .py diff --git a/Factorial Number.py b/factorial_number.py similarity index 100% rename from Factorial Number.py rename to factorial_number.py diff --git a/fibonacci_number.py b/fibonacci_number.py new file mode 100644 index 0000000..3e37778 --- /dev/null +++ b/fibonacci_number.py @@ -0,0 +1,16 @@ +#program to display fibonacci series +n=int(input('how many fibonaccis ?')) +f1=0 #this is first fibonacci no +f2=1 # this is second one +c=2 # counts the no of fibonaccis +if n==1: + print(f1) +elif n==2: + print(f1,'\n',f2,aug=' ') +else: + print(f1,'\n',f2,sep=' ') + while cbig: big=x[1] # if any other element is > big, take it as #big + if x[i] (a[i])) and b[i]): + min = a[i] + + print(arr[min - 1]) + + +# Driver code +arr = [10, 5, 3, 4, 3, 5, 6] +n = len(arr) + +printFirstRepeating(arr, n) + + diff --git a/first variable program .py b/first_variable.py similarity index 100% rename from first variable program .py rename to first_variable.py diff --git a/food_ordering_system.py b/food_ordering_system.py new file mode 100644 index 0000000..aebfea0 --- /dev/null +++ b/food_ordering_system.py @@ -0,0 +1,51 @@ +# 23-8-22 +# food ordering system + +import threading +import time + +from collections import deque + +class Queue: + def __init__(self): + self.buffer = deque() + + def enqueue(self, val): + self.buffer.appendleft(val) + + def dequeue(self): + if len(self.buffer) == 0: + print("Queue is empty") + return + + return self.buffer.pop() + + def is_empty(self): + return len(self.buffer) == 0 + + def size(self): + return len(self.buffer) + + food_order_queue = Queue() + +def place_orders(orders): + for order in orders: + print("Placing order for:",order) + food_order_queue.enqueue(order) + time.sleep(0.5) + + +def serve_orders(): + time.sleep(1) + while True: + order = food_order_queue.dequeue() + print("Now serving: ",order) + time.sleep(2) + +if __name__ == '__main__': + orders = ['pizza','samosa','pasta','biryani','burger'] + t1 = threading.Thread(target=place_orders, args=(orders,)) + t2 = threading.Thread(target=serve_orders) + + t1.start() + t2.start() \ No newline at end of file diff --git a/for_loop.py b/for_loop.py new file mode 100644 index 0000000..340f9fb --- /dev/null +++ b/for_loop.py @@ -0,0 +1,15 @@ +# To Display Each Character From A String +str='Hello' +for ch in str: + print(ch) + +# To Display Each Character From A String +Str='Hello' +n=len(Str) +#Find no. of chars in str +for i in range(n): + print(str[i]) + +#To display odd numbers between 1 to 10 +for i in range(1,10,2): + print(i) \ No newline at end of file diff --git a/for_loop_with_list.py b/for_loop_with_list.py new file mode 100644 index 0000000..19e4d7f --- /dev/null +++ b/for_loop_with_list.py @@ -0,0 +1,5 @@ +# take a list of elements +list=[20,30.5,'S','Singapore'] +#display each element from the list +for element in list: + print(element) \ No newline at end of file diff --git a/formal_and_actual_arguments.py b/formal_and_actual_arguments.py new file mode 100644 index 0000000..689da97 --- /dev/null +++ b/formal_and_actual_arguments.py @@ -0,0 +1,44 @@ +#5-2-22 +#positional arguments +def attach(s1,s2): + """ to join s1 and s2 and display total string """ + s3=s1+s2 + print('Total string: '+s3) +attach ('new','york') + +#keyword arguments +def grocery(item,price): + """ To display the given arguments """ + print('Item = %s' %item) + print('Price = %2.f' %price) +grocery(item='sugar',price=50.75) +grocery(price=88.00,item='oil') + +#default arguments +def grocery(item,price=40.00): + """to display the given arguments """ + print('item=%s'%item) + print('price=%.2f'%price) +grocery(item='sugar',price=50.75) +grocery(item='sugar') + +#variable length arguments +def add(farg,*args): + """ to add given numbers """ + print('formal arguments= ',farg) + sum=0 + for i in args: + sum+=i + print('sum of all numbers= ',(farg+sum)) +add(5,10) +add(5,10,20,30) + +def display(farg,**kwargs): + """to display given values """ + print('formal argument= ',farg) + for x,y in kwargs.items(): + print('key={},value={}'.format(x,y)) +display(5,rno=10) +print() +display(5,rno=10,name='Jay') + diff --git a/function_decorators.py b/function_decorators.py new file mode 100644 index 0000000..ff15da5 --- /dev/null +++ b/function_decorators.py @@ -0,0 +1,50 @@ +def decor(fun): + def inner(): + value= fun() + return value+2 + return inner +def num(): + return 10 +result_fun=decor(num) +print(result_fun()) + +def decor(fun): + def inner(): + value= fun() + return value+2 + return inner +@decor +def num(): + return 10 +print(num()) + +def decor(fun): + def inner(): + value=fun() + return value+2 + return inner +def decor1(fun): + def inner(): + value=fun() + return value*2 + return inner +def num(): + return 10 +result_fun=decor(decor1(num)) +print(result_fun()) + +def decor(fun): + def inner(): + value=fun() + return value+2 + return inner +def decor1(fun): + def inner(): + value=fun() + return value*2 + return inner +@decor +@decor1 +def num(): + return 10 +print(num()) \ No newline at end of file diff --git a/functions_are_first_class.py b/functions_are_first_class.py new file mode 100644 index 0000000..ce1242f --- /dev/null +++ b/functions_are_first_class.py @@ -0,0 +1,44 @@ +# function defined +def multiply_num(a): + b = 40 + r = a*b + return r + + +# drivercode +# assigning function +z = multiply_num + +# invoke function +print(z(6)) +print(z(10)) +print(z(100)) + + +def display(str): + return 'hai '+str +x=display("krishna") +print(x) + + +def display(str): + def message(): + return'How are U?' + result=message()+str + return result +print(display("Krishna")) + + +def display(fun): + return 'Hai'+fun +def message(): + return'How are U?' +print(display(message())) + + +def display(): + def message(): + return 'How are U?' + return message +fun=display() +print(fun()) diff --git a/functions_first_class.py b/functions_first_class.py new file mode 100644 index 0000000..9039d1d --- /dev/null +++ b/functions_first_class.py @@ -0,0 +1,4 @@ +def display(str): + return 'hai '+str +x=display("krishna") +print(x) diff --git a/generators.py b/generators.py new file mode 100644 index 0000000..9f97e83 --- /dev/null +++ b/generators.py @@ -0,0 +1,18 @@ +def mygen(x,y): + while x<=y: + yield x + x+=1 +g=mygen(5,10) +for i in g: + print(i,end=' ') + +def mygen(): + yield 'A' + yield 'B' + yield 'C' +g= mygen() +print(next(g)) +print(next(g)) +print(next(g)) +print(next(g)) + \ No newline at end of file diff --git a/greatest_three_numbers.py b/greatest_three_numbers.py new file mode 100644 index 0000000..62f9655 --- /dev/null +++ b/greatest_three_numbers.py @@ -0,0 +1,9 @@ +n1=int(input("enter your first number n1: ")) +n2=int(input("enter your second number n2: ")) +n3=int(input("enter your third number n3: ")) +if n1>=n2 and n1>=n3: + print("n1 is the greatest"); +if n2>=n1 and n2>=n3: + print("n2 is the greatest"); +if n3>=n2 and n3>=n2: + print("n3 is the greatest"); \ No newline at end of file diff --git a/handling_missing_keys.py b/handling_missing_keys.py new file mode 100644 index 0000000..4c8b962 --- /dev/null +++ b/handling_missing_keys.py @@ -0,0 +1,15 @@ +# 20-7-22 +# handling missing keys +# creating a dictionary +country_code = {'India': '0091', + 'Russia': '0007', + 'Brazil': '0055'} +# searching a dictionary for country code of india +print(country_code.get('India', 'Not Found')) +# searching a dictionary for country code of brazil +print(country_code.get('Brazil', 'Not Found')) +# searching a dictionary for country code of ukraine +print(country_code.get('Ukraine', 'Not Found')) + + + diff --git a/If.....Elif......Else.py b/if_elif_else.py similarity index 100% rename from If.....Elif......Else.py rename to if_elif_else.py diff --git a/If....Else Statement.py b/if_else_statement.py similarity index 100% rename from If....Else Statement.py rename to if_else_statement.py diff --git a/If Statement.py b/if_statement.py similarity index 100% rename from If Statement.py rename to if_statement.py diff --git a/illustrate set operaton.py b/illustrate_set_operaton.py similarity index 100% rename from illustrate set operaton.py rename to illustrate_set_operaton.py diff --git a/Index List using for loop.py b/index_list_using_for_loop.py similarity index 100% rename from Index List using for loop.py rename to index_list_using_for_loop.py diff --git a/indexing_and_slicing_array.py b/indexing_and_slicing_array.py new file mode 100644 index 0000000..6978f7e --- /dev/null +++ b/indexing_and_slicing_array.py @@ -0,0 +1,53 @@ +#accessing elements of an array using index +from array import * +x=array('i',[10,20,30,40,50,60,70,80,90,100]) +#find number of elements in the array +n=len(x) +#dispay array elements using indexing +for i in range(n): # repeat from 0 to n-1 + print(x[i],end=' ') + +#accessing elements of an array using index -v 2.0 +from array import * +x=array('i',[10,20,30,40,50,60]) +#find number of elements in the array +n=len(x) +#display array elements using indexing +i=0 +while i=-n: + print(str[i],end =' ') + i-=1 +print() +i=1 +n=len(str) +while i<=n: + print(str[-i],end=' ') + i+=1 + + +str='core python' +for i in str: + print(i,end=' ') +print() +for i in str[:: -1]: + print(i,end=' ') + + + + + + diff --git a/Input Statements Program.py b/input_statements.py similarity index 100% rename from Input Statements Program.py rename to input_statements.py diff --git a/inserting_elements_in_a_tuple.py b/inserting_elements_in_a_tuple.py new file mode 100644 index 0000000..a2b9fd3 --- /dev/null +++ b/inserting_elements_in_a_tuple.py @@ -0,0 +1,17 @@ +#inserting a new element into a tuple +names=('Vishnu','Anupama','Lakshmi','Bheeshma') +print(names) + +#accept new name and position number +lst=[input('Enter a new name: ')] +new=tuple(lst) +pos= int(input('enter position no: ')) + +#Copy from 0th to pos-2 into another tuple names 1 +names1= names[0:pos-1] +names1= names1+new + +#concatenate the remaining elements of names from pos-1 till end +names=names+names[pos-1:] +print(names) + \ No newline at end of file diff --git a/inserting_substring_string.py b/inserting_substring_string.py new file mode 100644 index 0000000..a25e390 --- /dev/null +++ b/inserting_substring_string.py @@ -0,0 +1,15 @@ +str=input('enter a string: ') +sub=input('enter a sub string: ') +n=int(input('enter position no: ')) +n-=1 +l1=len(str) +l2=len(sub) +str1=[] +for i in range(n): + str1.append(str[i]) +for i in range(l2) : + str1.append(sub[i]) +for i in range(n,l1): + str1.append(str[i]) +str=''.join(str1) +print(str1) \ No newline at end of file diff --git a/Interval Prime.py b/interval_prime.py similarity index 100% rename from Interval Prime.py rename to interval_prime.py diff --git a/Is Not Operators.py b/is_not_operators.py similarity index 100% rename from Is Not Operators.py rename to is_not_operators.py diff --git a/iterator_over_a_set.py b/iterator_over_a_set.py new file mode 100644 index 0000000..c573fa7 --- /dev/null +++ b/iterator_over_a_set.py @@ -0,0 +1,8 @@ +#16-7-22 +#iterator over a set +# Creating a set using string +test_set = set("Gold never rusts") + +# Iterating using for loop +for val in test_set: + print(val) diff --git a/jay $.py b/jay $.py deleted file mode 100644 index 0586a13..0000000 --- a/jay $.py +++ /dev/null @@ -1,4 +0,0 @@ -def no(i): - assert(i>=0),"no is less than zero" - return(i) -print(no(-5)) diff --git a/Joined Two List.py b/joined_two_list.py similarity index 100% rename from Joined Two List.py rename to joined_two_list.py diff --git a/Json 2 program.py b/json_2.py similarity index 89% rename from Json 2 program.py rename to json_2.py index fb705ec..3015a69 100644 --- a/Json 2 program.py +++ b/json_2.py @@ -11,5 +11,5 @@ import json json_data='{"name":"yamini","city":"ahmedabad"}' python_obj=json.loads(json_data) - print json.dumps(python_obj,sort_keys=true,indent=4) + print (json.dumps(python_obj,sort_keys=true,indent=4)) # display in real json syntax indent will give spacce to string diff --git a/lambdas.py b/lambdas.py new file mode 100644 index 0000000..b74f922 --- /dev/null +++ b/lambdas.py @@ -0,0 +1,46 @@ +#7-2-22 +f=lambda x: x*x +value=f(5) +print('square of 5=',value) + +f=lambda x,y:x+y +result=f(1.55,10) +print('sum= ',result) + +max=lambda x,y: x if x>y else y +a,b=[int(n) for n in input("enter two numbers:").split(',')] +print('bigger number= ',max(a,b)) + + +def is_even(x): + if x%2==0: + return True + else: + return False +lst=[10,23,45,56,70,59] +lst1=list(filter(is_even,lst)) +print(lst1) + +lst=[10,23,45,46,70,99] +lst1=list(filter(lambda x:(x%2==0),lst)) +print(lst1) + +def squares(x): + return x*x +lst=[1,2,3,4,5] +lst1=list(map(squares,lst)) +print(lst1) + +lst=[1,2,3,4,5] +lst1=list(map(lambda x: x*x,lst)) +print(lst) + +lst1=[1,2,3,4,5] +lst2=[10,20,30,40,50,60,70,80,90,100] +lst3=list(map(lambda x,y: x*y,lst1,lst2)) +print(lst3) + +from functools import* +lst=[1,2,3,4,5] +result=reduce(lambda x,y: x*y,lst) +print(result) \ No newline at end of file diff --git a/linspace.py b/linspace.py new file mode 100644 index 0000000..2a93f18 --- /dev/null +++ b/linspace.py @@ -0,0 +1,6 @@ +#28-1-22 +#creating an array using linspace() +from numpy import * +#divide 0 to 10 into 5 parts and take those points in the array +a=linspace(0,10,5) +print('a= ',a) diff --git a/list program.py b/list.py similarity index 98% rename from list program.py rename to list.py index 29d82bd..8f4760f 100644 --- a/list program.py +++ b/list.py @@ -1,4 +1,3 @@ #list program laptop=['dell','hp','macbook'] print(laptop[-2]) -u \ No newline at end of file diff --git a/list_processing_methods.py b/list_processing_methods.py new file mode 100644 index 0000000..7314274 --- /dev/null +++ b/list_processing_methods.py @@ -0,0 +1,36 @@ +#Python's list methods +num=[10,20,30,40,50,60,70,80] + +n=len(num) +print('No of elements in num: ',n) + +num.append(60) +print('num after appending 60: ',num) + +num.insert(0,5) +print('num after inserting 5 at 0th position: ',num) + +num1=num.copy() +print('Newly created list num1: ',num1) + +num.extend(num1) +print('num after appending num1: ',num) + +n=num.count(50) +print('No of times 50 found in the list num: ',n) + +num.remove(50) +print('num after removing 50: ',num) + +num.pop() +print('num after removing ending element: ',num) + +num.sort() +print('num after sorting: ',num) + +num.reverse() +print('num after reversing: ',num) + +num.clear() +print('num after removing all elements: ',num) + diff --git a/list_using_range.py b/list_using_range.py new file mode 100644 index 0000000..de8057b --- /dev/null +++ b/list_using_range.py @@ -0,0 +1,30 @@ +#creating lists using range() function +# create a list with 0 to 9 consecutive integer numbers +list1 = range(10) +for i in list1: # display element by element + print(i,', ', end='') +print() #throws cursor to nest line + +#create list with integers from 5 to 9 +list2= range(5,10) +for i in list2: + print(i, ',',end='') +print() + +#create a list with odd numbers from 5 to 9 +list3= range(5,10,2) #step size is 2 +for i in list3: + print(i, ',', end='') + + +#displaying list of elements using while and for loop +list=[10,20,30,40,50,60] +print('using while loop') +i=0 +while i= b: + if a >= b : return a else: return b diff --git a/merging_two_dictionaries.py b/merging_two_dictionaries.py new file mode 100644 index 0000000..7a1cb16 --- /dev/null +++ b/merging_two_dictionaries.py @@ -0,0 +1,16 @@ +#17-7-22 +# merging two dictionaries +# Python code to merge dict using update() method +def Merge(dict1, dict2): + return (dict1.update(dict2)) + + +# Driver code +dict1 = {'j': 20, 'r': 10} +dict2 = {'b': 15, 'y': 7} + +# This return None +print(Merge(dict1, dict2)) + +# changes made in dict2 +print(dict1) diff --git a/minium_two_numbers.py b/minium_two_numbers.py new file mode 100644 index 0000000..6d56123 --- /dev/null +++ b/minium_two_numbers.py @@ -0,0 +1,13 @@ +# 25-6-2022 +#Python program to find the minimum of two numbers +def minimum(a,b): + if a<=b: + return a + else: + return b + +# driver code +a=30 +b=10 +print(minimum(a,b)) + diff --git a/missing_elements_of_a_range.py b/missing_elements_of_a_range.py new file mode 100644 index 0000000..ec642a8 --- /dev/null +++ b/missing_elements_of_a_range.py @@ -0,0 +1,25 @@ +# 4-8-22 +# A hashing based Python3 program to +# find missing the elements of a range + +# Print all elements of range +# [low, high] that are not +# present in arr[0..n\ +def printMissing(arr, n, low, high): + +# Insert all elements of +# arr[] in set + s = set(arr) + +# Traverse through the range +# and print all missing elements + for x in range(low, high + 1): + if x not in s: + print(x, end = ' ') + +# Driver Code +arr = [10, 40, 50, 100, 150] +n = len(arr) +low, high = 11, 100 +printMissing(arr, n, low, high) + diff --git a/modifying_elements.py b/modifying_elements.py new file mode 100644 index 0000000..1d9798e --- /dev/null +++ b/modifying_elements.py @@ -0,0 +1,18 @@ +# Modifying an existing element of aa tuple +num=(10,20,30,40,50) +print(num) + +#accept new element and position number +lst=[int(input('Enter a new element: '))] +new=tuple(lst) +pos=int(input('Enter position no: ')) + +#copy from oth to pos-2 into another tuple num1 +num1=num[0:pos-1] + +#concatenate new element at pos-1 +num =num1+new + +# concatenate the remaining elements of num from pos till end +num=num1+num[pos:] +print(num) \ No newline at end of file diff --git a/module.py b/module.py new file mode 100644 index 0000000..2f168f4 --- /dev/null +++ b/module.py @@ -0,0 +1,9 @@ +# 8-2-22 +#This is use.py program +#import arith module usinng one of the above 4 days +import arith + +#call the functions of the module +arith.add(10,22) +result = arith.sub(10,22) +print('Result of subtraction= ',result) \ No newline at end of file diff --git a/multidimensional_array.py b/multidimensional_array.py new file mode 100644 index 0000000..bc9cc1d --- /dev/null +++ b/multidimensional_array.py @@ -0,0 +1,33 @@ +#1-2-22 +#retrieving the elements from a 2D array using indexing +from numpy import * + +#create a 2D array with 3 rows and 3 Cols +a=[[1,2,3],[4,5,6],[7,8,9]] + +#display only rows +for i in range(len(a)): + print(a[i]) + +#display element by element + for i in range(len(a)): + for j in range(len(a[i])): + print(a[i][j],end=' ') + + +#retrieving the elements from a 3D array using indexing +from numpy import * +#create a 3D array with size 2x2x3 +a=[[[1,2,3], + [4,5,6]], + + [[7,8,9], + [10,11,12]]] + +#display element by element +for i in range(len(a)): + for j in range(len(a[i])): + for k in range(len(a[i] [j])): + print(a[j][j][k],end='\t') + print() + print() \ No newline at end of file diff --git a/Multipication Table.py b/multipication_table.py similarity index 100% rename from Multipication Table.py rename to multipication_table.py diff --git a/multiplication_all_numbers.py b/multiplication_all_numbers.py new file mode 100644 index 0000000..67aa487 --- /dev/null +++ b/multiplication_all_numbers.py @@ -0,0 +1,13 @@ +#28-6-22 +# Python3 program to multiply all values in the +# list using numpy.prod() + +import numpy +list1 = [1, 2, 3] +list2 = [4, 5, 6] + +# using numpy.prod() to get the multiplications +result1 = numpy.prod(list1) +result2 = numpy.prod(list2) +print(result1) +print(result2) diff --git a/multiplication table.py b/multiplication_table.py similarity index 100% rename from multiplication table.py rename to multiplication_table.py diff --git a/mutation program.py b/mutation_program.py similarity index 100% rename from mutation program.py rename to mutation_program.py diff --git a/Natural Numbers.py b/natural_numbers.py similarity index 100% rename from Natural Numbers.py rename to natural_numbers.py diff --git a/negative_number_of_a_list.py b/negative_number_of_a_list.py new file mode 100644 index 0000000..aab8767 --- /dev/null +++ b/negative_number_of_a_list.py @@ -0,0 +1,16 @@ +#2-7-22 +# list of numbers +list=[-4,-20,-30,-40,25,60] +num=0 + +# While Loop +while(num= 0: + print(num, end=" ") diff --git a/processing_the_arrays.py b/processing_the_arrays.py new file mode 100644 index 0000000..b0215f5 --- /dev/null +++ b/processing_the_arrays.py @@ -0,0 +1,50 @@ +#operations on arrays +from array import* + +#create an array with int values +arr = array('i',[10,20,30,40,50]) +print('original array: ',arr) + +#append 30 to the array arr +arr.append(30) +arr.append(60) +print('after appending 30 and 60:',arr) + +#insert 99 at position number 1 in arr +arr.insert(1,99) +print('after inserting 99 in 1st position: ',arr) + +#remove an element from arr +arr.remove(20) +print('after removing 20: ',arr) + +#remove last element using pop() method +n=arr.pop() +print('array after pop(): ',arr) +print('popped element:',n) + +#finding position of element using index() method +n=arr.index(30) +print('first occurence of element 30 is at: ',n) + +#convert an array into a list using tolist() method +lst=arr.tolist() +print('list: ',lst) +print('array: ',arr) + + +from array import * +#accept marks from keyboard into a list +lst = [int(i) for i in input('enter marks: ').split(',')] +#create an integer array from the above list +marks=array('i',lst) +#display the marks and find total +sum=0 +for x in marks: + print(x) + sum+=x +print('total marks: ',sum) +#display percentage +n=len(marks) # n=no. of elements in marks array +percent =sum/n +print('percentage: ',percent) diff --git a/python program without new line .py b/program_without_new_line .py similarity index 100% rename from python program without new line .py rename to program_without_new_line .py diff --git a/queue_immplementation__using_list.py b/queue_immplementation__using_list.py new file mode 100644 index 0000000..5ebf423 --- /dev/null +++ b/queue_immplementation__using_list.py @@ -0,0 +1,24 @@ +# 23-8-22 +# queue implementation_using_list + +# Initializing a queue +queue = [] + +# Adding elements to the queue +queue.append('a') +queue.append('b') +queue.append('c') + +print("Initial queue") +print(queue) + +# Removing elements from the queue +print("\nElements dequeued from queue") +print(queue.pop(0)) +print(queue.pop(0)) +print(queue.pop(0)) + +print("\nQueue after removing elements") +print(queue) + + diff --git a/Random Number.py b/random_number.py similarity index 100% rename from Random Number.py rename to random_number.py diff --git a/RangeFunction.py b/range_function.py similarity index 100% rename from RangeFunction.py rename to range_function.py diff --git a/recursive_functions.py b/recursive_functions.py new file mode 100644 index 0000000..12c1ace --- /dev/null +++ b/recursive_functions.py @@ -0,0 +1,21 @@ +#5-2-22 +def factorial(n): + """ to find factorial of n """ + if n==0: + result=1 + else: + result=n*factorial(n-1) + return result +for i in range(1,11): + print('factorial of {} is {}'.format(i,factorial(i))) + +def towers(n,a,c,b): + if n==1: + print('move disk %i from %s to pole %s' %(n,a,c)) + else: + towers(n-1,a,b,c) + print('move disk %i from pole %s to pole %s'%(n,a,c)) + towers(n-1,b,c,a) +n=int(input('enter number of disks:')) +towers(n,'A','C','B') + \ No newline at end of file diff --git a/relational operators.py b/relational_operators.py similarity index 100% rename from relational operators.py rename to relational_operators.py diff --git a/remove_dictionary_key_words.py b/remove_dictionary_key_words.py new file mode 100644 index 0000000..6de15f3 --- /dev/null +++ b/remove_dictionary_key_words.py @@ -0,0 +1,21 @@ +#18-7-22 +# remove dictionary key words +# Using split() + loop + replace() + +# initializing string +test_str = 'Believe In Yourself ' + +# printing original string +print("The original string is : " + str(test_str)) + +# initializing Dictionary +test_dict = {'Believe' : 1, 'In': 6} + +# Remove Dictionary Key Words +# Using split() + loop + replace() +for key in test_dict: + if key in test_str.split(' '): + test_str = test_str.replace(key, "") + +# printing result +print("The string after replace : " + str(test_str)) diff --git a/remove_duplicates_given_string.py b/remove_duplicates_given_string.py new file mode 100644 index 0000000..d281960 --- /dev/null +++ b/remove_duplicates_given_string.py @@ -0,0 +1,23 @@ +#20-7-22 +#remove duplicated given string + +from collections import OrderedDict +ordinary_dictionary={} +ordinary_dictionary['a'] = 1 +ordinary_dictionary['b'] = 2 +ordinary_dictionary['c'] = 3 +ordinary_dictionary['d'] = 4 +ordinary_dictionary['e'] = 5 +ordinary_dictionary['f'] = 6 + +print(ordinary_dictionary) + +ordered_dictionary = OrderedDict() +ordered_dictionary['a'] = 1 +ordered_dictionary['b'] = 2 +ordered_dictionary['c'] = 3 +ordered_dictionary['d'] = 4 +ordered_dictionary['e'] = 5 +ordered_dictionary['f'] = 6 + +print(ordered_dictionary) diff --git a/remove_empty_list_from_list.py b/remove_empty_list_from_list.py new file mode 100644 index 0000000..0f524ee --- /dev/null +++ b/remove_empty_list_from_list.py @@ -0,0 +1,12 @@ +#5-6-22 +#remove empty list from list +# Initializing list by custom values +lst=[10,20,30,40,50,[],[],[]] +#printing original list +print("The original list is: "+str(lst)) +#Remove empty list to list +#using filter method +res = list(filter(None, lst)) + +# Printing the resultant list +print("List after empty list removal : " + str(res)) diff --git a/remove_empty_tuples.py b/remove_empty_tuples.py new file mode 100644 index 0000000..256b151 --- /dev/null +++ b/remove_empty_tuples.py @@ -0,0 +1,12 @@ +#28-6-22 +# Python program to remove empty tuples from a +# list of tuples function to remove empty tuples +# using list comprehension +def Remove(tuples): + tuples = [t for t in tuples if t] + return tuples + +# Driver Code +tuples = [(), ('Jalpa','15','8'), (), ('Bandana', 'Rushita'), + ('Jay', 'Dharmesh', '45'), ('',''),()] +print(Remove(tuples)) diff --git a/remove punctuation as string.py b/remove_punctuation_as_string.py similarity index 100% rename from remove punctuation as string.py rename to remove_punctuation_as_string.py diff --git a/remove_tuples_of_length_k.py b/remove_tuples_of_length_k.py new file mode 100644 index 0000000..1d54677 --- /dev/null +++ b/remove_tuples_of_length_k.py @@ -0,0 +1,19 @@ +#30-6-22 +# Python3 code to demonstrate working of +# Remove Tuples of Length K +# Using filter() + lambda + len() + +# initializing list +test_lis00t = [(1, ), (3, ), (5, 6, 7), (8, ), (9, 10, 11, 12)] + +# printing original list +print("The original list : " + str(test_list)) + +# initializing K +K = 1 + +# filter() filters non K length values and returns result +res = list(filter(lambda x : len(x) != K, test_list)) + +# printing result +print("Filtered list : " + str(res)) diff --git a/returning_multiple_values.py b/returning_multiple_values.py new file mode 100644 index 0000000..9cf02ac --- /dev/null +++ b/returning_multiple_values.py @@ -0,0 +1,20 @@ +def sum_sub(a,b): + """ This function returns results of addition and substraction of a,b """ + c=a+b + d=a-b + return c,d +x,y = sum_sub(10,5) +print("result of addition: ",x) +print("result of subtraction: ",y) + +def sum_sub_mul_div(a,b): + """ this function returns results of addition ,subtraction,multiplication and division of a,b """ + c=a+b + d=a-b + e=a*b + f=a/b + return c,d,e,f +t=sum_sub_mul_div(10,5) +print('The results are: ') +for i in t: + print(i,end=', ') diff --git a/returning_results_functions.py b/returning_results_functions.py new file mode 100644 index 0000000..19ffd12 --- /dev/null +++ b/returning_results_functions.py @@ -0,0 +1,30 @@ +#4-2-22 +def sum(a,b): + """ This function finds sum of two numbers """ + c=a+b + return c +x=sum(10,15) +print('the sum is: ',x) +y=sum(1.5,10.75) +print('the sum is: ',y) + + +def even_odd(num): + """ to know num is even or odd """ + if num % 2==0: + print(num,"is even") + else: + print(num,"is odd") +even_odd(12) +even_odd(13) + +def fact(n): + """ to find factorial value """ + prod=1 + while n>=1: + prod*=n + n-=1 + return prod +for i in range(1,11): + print('factorial of {} is {}'.format(i,fact(i))) + diff --git a/reverse_number.py b/reverse_number.py new file mode 100644 index 0000000..6bf527f --- /dev/null +++ b/reverse_number.py @@ -0,0 +1,7 @@ +n=int(input("enter your number")) +print("before you enter previous reverse number is:%d"%n) +reverse=0 +while n!=0: + reverse=reverse*20 + n%10 + n=(n//10) +print("after reverse: %d" %reverse) \ No newline at end of file diff --git a/reversing_list.py b/reversing_list.py new file mode 100644 index 0000000..31a6686 --- /dev/null +++ b/reversing_list.py @@ -0,0 +1,15 @@ +#27-8-22 +# Python code +#To reverse list + +#input list +lst=[10,20,30,40,50,60,70,80,90,100] +# the above input can also be given as +# lst=list(map(int,input().split())) +l=[] # empty list +# checking if elements present in the list or not +for i in lst: + #reversing the list + l.insert(0,i) +# printing result +print(l) diff --git a/Scoping Program.py b/scoping_program.py similarity index 100% rename from Scoping Program.py rename to scoping_program.py diff --git a/searching_in_the_strings.py b/searching_in_the_strings.py new file mode 100644 index 0000000..fa7f098 --- /dev/null +++ b/searching_in_the_strings.py @@ -0,0 +1,16 @@ +#3-2-22 +str=[] +n=int(input('how many strings? ')) +for i in range(n): + print('enter string: ',end=' ') + str.append(input()) +s=input('enter string to search: ') +flag=False +for i in range(len(str)): + if s==str[i]: + print('found at: ',i+1) + flag=False +if flag==False: + print('not found') + + diff --git a/searching_list_of _elements.py b/searching_list_of _elements.py new file mode 100644 index 0000000..9cf8dbd --- /dev/null +++ b/searching_list_of _elements.py @@ -0,0 +1,9 @@ +#searching for an element in a list +group1 = [1,2,3,4,5] #take a list of elements +search = int(input('enter element to search: ')) +for element in group1: + if search == element: + print('element found in group1') + break # come out of for loop +else: + print('element not found in group1') #this is a suite \ No newline at end of file diff --git a/second_largest_number.py b/second_largest_number.py new file mode 100644 index 0000000..52ecafa --- /dev/null +++ b/second_largest_number.py @@ -0,0 +1,15 @@ +#29-6-22 +# Python program to find largest number +# in a list + +# List of numbers +list1 = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000] + +# Removing duplicates from the list +list2 = list(set(list1)) + +# Sorting the list +list2.sort() + +# Printing the second last element +print("Second largest element is:", list2[-2]) diff --git a/sequential_search.py b/sequential_search.py new file mode 100644 index 0000000..e7bbcf0 --- /dev/null +++ b/sequential_search.py @@ -0,0 +1,22 @@ +#28-1-22 +#searching an array for an element +from array import * +#creating an empty array to store integers +x=array('i',[]) +#store elements into the array x +print('how many elements? ', end=' ') +n=int(input())#accept input into n +for i in range(n): #repeat for n times + print('enter element: ',end=' ') + x.append(int(input())) #add the element to the array +print('Original array: ',x) +print('enter element to search: ',end=' ') +s=int(input()) #accept element to be searched +#linear search or sequential search +flag=False #this becomes True if element is found +for i in range(len(x)): #repeat i from 0 to no. of elements + if s==x[i]: + print('found at position= ',i+1) + flag=True +if flag==False: + print('Not found in the array') diff --git a/set_into_dictionary.py b/set_into_dictionary.py new file mode 100644 index 0000000..964159f --- /dev/null +++ b/set_into_dictionary.py @@ -0,0 +1,17 @@ +#16-7-22 +#convert set to dictionary + +# initializing set +set = {11, 22, 33, 49, 50} + +# printing initialized set +print ("initial string", set) +print (type(set)) + +str = 'fg' +# Converting set to dict +res = {element:'Never give up'+str for element in set} + +# printing final result and its type +print ("final list", res) +print (type(res)) diff --git a/set program.py b/set_program.py similarity index 100% rename from set program.py rename to set_program.py diff --git a/Simple Calculator.py b/simple_calculator.py similarity index 100% rename from Simple Calculator.py rename to simple_calculator.py diff --git a/simple_interest.py b/simple_interest.py new file mode 100644 index 0000000..068e137 --- /dev/null +++ b/simple_interest.py @@ -0,0 +1,14 @@ +#4-7-22 +#simple interest +def simple_interest(p,t,r): + print("the principal is",p) + print("the time period is",t) + print("the rate of interest",r) + + si=(p*t*r)/100 + + print("the simple interest is",si) + return si + +#driver code +simple_interest(9,1,9) diff --git a/simple variable program.py b/simple_variable.py similarity index 100% rename from simple variable program.py rename to simple_variable.py diff --git a/sine_series.py b/sine_series.py new file mode 100644 index 0000000..1a3657e --- /dev/null +++ b/sine_series.py @@ -0,0 +1,22 @@ +#26-3-22 +#program to evaluate sine series +#accept user input +x,n=[int(i) for i in input("enter angle value, no. of iterations: ").split(',')] +#convert the angle from degrees into radians +r=(x*3.14159)/180 +#this become the first term +t=r +#till now,find the sum +sum=r +#display the iteration number and sum +print('Iteration=%d\tsum=%f'%(1,sum)) +#denominator for the second term +i=2 +#repeat for 2nd to nth terms +for j in range(2,n+1): + t=(-1)*t*r*r/(i*(i+1))#find the next term + sum=sum+t; #add it to sum + print('iteration= %d\tsum= %f' %(j,sum)) + i+=2 #increaase i value by 2 for denominator for next term + + diff --git a/slicing_and_indexing.py b/slicing_and_indexing.py new file mode 100644 index 0000000..1c7e83d --- /dev/null +++ b/slicing_and_indexing.py @@ -0,0 +1,41 @@ +#31-1-22 +#slicing an array +from numpy import * +#create array with elements 10 to 15 +a=arange(10,16) +print(a) + +#retrieve from 1st to ine element prior to 6th element in steps of 2 +b=a[1:6:2] +print(b) + +#retrieve all elements from a +b=a[::] +print(b) + +#retrieve from 6-2=4th to one element prior to 2nd element in +#decreasing step size +b=a[-2:2:-1] +print(b) + +#retrieve from 0th to one element prior to 4th element (6-2=4th) +b=a[:-2:] +print(b) + + +#indexing an array +from numpy import * +a=arange(10,16) +print(a) +#retrieve from 1st to one element prior to 6th element in steps of 2 +a=a[1:6:2] +print(a) + +#display elements using indexing +i=0 +while(i x[j+1]: # if 1st element is bigger than the 2nd one + t= x[j] #swap j and j+1 elements + x[j] = x[j+1] + x[j+1]=t + flag=True #swapping done,hence flag is True + if flag==False: # no swapping means list is in sorted order + break #come out inner for loop + else: + flag= False #assign inital value to flag + print('Sorted list: ', x) \ No newline at end of file diff --git a/sorting_types_of_data.py b/sorting_types_of_data.py new file mode 100644 index 0000000..2656c05 --- /dev/null +++ b/sorting_types_of_data.py @@ -0,0 +1,25 @@ +#11-2-22 +#retrieving employee details from a list +emp=[] + +n=int(input('How many employee? ')) # accept input into n + +for i in range(n): #repeat for n times + print('Enter id: ',end='') + emp.append(int(input())) + print('Enter name: ',end='') + emp.append(input()) + print('Enter Salary: ',end='') + emp.append(float(input())) + +print('the list is created with employee data. ') + +id=int(input('Enter employee id: ')) + +#display employee details upon talking id . +for i in range(len(emp)): + if id==emp[i]: + print('id= {:d},Name= {:s}, Salary={:.2f}'.format(emp[i],emp[i+1],emp[i+2])) + break + + \ No newline at end of file diff --git a/python specification program.py b/specification_program.py similarity index 74% rename from python specification program.py rename to specification_program.py index fe21988..d63d18c 100644 --- a/python specification program.py +++ b/specification_program.py @@ -1,6 +1,6 @@ -class a +class a: --a=2 - def(self) +def(self) print(--a) obj=f() print(--a) diff --git a/splitting_and_joining_strings.py b/splitting_and_joining_strings.py new file mode 100644 index 0000000..2b5a796 --- /dev/null +++ b/splitting_and_joining_strings.py @@ -0,0 +1,4 @@ +str=input('enter of numbers separted by space: ') +lst=str.split(' ') +for i in lst: + print(i) \ No newline at end of file diff --git a/stack_immplementation_in_a_list.py b/stack_immplementation_in_a_list.py new file mode 100644 index 0000000..d9ec94b --- /dev/null +++ b/stack_immplementation_in_a_list.py @@ -0,0 +1,28 @@ +# 12-8-22 +# stack implementation in a list + +stack = [] + +# append() function to push +# element in the stack +stack.append('a') +stack.append('b') +stack.append('c') + +print('Initial stack') +print(stack) + +# pop() function to pop +# element from stack in +# LIFO order +print('\nElements popped from stack:') +print(stack.pop()) +print(stack.pop()) +print(stack.pop()) + +print('\nStack after elements are popped:') +print(stack) + +# uncommenting print(stack.pop()) +# will cause an IndexError +# as the stack is now empty diff --git a/stack_implementation_using_collections_of_deque.py b/stack_implementation_using_collections_of_deque.py new file mode 100644 index 0000000..eb44bba --- /dev/null +++ b/stack_implementation_using_collections_of_deque.py @@ -0,0 +1,28 @@ +# 17-8-22 +# stack implementation using collections of deque +from collections import deque + +stack = deque() + +# append() function to push +# element in the stack +stack.append('j') +stack.append('s') +stack.append('r') + +print('Initial stack:') +print(stack) + +# pop() function to pop +# element from stack in +# LIFO order +print('\nElements popped from stack:') +print(stack.pop()) +print(stack.pop()) +print(stack.pop()) + +print('\nStack after elements are popped:') +print(stack) + + + diff --git a/stack_using_queue_model.py b/stack_using_queue_model.py new file mode 100644 index 0000000..4d1239b --- /dev/null +++ b/stack_using_queue_model.py @@ -0,0 +1,31 @@ +## Implementing stack using the queue module +from queue import LifoQueue + +# Implementing stack using the queue module +from queue import LifoQueue + +# Initializing a my_stack stack with maxsize +my_stack = LifoQueue(maxsize = 5) + +# qsize() display the number of elements +# in the my_stack +print(my_stack.qsize()) + +# put() function is used to push +# element in the my_stack +my_stack.put('x') +my_stack.put('y') +my_stack.put('z') + +print("Stack is Full: ", my_stack.full()) +print("Size of Stack: ", my_stack.qsize()) + +# To pop the element we used get() function +# from my_stack in +# LIFO order +print('\nElements poped from the my_stack') +print(my_stack.get()) +print(my_stack.get()) +print(my_stack.get()) + +print("\nStack is Empty: ", my_stack.empty()) diff --git a/whitespaces to string with tabs or nunbers.py b/string_with_tabs_or_numbers.py similarity index 100% rename from whitespaces to string with tabs or nunbers.py rename to string_with_tabs_or_numbers.py diff --git a/structured_programming.py b/structured_programming.py new file mode 100644 index 0000000..d8d6380 --- /dev/null +++ b/structured_programming.py @@ -0,0 +1,27 @@ +def da(basic): + da=basic*80/100 + return da + +def hra(basic): + hra=basic*15/100 + return hra + +def pf(basic): + pf=basic*12/100 + return pf + +def itax(gross): + tax=gross*0.1 + return tax + +basic=float(input('enter basic salary: ')) + +gross = basic+da(basic)+hra(basic) +print('your gross salary: {:10.2f}'.format(gross)) + +print('Gross value: ', gross) +print('PF value: ', pf(basic)) +net = gross - pf(basic) - itax(gross) +print('your net salary: {:10.2f}'. format(net)) + + diff --git a/StudentResult.py b/student_result.py similarity index 100% rename from StudentResult.py rename to student_result.py diff --git a/sum_of_average_of_a _list.py b/sum_of_average_of_a _list.py new file mode 100644 index 0000000..9b81f54 --- /dev/null +++ b/sum_of_average_of_a _list.py @@ -0,0 +1,12 @@ +#2-7-22 +#Find sum and average of List +list=[1,2,3,4,5,6,7,8,9,10] + +#sum()method +count=sum(list) + +#finding average +avg=count/len(list) + +print("sum = ",count) +print("average= ",avg) \ No newline at end of file diff --git a/sum_of_list_using_for_loop.py b/sum_of_list_using_for_loop.py new file mode 100644 index 0000000..42b2f69 --- /dev/null +++ b/sum_of_list_using_for_loop.py @@ -0,0 +1,8 @@ +#To find sum of list of numbers using for +#take a list of numbers +list = [10,20,30,40,50,60,70,80,90,100] +sum=0 # initially sum is zero +for i in list: + print(i) #display the element from list + sum+=i #add each element to sum +print('sum= ',sum) \ No newline at end of file diff --git a/sum_of_list_using_while_loop.py b/sum_of_list_using_while_loop.py new file mode 100644 index 0000000..a9ed77b --- /dev/null +++ b/sum_of_list_using_while_loop.py @@ -0,0 +1,12 @@ +#to find sum of the list of members using while - v2.0 +#take a list of numbers\ +list=[10,20,30,40,50,60,70,80,90,100] +sum=0 #initally sum is zero +i=0 #take a variable +while i=1: + print('x= ',x) + x-=1 +print("out of loop") + + + + + +#using break to come out of while loop +x=10 +while x>=1: + print('x= ',x) + x-=1 + if x==5: # if x is 5 then come out from while loop + break +print("out of loop") \ No newline at end of file diff --git a/the_else_suite.py b/the_else_suite.py new file mode 100644 index 0000000..7a05d54 --- /dev/null +++ b/the_else_suite.py @@ -0,0 +1,13 @@ +for i in range(10): + print("yes") +else: + print("no") + + + +for i in range(0): + print("yes") +else: + print("no") + + \ No newline at end of file diff --git a/the_global_keyword.py b/the_global_keyword.py new file mode 100644 index 0000000..66147f8 --- /dev/null +++ b/the_global_keyword.py @@ -0,0 +1,25 @@ +#5-2-22 +a=1 +def myfunction(): + a=2 + print('a= ',a) +myfunction() +print('a= ',a) + +a=1 +def myfunction(): + global a + print('global a= ',a) + a=2 + print('modified a= ',a) +myfunction() +print('global a= ',a) + +a=1 +def myfunction(): + a=2 + x=globals()['a'] + print('global var a=',x) + print('local var a= ',a) +myfunction() +print('global var a=',a) \ No newline at end of file diff --git a/the_pass_statement.py b/the_pass_statement.py new file mode 100644 index 0000000..bb8b42e --- /dev/null +++ b/the_pass_statement.py @@ -0,0 +1,19 @@ +# 26-3-22 +# using pass to do nothing +x=0 +while x<10: + x+=1 + if x>5: # if x>5 then continue next iteration + pass + print('x= ',x) +print("out of loop") + + + +#retrieving only negative numbers from a ist +num=[1,2,3,-5,-6,-7,-8,-9,10,11,12,13,14,15] +for i in num: + if(i>0): + pass # we are not interested\ + else: + print(i) #this is what we need diff --git a/the_return_statement.py b/the_return_statement.py new file mode 100644 index 0000000..7f8ff09 --- /dev/null +++ b/the_return_statement.py @@ -0,0 +1,31 @@ +#26-3-22 +#a function to find sum of two numbers +def sum(a,b): + print('sum= ',a+b) +sum(6,12) #call sum() and pass 5 to 10 +sum(1.5,2.5)#call sum() and pass 1.5 and 2.5 + + +# a function to return sum of the two numbers +def sum (a,b): + return a+b # result is returned from here +#call sum() and pass 5 and 10 +#get the returned result into res +res=sum(12,20) +print('The result is ',res) + + +#Program to print prime numbers upto a given numbers +#accept upto what numbers the user wants\ +max = int(input("upto what numbers?")) + +for num in range(3,max+1): #generate from 2 onwards till max + for i in range(3,num): # i represents numbers from 2 to num-1 + if(num %i)==0: # if num is divisible by i + break # it is not prime, hence go back and check next num + else: + print(num)# otherwise it is prime and hence display + + + + diff --git a/The While Loop.py b/the_while_loop.py similarity index 100% rename from The While Loop.py rename to the_while_loop.py diff --git a/transpose_of_a_matrix.py b/transpose_of_a_matrix.py new file mode 100644 index 0000000..9754b13 --- /dev/null +++ b/transpose_of_a_matrix.py @@ -0,0 +1,9 @@ +from numpy import* +r,c=[int(a) for a in input("enter rows,cols: ").split()] +str=input('enter matrix elements:\n') +x=reshape(matrix(str),(r,c)) +print('the original matrix: ') +print(x) +print('the transpose matrix: ') +y=x.transpose() +print(y) \ No newline at end of file diff --git a/tuple_into_set.py b/tuple_into_set.py new file mode 100644 index 0000000..4d126aa --- /dev/null +++ b/tuple_into_set.py @@ -0,0 +1,15 @@ +#12-7-22 +#tuple into set + + +# create tuple +t = (1, 2, 3) + +# print tuple +print(type(t), " ", t) + +# call set() method +s = set(t) + +# print set +print(type(s), " ", s) diff --git a/unaryminasoperator.py b/unary_minas_operator.py similarity index 100% rename from unaryminasoperator.py rename to unary_minas_operator.py diff --git a/updating_dictionary.py b/updating_dictionary.py new file mode 100644 index 0000000..7b797f9 --- /dev/null +++ b/updating_dictionary.py @@ -0,0 +1,7 @@ +# 4-8-22 +# updating a dictionary +dict = {'Name': 'Jalpa', 'Age': 22, 'Class': 'MCA'} +dict['Age'] = 22; # update existing entry +dict['School'] = "NJMS school"; # Add new entry +print("dict['Age']: ", dict['Age']) +print("dict['School']: ", dict['School']) \ No newline at end of file diff --git a/updating_elements_of_a_list.py b/updating_elements_of_a_list.py new file mode 100644 index 0000000..af4f10d --- /dev/null +++ b/updating_elements_of_a_list.py @@ -0,0 +1,14 @@ +#displaying list elements in reverse order +days=['Sunday','Monday','Tuesday','Wednesday','Thursday'] + +print('\nIn reverse order: ') +i=len(days)-1 # i will be 4 +while i>=0: + print(days[i]) #display from 4th to 0th elements + i-=1 + +print('\nIn reverse order: ') +i=-1 #days [-1] represents lasst element +while i>=-len(days): #display from -1th to -5th elements + print(days[i]) + i-=1 \ No newline at end of file diff --git a/uppercase and lowercase program.py b/uppercase_and_lowercase.py similarity index 100% rename from uppercase and lowercase program.py rename to uppercase_and_lowercase.py diff --git a/using_for_loop_with_dictionaries.py b/using_for_loop_with_dictionaries.py new file mode 100644 index 0000000..be435fa --- /dev/null +++ b/using_for_loop_with_dictionaries.py @@ -0,0 +1,33 @@ +#using for loop with dictionaries +#take a dictionary +colors={'r':"red",'g':"green",'b':"blue",'w': "white"} + +# display only keys +for k in colors: + print(k) + +#pass keys to dictionary and display the values + for k in colors: + print(colors[k]) + +# items() method returns key and value pair into k,v +for k,v in colors.items(): + print('keys={} Value={}'.format(k,v)) + + +#finding how many times each letter is repeated in a string +#take a string with some letters +str="Book" + +#take an empty dictionary +dict={} + +#store into dict each letter as key and its +#number of coccurences as value +for x in str: + dict[x]= dict.get(x,0) +1 + +#display key and value parts of dict + for x,v in dict.items(): + print('key= {}\t Its occurence={}'.format(k,v)) + diff --git a/using variable in string.py b/using_variable_in_string.py similarity index 100% rename from using variable in string.py rename to using_variable_in_string.py diff --git a/viewing_and_copying_arrays.py b/viewing_and_copying_arrays.py new file mode 100644 index 0000000..da36d0a --- /dev/null +++ b/viewing_and_copying_arrays.py @@ -0,0 +1,26 @@ +#29-1-22 +#creating view for an array +from numpy import * + +a=arange(1,6)#create a with elements 1 to 5 +b=a.view() #create a view of a and call it b +print('original array: ',a) +print('new array: ',b) + +b[0]=99 #modify 0th element of b +print('after modification: ') +print('original array: ',a) +print('new array: ',b) + + +#copying an array +from numpy import * +a= arange(1,6) # create a with elements 1 to 5 +b=a.copy() #create a copy of a and call it b +print('original array: ',a) +print('new array: ',b) + +b[0]=99 #modify 0th element of b +print('after modification: ') +print('original array: ',a) +print('new array: ',b) diff --git a/while_loop.py b/while_loop.py new file mode 100644 index 0000000..855d9f3 --- /dev/null +++ b/while_loop.py @@ -0,0 +1,4 @@ +i=1 +while i<=10: + print(i) + i+=1 \ No newline at end of file diff --git a/working_with_characters.py b/working_with_characters.py new file mode 100644 index 0000000..82f4341 --- /dev/null +++ b/working_with_characters.py @@ -0,0 +1,17 @@ +str=input('enter a characters: ') +ch=str[0] +if ch.isalnum(): + print('It is alphabet or numeric character') + if ch.isalpha(): + print('It is an alphabet') + if ch.isupper(): + print('it is capital letter') + else: + print('it is a lower letter') + else: + print('It is numeric digit') +elif ch.isspace(): + print('it is a space') +else: + print('it may be a special character') + diff --git a/zeros_and_ones_function.py b/zeros_and_ones_function.py new file mode 100644 index 0000000..1a12ebe --- /dev/null +++ b/zeros_and_ones_function.py @@ -0,0 +1,8 @@ +#29-1-22 +#creating arrays using zeros() and ones() +from numpy import * +a=zeros(5,int) +print(a) + +b=ones(5)#default datatype is float +print(b) \ No newline at end of file