Ad Code

How to Write Complex programs Using Python Functions

Many times, in job interviews, you need to give a written test. There, you may need to answer tricky programs. Historically, people are afraid to take a test. Especially in python


Functions

Python programs using functions


  • Counting lower and upper case letters
  • Create a list

1. Counting lower and upper case letters

Below program counts the upper and lower case letters.


def count_lower_upper(s): dlu = {'Lower': 0, 'Upper': 0} for ch in s: if ch.islower(): dlu['Lower'] += 1 elif ch.isupper(): dlu['Upper'] += 1 return(dlu) d = count_lower_upper('James BOnd') print(d) d = count_lower_upper('Anant Amrut Mahalle') print(d)


python function program


Also read: How to Lose Your Weight


2. Create a List


The below program uses two input lists for creating a new list. Additionally, it uses the list built-in function.


def create_list(l1, l2): l3 = list(set(l1) & set(l2)) return(l3) lst1 = [10, 20, 30, 40, 50] lst2 = [1, 2, 3, 40, 10] lst3 = create_list(lst1, lst2) print(lst3)


tricky python program


Related

Post a Comment

0 Comments