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 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 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 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 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 a list, | |
# sorted in increasing order by the last element | |
# in each tuple from a given list of non-empty tuples. | |
# Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 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
# Create a program that asks the user for a number | |
# and then prints out a list of all the divisors of that number. | |
num = int(input("Enter a number")) | |
check = list(range(1, num+5)) | |
for number in check: | |
if num % number == 0: | |
print(number) |
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 two lists, say for example these two: | |
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] | |
# b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] | |
# and write a program that returns a list that contains only the elements | |
# that are common between the lists (without duplicates). | |
# Make sure your program works on two lists of different sizes. | |
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] | |
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] | |
c = [] |
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 two random lists | |
# and write a program that returns a list that contains only the elements | |
# that are common between the lists (without duplicates). | |
# Make sure your program works on two lists of different sizes. | |
import random | |
a = random.sample(range(100), 10) | |
b = random.sample(range(100), 15) | |
c = [] |
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 string and print out whether this string is a palindrome or not. | |
raw = str(input("Enter a string: ")) | |
raw.replace(" ", "") | |
set1 = str.lower(raw[::-1]) | |
set2 = str.lower(raw[0::]) | |
if set2 == set1: | |
print("Palindrome") | |
else: | |
print("Not a palindrome") |