Created
January 17, 2019 19:47
-
-
Save MarketaP/6a97772ec1757021c080b23a8c1a873c to your computer and use it in GitHub Desktop.
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Jan 17 11:25:18 2019 | |
@author: mpodebradska2 | |
""" | |
# Exercise 6 | |
my_list = [1, 2, 3] | |
def mean(sequence): | |
'''Calculates the arithmetic mean of a sequence from a list''' | |
return sum(sequence)/len(sequence) | |
print("Ecercise 6: ", mean(my_list)) | |
# Exercise 2 | |
my_list = [20.0, 22.0, 19.0] | |
new_list = [] | |
for item in my_list: | |
new_list.append(item*5) | |
print(new_list) | |
# Exercise 8 | |
my_list = [1, 2, 3] | |
def mean(sequence): | |
'''Calculates the arithmetic mean of a sequence from a list''' | |
return sum(sequence)/len(sequence) | |
def square_root(sequence): | |
var_mean = mean(sequence) | |
squared_errors = [] | |
for i in sequence: | |
squared_errors.append((i - var_mean) ** 2) | |
sse = sum(squared_errors) | |
n = len(sequence) | |
return(sse / (n - 1)) ** (1 / 2) | |
print("Ecercise 8: ", square_root(my_list)) | |
# Indexing | |
new_list2 = [1, 2, [3, 4], "Bob"] | |
print(my_list[-1]) | |
print(new_list2[2][1]) | |
# Slicing some_list[start:stop:step] | |
my_list2 = list(range(7)) | |
print(my_list2) | |
print(my_list2[0::2]) | |
# Exercise 5 | |
my_list3 = list(range(1, 10, 2)) | |
def maximum(sequence): | |
'''Gets a max value from a list''' | |
new_sequence = sequence.copy() | |
new_sequence.sort | |
return new_sequence[-1] | |
def minimum(sequence): | |
'''Gets a min value from a list''' | |
new_sequence = sequence.copy() # we are copying because we don't want to mutate the original list as we sort in the next line | |
new_sequence.sort | |
return new_sequence[0] | |
print(maximum(my_list3)) | |
print(minimum(my_list3)) | |
# Conditionals | |
x = 10 | |
if x < 5: | |
print("less than 5") | |
elif x < 10: | |
print("less than 10") | |
else: | |
print("greater or equal to 10") | |
# Ecercise 6 | |
def even_odd(number): | |
if number % 2 == 0: | |
print("Even") | |
else: | |
print("Odd") | |
even_odd(100) | |
def is_even(number): | |
return number % 2 == 0 | |
print(is_even(9)) | |
# Exercise 7 | |
ages = [20, 20, 21, 43, 33, 27, 19, 23, 22, 29] | |
ages2 = list(range(100)) | |
def younger_than_25(sequence): | |
new_list = [] | |
for i in sequence: | |
if i < 25: | |
new_list.append(i) | |
print(new_list) | |
return len(new_list) | |
younger_than_25(ages2) | |
# Exercise 8 | |
new_list_4 = list(range(1, 101)) | |
squares = [a ** 2 for a in new_list_4] | |
print(squares) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment