From 107684f8091098081c8b6c58d23df426d7c8c80c Mon Sep 17 00:00:00 2001 From: aj Date: Tue, 7 Jul 2020 19:59:34 +0100 Subject: [PATCH] adding as-is from uni onedrive --- Exercise1.py | 12 ++++++++++++ Exercise1Try1.py | 35 +++++++++++++++++++++++++++++++++++ README.md | 4 ++++ booleaninto.py | 9 +++++++++ date.py | 6 ++++++ dictionaryintro.py | 26 ++++++++++++++++++++++++++ exercise3.py | 31 +++++++++++++++++++++++++++++++ exercise32.py | 41 +++++++++++++++++++++++++++++++++++++++++ exercise_1.py | 11 +++++++++++ exercise_2.py | 26 ++++++++++++++++++++++++++ exercise_3.py | 24 ++++++++++++++++++++++++ for.py | 2 ++ functionintro.py | 9 +++++++++ helloworld.py | 1 + inputintro.py | 9 +++++++++ listappend.py | 11 +++++++++++ listintro.py | 9 +++++++++ loopsinto.py | 25 +++++++++++++++++++++++++ matrixinto.py | 21 +++++++++++++++++++++ numberinputexercise.py | 16 ++++++++++++++++ sort.py | 2 ++ variableintro.py | 4 ++++ 22 files changed, 334 insertions(+) create mode 100644 Exercise1.py create mode 100644 Exercise1Try1.py create mode 100644 README.md create mode 100644 booleaninto.py create mode 100644 date.py create mode 100644 dictionaryintro.py create mode 100644 exercise3.py create mode 100644 exercise32.py create mode 100644 exercise_1.py create mode 100644 exercise_2.py create mode 100644 exercise_3.py create mode 100644 for.py create mode 100644 functionintro.py create mode 100644 helloworld.py create mode 100644 inputintro.py create mode 100644 listappend.py create mode 100644 listintro.py create mode 100644 loopsinto.py create mode 100644 matrixinto.py create mode 100644 numberinputexercise.py create mode 100644 sort.py create mode 100644 variableintro.py diff --git a/Exercise1.py b/Exercise1.py new file mode 100644 index 0000000..5725746 --- /dev/null +++ b/Exercise1.py @@ -0,0 +1,12 @@ +unsorted_array = [6,2,9,0,2,5,3] +sorted_array = [] + +while unsorted_array: + minimum_value = unsorted_array[0] + for x in unsorted_array: + if x < minimum_value: + minimum_value = x + sorted_array.append(minimum_value) + unsorted_array.remove(minimum_value) + print sorted_array[len(sorted_array)-1] +#print sorted_array \ No newline at end of file diff --git a/Exercise1Try1.py b/Exercise1Try1.py new file mode 100644 index 0000000..093bae1 --- /dev/null +++ b/Exercise1Try1.py @@ -0,0 +1,35 @@ +numbers = [6,2,9,0,2,5,3] #list of input numbers +print numbers #test +#sorted = [numbers[0]] +sorted = [] #list of sorted numbers +#print sorted #test +counter = 1 #counter to be used as reference for input numbers list +sorted_counter = counter-1 #coutner to be used as reference for positioning in sorted list +#print sorted_counter #test +#print len(numbers) #test +#while counter <= len(numbers): +# if numbers[counter] < sorted[sorted_counter]: +# sorted.insert(sorted_counter, numbers[counter]) +# counter += 1 +# print sorted +# print counter +# else: +# counter += 1 + +for x in numbers: + print x + if counter == 1: + sorted.append(x) + counter += 1 + else: + for y in sorted: + if x < y: + print "yes" + break +# if numbers[x] < sorted[sorted_counter]: +# sorted.insert(sorted_counter, numbers[counter]) +# counter += 1 +# print sorted +# print counter +# else: +# counter += 1 \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..7160195 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +Python Labs +============== + +Lab files and exercises for first year python module. \ No newline at end of file diff --git a/booleaninto.py b/booleaninto.py new file mode 100644 index 0000000..79aa0ef --- /dev/null +++ b/booleaninto.py @@ -0,0 +1,9 @@ +x = 2 +print x == 2 #true +print x is 2 # true +print x == 3 #false + +str1 = "hello" +str2 = str1 +print str1 is str2 #true +print str1 == str2 #true \ No newline at end of file diff --git a/date.py b/date.py new file mode 100644 index 0000000..1e0f9eb --- /dev/null +++ b/date.py @@ -0,0 +1,6 @@ +from datetime import date + +d0 = date(2008, 8, 18) +d1 = date(2008, 9, 26) +delta = d0 - d1 +print delta.days \ No newline at end of file diff --git a/dictionaryintro.py b/dictionaryintro.py new file mode 100644 index 0000000..a8a70c9 --- /dev/null +++ b/dictionaryintro.py @@ -0,0 +1,26 @@ +phonebook = {} #empty dictionary, {} for dictionary [] for list +phonebook["Jack"] = "1" +phonebook["Nikos"] = "2" +phonebook ["Fiona"] = "3" + +print phonebook["Jack"] #prints phone number of jack + +phonebookother = { #other way to open dictionary" +"jack": "1", +"nikos": "2", +"fiona" : "3" +} + +phonebook["John"] = "4" #adds element + +for key, value in phonebook.iteritems(): + print key + "'s phone number is " + str(value) + +phonebook["Nikos"] = "7" #change number + +print phonebook["Nikos"] #reprint + +del phonebook["Fiona"] #deletes number + +for key, value in phonebook.iteritems(): + print key + "'s phone number is " + str(value) diff --git a/exercise3.py b/exercise3.py new file mode 100644 index 0000000..8bdeb69 --- /dev/null +++ b/exercise3.py @@ -0,0 +1,31 @@ +try: + day_1 = eval(raw_input("Enter Day 1: ")) +except NameError: + print "fucked it" + pass +try: + month_1 = eval(raw_input("Enter Month 1: ")) +except NameError: + print "fucked it" + pass +try: + year_1 = eval(raw_input("Enter Year 1: ")) +except NameError: + print "fucked it" + pass +try: + day_2 = eval(raw_input("Enter Day 2: ")) +except NameError: + print "fucked it" + pass +try: + month_2 = eval(raw_input("Enter Month 2: ")) +except NameError: + print "fucked it" + pass +try: + year_2 = eval(raw_input("Enter Year 2: ")) +except NameError: + print "fucked it" + pass +print day_1 \ No newline at end of file diff --git a/exercise32.py b/exercise32.py new file mode 100644 index 0000000..71c515c --- /dev/null +++ b/exercise32.py @@ -0,0 +1,41 @@ +day_1 = eval(raw_input("Enter Day 1: ")) +month_1 = eval(raw_input("Enter Month 1: ")) +year_1 = eval(raw_input("Enter Year 1: ")) +day_2 = eval(raw_input("Enter Day 2: ")) +month_2 = eval(raw_input("Enter Month 2: ")) +year_2 = eval(raw_input("Enter Year 2: ")) + +month_length = [0, 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + +month_1_math = month_1 +day_sum = 0 +while month_1_math != 0: + day_sum += month_length[month_1_math] + month_1_math -= 1 + +month_2_math = 11 - month_2 +day_sum_2 = 0 +while month_2_math != 0: + day_sum_2 += month_length[month_2_math] + month_2_math -= 1 + +day_2_difference = month_length[month_2] - day_2 + +year_dif = ((year_1 - year_2 - 1)*365) + +if year_dif < 0: + year_dif = 0 + +total = year_dif + day_sum + day_1 + day_sum_2 + day_2_difference +print total +#delta_day = day_2 - day_1 +#delta_month = month_2 - month_1 +#delta_year = year_2 - year_1 - 1 + +# +#if day_2 < day_1: +# delta_month -= 1 +# +#if month_2 < month_2: +# delta_year -= 1 + diff --git a/exercise_1.py b/exercise_1.py new file mode 100644 index 0000000..3ce8720 --- /dev/null +++ b/exercise_1.py @@ -0,0 +1,11 @@ +unsorted_array = [6,2,9,0,2,5,3] #array of values given by exercise +sorted_array = [] #empty array that will be populated by sorted values + +while unsorted_array: #open while loop, active while the unsorted array still has values in it + minimum_value = unsorted_array[0] #takes first value in array and sets it as "minimum value" + for x in unsorted_array: + if x < minimum_value: #for loops check "minimum value" against each value in array + minimum_value = x #and sets it as new "minimum value" if a value is found that is smaller + sorted_array.append(minimum_value) #adds smallest value from unsorted array to the first value of the sorted array + print minimum_value #prints smallest value + unsorted_array.remove(minimum_value)#removes smallest value from unsorted array so it can not be picked and added again \ No newline at end of file diff --git a/exercise_2.py b/exercise_2.py new file mode 100644 index 0000000..275ae2d --- /dev/null +++ b/exercise_2.py @@ -0,0 +1,26 @@ +rows = eval(raw_input("Enter Row Number: ")) #user defines number of columns +columns = eval(raw_input("Enter Column Number: ")) #user defines number of columns + +matrix = [] #define empty matrix + +for i in range(0, rows): #populates matrix with user elements + newrow = [] + for j in range(0, columns): #defines a row of elements added by user and appends it to matrix + num = int(raw_input("Enter Matrix Element: ")) + newrow.append(num) + matrix.append(newrow) + +for i in matrix: #print first matrix + print i + +transposed_matrix = [] #defines new matrix which will become transpose of previous matrix + +for i in range(0, columns): #same function as before to populate new matrix, defining number of rows as + newrow = [] #previous matrix had columns and vise versa + for j in range(0, rows): + num = matrix[j][i] #takes each element from previous matrix rather than user input as before + newrow.append(num) + transposed_matrix.append(newrow) + +for i in transposed_matrix: + print i #print transpose matrix \ No newline at end of file diff --git a/exercise_3.py b/exercise_3.py new file mode 100644 index 0000000..65afa64 --- /dev/null +++ b/exercise_3.py @@ -0,0 +1,24 @@ +print "Function takes Date 1 and subtracts Date 2, enter string of integers separated by spaces" + +date_1 = raw_input("Enter date 1 in format dd mm yyyy: ") #takes dates as string of integers +date_2 = raw_input("Enter date 2 in format dd mm yyyy: ") + +date_1 = [int(i) for i in date_1.split() if i.isdigit()] #creates array of integers extracted from string input +date_2 = [int(j) for j in date_2.split() if j.isdigit()] + +month_length = [0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365] +#month_length is array of months denoted by cumulative days up to month before month entered hence 0,0 at the start +#ie entering 1 as january will return 0 as no complete months have passed +#entering 3 for march will return 59 representing january's and february's complete days + +def date_to_days(day, month, year): #function defined to take date and turn it into days + year *= 365 #takes total years inputed and multiplies it by 365 + month = month_length[month] #returns days in month from array previously explained + return day + month + year #returns total days as sum of previous calculations + +date_1_days = date_to_days(date_1[0], date_1[1], date_1[2]) #calls function for first date +date_2_days = date_to_days(date_2[0], date_2[1], date_2[2]) #calls function for second date + +#print date_1_days #prints everything +#print date_2_days +print abs(date_1_days-date_2_days) #takes absolute value so days is always positive \ No newline at end of file diff --git a/for.py b/for.py new file mode 100644 index 0000000..43ed80e --- /dev/null +++ b/for.py @@ -0,0 +1,2 @@ +for x in range(4, 7): + print "We're on time %d" % (x) \ No newline at end of file diff --git a/functionintro.py b/functionintro.py new file mode 100644 index 0000000..5ec1833 --- /dev/null +++ b/functionintro.py @@ -0,0 +1,9 @@ +def my_print(name): + print "hello " + name + "!." + +my_print("Andy") + +def my_sum(x,y): + return x+y + +print my_sum(4,5) \ No newline at end of file diff --git a/helloworld.py b/helloworld.py new file mode 100644 index 0000000..b867bc4 --- /dev/null +++ b/helloworld.py @@ -0,0 +1 @@ +print "Hello World" #comment \ No newline at end of file diff --git a/inputintro.py b/inputintro.py new file mode 100644 index 0000000..383fe3a --- /dev/null +++ b/inputintro.py @@ -0,0 +1,9 @@ +mystr = raw_input("enter a string: ") +print mystr +myint = eval(raw_input("enter a number: ")) +print str(myint) +mymatrix = raw_input("enter characters separated by commas: ") +print mymatrix.split(",") #split a string into a list of elements where the delimiter is , + +for x in mymatrix: + print x \ No newline at end of file diff --git a/listappend.py b/listappend.py new file mode 100644 index 0000000..54d2a65 --- /dev/null +++ b/listappend.py @@ -0,0 +1,11 @@ +lst = [] # empty list +lst.append(1) +lst.append(2) +lst.append(3) +print(lst[0]) +print(lst[2]) + +for x in lst: + print x + +print lst \ No newline at end of file diff --git a/listintro.py b/listintro.py new file mode 100644 index 0000000..dda93c3 --- /dev/null +++ b/listintro.py @@ -0,0 +1,9 @@ +odd_numbers_lst = [1,3,5,7] +even_number_lst = [2,4,6,8] +print odd_numbers_lst + even_number_lst # to concatenate two lists together + +mixed_lst = [1, "lab1"] +print mixed_lst + +lst = [1,2,3,4] +print lst*3 \ No newline at end of file diff --git a/loopsinto.py b/loopsinto.py new file mode 100644 index 0000000..b382eb5 --- /dev/null +++ b/loopsinto.py @@ -0,0 +1,25 @@ +even_number = [2,4,6,8] #loop and print list +for num in even_number: + print num + +numbers = [1,2,3,4,5,6,7,8,9] +for num in numbers: + #check if number is even + if num %2 == 0: + print num + +counter = 1 +while counter < 10: + print counter + counter += 1 + +for x in range(10): + print(x) + if x==5: + break + +import string +for letter in string.ascii_lowercase: + if letter == "a": + continue #print all letters except 'a' + print letter \ No newline at end of file diff --git a/matrixinto.py b/matrixinto.py new file mode 100644 index 0000000..a52da5f --- /dev/null +++ b/matrixinto.py @@ -0,0 +1,21 @@ +mymatrix = [[1,2,3], [4,5,6], [7,8,9]] +#to iterate through rows +for i in range(len(mymatrix)): + print i + print(mymatrix[i]) + +#ask user for the number of columns and rows, then creates the matrix +rows = int(raw_input("enter number of rows: ")) +columns = int(raw_input("enter number of columns: ")) +#create matrix +mymatrix = [] +for i in range(0, rows): + myrow = [] #row has to be initialized ever iteration + for j in range(0, columns): + num = int(raw_input("enter a number: ")) + myrow.append(num) + mymatrix.append(myrow) + +for i in range(len(mymatrix)): + print i + print(mymatrix[i]) \ No newline at end of file diff --git a/numberinputexercise.py b/numberinputexercise.py new file mode 100644 index 0000000..ccbbf00 --- /dev/null +++ b/numberinputexercise.py @@ -0,0 +1,16 @@ +num_list = [] +while (True):dicks + num = eval(raw_input("Enter a Number: ")) + if num == 0: + break + num_list.append(num) + +print num_list +print sum(num_list) +print max(num_list) +print min(num_list) + +product = 1 +for numu in num_list: + product *= numu +print product \ No newline at end of file diff --git a/sort.py b/sort.py new file mode 100644 index 0000000..01c0986 --- /dev/null +++ b/sort.py @@ -0,0 +1,2 @@ +list = ["aaaa", "ffff", "adadad", "cccc", 5, 2, 4] +print list.sort() \ No newline at end of file diff --git a/variableintro.py b/variableintro.py new file mode 100644 index 0000000..ea4bdce --- /dev/null +++ b/variableintro.py @@ -0,0 +1,4 @@ +num = 2 +strn = "hello" +#print str + num +print strn + str(num) \ No newline at end of file