INFO 253B: Backend Web Architecture
Kay Ashaolu
def conditions():
name = "John"
age = 23
if name == "John" and age == 23:
return "Your name is John, and you are also 23 years old."
elif name == "Jake" or name == "Rick":
return "Your name is either Jake or Rick"
def conditions_in():
name = "John"
if name in ["John", "Rick"]:
return "Your name is either John or Rick."
return "Nothing"
if __name__ == "__main__":
print(conditions())
print(conditions_in())
def while_loop():
count = 0
while count < 5:
print(count)
count += 1
def while_loop_with_break():
count = 0
while True:
print(count)
count += 1
if count >= 5:
break
def for_loop():
for x in range(10):
# Check if x is even
if x % 2 == 0:
continue
print(x)
if __name__ == "__main__":
print(while_loop())
print(while_loop_with_break())
print(for_loop())
def my_function():
print("Hello From My Function!")
def my_function_with_args(username, greeting):
print("Hello, {} , From My Function!, I wish you {}"
.format(username, greeting))
def sum_two_numbers(a, b):
return a + b
if __name__ == "__main__":
my_function()
#prints - "Hello, John Doe, From My Function!, I wish you a great year!"
my_function_with_args("John Doe", "a great year!")
# after this line x will hold the value 3!
x = sum_two_numbers(1,2)
print(x)
# from lib.py
def my_function():
print("Hello From My Function!")
def my_function_with_args(username, greeting):
print("Hello, {} , From My Function!, I wish you {}"
.format(username, greeting))
def sum_two_numbers(a, b):
return a + b
# from test.py in same folder
import lib
if __name__ == "__main__":
lib.my_function()
#prints - "Hello, John Doe, From My Function!, I wish you a great year!"
lib.my_function_with_args("John Doe", "a great year!")
# after this line x will hold the value 3!
x = lib.sum_two_numbers(1,2)
print(x)
class MyClass:
variable = "blah"
def function(self):
print("This is a message inside the class.")
if __name__ == "__main__":
myobjectx = MyClass()
myobjecty = MyClass()
myobjecty.variable = "yackity"
# Then print out both values
print(myobjectx.variable)
print(myobjecty.variable)