Python Basic Programs




# Total number of odd numbers in list

numbers=[1,2,3,4,5,6,7,8,10,11,12,13,14,15,16];
count=0;
for i in numbers:
if i==1:
count=count+1;
else:
if (i%2 != 0):
count=count+1;
print("Total odd numbers: ",count);
# Using comprehension
odd = [num for num in numbers if num %2 !=0 ];
print("Odd Numbers: ",odd);
# Using lambda
odd = list(filter(lambda x:x%2 != 0, numbers));
print("Odd Numbers: ",odd);
---------------------------------------------------------------------
# Remove items from a list while iterating
# You need to remove items from a list while iterating but without creating a different copy of a list.
# Remove numbers greater than 50

number_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
for i in range(len(number_list)-1,-1,-1):
print(i);
---------------------------------------------------------------------
# Print the following number pattern
# 1 1 1 1 1
# 2 2 2 2
# 3 3 3
# 4 4
# 5

rows=5;
x=0;
for i in range(rows,0,-1):
x = x + 1;
for j in range(1,i+1):
print(x,end=" ");
print('\r');
---------------------------------------------------------------------
# Create an inner function
# Create an outer function that will accept two strings, x and y. (x= 'Emma' and y = 'Kelly'.
# Create an inner function inside an outer function that will concatenate x and y.
# At last, an outer function will join the word 'developer' to it.
# Expected Output: - EmmaKellyDevelopers

def outer(x,y):
z="Developers";
def concan(x,y):
return x+y;
temp = concan(x,y);
print(temp+z);

outer('Emma','Kelly');
---------------------------------------------------------------------
# Modify the element of a nested list inside the following list
# Change the element 35 to 3500

list1 = [5, [10, 15, [20, 25, [30, 35], 40], 45], 50];
list1[1][2][2][1]=3500;
print(list1);
---------------------------------------------------------------------
# Access the nested key increment from the following dictionary
emp_dict = {
"company": {
"employee": {
"name": "Jess",
"payable": {
"salary": 9000,
"increment": 12
}
}
}
};
print(emp_dict.get('company').get('employee').get('payable').get('increment'));
---------------------------------------------------------------------
# Exercise 4: Reverse Dictionary mapping
# ascii_dict = {'A': 65, 'B': 66, 'C': 67, 'D': 68}
# Expected Output:
# {65: 'A', 66: 'B', 67: 'C', 68: 'D'}

ascii_dict = {'A': 65, 'B': 66, 'C': 67, 'D': 68};
temp={};
for i in ascii_dict.items():
temp[i[1]]=i[0];
print(temp);

new_dict={value: key for key,value in ascii_dict.items()};
print(new_dict);
---------------------------------------------------------------------
# Display all duplicate items from a list
# sample_list = [10, 20, 60, 30, 20, 40, 30, 60, 70, 80]
# Output: [20, 60, 30]

sample_list = [10, 20, 60, 30, 20, 40, 30, 60, 70, 80];
duplicate=[];
import collections;
print(collections.Counter(sample_list)); # Counter({20: 2, 60: 2, 30: 2, 10: 1, 40: 1, 70: 1, 80: 1})
print(collections.Counter(sample_list).items()); #dict_items([(10, 1), (20, 2), (60, 2), (30, 2), (40, 1), (70, 1), (80, 1)])
for items, count in collections.Counter(sample_list).items():
if count > 1:
duplicate.append(items);
print(duplicate);
---------------------------------------------------------------------
# Filter dictionary to contain keys present in the given list
# # Dictionary
# d1 = {'A': 65, 'B': 66, 'C': 67, 'D': 68, 'E': 69, 'F': 70}
# # Filter dict using following keys
# l1 = ['A', 'C', 'F']
# new dict {'A': 65, 'C': 67, 'F': 70}

d1 = {'A': 65, 'B': 66, 'C': 67, 'D': 68, 'E': 69, 'F': 70}
l1 = ['A', 'C', 'F']
temp={};
for i in l1:
temp[i]=d1.get(i);
print(temp);

Comments

Popular posts from this blog

SRE/DevOps Syllabus

AWS Code Commit - CI/CD Series Part 1

Docker - Preventing IP overlapping