5/3/2018 Let's Do Together - Session 1
In [1]:
# Install Related
# 1. Install both Python 2.7 and 3.7 from conda environment
# 2. Install advanced libraries through conda environment
In [2]:
#Write a Python program to sum all the items in a list.
In [3]:
#solution
def sum_list(items):
sum_numbers = 0
for x in items:
sum_numbers += x
return sum_numbers
print(sum_list([1,2,-8]))
-5
In [7]:
# Write a Python program to count the number of strings where the string length is 2 or
# more and the first and last character are same from a given list of strings.
In [8]:
#solution
def match_words(words):
ctr = 0
for word in words:
if len(word) > 1 and word[0] == word[-1]:
ctr += 1
return ctr
In [9]:
a=['abc', 'xyz', 'aba', '1221','bhgsskknb','aa']
In [10]:
match_words(a)
Out[10]:
In [11]:
#Write a Python program to remove duplicates from a list.
[Link]
5/3/2018 Let's Do Together - Session 1
In [20]:
#Solution
samp_list = [10, 20, 30, 20, 10, 50, 60, 40, 80, 50, 40]
dup_items = set()
uniq_items = []
for x in a:
if x not in dup_items:
uniq_items.append(x)
dup_items.add(x)
print(dup_items)
{40, 10, 80, 50, 20, 60, 30}
In [ ]:
[Link]