Professional Documents
Culture Documents
import random
import time
import matplotlib.pyplot as plt
def reportGroup(personList):
print()
print("Printing Group")
for i in range(0, len(personList)):
p = personList[i]
print(p.name + " " + str(p.weight) + " " + str(p.height))
print(2)
print(1)
print("Hello World")
myGreeting = 6
print(myGreeting)
myGreeting = "Hello MSc students"
print(myGreeting)
a = 8
b = 2
c = a + 5
c = b
a = a + 1
b = c + a
e = abs(-7)
# std library
p = math.pi
# imported modules
a = math.cos(0)
a = math.sin(p / 6)
a = round(a, 2)
myA36276 = 21.4599943
BUYGF8474743 = 3.58487376
result = myA36276 - myA36276
print(type(a))
print(type(result))
print(id(a))
i = 0
while i < 10:
print(i)
i = i + 2
# what would happen to for with i = i + 2
condition1 = True
condition2 = False
if condition1 == True:
print("YES")
else:
print("NO")
if condition1 == True ^ 5 == 5:
print("YES")
else:
print("NO")
cond = 3 == 7
if cond:
print("Test 1")
if not cond:
print("Test 2")
b = False
for i in range(10):
b = not b
print(b)
def my_isfloat(val):
try:
float(val)
return True
except:
return False
userNumber = 12
userNumber = int(userNumber)
iterator = 0
terminationCondition = False
myString = "exampleString"
i = 0
while i < 10:
print(myGreeting + " " + str(i))
i = i + 1
# Functions
x = 12
y = 14
sum = x + y
print(sum)
sum2 = sumThemUp(x, y)
print(sum2)
print(x)
print(y)
third_one = sum_or_subtract_optional(x, y)
fourth_one = sum_or_subtract_optional(x, y, False)
res = my_fact(3)
print(res)
class Person():
def __init__(self):
self.weight = 0
self.height = 0
self.name = ""
self.myFriends = []
p1.weight = 60
p1.height = 165
p1.name = "Mary"
name = "Alex"
p2.name = name
p2.weight = 73
p2.height = 175
p3 = Person()
p3.name = "George"
p3.weight = 80.0
p3.height = 180.0
diet(p1, 2)
p1.dietInObject(2)
def changePerson(per):
per.weight = 1000
pp = Person()
pp.name = "Manos"
pp.weight = -100
pp.height = -100
per = pp
changePerson(p3)
print(p3.weight)
print(p3.height)
print(p3.name)
print(p1)
print(str(p1))
# lists
group1 = []
group2 = []
group1.append(p1)
group1.append(p3)
group1.append(p2)
group1.append(p3)
group1.append(p1)
reportGroup(group1)
diet(p3, 5)
reportGroup(group1)
removed = group1.pop(1)
reportGroup(group1)
group2 = group1
group1.insert(0, p1)
reportGroup(group2)
group1.insert(0, p2)
reportGroup(group2)
def naiveSortOnWeight(listPersons):
for i in range(0, len(listPersons)):
for j in range(0, len(listPersons) - 1):
if listPersons[j].weight > listPersons[j + 1].weight:
temp = listPersons[j]
listPersons[j] = listPersons[j + 1]
listPersons[j + 1] = temp
naiveSortOnWeight(group1)
group1 = []
group1.append(p1)
group1.append(p3)
group1.append(p2)
group1.append(p3)
group1.append(p1)
group1.append(p1)
group1.append(p3)
group1.append(p2)
group1.append(p3)
group1.append(p1)
def WeightGetter(x):
return x.weight
# lambda arguments: return
doubleTheNumber = lambda x: x * 2
res = doubleTheNumber(3)
print(res)
doubleFirstNumberAndAddSecond = lambda x, y: x * 2 + y
res = doubleFirstNumberAndAddSecond(3, 10)
print(res)
reportGroup(group1)
group1.sort(key=WeightGetter)
group1.sort(key=lambda p: p.weight)
reportGroup(group1)
reportGroup(group2)
# As I iterate through the list one element at a time , I'm going to pass the
current element to the function provided in the key argument
# The sorting will be made according to the value returned by this function
listOfLists[2][3] = 45
listOfLists3[2][3] = 45
print(listOfLists)
# what went wrong???
print(listOfLists3)
a = [1, 2, 3]
b = [1, 2, 3]
eq = a == b
print(eq)
# List slicing
# makes a copy of the slice of a and assigns it to b.
b = a[0:2]
print(b)
my_lst = list(range(0, 10))
print(my_lst)
my_other_list = my_lst[2:5:1]
print(my_other_list)
# when we slice, a new list object is created with copies of the elements of the
original list
my_other_list[0] = 1000
print('original_list', my_lst)
print('new_list', my_other_list)
my_lst = list(range(0, 10))
my_other_list = my_lst[-7:-2:1]
print(my_other_list)
# Slice assignment:
my_lst = list(range(0, 10))
print(my_lst)
my_lst[7:9] = [1000]
print(my_lst)
# can be used for insertions, in this case start and end is the same (no
replacement takes place)
my_lst[1:1] = [-10, -20]
print(my_lst)
my_lst[1:1] = []
print(my_lst)
# Tuples
a = (12, 12, 34.0, 34.0, 'manos')
first = a[0]
second = a[1]
third = a[2]
print(first, second, third)
# a[2] = 23.0
elements_of_tuples = len(a)
print(elements_of_tuples)
tms = a.count(34)
# if we want to remove the first 34.0 we have to create a new tuple
other_tuple = a[:2] + a[3:]
print (other_tuple)
# or change to list
lst = list(a)
lst.pop(2)
other_tuple_2 = tuple(lst)
print (other_tuple_2)
# very common to build functions which return two (or more) results
def double_and_triple (arg):
return 2*arg, 3*arg
res = double_and_triple(10)
print(res)
r1, r2 = double_and_triple(10)
print(r1, r2)
my_lst = [1, 2, 3, 4, 5]
my_other_list = [10, 100, 1000, 10000, 100000]
mapp2 = map(lambda x, y: x + y, my_lst, my_other_list)
new_lst = list(map(lambda x, y: x + y, my_lst, my_other_list))
print(new_lst)
# size = 100
#
# lst = [i for i in range(size)]
# m = map(lambda x:-x, lst)
# for i in m:
# print('Iterating at map:', i)
#
# def negate(x):
# print('Processing Arg', x)
# return -x
#
# lst = [i for i in range(size)]
# m = map(negate, lst)
# for i in m:
# print('Iterating at map:', i)
#
# empty_lst = list(m)
#
# m = map(negate, lst)
# lst_neg = list(m)
#
# print(1)
def filter_above_5(candidate):
print('checking', candidate)
return candidate > 5
list1 = [1, 2, 3, 4, 5]
list2 = [100, 200, 300, 400, 500]
list_of_tuples = list(zip(list1, list2))
print(list_of_tuples)
list_of_tuples = list(zip(list1, list2, list1))
print(list_of_tuples)
# paired calculations
revenues = [1000, 2000, 1500, 1800]
fixed_costs = [200, 210, 210, 220]
running_costs = [150, 230, 100, 400]
# also zip is useful for building dictionaries (this is shown when dictionaries are
presented)
# The functions supplied by random are actually methods of a hidden instance of the
random.Random class.
# You can instantiate your own instances of Random to get generators that don’t
share state.
print("***************")
randomGenerator1 = random.Random(1)
randomGenerator2 = random.Random(2)
print("Thats all!!!")
# sets (they store objects unordered but they offer very fast retrievals)
myset = set()
myset.add(5)
myset.add(7)
i = 8
if i in myset:
print("Its there " + str(i))
else:
print("Its NOT there " + str(i))
myset.remove(12)
# myset.remove(70)
myset.discard(70)
myset.discard(90)
print(myset)
print(1)
manos1 = Person()
manos2 = Person()
manos3 = Person()
manos4 = Person()
manos5 = manos4
personSet = set([manos1, manos2, manos3, manos4, manos5])
print(personSet)
# size = 10000000
#
# abyss_lst = []
# abyss_set = set()
# for i in range(0, 10000000):
# abyss_lst.append(i)
# abyss_set.add(i)
#
# target_value = size - 1
# t0 = time.time()
# a = target_value in abyss_lst
# t1 = time.time()
# tot_time = t1-t0
# print('in list ' + str(tot_time))
#
# t0 = time.time()
# a = target_value in abyss_set
# t1 = time.time()
# tot_time = t1-t0
# print('in set ' + str(tot_time))
#
# t0 = time.time()
# time.sleep(1)
# t1 = time.time()
# tot_time = t1-t0
# print('sleep ' + str(tot_time))
print(1)
st = set()
a = [1, 2, 3]
# not allowed. lists are unhashable -> since they typically change it is not secure
to compute a hash
# st.add(a)
a = (1, 2, 3)
st.add(a)
if (1, 2, 3) in st:
print ('its there!')
# Dictionaries
simpleAnimalInfoDictionary = {
"Name": "lion",
"Weight": 680,
"LifeExpectancy": 40
}
print(simpleAnimalInfoDictionary)
simpleAnimalInfoDictionary['is_mammal'] = False
print(simpleAnimalInfoDictionary)
del simpleAnimalInfoDictionary['is_mammal']
print(simpleAnimalInfoDictionary)
test1 = simpleAnimalInfoDictionary["Name"]
test2 = simpleAnimalInfoDictionary.get("Name")
print(test1)
print(test2)
simpleAnimalInfoDictionary["Name"] = "Liontari"
test1 = simpleAnimalInfoDictionary["Name"]
print(test1)
for i in simpleAnimalInfoDictionary:
print(i)
for i in simpleAnimalInfoDictionary:
print(simpleAnimalInfoDictionary[i])
for i in simpleAnimalInfoDictionary.values():
print(i)
st = "Extinct"
if (st in simpleAnimalInfoDictionary):
print("Already there")
else:
simpleAnimalInfoDictionary[st] = False
print(simpleAnimalInfoDictionary)
keys = list(simpleAnimalInfoDictionary.keys())
Zoo = {}
Zoo[simpleAnimalInfoDictionary["Name"]] = simpleAnimalInfoDictionary
print(Zoo)
print(Zoo)
tst_name = "Dino"
if tst_name in Zoo:
print(Zoo[tst_name])
print(5)
list_x_values = []
list_y_values = []
for i in range(0, 100):
x = i
y = 3 * i + random.normalvariate(0, 3)
list_x_values.append(x)
list_y_values.append(y)
#
# plt.scatter(list_x_values, list_y_values)
# plt.show()
# plt.scatter(list_x_values, list_y_values, marker='x')
# plt.xlabel('x-values')
# plt.ylabel('ys')
# plt.show()
# plt.hist(list_y_values)
# plt.show()
# plt.hist(list_y_values)
# plt.show()
# pd.read_csv('data.csv')