diff --git a/Ch2 - Basics/conditionals_start.py b/Ch2 - Basics/conditionals_start.py index f6b58d6..2025bdf 100644 --- a/Ch2 - Basics/conditionals_start.py +++ b/Ch2 - Basics/conditionals_start.py @@ -10,10 +10,33 @@ def main(): # 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 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..0cce959 100644 --- a/Ch2 - Basics/exceptions_start.py +++ b/Ch2 - Basics/exceptions_start.py @@ -6,9 +6,28 @@ # 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: + # 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 din't work!!")) + + # TODO: You can also catch specific exceptions +try: + answer = input( " what shoul I ivie 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 don'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..1f0072f 100644 --- a/Ch2 - Basics/functions_start.py +++ b/Ch2 - Basics/functions_start.py @@ -5,17 +5,53 @@ # 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,10)) \ No newline at end of file diff --git a/Ch2 - Basics/helloworld_start.py b/Ch2 - Basics/helloworld_start.py index 7d6b753..e99363b 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 Wolrd!") + name = input ("What is your name?") + print("Nice to meet you" , name) +if __name__ == "_main_": + main() diff --git a/Ch2 - Basics/loops_start.py b/Ch2 - Basics/loops_start.py index f7d2e75..b28b1cf 100644 --- a/Ch2 - Basics/loops_start.py +++ b/Ch2 - Basics/loops_start.py @@ -8,20 +8,34 @@ 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"] - + # 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..4299d10 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 o 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: diff --git a/Ch2 - Basics/test.py b/Ch2 - Basics/test.py new file mode 100644 index 0000000..146741c --- /dev/null +++ b/Ch2 - Basics/test.py @@ -0,0 +1,9 @@ +thestr = "Ogres are often foolhardy oafs" +newstr = "" +for i, c in enumerate(thestr): + if c == "o": + continue + if i > 20: + break + newstr += c +print(newstr) \ No newline at end of file diff --git a/Ch2 - Basics/variables_start.py b/Ch2 - Basics/variables_start.py index b2756cc..a91b85c 100644 --- a/Ch2 - Basics/variables_start.py +++ b/Ch2 - Basics/variables_start.py @@ -13,25 +13,41 @@ 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 - -# you can use slices to reverse a sequence +# # 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("string type" + str (123)) # ERROR: variables of different types cannot be combined # Global vs. local variables in functions +def someFunction(): + global mystr + mystr = "def" + print (mystr) +someFunction () +print(mystr) + +del mystr +print(mystr)