Created
August 12, 2019 15:49
-
-
Save ibirnam/aa671ffd03004bc3318aebeccf024e22 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 addTen(x): | |
plusTen = x + 10 | |
return plusTen | |
print(addTen(4)) | |
# 14 | |
print(addTen(10)) | |
# 20 | |
print(addTen(99)) | |
# 109 | |
######## | |
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 | |
######## | |
side_a = 15 | |
side_b = 6 | |
def calculateHypotenuse(): | |
side_c = sqrt(side_a * side_a + side_b * side_b) | |
return side_c | |
w = 15 | |
y = 6 | |
def calculateHypotenuse(): | |
x = sqrt(w * w + y * y) | |
return x | |
######## | |
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' | |
######## | |
def refDemo(x): | |
print("fun: x=",x) | |
x = 42 | |
print("fun: x=",x) | |
x = 9 | |
print("out: x=",x) | |
refDemo(x) | |
print("out: x=",x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment