Skip to content

Instantly share code, notes, and snippets.

@ehedaoo
ehedaoo / List1a.py
Created May 2, 2017 11:10
Write a Python program to sum all the items in a list.
# Write a Python program to sum all the items in a list.
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
# simple method is to call sum on list itself
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
print(sum(a))
@ehedaoo
ehedaoo / List1d.py
Created May 2, 2017 11:09
Write a Python program to get the largest number from a list.
# Write a Python program to get the largest number from a list.
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
print(max(a))
@ehedaoo
ehedaoo / List1h.py
Created May 2, 2017 10:59
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.
# 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.
# Sample List : ['abc', 'xyz', 'aba', '1221']
def compare(a):
ctr = 0
for i in a:
if len(i) > 2 and i[0] == i[-1]:
@ehedaoo
ehedaoo / List1g.py
Created May 2, 2017 10:28
Write a Python program to get the smallest number from a list. Using Functions
# Write a Python program to get the smallest number from a list.
# Using Functions
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
def min_item(a):
flag = a[0]
for i in a:
if i < flag:
flag = i
return flag
@ehedaoo
ehedaoo / List1f.py
Created May 2, 2017 09:47
Write a Python program to get the smallest number from a list.
# Write a Python program to get the smallest number from a list.
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
a = [1, 1, 2, 3, 5, 8, 0, 13, 21, 34, 55, 89]
print(min(a))
@ehedaoo
ehedaoo / List1e.py
Created May 2, 2017 09:46
Write a Python program to get the largest number from a list. Using function
# Write a Python program to get the largest number from a list.
# Using function
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
def max_num(a):
flag = 0
for i in a:
if i > flag:
flag = i
@ehedaoo
ehedaoo / List1c.py
Created May 2, 2017 07:04
Write a Python program to multiplies all the items in a list.
# Write a Python program to multiplies all the items in a list.
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
def multiply_items(a):
mult = 1
for i in a:
mult = mult*i
return mult
@ehedaoo
ehedaoo / List1b.py
Created May 2, 2017 05:10
Write a Python program to sum all the items in a list. using function
# Write a Python program to sum all the items in a list.
# use function
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
def sum_list(a):
sum_test = 0
for i in a:
sum_test += i
@ehedaoo
ehedaoo / Practice3a.py
Created May 1, 2017 06:09
Ask the user for a number and return a list that contains only elements from the original list a that are smaller than that number given by the user.
# Ask the user for a number and return a list that contains only elements
# from the original list a that are smaller than that number given by the user.
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
new_list = []
num = input("Enter a number to be compared with: ")
for i in a:
if i<num:
new_list.append(i)
@ehedaoo
ehedaoo / Practice3.py
Created May 1, 2017 05:48
Take a list, say for example this one: a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] and write a program that prints out all the elements of the list that are less than 5.
# Take a list, say for example this one:
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
# and write a program that prints out all the elements of the list that are less than 5.
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
new_list = []
num = 5
for i in a:
if i<num: