From d3653fdc5b4e99bd4f79a2114ace2aeb4a40d915 Mon Sep 17 00:00:00 2001 From: JaySimaria Date: Fri, 21 Jan 2022 11:17:38 +0530 Subject: [PATCH 01/10] Programs for practice --- For Loop.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 For Loop.py 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 From 63409d98fbaace6cbc9d0c9139703f9af9c73be1 Mon Sep 17 00:00:00 2001 From: JaySimaria Date: Sat, 22 Jan 2022 07:03:11 +0530 Subject: [PATCH 02/10] Programs for Practice --- Desecending order.py | 9 +++++++++ for loop with list.py | 5 +++++ sum of list using for loop.py | 8 ++++++++ 3 files changed, 22 insertions(+) create mode 100644 Desecending order.py create mode 100644 for loop with list.py create mode 100644 sum of list using for loop.py 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/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/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 From 5ee3e1e38f7ac47fdb4ef765001d7ec0eca75b37 Mon Sep 17 00:00:00 2001 From: JaySimaria Date: Mon, 24 Jan 2022 11:54:12 +0530 Subject: [PATCH 03/10] programs for practice --- .idea/.name | 1 + Check file size .py | 4 ---- Infinite_loop.py | 4 ++++ ... in python.py => Positive_and_negative_number.py | 0 add two numbers.py => add _two _numbers.py | 0 AddTwoNumber.py => add _two_number.py | 0 area of triangle.py => area _of_ triangle.py | 0 assertion.py | 9 +++++++++ Boolean operator.py => boolean_operator.py | 0 byte types array.py => byte _types_ array.py | 0 ByteArray.py => byte_array (2).py | 0 ByteArray,py.py => byte_array.py | 0 calender.py | 4 ++-- check_file_ size .py | 13 +++++++++++++ ... line arguments.py => command _line_arguments.py | 4 ++-- ...s obh system.py => convert_numbers_obh_system.py | 0 count program.py => count.py | 0 count less than.py => count_less_than.py | 0 Desecending order.py => desecending_order.py | 0 ...de Another Number.py => divide_another_number.py | 0 doc string.py => doc_string.py | 0 even or odd number.py => even _or _odd_number.py | 0 Factor Number .py => factor_number .py | 0 Factorial Number.py => factorial_number.py | 0 first variable program .py => first_variable.py | 0 For Loop.py => for_loop.py | 0 for loop with list.py => for_loop_with_list.py | 0 If.....Elif......Else.py => if_elif_else.py | 0 If....Else Statement.py => if_else_statement.py | 0 If Statement.py => if_statement.py | 0 ...e set operaton.py => illustrate_set_operaton.py | 0 ...sing for loop.py => index_list_using_for_loop.py | 0 Input Statements Program.py => input_statements.py | 0 Interval Prime.py => interval_prime.py | 0 Is Not Operators.py => is_not_operators.py | 0 jay $.py | 4 ---- Joined Two List.py => joined_two_list.py | 0 Json 2 program.py => json_2.py | 2 +- list program.py => list.py | 1 - logical operators.py => logical_operators.py | 0 ...m Of two numbers.py => maxmium_of_two_numbers.py | 4 ++-- Multipication Table.py => multipication_table.py | 0 multiplication table.py => multiplication_table.py | 0 mutation program.py => mutation_program.py | 0 Natural Numbers.py => natural_numbers.py | 0 nested_loop.py | 3 +++ Output Statements.py => output_statements.py | 0 ...st.py => output_statements_with_variable_list.py | 0 ...ping.py => parameters_in_function_and_scoping.py | 0 polymorphism program.py => polymorphism.py | 0 ...out new line .py => program_without_new_line .py | 0 Random Number.py => random_number.py | 0 RangeFunction.py => range_function.py | 0 relational operators.py => relational_operators.py | 0 ... as string.py => remove_punctuation_as_string.py | 0 Scoping Program.py => scoping_program.py | 0 set program.py => set_program.py | 0 Simple Calculator.py => simple_calculator.py | 0 simple variable program.py => simple_variable.py | 0 Sort Words.py => sort_words.py | 0 ...ification program.py => specification_program.py | 4 ++-- ... or nunbers.py => string_with_tabs_or_numbers.py | 0 StudentResult.py => student_result.py | 0 ...ing for loop.py => sum_of_list_using_for_loop.py | 0 sum_of_list_using_while_loop.py | 12 ++++++++++++ swap two variables.py => swap_two_variables.py | 0 ... Two Variables .py => swapping_two_variables .py | 0 The While Loop.py => the_while_loop.py | 0 unaryminasoperator.py => unary_minas_operator.py | 0 ...wercase program.py => uppercase_and_lowercase.py | 0 ...able in string.py => using_variable_in_string.py | 0 while_loop.py | 4 ++++ 72 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 .idea/.name delete mode 100644 Check file size .py create mode 100644 Infinite_loop.py rename Positive and negative number program in python.py => Positive_and_negative_number.py (100%) rename add two numbers.py => add _two _numbers.py (100%) rename AddTwoNumber.py => add _two_number.py (100%) rename area of triangle.py => area _of_ triangle.py (100%) create mode 100644 assertion.py rename Boolean operator.py => boolean_operator.py (100%) rename byte types array.py => byte _types_ array.py (100%) rename ByteArray.py => byte_array (2).py (100%) rename ByteArray,py.py => byte_array.py (100%) create mode 100644 check_file_ size .py rename command line arguments.py => command _line_arguments.py (86%) rename convert numbers obh system.py => convert_numbers_obh_system.py (100%) rename count program.py => count.py (100%) rename count less than.py => count_less_than.py (100%) rename Desecending order.py => desecending_order.py (100%) rename # Divide Another Number.py => divide_another_number.py (100%) rename doc string.py => doc_string.py (100%) rename even or odd number.py => even _or _odd_number.py (100%) rename Factor Number .py => factor_number .py (100%) rename Factorial Number.py => factorial_number.py (100%) rename first variable program .py => first_variable.py (100%) rename For Loop.py => for_loop.py (100%) rename for loop with list.py => for_loop_with_list.py (100%) rename If.....Elif......Else.py => if_elif_else.py (100%) rename If....Else Statement.py => if_else_statement.py (100%) rename If Statement.py => if_statement.py (100%) rename illustrate set operaton.py => illustrate_set_operaton.py (100%) rename Index List using for loop.py => index_list_using_for_loop.py (100%) rename Input Statements Program.py => input_statements.py (100%) rename Interval Prime.py => interval_prime.py (100%) rename Is Not Operators.py => is_not_operators.py (100%) delete mode 100644 jay $.py rename Joined Two List.py => joined_two_list.py (100%) rename Json 2 program.py => json_2.py (89%) rename list program.py => list.py (98%) rename logical operators.py => logical_operators.py (100%) rename Maxmium Of two numbers.py => maxmium_of_two_numbers.py (78%) rename Multipication Table.py => multipication_table.py (100%) rename multiplication table.py => multiplication_table.py (100%) rename mutation program.py => mutation_program.py (100%) rename Natural Numbers.py => natural_numbers.py (100%) create mode 100644 nested_loop.py rename Output Statements.py => output_statements.py (100%) rename Output Statements with Variable list.py => output_statements_with_variable_list.py (100%) rename parameters in function and scoping.py => parameters_in_function_and_scoping.py (100%) rename polymorphism program.py => polymorphism.py (100%) rename python program without new line .py => program_without_new_line .py (100%) rename Random Number.py => random_number.py (100%) rename RangeFunction.py => range_function.py (100%) rename relational operators.py => relational_operators.py (100%) rename remove punctuation as string.py => remove_punctuation_as_string.py (100%) rename Scoping Program.py => scoping_program.py (100%) rename set program.py => set_program.py (100%) rename Simple Calculator.py => simple_calculator.py (100%) rename simple variable program.py => simple_variable.py (100%) rename Sort Words.py => sort_words.py (100%) rename python specification program.py => specification_program.py (74%) rename whitespaces to string with tabs or nunbers.py => string_with_tabs_or_numbers.py (100%) rename StudentResult.py => student_result.py (100%) rename sum of list using for loop.py => sum_of_list_using_for_loop.py (100%) create mode 100644 sum_of_list_using_while_loop.py rename swap two variables.py => swap_two_variables.py (100%) rename Swapping Two Variables .py => swapping_two_variables .py (100%) rename The While Loop.py => the_while_loop.py (100%) rename unaryminasoperator.py => unary_minas_operator.py (100%) rename uppercase and lowercase program.py => uppercase_and_lowercase.py (100%) rename using variable in string.py => using_variable_in_string.py (100%) create mode 100644 while_loop.py 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/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/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/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/AddTwoNumber.py b/add _two_number.py similarity index 100% rename from AddTwoNumber.py rename to add _two_number.py 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/assertion.py b/assertion.py new file mode 100644 index 0000000..ed304a8 --- /dev/null +++ b/assertion.py @@ -0,0 +1,9 @@ +def avg(marks): + assert len(marks) != 0,"List is empty." + return sum(marks)/len(marks) + +mark2 = [55,88,78,90,79] +print("Average of mark2:",avg(mark2)) + +mark1 = [] +print("Average of mark1:",avg(mark1)) \ No newline at end of file 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/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/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/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/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/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/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/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/Desecending order.py b/desecending_order.py similarity index 100% rename from Desecending order.py rename to desecending_order.py 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/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/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/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/For Loop.py b/for_loop.py similarity index 100% rename from For Loop.py rename to for_loop.py diff --git a/for loop with list.py b/for_loop_with_list.py similarity index 100% rename from for loop with list.py rename to for_loop_with_list.py 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/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/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/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/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/logical operators.py b/logical_operators.py similarity index 100% rename from logical operators.py rename to logical_operators.py diff --git a/Maxmium Of two numbers.py b/maxmium_of_two_numbers.py similarity index 78% rename from Maxmium Of two numbers.py rename to maxmium_of_two_numbers.py index 5526a23..1b04984 100644 --- a/Maxmium Of two numbers.py +++ b/maxmium_of_two_numbers.py @@ -1,9 +1,9 @@ # python program to find the -# maxmiun of two numbers +# maxmimum of two numbers def maxmium(a,b): df - if a >= b: + if a >= b : return a else: return b 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 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/nested_loop.py b/nested_loop.py new file mode 100644 index 0000000..4d6a281 --- /dev/null +++ b/nested_loop.py @@ -0,0 +1,3 @@ +for i in range(3): # i values are from o to 2 + for j in range(4): # j values are from 0 to 3 + print('i=',i,'\t','j=',j)# display i and j values \ No newline at end of file diff --git a/Output Statements.py b/output_statements.py similarity index 100% rename from Output Statements.py rename to output_statements.py diff --git a/Output Statements with Variable list.py b/output_statements_with_variable_list.py similarity index 100% rename from Output Statements with Variable list.py rename to output_statements_with_variable_list.py diff --git a/parameters in function and scoping.py b/parameters_in_function_and_scoping.py similarity index 100% rename from parameters in function and scoping.py rename to parameters_in_function_and_scoping.py diff --git a/polymorphism program.py b/polymorphism.py similarity index 100% rename from polymorphism program.py rename to polymorphism.py 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/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/relational operators.py b/relational_operators.py similarity index 100% rename from relational operators.py rename to relational_operators.py 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/Scoping Program.py b/scoping_program.py similarity index 100% rename from Scoping Program.py rename to scoping_program.py 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 variable program.py b/simple_variable.py similarity index 100% rename from simple variable program.py rename to simple_variable.py diff --git a/Sort Words.py b/sort_words.py similarity index 100% rename from Sort Words.py rename to sort_words.py 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/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/StudentResult.py b/student_result.py similarity index 100% rename from StudentResult.py rename to student_result.py diff --git a/sum of list using for loop.py b/sum_of_list_using_for_loop.py similarity index 100% rename from sum of list using for loop.py rename to sum_of_list_using_for_loop.py 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 From dc04bc999bb396ea1534b4e6c020a532d1b8d8b1 Mon Sep 17 00:00:00 2001 From: JaySimaria Date: Wed, 26 Jan 2022 14:05:49 +0530 Subject: [PATCH 05/10] Practice Programs --- add _two_number.py => add_two_number.py | 0 add _two _numbers.py => add_two_numbers.py | 0 area _of_ triangle.py => area_of_triangle.py | 0 assertion.py | 23 +++++++++----- byte _types_ array.py => byte_types_array.py | 0 check_file_ size .py => check_file_size .py | 0 ..._arguments.py => command_line_arguments.py | 0 continue_statement.py | 9 ++++++ cosine_series.py | 20 ++++++++++++ ...r _odd_number.py => even_or _odd_number.py | 0 exponential_series.py | 16 ++++++++++ fibonacci_number.py | 16 ++++++++++ sine_series.py | 22 +++++++++++++ the_pass_statement.py | 19 ++++++++++++ the_return_statement.py | 31 +++++++++++++++++++ 15 files changed, 149 insertions(+), 7 deletions(-) rename add _two_number.py => add_two_number.py (100%) rename add _two _numbers.py => add_two_numbers.py (100%) rename area _of_ triangle.py => area_of_triangle.py (100%) rename byte _types_ array.py => byte_types_array.py (100%) rename check_file_ size .py => check_file_size .py (100%) rename command _line_arguments.py => command_line_arguments.py (100%) create mode 100644 continue_statement.py create mode 100644 cosine_series.py rename even _or _odd_number.py => even_or _odd_number.py (100%) create mode 100644 exponential_series.py create mode 100644 fibonacci_number.py create mode 100644 sine_series.py create mode 100644 the_pass_statement.py create mode 100644 the_return_statement.py diff --git a/add _two_number.py b/add_two_number.py similarity index 100% rename from add _two_number.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/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/assertion.py b/assertion.py index ed304a8..4bf3a4b 100644 --- a/assertion.py +++ b/assertion.py @@ -1,9 +1,18 @@ -def avg(marks): - assert len(marks) != 0,"List is empty." - return sum(marks)/len(marks) +#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) -mark2 = [55,88,78,90,79] -print("Average of mark2:",avg(mark2)) -mark1 = [] -print("Average of mark1:",avg(mark1)) \ No newline at end of file + + +#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/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/check_file_ size .py b/check_file_size .py similarity index 100% rename from check_file_ size .py rename to check_file_size .py diff --git a/command _line_arguments.py b/command_line_arguments.py similarity index 100% rename from command _line_arguments.py rename to command_line_arguments.py 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/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/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/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 c5: # 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 + + + + From 72add97bdf4cb40bb82ecdedbe891f52c0d1f943 Mon Sep 17 00:00:00 2001 From: JaySimaria Date: Mon, 31 Jan 2022 13:27:43 +0530 Subject: [PATCH 06/10] Programs for Practice --- aliasing_the_array.py | 11 ++++++ arange.py | 6 ++++ array_module.py | 23 +++++++++++++ array_using_index_method.py | 22 ++++++++++++ array_using_numpy.py | 11 ++++++ attributes_an_array.py | 22 ++++++++++++ bubble_sort.py | 33 ++++++++++++++++++ comparing_arrays.py | 63 ++++++++++++++++++++++++++++++++++ creating_arrays_using_array.py | 16 +++++++++ dimensions_of_arrays.py | 15 ++++++++ indexing_and_slicing_array.py | 53 ++++++++++++++++++++++++++++ linspace.py | 6 ++++ logspace.py | 11 ++++++ mathematical_operations.py | 26 ++++++++++++++ processing_the_arrays.py | 50 +++++++++++++++++++++++++++ sequential_search.py | 22 ++++++++++++ slicing_and_indexing.py | 41 ++++++++++++++++++++++ viewing_and_copying_arrays.py | 26 ++++++++++++++ zeros_and_ones_function.py | 8 +++++ 19 files changed, 465 insertions(+) create mode 100644 aliasing_the_array.py create mode 100644 arange.py create mode 100644 array_module.py create mode 100644 array_using_index_method.py create mode 100644 array_using_numpy.py create mode 100644 attributes_an_array.py create mode 100644 bubble_sort.py create mode 100644 comparing_arrays.py create mode 100644 creating_arrays_using_array.py create mode 100644 dimensions_of_arrays.py create mode 100644 indexing_and_slicing_array.py create mode 100644 linspace.py create mode 100644 logspace.py create mode 100644 mathematical_operations.py create mode 100644 processing_the_arrays.py create mode 100644 sequential_search.py create mode 100644 slicing_and_indexing.py create mode 100644 viewing_and_copying_arrays.py create mode 100644 zeros_and_ones_function.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/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/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/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/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/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/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/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 Date: Sat, 5 Feb 2022 19:28:01 +0530 Subject: [PATCH 07/10] Programs for practice --- caling_a_function.py | 8 +++++ check_even_or_prime.py | 40 +++++++++++++++++++++++++ checking_membership.py | 7 +++++ finding_sub.py | 17 +++++++++++ formal_and_actual_arguments.py | 44 ++++++++++++++++++++++++++++ functions_are_first_class.py | 44 ++++++++++++++++++++++++++++ functions_first_class.py | 4 +++ indexing_in_strings.py | 32 ++++++++++++++++++++ inserting_substring_string.py | 15 ++++++++++ matrix_addition_and_mutiplication.py | 15 ++++++++++ multidimensional_array.py | 33 +++++++++++++++++++++ number_of_characters_and_words.py | 21 +++++++++++++ occurences_substring.py | 29 ++++++++++++++++++ pass_by_object_reference.py | 8 +++++ passing_a_group_of_elements.py | 8 +++++ recursive_functions.py | 21 +++++++++++++ returning_multiple_values.py | 20 +++++++++++++ returning_results_functions.py | 30 +++++++++++++++++++ searching_in_the_strings.py | 16 ++++++++++ sorting_strings.py | 9 ++++++ splitting_and_joining_strings.py | 4 +++ the_global_keyword.py | 25 ++++++++++++++++ transpose_of_a_matrix.py | 9 ++++++ working_with_characters.py | 17 +++++++++++ 24 files changed, 476 insertions(+) create mode 100644 caling_a_function.py create mode 100644 check_even_or_prime.py create mode 100644 checking_membership.py create mode 100644 finding_sub.py create mode 100644 formal_and_actual_arguments.py create mode 100644 functions_are_first_class.py create mode 100644 functions_first_class.py create mode 100644 indexing_in_strings.py create mode 100644 inserting_substring_string.py create mode 100644 matrix_addition_and_mutiplication.py create mode 100644 multidimensional_array.py create mode 100644 number_of_characters_and_words.py create mode 100644 occurences_substring.py create mode 100644 pass_by_object_reference.py create mode 100644 passing_a_group_of_elements.py create mode 100644 recursive_functions.py create mode 100644 returning_multiple_values.py create mode 100644 returning_results_functions.py create mode 100644 searching_in_the_strings.py create mode 100644 sorting_strings.py create mode 100644 splitting_and_joining_strings.py create mode 100644 the_global_keyword.py create mode 100644 transpose_of_a_matrix.py create mode 100644 working_with_characters.py 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/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/finding_sub.py b/finding_sub.py new file mode 100644 index 0000000..964861a --- /dev/null +++ b/finding_sub.py @@ -0,0 +1,17 @@ +str=input('enter main string') +sub=input('enter sub string') +n=str.find(sub,0,len(str)) +if n==-1: + print('sub string not found') +else: + print('sub string found at position: ',n+1) + +str = input('enter main string') +sub = input('enter sub string') +try: + n=str.index(sub,0,len(str)) +except valueerror: + print('sub string not found') +else: + print('sub string found at position: ',n+1) + 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/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/indexing_in_strings.py b/indexing_in_strings.py new file mode 100644 index 0000000..d0bd96d --- /dev/null +++ b/indexing_in_strings.py @@ -0,0 +1,32 @@ +#indexing on strings +str='core python' +n=len(str) # n = no.of chars in str +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/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/matrix_addition_and_mutiplication.py b/matrix_addition_and_mutiplication.py new file mode 100644 index 0000000..d04f80f --- /dev/null +++ b/matrix_addition_and_mutiplication.py @@ -0,0 +1,15 @@ +# matrix multiplication +import sys +from numpy import* +r1,c1=[int(a) for a in input("first matrix rows,cols: ").split()] +r2,c2=[int(a) for a in input("second matrix rows,cols: ").split()] +if c1!=r2: + print('multiplication is not possible') + sys.exit() +str1=input('enter first matrix elements:\n') +x=reshape(matrix(str1),(r1,c1)) +str2=input('enter second matrix elements:\n') +y=reshape(matrix(str2),(r2,c2)) +print('the product matrix:') +z=x*y +print(z) \ 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/number_of_characters_and_words.py b/number_of_characters_and_words.py new file mode 100644 index 0000000..1437658 --- /dev/null +++ b/number_of_characters_and_words.py @@ -0,0 +1,21 @@ +str=input('enter a string: ') +i=0 +for s in str: + print(str[i],end=' ') + i+=1 +print('\nNO. of characters: ',i) + +str=input('enter a string: ') +i=c=0 +flag=True +for s in str: + if flag==False and str[i]==' ': + c+=1 +if str[i]==' ': + flag=True +else: + flag=False +i+=1 +print('no of words: ',c+1) + + diff --git a/occurences_substring.py b/occurences_substring.py new file mode 100644 index 0000000..3644c77 --- /dev/null +++ b/occurences_substring.py @@ -0,0 +1,29 @@ +str=input('enter main string: ') +sub=input('enter sub string: ') +i=0 +flag=False +n=len(str) +while i=1: + prod*=n + n-=1 + return prod +for i in range(1,11): + print('factorial of {} is {}'.format(i,fact(i))) + 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/sorting_strings.py b/sorting_strings.py new file mode 100644 index 0000000..e84215f --- /dev/null +++ b/sorting_strings.py @@ -0,0 +1,9 @@ +str=[] +n=int(input('how many strings?')) +for i in range(n): + print('enter string: ',end=' ') + str.append(input()) + str1=sorted(str) + print('sorted list: ') + for i in str1: + print(i) 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/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/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/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') + From 7306267c90a4b22fbde9c858b8aee8817e3c30ae Mon Sep 17 00:00:00 2001 From: JaySimaria Date: Fri, 11 Feb 2022 19:42:42 +0530 Subject: [PATCH 08/10] Practice Programs --- finding_biggest_smallest_elements.py | 19 +++++++++++ finding_common_elements.py | 17 ++++++++++ function_decorators.py | 50 ++++++++++++++++++++++++++++ generators.py | 18 ++++++++++ lambdas.py | 46 +++++++++++++++++++++++++ list_processing_methods.py | 36 ++++++++++++++++++++ list_using_range.py | 31 +++++++++++++++++ list_with_types_of_elements.py | 16 +++++++++ module.py | 9 +++++ nested_lists.py | 8 +++++ number_of_occurences.py | 17 ++++++++++ passing_a_group_of_elements.py | 14 ++++++++ sorting_the_list_elements.py | 28 ++++++++++++++++ sorting_types_of_data.py | 25 ++++++++++++++ structured_programming.py | 27 +++++++++++++++ updating_elements_of_a_list.py | 14 ++++++++ 16 files changed, 375 insertions(+) create mode 100644 finding_biggest_smallest_elements.py create mode 100644 finding_common_elements.py create mode 100644 function_decorators.py create mode 100644 generators.py create mode 100644 lambdas.py create mode 100644 list_processing_methods.py create mode 100644 list_using_range.py create mode 100644 list_with_types_of_elements.py create mode 100644 module.py create mode 100644 nested_lists.py create mode 100644 number_of_occurences.py create mode 100644 sorting_the_list_elements.py create mode 100644 sorting_types_of_data.py create mode 100644 structured_programming.py create mode 100644 updating_elements_of_a_list.py diff --git a/finding_biggest_smallest_elements.py b/finding_biggest_smallest_elements.py new file mode 100644 index 0000000..9bf290f --- /dev/null +++ b/finding_biggest_smallest_elements.py @@ -0,0 +1,19 @@ +# finding biggest and smallest numbers in a list of numbers +x=[] # take an empty list + +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 list x +print('The list is: ',x) #display the list +big=x[0] # initially Oth element becomes maximum and minimum +small=x[0] + +for i in range(1,n): # repeat from 1 to n-1 elements + if x[i]>big: big=x[1] # if any other element is > big, take it as #big + if x[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/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/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 From 967c5f54b0628dd314f22dee76ef56c8290240fb Mon Sep 17 00:00:00 2001 From: JaySimaria Date: Tue, 15 Feb 2022 20:37:10 +0530 Subject: [PATCH 09/10] pratice program --- Deleting_elements_from_a_tuple.py | 12 +++++ converting_lists_into_dictionary.py | 13 +++++ converting_strings_into_dictionary.py | 21 ++++++++ dictionary.py | 13 +++++ dictionary_methods.py | 73 +++++++++++++++++++++++++++ dictionary_using_lambdas.py | 10 ++++ inserting_elements_in_a_tuple.py | 17 +++++++ list_using_range.py | 1 - modifying_elements.py | 18 +++++++ ordered_dictionaries.py | 11 ++++ passing_dictionaries_to_functions.py | 9 ++++ sorting_nested_tuples.py | 9 ++++ using_for_loop_with_dictionaries.py | 33 ++++++++++++ 13 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 Deleting_elements_from_a_tuple.py create mode 100644 converting_lists_into_dictionary.py create mode 100644 converting_strings_into_dictionary.py create mode 100644 dictionary.py create mode 100644 dictionary_methods.py create mode 100644 dictionary_using_lambdas.py create mode 100644 inserting_elements_in_a_tuple.py create mode 100644 modifying_elements.py create mode 100644 ordered_dictionaries.py create mode 100644 passing_dictionaries_to_functions.py create mode 100644 sorting_nested_tuples.py create mode 100644 using_for_loop_with_dictionaries.py 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/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/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/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/list_using_range.py b/list_using_range.py index 5971e68..de8057b 100644 --- a/list_using_range.py +++ b/list_using_range.py @@ -19,7 +19,6 @@ #displaying list of elements using while and for loop list=[10,20,30,40,50,60] - print('using while loop') i=0 while i Date: Tue, 23 Aug 2022 19:56:25 +0530 Subject: [PATCH 10/10] Add files via upload --- Insertion_array_element.py | 10 ++++ Remove_items_from_a_list.py | 10 ++++ Swap_function.py | 19 ++++++ all_negative_integers_of_a_range.py | 12 ++++ all_positive_integers_in_a_range.py | 11 ++++ binary_numbers_using_queue.py | 43 +++++++++++++ check_if_two_arrays_are_equal_or_not.py | 31 ++++++++++ cloning_or_copying.py | 11 ++++ compound_interest.py | 11 ++++ convert_set_into_list.py | 7 +++ convert_set_to_string.py | 12 ++++ count_occurance_of_an_element_in_list.py | 13 ++++ different_ways_to_clear_a_list.py | 11 ++++ duplicate_integers_of_a_list.py | 13 ++++ element_exist_in_a_list.py | 14 +++++ even_length_words_of_string.py | 11 ++++ find_largest_number.py | 12 ++++ find_the_length_of_string_using_sum.py | 8 +++ find_the_size_of_a_set.py | 12 ++++ find_the_size_of_tuple.py | 8 +++ find_the_smallest_number.py | 12 ++++ find_the_sum_of_all_items_dict.py | 18 ++++++ finding_length_of_list.py | 20 +++++++ first_repeating_element_array_integers.py | 60 +++++++++++++++++++ food_ordering_system.py | 51 ++++++++++++++++ greatest_three_numbers.py | 9 +++ handling_missing_keys.py | 15 +++++ iterator_over_a_set.py | 8 +++ max_and_min.py | 25 ++++++++ merging_two_dictionaries.py | 16 +++++ minium_two_numbers.py | 13 ++++ missing_elements_of_a_range.py | 25 ++++++++ multiplication_all_numbers.py | 13 ++++ negative_number_of_a_list.py | 16 +++++ non_repeating_element.py | 24 ++++++++ odd_number_in_a_list.py | 10 ++++ odd_numbers_in_range.py | 10 ++++ override.py | 1 + pop_into_the_stack.py | 29 +++++++++ positive_number_of_list.py | 12 ++++ queue_immplementation__using_list.py | 24 ++++++++ remove_dictionary_key_words.py | 21 +++++++ remove_duplicates_given_string.py | 23 +++++++ remove_empty_list_from_list.py | 12 ++++ remove_empty_tuples.py | 12 ++++ remove_tuples_of_length_k.py | 19 ++++++ reverse_number.py | 7 +++ reversing_list.py | 15 +++++ second_largest_number.py | 15 +++++ set_into_dictionary.py | 17 ++++++ simple_interest.py | 14 +++++ sorted_dict_by_key_or_value.py | 9 +++ stack_immplementation_in_a_list.py | 28 +++++++++ ...plementation_using_collections_of_deque.py | 28 +++++++++ stack_using_queue_model.py | 31 ++++++++++ sum_of_average_of_a _list.py | 12 ++++ sum_of_tuple_elements.py | 17 ++++++ swap_elements_in_string_list.py | 16 +++++ tuple_into_set.py | 15 +++++ updating_dictionary.py | 7 +++ 60 files changed, 1008 insertions(+) create mode 100644 Insertion_array_element.py create mode 100644 Remove_items_from_a_list.py create mode 100644 Swap_function.py create mode 100644 all_negative_integers_of_a_range.py create mode 100644 all_positive_integers_in_a_range.py create mode 100644 binary_numbers_using_queue.py create mode 100644 check_if_two_arrays_are_equal_or_not.py create mode 100644 cloning_or_copying.py create mode 100644 compound_interest.py create mode 100644 convert_set_into_list.py create mode 100644 convert_set_to_string.py create mode 100644 count_occurance_of_an_element_in_list.py create mode 100644 different_ways_to_clear_a_list.py create mode 100644 duplicate_integers_of_a_list.py create mode 100644 element_exist_in_a_list.py create mode 100644 even_length_words_of_string.py create mode 100644 find_largest_number.py create mode 100644 find_the_length_of_string_using_sum.py create mode 100644 find_the_size_of_a_set.py create mode 100644 find_the_size_of_tuple.py create mode 100644 find_the_smallest_number.py create mode 100644 find_the_sum_of_all_items_dict.py create mode 100644 finding_length_of_list.py create mode 100644 first_repeating_element_array_integers.py create mode 100644 food_ordering_system.py create mode 100644 greatest_three_numbers.py create mode 100644 handling_missing_keys.py create mode 100644 iterator_over_a_set.py create mode 100644 max_and_min.py create mode 100644 merging_two_dictionaries.py create mode 100644 minium_two_numbers.py create mode 100644 missing_elements_of_a_range.py create mode 100644 multiplication_all_numbers.py create mode 100644 negative_number_of_a_list.py create mode 100644 non_repeating_element.py create mode 100644 odd_number_in_a_list.py create mode 100644 odd_numbers_in_range.py create mode 100644 override.py create mode 100644 pop_into_the_stack.py create mode 100644 positive_number_of_list.py create mode 100644 queue_immplementation__using_list.py create mode 100644 remove_dictionary_key_words.py create mode 100644 remove_duplicates_given_string.py create mode 100644 remove_empty_list_from_list.py create mode 100644 remove_empty_tuples.py create mode 100644 remove_tuples_of_length_k.py create mode 100644 reverse_number.py create mode 100644 reversing_list.py create mode 100644 second_largest_number.py create mode 100644 set_into_dictionary.py create mode 100644 simple_interest.py create mode 100644 sorted_dict_by_key_or_value.py create mode 100644 stack_immplementation_in_a_list.py create mode 100644 stack_implementation_using_collections_of_deque.py create mode 100644 stack_using_queue_model.py create mode 100644 sum_of_average_of_a _list.py create mode 100644 sum_of_tuple_elements.py create mode 100644 swap_elements_in_string_list.py create mode 100644 tuple_into_set.py create mode 100644 updating_dictionary.py 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/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/all_negative_integers_of_a_range.py b/all_negative_integers_of_a_range.py new file mode 100644 index 0000000..e74ede1 --- /dev/null +++ b/all_negative_integers_of_a_range.py @@ -0,0 +1,12 @@ +# 23=7-22 +# print all negative integers of a range +start = int(input("enter the starting range: ")) +end = int(input("enter the ending range: ")) +# iterating the number +for num in range(start,end+1): + + # if checking condition + if num < 0: + print(num, end=" ") + + diff --git a/all_positive_integers_in_a_range.py b/all_positive_integers_in_a_range.py new file mode 100644 index 0000000..a42ebcf --- /dev/null +++ b/all_positive_integers_in_a_range.py @@ -0,0 +1,11 @@ +# 21-7-22 +# to print all positive integers in a range +# input string +start = int(input("enter a starting range: ")) +end = int(input("enter a ending range: ")) +# iterating each number +for num in range(start,end+1): + + # checking condition + if num >= 0: + print(num, end=" ") diff --git a/binary_numbers_using_queue.py b/binary_numbers_using_queue.py new file mode 100644 index 0000000..826b32b --- /dev/null +++ b/binary_numbers_using_queue.py @@ -0,0 +1,43 @@ +# 23-8-22 +# binary numbers using queue + +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) + + def front(self): + return self.buffer[-1] + +def produce_binary_numbers(n): + numbers_queue = Queue() + numbers_queue.enqueue("1") + + for i in range(n): + front = numbers_queue.front() + print(" ", front) + numbers_queue.enqueue(front + "0") + numbers_queue.enqueue(front + "1") + + numbers_queue.dequeue() + + +if __name__ == '__main__': + produce_binary_numbers(10) diff --git a/check_if_two_arrays_are_equal_or_not.py b/check_if_two_arrays_are_equal_or_not.py new file mode 100644 index 0000000..3a3c16e --- /dev/null +++ b/check_if_two_arrays_are_equal_or_not.py @@ -0,0 +1,31 @@ +# 5-8-22 +# check if two array are equal or not +def areequal(arr1, arr2, j, s): + + if(j !=s): + return False + + # sort both array + arr1.sort() + arr2.sort() + + # linearly compare + for i in range(0,j): + if(arr1[i] != arr2[i]): + return False + + # If all elements are same + return True + +# Driver Code +if __name__ == "__main__": + arr1 = [3, 5, 2, 5, 2] + arr2 = [2, 3, 5, 5, 2] + j = len(arr1) + s = len(arr2) + + if (areequal(arr1, arr2, j, s)): + print("Yes") + else: + print("No") + 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/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/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/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/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/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/find_largest_number.py b/find_largest_number.py new file mode 100644 index 0000000..b163aed --- /dev/null +++ b/find_largest_number.py @@ -0,0 +1,12 @@ +#29-6-22 +# Python program to find largest +# number in a list + +# list of numbers +list1 = [10, 20, 30, 40, 10000] + +# sorting the list +list1.sort() + +# printing the last element +print("Largest element is:", list1[-1]) diff --git a/find_the_length_of_string_using_sum.py b/find_the_length_of_string_using_sum.py new file mode 100644 index 0000000..4b52781 --- /dev/null +++ b/find_the_length_of_string_using_sum.py @@ -0,0 +1,8 @@ +#18-7-22 +# find the length of a string using sum +def findlen(string): + return sum(10 for i in string) + +#Drivercode +string='Believe it done(just do it)' +print(findlen(string)) diff --git a/find_the_size_of_a_set.py b/find_the_size_of_a_set.py new file mode 100644 index 0000000..5f058b6 --- /dev/null +++ b/find_the_size_of_a_set.py @@ -0,0 +1,12 @@ +#1-7-22 +#import sys + +# sample Sets +Set1 = {"A", 1, "B", 2, "C", 3} +Set2 = {"Jalpa", "Jay", "Bandana", "Dharmesh", "Rushita", "Mayur"} +Set3 = {(1, "Lion"), ( 2, "Tiger"), (3, "Fox")} + +# print the sizes of sample Sets +print("Size of Set1: " + str(Set1.__sizeof__()) + "bytes") +print("Size of Set2: " + str(Set2.__sizeof__()) + "bytes") +print("Size of Set3: " + str(Set3.__sizeof__()) + "bytes") \ No newline at end of file diff --git a/find_the_size_of_tuple.py b/find_the_size_of_tuple.py new file mode 100644 index 0000000..038a7af --- /dev/null +++ b/find_the_size_of_tuple.py @@ -0,0 +1,8 @@ +Tuple1 = ("A", 1, "B", 2, "C", 3) +Tuple2 = ("Jay", "Jalpa", "Rushita", "Yamini", "Suman", "Bandana") +Tuple3 = ((1, "Lion"), (2, "Tiger"), (3, "Fox"), (4, "Wolf")) + +# print the sizes of sample Tuples +print("Size of Tuple1: " + str(Tuple1.__sizeof__()) + "bytes") +print("Size of Tuple2: " + str(Tuple2.__sizeof__()) + "bytes") +print("Size of Tuple3: " + str(Tuple3.__sizeof__()) + "bytes") \ No newline at end of file diff --git a/find_the_smallest_number.py b/find_the_smallest_number.py new file mode 100644 index 0000000..c78d770 --- /dev/null +++ b/find_the_smallest_number.py @@ -0,0 +1,12 @@ +#27-8-22 +# Python program to find smallest +# number in a list + +# list of numbers +list1 = [10, 20, 40, 45, 99] + +# sorting the list +list1.sort() + +# printing the first element +print("Smallest element is:", *list1[:1]) diff --git a/find_the_sum_of_all_items_dict.py b/find_the_sum_of_all_items_dict.py new file mode 100644 index 0000000..b373ca8 --- /dev/null +++ b/find_the_sum_of_all_items_dict.py @@ -0,0 +1,18 @@ +#12-7-22 +# find the sum of all items in dict + +# Function to print sum + + +def returnSum(dict): + + sum = 0 + for i in dict.values(): + sum = sum + i + + return sum + + +# Driver Function +dict = {'j': 500, 'd': 400, 'b': 300} +print("Sum :", returnSum(dict)) diff --git a/finding_length_of_list.py b/finding_length_of_list.py new file mode 100644 index 0000000..d4483b7 --- /dev/null +++ b/finding_length_of_list.py @@ -0,0 +1,20 @@ +# python code demoscrate +#length of a list +# using a naive method + +#initializing list +list=[1,4,5,7,8] + +#printing the list +print("The list is : " + str(list)) + +# Finding length of list +# using loop +# Initializing counter +counter = 0 +for i in list: + # incrementing counter + counter = counter + 1 + +# Printing length of list +print("Length of list using naive method is : " + str(counter)) \ No newline at end of file diff --git a/first_repeating_element_array_integers.py b/first_repeating_element_array_integers.py new file mode 100644 index 0000000..4cc094e --- /dev/null +++ b/first_repeating_element_array_integers.py @@ -0,0 +1,60 @@ +# 6-8-22 +# find the repeating element array integers +# This function prints the +# first repeating element in arr[] +def printFirstRepeating(arr, n): + # This will set k=1, if any + # repeating element found + k = 0 + + # max = maximum from (all elements & n) + max = n + + for i in range(n): + if (max < arr[i]): + max = arr[i] + + # Array a is for storing + # 1st time occurrence of element + # initialized by 0 + a = [0 for i in range(max + 1)] + + # Store 1 in array b + # if element is duplicate + # initialized by 0 + b = [0 for i in range(max + 1)] + + for i in range(n): + + # Duplicate element found + if (a[arr[i]]): + b[arr[i]] = 1 + k = 1 + continue + else: + + # Storing 1st occurrence of arr[i] + a[arr[i]] = i + 1 + + if (k == 0): + print("No repeating element found") + else: + min = max + 1 + + for i in range(max + 1): + + # Trace array a & find repeating + # element with min index + if (a[i] and (min > (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/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/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/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/max_and_min.py b/max_and_min.py new file mode 100644 index 0000000..9759a9c --- /dev/null +++ b/max_and_min.py @@ -0,0 +1,25 @@ +#11-7-22 +# max() +def MAX(sets): + return (max(sets)) + + +# Driver Code +sets = set([10,20,30,40,50,60,70,80,90,100]) +print(MAX(sets)) + + +# min() +def MIN(sets): + return (min(sets)) + + +# Driver Code +sets = set([10,15,20,25,30,35,40,45,50,55,60]) +print(MIN(sets)) + + + + + + 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/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/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/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/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_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/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/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/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/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/sorted_dict_by_key_or_value.py b/sorted_dict_by_key_or_value.py new file mode 100644 index 0000000..a2b6a23 --- /dev/null +++ b/sorted_dict_by_key_or_value.py @@ -0,0 +1,9 @@ +#17-7-22 +#sorted dict by key or value +# Creates a sorted dictionary (sorted by key) +from collections import OrderedDict + +dict = {'Jalpa': '10', 'Dharmesh': '9', + 'Sarthi': '15', 'Bandana': '2', 'Jay': '32'} +dict1 = OrderedDict(sorted(dict.items())) +print(dict1) 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/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_tuple_elements.py b/sum_of_tuple_elements.py new file mode 100644 index 0000000..d1c98cc --- /dev/null +++ b/sum_of_tuple_elements.py @@ -0,0 +1,17 @@ +#30-6-22 +# Python3 code to demonstrate working of +# Tuple summation +# Using list() + sum() + +# initializing tup +tuples = (10, 11, 12, 13, 14, 15) + +# printing original tuple +print("The original tuple is : " + str(tuples)) + +# Tuple elements inversions +# Using list() + sum() +res = sum(list(tuples)) + +# printing result +print("The summation of tuple elements are : " + str(res)) diff --git a/swap_elements_in_string_list.py b/swap_elements_in_string_list.py new file mode 100644 index 0000000..b12d7f8 --- /dev/null +++ b/swap_elements_in_string_list.py @@ -0,0 +1,16 @@ +# Python3 code to demonstrate +# Swap elements in String list +# using replace() + list comprehension + +# Initializing list +test_list = ['I', 'love', 'python', 'programming', 'languages'] + +# printing original lists +print("The original list is : " + str(test_list)) + +# Swap elements in String list +# using replace() + list comprehension +res = [sub.replace('G', '-').replace('e', 'G').replace('-', 'e') for sub in test_list] + +# printing result +print ("List after performing character swaps : " + str(res)) 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/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