-
-
Save neptunius/ec6b020cfbadb723200deb2b7459c49b to your computer and use it in GitHub Desktop.
code snippets used for the first class of CS 1.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # CS-1.1 Variables, Function, and Program Design | |
| def greet_by_name(name): | |
| greeting = "Hello, " + name + "!" | |
| return greeting | |
| print(greet_by_name("Alan")) | |
| # Hello, Alan! | |
| print(greet_by_name("Jess")) | |
| # Hello, Jess! | |
| print(greet_by_name("Ian")) | |
| # Hello, Ian! | |
| ######## | |
| days = 7 | |
| name = "Jane Doe" | |
| fruits = ["apple", "banana", "pear"] | |
| daysCopy = days | |
| days == daysCopy # True | |
| daysCopy = daysCopy + 1 | |
| days == daysCopy # False | |
| fruitsCopy = fruits | |
| fruits == fruitsCopy # True | |
| fruits.pop() # removes the last item from the list | |
| fruits == fruitsCopy # True | |
| ######## | |
| w = 15 | |
| y = 6 | |
| def calculate_hypotenuse(): | |
| x = sqrt(w ** 2 + y ** 2) | |
| return x | |
| side_a = 15 | |
| side_b = 6 | |
| def calculate_hypotenuse(): | |
| side_c = sqrt(side_a ** 2 + side_b ** 2) | |
| return side_c | |
| ######## | |
| def printHelloWorld(): | |
| str1 = "Hello" | |
| str2 = "World" | |
| print (str1 + ' ' + str2) | |
| ######## | |
| # int | |
| numThree = 3 | |
| # float | |
| shortPi = 3.14 | |
| sum = numThree + shortPi | |
| print(sum) # 6.14000000000001 | |
| # int | |
| numThree = 3 | |
| # string | |
| shortPi = "3.14" | |
| sum = numThree + shortPi | |
| print(sum) | |
| # Traceback (most recent call last): | |
| # File "<stdin>", line 1, in <module> | |
| # TypeError: unsupported operand type(s) for +: 'int' and 'str' | |
| ######## | |
| threeInt = int("3") # threeInt will be 3 | |
| twoString = str(2) # twoString will be '2' | |
| ######## | |
| print("Example 1: Testing reassign_number:") | |
| def reassign_number(number): | |
| print("local number:", number) | |
| number = 42 | |
| print("local number:", number) | |
| global_number = 9 | |
| print("global number:", global_number) | |
| reassign_number(global_number) | |
| print("global number:", global_number) | |
| print() | |
| print("Example 2: Testing reassign_list:") | |
| def reassign_list(fruits): | |
| print("local fruits:", fruits) | |
| fruits = ["Cherry", "Kiwi", "Pineapple"] | |
| print("local fruits:", fruits) | |
| global_fruits = ["Cherry", "Kiwi"] | |
| print("global fruits:", global_fruits) | |
| reassign_list(global_fruits) | |
| print("global fruits:", global_fruits) | |
| print() | |
| print("Example 3: Testing modify_list:") | |
| def modify_list(fruits): | |
| print("local fruits:", fruits) | |
| fruits.append("Pineapple") | |
| print("local fruits:", fruits) | |
| global_fruits = ["Cherry", "Kiwi"] | |
| print("global fruits:", global_fruits) | |
| modify_list(global_fruits) | |
| print("global fruits:", global_fruits) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment