-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionals.py
More file actions
48 lines (39 loc) · 1.04 KB
/
conditionals.py
File metadata and controls
48 lines (39 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
mylist = [12, 32, 34, 2, 4]
mydictinry = {"one": 32, "two": 322, "three": 32, "four": 4}
def avg(n):
if type(n) == dict:
print("its a dictionary")
avg = sum(n.values()) / len(n)
print("average is ", avg)
else:
print("its a list")
avg = sum(mylist) / len(mylist)
print("average is ", avg)
avg(mylist)
avg(mydictinry)
'''
The isinstance() function returns True if the specified object is of the specified type, otherwise False.
If the type parameter is a tuple, this function will return True if the object is one of the types in the tuple.
isinstance(object, type)
so the code can be changed as
'''
def avgs(n):
if isinstance(n, dict):
print("its a dictionary")
avg = sum(n.values()) / len(n)
print("average is ", avg)
else:
print("its a list")
avg = sum(mylist) / len(mylist)
print("average is ", avg)
avgs(mylist)
avgs(mydictinry)
'''
if else
'''
if 3 > 1:
print("3 greater")
elif 3 == 1:
print("equal")
else:
print("3 smaller")