From 9ef8820008029df883d2bad1f4f16cf1fcf77ee1 Mon Sep 17 00:00:00 2001 From: Nani1979 <139854785+Nani1979@users.noreply.github.com> Date: Thu, 4 Dec 2025 11:18:20 +0000 Subject: [PATCH 1/2] Initial commit From b6848b23b1302999b0836e0641948afafddf14da Mon Sep 17 00:00:00 2001 From: Nani1979 <139854785+Nani1979@users.noreply.github.com> Date: Thu, 4 Dec 2025 11:18:21 +0000 Subject: [PATCH 2/2] Pending changes exported from your codespace --- Start/Ch2/conditionals_start.py | 9 ++++++- Start/Ch2/dict_start.py | 27 ++++++++++++++------- Start/Ch2/functions_start.py | 43 ++++++++++++++++++++++++--------- Start/Ch2/helloworld_start.py | 3 +++ Start/Ch2/loops_start.py | 23 +++++++++++++++--- Start/Ch2/sequence_start.py | 36 +++++++++++++++++---------- Start/Ch2/test.py | 0 Start/Ch2/variables_start.py | 21 ++++++++++++---- Start/Ch3/builtin_start.py | 19 +++++++++------ Start/Ch3/classes_start.py | 38 ++++++++++++++++++++++++++++- Start/Ch3/exceptions_start.py | 18 ++++++++++++-- Start/Ch3/modules_start.py | 14 ++++++----- 12 files changed, 192 insertions(+), 59 deletions(-) create mode 100644 Start/Ch2/test.py diff --git a/Start/Ch2/conditionals_start.py b/Start/Ch2/conditionals_start.py index fb3a7f8..9f2545c 100644 --- a/Start/Ch2/conditionals_start.py +++ b/Start/Ch2/conditionals_start.py @@ -5,5 +5,12 @@ x, y = 10, 100 # conditional flow uses if, elif, else - +if x < y : + print("x is less than y") +elif x > y: + print("x is greater than y") +else: + print("x is equal to y") # conditional statements let you use "a if C else b" +result = "x is less than y" if (x < y) else "x is greater or equal to y" +print(result) \ No newline at end of file diff --git a/Start/Ch2/dict_start.py b/Start/Ch2/dict_start.py index 553a525..5da8b77 100644 --- a/Start/Ch2/dict_start.py +++ b/Start/Ch2/dict_start.py @@ -3,21 +3,30 @@ # Dictionary: a key-value data structure - - +mydict = { + "one" : 1, + "two" : 2, + 3 : "three", + 4.5 : ["four" , "point" , "five"] +} +print(mydict) # dictionaries are accessed via keys - - +# print(mydict["one"]) +# print(mydict[3]) # you can also set dictionary data by creating a new key - +# mydict["seven"] = 7 +# print(mydict) # Trying to access a nonexistent key will produce an error - - +#print(mydict["b"]) +# print("two" in mydict) +# print("b" in mydict) # To avoid this, you can use the "in" operator to see if a key exists - # You can retrieve all of the keys and values from a dictionary - +print(mydict.keys()) +print(mydict.values()) # You can also iterate over all the items in a dictionary +for key, val in mydict.items(): + print(key, val) \ No newline at end of file diff --git a/Start/Ch2/functions_start.py b/Start/Ch2/functions_start.py index 98399ce..6d5b1d7 100644 --- a/Start/Ch2/functions_start.py +++ b/Start/Ch2/functions_start.py @@ -3,17 +3,38 @@ # define a basic function -print("hello world!") -name = input("What is your name? ") -print("Nice to meet you,", name) - +# def hello_func(): +# print("hello world!") +# name = input("What is your name? ") +# print("Nice to meet you,", name) +# hello_func() # function that takes parameters - - +# def hello_func(greeting): +# print("hello world!") +# name = input("What is your name? ") +# print(greeting, name) +# hello_func("how are you doing") +# hello_func("what's up") # function that returns a value - - +# def cube(x): +# return x*x*x +# result = cube(3) +# print(result) # function with default value for an parameter - - -# function with variable number of parameters +# def hello_func(greeting, name=None): +# print("hello world!") +# if name == None: +# name = input("What is your name? ") +# print(greeting, name) + +# hello_func("how are you doing") +# hello_func(name="nani" , greeting="nice to meet you") + +# function with variable nnumber of parameters +def multi_add(start, *args): + result = start + for x in args: + result = result + x + return result + +print(multi_add(10,4,5,10,4,10)) diff --git a/Start/Ch2/helloworld_start.py b/Start/Ch2/helloworld_start.py index 661e6e1..51ce03f 100644 --- a/Start/Ch2/helloworld_start.py +++ b/Start/Ch2/helloworld_start.py @@ -1,2 +1,5 @@ # LinkedIn Learning Python course by Joe Marini # Example file for HelloWorld +print("hello world") +name = input("what is your name? ") +print("nice to meet you ", name) diff --git a/Start/Ch2/loops_start.py b/Start/Ch2/loops_start.py index df65743..b78f0b6 100644 --- a/Start/Ch2/loops_start.py +++ b/Start/Ch2/loops_start.py @@ -5,15 +5,30 @@ x = 0 # define a while loop - +# while x < 5: +# print(x) +# x = x + 1 +#answer = input("should i stop?") +# while answer != "yes": +# print(answer) +# answer = input("should I stop?") # define a for loop - - +# days = ["mon", "tue", "wed", "thu", "fri","sat","sun"] +# for d in days: +# print(d) # use a for loop over a collection # use the break and continue statements - +days = ["mon", "tue", "wed", "thu", "fri","sat","sun"] +for d in days: + if (d == "thu"): + #break + continue + print(d) # using the enumerate() function to get an index and an item +days = ["mon", "tue", "wed", "thu", "fri","sat","sun"] +for i,d in enumerate(days): + print(i,d) \ No newline at end of file diff --git a/Start/Ch2/sequence_start.py b/Start/Ch2/sequence_start.py index 66e78c5..5c2d3dd 100644 --- a/Start/Ch2/sequence_start.py +++ b/Start/Ch2/sequence_start.py @@ -3,26 +3,36 @@ # Sequences: Lists and Tuples # These are -- surprise -- sequences of values - +mylist = [0,1,"two",3.2,False] +print(len(mylist)) # to access a member of a sequence type, use [] - - +# print(mylist[2]) +# print(mylist[-1]) +# mylist[0] = 10 +# print(mylist) # add a list to another list - - -# use slices to get parts of a sequence - - +# another_list = [6,7,8] +# mylist = mylist + another_list +# print(mylist) +# # use slices to get parts of a sequence +# mystr = "this is a string" +# print(mystr[2]) # you can use slices to reverse a sequence - +# print("my slice", mylist[1:4]) +# print("my slice", mylist[:4:2]) +# print("my slice", mylist[::-1]) # Tuples are like lists, but they are immutable - - +mytuple = (0,1,3,"three") +# print("my tuple", mytuple[::-1]) # Sets are also sequences, but they contain unique values - +myset = {1,2,3,3,4,"hey"} +print(myset) # Set, however, can not be indexed like lists or tuples -# print(myset[0]) # this will cause an error +#print(myset[0]) # this will cause an error # Test for membership +print(1 in mylist) +print(3 in mytuple) +print(5 in myset) \ No newline at end of file diff --git a/Start/Ch2/test.py b/Start/Ch2/test.py new file mode 100644 index 0000000..e69de29 diff --git a/Start/Ch2/variables_start.py b/Start/Ch2/variables_start.py index 258c710..fc88d2e 100644 --- a/Start/Ch2/variables_start.py +++ b/Start/Ch2/variables_start.py @@ -11,11 +11,22 @@ # We can display the content of a variable using the print() function - +print("my integer is ",myint) +print("my string is ",mystr) # Operators are used to perform operations on variables - - +# print("my int added to my flot",myint + myfloat) +# print("my int times my flot",myint * myfloat) +# print("my int divided by my flot",myint / myfloat) +# another_str = "this is another string" +# print("string added", mystr + another_str) +# print("nom " * 3) # Logical and comparison operators - - +# print(myint == 10) +# print(myfloat != 20) +# print(myint > 20) +# print(myfloat < 10) +# print(myint > 5 and myfloat <25.0) +# print(not(myint > 5 and myfloat <25.0)) # re-declaring a variable works +myint = "abc" +print("change int to string", myint) \ No newline at end of file diff --git a/Start/Ch3/builtin_start.py b/Start/Ch3/builtin_start.py index 33801b3..870feb7 100644 --- a/Start/Ch3/builtin_start.py +++ b/Start/Ch3/builtin_start.py @@ -6,20 +6,25 @@ mynumbers = [1,3,5,6,9,12,14,17,20,30] # the len() function calculates the length of a sequence - +# print(len(mystring)) +# print(len(mynumbers)) # the max() and min() functions will find the largest and smallest value in a sequence - - +# print(max(mystring)) +# print(min(mynumbers)) # the str() function will return a string version of an object -prefix = "result: " -result = 5 - +# prefix = "result: " +# result = 5 +# print(prefix, str(result)) # range(start, stop, step) will create a range of numbers # You can use ranges along with loops +# for i in range(5,15,3): +# print(i) - +# for i in range(5,len(mystring),2): +# print(mystring[i]) # the print function itself is pretty flexible - you can embed variables directly in it greeting = "Hello!" count = 10 +print(f"{greeting} you are visitor number {count}") \ No newline at end of file diff --git a/Start/Ch3/classes_start.py b/Start/Ch3/classes_start.py index 2091592..f4ea673 100644 --- a/Start/Ch3/classes_start.py +++ b/Start/Ch3/classes_start.py @@ -1,4 +1,40 @@ # LinkedIn Learning Python course by Joe Marini # Example file for working with classes -# +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.engine = enginetype + def drive(self, speed): + super().drive(speed) + print(f"driving my {self.engine} 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(f"driving my {self.enginetype} car at {self.speed}") + +car1 = Car("gas") +car2 = Car("elecrtric") +mc1 = Motorcycle("gas", True) +print(f"the MC has {mc1.wheels} wheels and engine type is {mc1.enginetype}") +print(f"the car1 has {car1.doors} doors and engine type is {car1.engine}") +car1.drive(20) +car2.drive(40) +mc1.drive(50) \ No newline at end of file diff --git a/Start/Ch3/exceptions_start.py b/Start/Ch3/exceptions_start.py index 12d5ff9..a8bef75 100644 --- a/Start/Ch3/exceptions_start.py +++ b/Start/Ch3/exceptions_start.py @@ -4,10 +4,24 @@ # Errors can happen in programs, and we need a clean way to handle them # This code will cause an error because you can't divide by zero: +#x = 10 / 0 # 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") # 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("divistion by zero") +except ValueError as e: + print("you didn't give me a valid number") + print(e) +finally: + print("finally always runs") \ No newline at end of file diff --git a/Start/Ch3/modules_start.py b/Start/Ch3/modules_start.py index f245d53..742537d 100644 --- a/Start/Ch3/modules_start.py +++ b/Start/Ch3/modules_start.py @@ -2,14 +2,15 @@ # Working with modules of code # import the math module, which contains features for working with mathematics - +import math +print("the square root of 16 is ", math.sqrt(16)) # import a specific part of the module so you can refer to it more easily - - +from math import pi +print("pi",pi) # import a module and give it a different name - - +import random as r +print("random", r.randint(110,400)) # the math module contains lots of pre-built functions @@ -22,7 +23,7 @@ # try some of the math functions for yourself here: # Use the 3rd party tabulate module to print tabulated data: - +from tabulate import tabulate # Sample data data = [ ["Product", "Price", "Stock"], @@ -32,3 +33,4 @@ ] # Create a formatted table +print(tabulate(data, headers="firstrow",tablefmt="fancy_grid")) \ No newline at end of file