From 90d17bcc4781b88e7ba70cb7a8ac351be1ad68b7 Mon Sep 17 00:00:00 2001 From: Daniel Bruno Date: Mon, 19 Apr 2021 17:46:30 -0400 Subject: [PATCH] added labs 2, 3 --- .vscode/launch.json | 15 ++++++++ Demos/Module2/variables_start.py | 49 +++++++++++++++++++++------ Demos/Module3/conditionals_start.py | 30 ++++++++++++++++ Demos/Module3/error_handling_start.py | 15 +++++++- Demos/Module3/functions_start.py | 21 ++++++++---- Labs/Lab02/simple_calculator.py | 10 ++++++ Labs/Lab03/simple_calculator.py | 36 ++++++++++++++++++++ 7 files changed, 159 insertions(+), 17 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 Labs/Lab02/simple_calculator.py create mode 100644 Labs/Lab03/simple_calculator.py diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..17e15f2 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/Demos/Module2/variables_start.py b/Demos/Module2/variables_start.py index e9c6759..006d077 100644 --- a/Demos/Module2/variables_start.py +++ b/Demos/Module2/variables_start.py @@ -2,23 +2,52 @@ # Demo file for variables # -# Declare a variable and initialize it +# # Declare a variable and initialize it +# person_name = 'John Smith' +# person_address = '101 Main Street' +# print(f'{person_name} lives at {person_address}') -# Inspect the data type +# # Inspect the data type +# print(type(person_name)) -# re-declaring the variable works +# # re-declaring the variable works +# person_name = 1 +# print(person_name) +# print(type(person_name)) +# print(f'{person_name} lives at {person_address}') +# print(str(person_name) + " lives at " + person_address) +# # String literals +# print('John says: "How are you?"') +# print('''John says: "That is Jenny's car".''') -# ERROR: variables of different types cannot be combined -#print("this is a string" + 123) +# # delete definition of variable previously declared +# del(person_name) +# print(person_name) -# But this works: variables of different types cannot be combined +# # data type casting +# item_price = '12.10' +# stock_price = 12.15 +# result = 25 / 5 +# type(result) +# print(result) -# Global vs. local variables in functions +# print(item_price * 2) -# delete definition of variable previously declared +# type(item_price) +# type(stock_price) -# data type casting +# greeting = 'Hello world' +# print(greeting.upper()) +# print(greeting.isnumeric()) +# print(len(greeting)) -# comparing float numbers +full_name = input('Please enter Name:') + +print(f'You entered: {full_name}') + +age = input('Please enter age:') +print(type(int(age))) + +print(f'{full_name} is {age} years old') diff --git a/Demos/Module3/conditionals_start.py b/Demos/Module3/conditionals_start.py index 1b2dda9..af771b5 100644 --- a/Demos/Module3/conditionals_start.py +++ b/Demos/Module3/conditionals_start.py @@ -6,10 +6,40 @@ def main(): x, y = 10, 100 # conditional flow uses if, elif, else + if( x < y): + st = 'x is less than y' + elif(x > y): + st = 'x is greater than y' + else: + st = 'x is the same as y' + + print(st) # conditional statements let you use "a if C else b" + st = 'x is less than y' if (x < y) else "x is greater or the same as y" + + print(st) # conditional logic with IN operator + city = "Raleigh" + if city in ('Raleigh', 'Charlotte', 'Asheville'): + print('You live in North Carolina') + else: + print('You live somewhere else.') + + if 1.1 + 2.2 == 3.3: + print('1.1 + 2.2 == 3.3') + else: + print('1.1 + 2.2 != 3.3') + + tolerance = 0.00001 + + if abs((1.1 + 2.2) - 3.3) < tolerance: + print('1.1 + 2.2 == 3.3') + else: + print('1.1 + 2.2 != 3.3') + + if __name__ == "__main__": main() \ No newline at end of file diff --git a/Demos/Module3/error_handling_start.py b/Demos/Module3/error_handling_start.py index fce6d20..136ba50 100644 --- a/Demos/Module3/error_handling_start.py +++ b/Demos/Module3/error_handling_start.py @@ -4,6 +4,19 @@ x = 42 -y = 0 +y = '2' # Division +try: + result = x / y +except ZeroDivisionError: + result = 0 + print('y cannot be zero') +except TypeError as e: + result = float(x) / float(y) + print('Automatically casted string to float') +except: + result = '0' + print('Something went wrong!') +finally: + print(f'Result: {result}') diff --git a/Demos/Module3/functions_start.py b/Demos/Module3/functions_start.py index 4775469..e28228e 100644 --- a/Demos/Module3/functions_start.py +++ b/Demos/Module3/functions_start.py @@ -2,14 +2,23 @@ # Example file for working with functions # -# define a basic function +# name = "John Doe" -# function with arguments +# # define a basic function +# def some_function(): +# global name +# name = 'James Smith' +# return f'Hello, {name}' -# function with return value +# greeting = some_function() -# function with default value for an argument +# print(name) +# print(greeting) -#function with variable number of arguments -# Lambda functions \ No newline at end of file +# Lambda functions +def addition(arg1, arg2): + return arg1 + arg2 + +print(addition) +print((lambda arg1, arg2: arg1 + arg2)) \ No newline at end of file diff --git a/Labs/Lab02/simple_calculator.py b/Labs/Lab02/simple_calculator.py new file mode 100644 index 0000000..7254670 --- /dev/null +++ b/Labs/Lab02/simple_calculator.py @@ -0,0 +1,10 @@ +# +# Lab 2 +# + +# calculate the circumference of a circle + +radius = float(input("Please enter radius:")) +pi = 3.1416 +circumference = 2 * pi * radius +print(f'The circumference is:{circumference}') diff --git a/Labs/Lab03/simple_calculator.py b/Labs/Lab03/simple_calculator.py new file mode 100644 index 0000000..ece85da --- /dev/null +++ b/Labs/Lab03/simple_calculator.py @@ -0,0 +1,36 @@ +def add(x, y): + return x + y +def sub(x, y): + return x - y +def mul(x, y): + return x * y +def div(x, y): + try: + result = x / y + except ZeroDivisionError: + result = 0 + print("Cannot divide by zero.") + finally: + return result + + +operation = input("Please enter operation:") +first_number = input("Please enter first number:") +second_number = input("Please enter second number:") + +first_number = float(first_number) +second_number = float(second_number) + +if operation.upper() == 'ADD': + result = add(first_number, second_number) +elif operation.upper() == 'SUB': + result = sub(first_number, second_number) +elif operation.upper() == 'MUL': + result = mul(first_number, second_number) +elif operation.upper() == 'DIV': + result = div(first_number, second_number) +else: + result = 0 + print(f'Unknown Operation: {operation}') + +print(f"Result: {result}") \ No newline at end of file