diff --git a/Ch2 - Basics/classes_start.py b/Ch2 - Basics/classes_start.py index de3226d..3b20492 100644 --- a/Ch2 - Basics/classes_start.py +++ b/Ch2 - Basics/classes_start.py @@ -3,3 +3,47 @@ # LinkedIn Learning Python course by Joe Marini # +class Vehicle(): + def __init__(self, bodystyle): + self.bodystyle = bodystyle + + def drive(self, speed): + self.mode = "driving" + self.speed = speed + +class Car(Vehicle): + def __init__(self, enginetype): + super().__init__("Car") + self.wheels = 4 + self.doors = 4 + self.enginetype = enginetype + + def drive(self, speed): + super().drive(speed) + print("Driving my", self.enginetype, "car at", self.speed) + +class Motorcycle(Vehicle): + def __init__(self, enginetype, hassidecar): + super().__init__("Motorcycle") + if(hassidecar): + self.wheels = 3 + else: + self.wheels = 2 + self.doors = 0 + self.enginetype = enginetype + + def drive(self, speed): + super().drive(speed) + print("Driving my", self.enginetype, "motorcycle at", self.speed) + +car1 = Car("Gas") +car2 = Car("Electric") +mc1 = Motorcycle("Gas", True) + +print(mc1.wheels) +print(car1.enginetype) +print(car2.doors) + +car1.drive(30) +car2.drive(40) +mc1.drive(50) \ No newline at end of file diff --git a/Ch2 - Basics/conditionals_start.py b/Ch2 - Basics/conditionals_start.py index f6b58d6..1346e53 100644 --- a/Ch2 - Basics/conditionals_start.py +++ b/Ch2 - Basics/conditionals_start.py @@ -9,11 +9,32 @@ def main(): x, y = 10, 100 # conditional flow uses if, elif, else + # if x < y: + # result = "x is less than y" + # elif x==y: + # result = "x is the same as y" + # else: + # result = "x is greater than y" + + #print(result) # conditional statements let you use "a if C else b" + #result = "x is less than y" if x < y else "x is greater than or equal to y" + #print(result) # match-case makes it easy to compare multiple values - value = "one" + value = "42" + match value: + case "one": + result = 1 + case "two": + result = 2 + case "three" | "four": + result = (3,4) + case _: + result = -1 + + print(result) if __name__ == "__main__": main() diff --git a/Ch2 - Basics/exceptions_start.py b/Ch2 - Basics/exceptions_start.py index a1209c4..1baf64b 100644 --- a/Ch2 - Basics/exceptions_start.py +++ b/Ch2 - Basics/exceptions_start.py @@ -5,10 +5,25 @@ # Errors can happen in programs, and we need a clean way to handle them # TODO: This code will cause an error because you can't divide by zero: +#x = 10 / 0 + # TODO: Exceptions provide a way of catching errors and then handling them in # a separate section of the code to group them together - +# try: +# x = 10 / 0 +# except: +# print("Well that didn't work!") # TODO: You can also catch specific exceptions - +try: + answer = input("What should I divide 10 by?") + num = int(answer) + print(10/num) +except ZeroDivisionError as e: + print("You can't divide by zero!") +except ValueError as e: + print("You didn't give me a valid number!") + print(e) +finally: + print("This code always runs") diff --git a/Ch2 - Basics/functions_start.py b/Ch2 - Basics/functions_start.py index 56cb247..622920d 100644 --- a/Ch2 - Basics/functions_start.py +++ b/Ch2 - Basics/functions_start.py @@ -5,17 +5,43 @@ # TODO: define a basic function - +def func1(): + print("I am a function") # TODO: function that takes arguments - +def func2(arg1, arg2): + print(arg1," ", arg2) # TODO: function that returns a value - +def cube(x): + return x * x * x # TODO: function with default value for an argument - +def power(num, x=1): + result = 1; + for i in range(x): + result = result * num + return result # TODO: function with variable number of arguments +def multi_add(*args): + result = 0 + for x in args: + result = result + x + return result + +# func1() +# print(func1()) +# print(func1) + +#func2(10, 20) +#print(func2(10, 20)) +#print(cube(3)) + +#print(power(2)) +#print(power(2,3)) +#print(power(x=3, num=2)) +print(multi_add(4,5,10,4)) +print(multi_add(4,5,10,4,10)) \ No newline at end of file diff --git a/Ch2 - Basics/helloworld_start.py b/Ch2 - Basics/helloworld_start.py index 7d6b753..34c28a2 100644 --- a/Ch2 - Basics/helloworld_start.py +++ b/Ch2 - Basics/helloworld_start.py @@ -3,4 +3,10 @@ # LinkedIn Learning Python course by Joe Marini # +def main(): + print("Hello World!") + name = input("What is your name?") + print("Nice to meet you!", name) +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/Ch2 - Basics/loops_start.py b/Ch2 - Basics/loops_start.py index f7d2e75..8415ff1 100644 --- a/Ch2 - Basics/loops_start.py +++ b/Ch2 - Basics/loops_start.py @@ -8,20 +8,31 @@ def main(): x = 0 # TODO: define a while loop - + # while(x < 5): + # print(x) + # x = x + 1 # TODO: define a for loop - + #for x in range(5, 10): + # print(x) # TODO: use a for loop over a collection days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] - + #for d in days: + # print(d) # TODO: use the break and continue statements - + # for x in range(5, 10): + # #if x ==7: + # # break + # if x % 2 == 0: + # continue + # print(x) # TODO: using the enumerate() function to get index - + days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] + for i,d in enumerate(days): + print(i,d) if __name__ == "__main__": main() diff --git a/Ch2 - Basics/modules_start.py b/Ch2 - Basics/modules_start.py index 8c8bf6c..0c48575 100644 --- a/Ch2 - Basics/modules_start.py +++ b/Ch2 - Basics/modules_start.py @@ -3,12 +3,12 @@ # TODO: import the math module, which contains features for working with mathematics - +import math # TODO: the math module contains lots of pre-built functions - +print("The square root of 16 is", math.sqrt(16)) # TODO: in addition to functions, some modules contain useful constants +print("Pi is", math.pi) - -# TODO: try some of the math functions for yourself here: +# TODO: try some of the math functions for yourself here \ No newline at end of file diff --git a/Ch2 - Basics/variables_start.py b/Ch2 - Basics/variables_start.py index b2756cc..f8d6cd5 100644 --- a/Ch2 - Basics/variables_start.py +++ b/Ch2 - Basics/variables_start.py @@ -13,25 +13,43 @@ mytuple = (0, 1, 2) mydict = {"one" : 1, "two" : 2} -print(myint) -print(myfloat) -print(mystr) -print(mybool) -print(mylist) -print(mytuple) -print(mydict) +#print(myint) +#print(myfloat) +#print(mystr) +#print(mybool) +#print(mylist) +#print(mytuple) +#print(mydict) # re-declaring a variable works +# myint = "abc" +# print(myint) # to access a member of a sequence type, use [] +# print(mylist[2]) +# print(mytuple[1]) # use slices to get parts of a sequence +# print(mylist[1:5]) +# print(mylist[1:5:2]) # you can use slices to reverse a sequence +#print(mylist[::-1]) # dictionaries are accessed via keys +#print(mydict["one"]) # ERROR: variables of different types cannot be combined +#print("string type " + str(123)) # Global vs. local variables in functions +def someFunction(): + global mystr + mystr = "def" + print(mystr) + +someFunction() +print(mystr) +del mystr +print(mystr) \ No newline at end of file diff --git a/Ch3 - Files/files_start.py b/Ch3 - Files/files_start.py index fb026bb..abd8095 100644 --- a/Ch3 - Files/files_start.py +++ b/Ch3 - Files/files_start.py @@ -6,19 +6,27 @@ def main(): # Open a file for writing and create it if it doesn't exist + #myfile = open("textfile.txt", "w+") # Open the file for appending text to the end + # myfile = open("textfile.txt", "a+") - - # write some lines of data to the file - + # # write some lines of data to the file + # for i in range(10): + # myfile.write("This is some new text\n") - # close the file when done - + # # close the file when done + # myfile.close # Open the file back up and read the contents - + myfile = open("textfile.txt", "r") + if myfile.mode == 'r': + # contents = myfile.read() + # print(contents) + fl = myfile.readlines() + for x in fl: + print(x) if __name__ == "__main__": main() diff --git a/Ch3 - Files/ospathutils_start.py b/Ch3 - Files/ospathutils_start.py index 3384cbd..b7c52b5 100644 --- a/Ch3 - Files/ospathutils_start.py +++ b/Ch3 - Files/ospathutils_start.py @@ -12,18 +12,27 @@ def main(): # Print the name of the OS - + # print(os.name) - # Check for item existence and type - + # # Check for item existence and type + # print("Item exists:", str(path.exists("textfile.txt"))) + # print("Item is a file:", path.isfile("textfile.txt")) + # print("Item is a directory:", path.isdir("textfile.txt")) - # Work with file paths + # # Work with file paths + # print("Item's path:", path.realpath("textfile.txt")) + # print("Item's path and name:", path.split(path.realpath("textfile.txt"))) # Get the modification time - + t = time.ctime(path.getmtime("textfile.txt")) + print(t) + print(datetime.datetime.fromtimestamp(path.getmtime("textfile.txt"))) # Calculate how long ago the item was modified + td = datetime.datetime.now() - datetime.datetime.fromtimestamp(path.getmtime("textfile.txt")) + print("It has been", td, "since the file was modified") + print("Or,", td.total_seconds(), "seconds") if __name__ == "__main__": diff --git a/Ch3 - Files/shell_start.py b/Ch3 - Files/shell_start.py index 5cb9ec5..dcb3eaf 100644 --- a/Ch3 - Files/shell_start.py +++ b/Ch3 - Files/shell_start.py @@ -5,19 +5,32 @@ import os from os import path +import shutil +from shutil import make_archive +from zipfile import ZipFile def main(): # make a duplicate of an existing file - if path.exists("textfile.txt"): + if path.exists("textfile.txt.bak"): # get the path to the file in the current directory - + src = path.realpath("textfile.txt") + # let's make a backup copy by appending "bak" to the name - + # dst = src + ".bak" + # shutil.copy(src, dst) + # rename the original file - + #os.rename("textfile.txt", "newfile.txt") + # now put things into a ZIP archive + #root_dir, tail = path.split(src) + #shutil.make_archive("archive", "zip", root_dir) # more fine-grained control over ZIP files + with ZipFile("testzip.zip", "w") as newzip: + newzip.write("newfile.txt") + newzip.write("textfile.txt.bak") + if __name__ == "__main__": diff --git a/archive.zip b/archive.zip new file mode 100644 index 0000000..be4d701 Binary files /dev/null and b/archive.zip differ diff --git a/newfile.txt b/newfile.txt new file mode 100644 index 0000000..0d5bc3a --- /dev/null +++ b/newfile.txt @@ -0,0 +1,20 @@ +This is some text +This is some text +This is some text +This is some text +This is some text +This is some text +This is some text +This is some text +This is some text +This is some text +This is some new text +This is some new text +This is some new text +This is some new text +This is some new text +This is some new text +This is some new text +This is some new text +This is some new text +This is some new text diff --git a/testzip.zip b/testzip.zip new file mode 100644 index 0000000..3d90322 Binary files /dev/null and b/testzip.zip differ diff --git a/textfile.txt.bak b/textfile.txt.bak new file mode 100644 index 0000000..0d5bc3a --- /dev/null +++ b/textfile.txt.bak @@ -0,0 +1,20 @@ +This is some text +This is some text +This is some text +This is some text +This is some text +This is some text +This is some text +This is some text +This is some text +This is some text +This is some new text +This is some new text +This is some new text +This is some new text +This is some new text +This is some new text +This is some new text +This is some new text +This is some new text +This is some new text