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
# 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)) | |
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
# 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)) |
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
# 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]: |
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
# 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 |
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
# 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)) |
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
# 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 |
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
# 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 |
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
# 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 |
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
# 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) |
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
# 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: |