__author__ = "Narendra Boyina"
# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Technologies PVT LTD
# -----------------------------------------------------------------------------
"""
Topics to be covered in today's class:
--> What is meant by comprehension & types ?
1. List Comprehensions with example
2. set Comprehensions with example
3. Dictionary Comprehensions with example
--> Why we not using tuple comprehension ?
--> Usage of help() and dir() function in Python?
--> Practice programs
"""
"""
1. List Comprehensions
partial Syntax: Variable = [expression(element) for element in iterable]
Full Syntax: Variable = [expression(element) for element in iterable [if else]]
2. set Comprehensions
partial Syntax: Variable = {expression(element) for element in iterable}
Full Syntax: Variable = {expression(element) for element in iterable [if else]}
set function eliminates duplicate objects present if any
result set will not store in order because set is an unordered
3. Dictionary Comprehensions
Syntax:Variable = {key_expr:value_expr for item in iterable}
In another way:
==============
A list comprehension generally consist of these parts :
Output expression,
Input sequence,
A variable representing a member of the input sequence and
An optional predicate part.
For example :
lst = [x ** 2 for x in range (1, 11) if x % 2 == 0]
here, x ** 2 is output expression,
range (1, 11) is input sequence,
x is variable and
if x % 2 == 1 is predicate part.
"""
"""
List Comprehensions:
List comprehension is a concise way of creating lists from the ones that already exist.
It provides a shorter syntax to create new lists from existing lists and their values.
Partial Syntax: Variable = [expression(element) for element in iterable [if else]]
Example 1:
=========
"""
iterable = "why sometimes I have believed others blindly it's my mistake".split() # returns list of strings
# print(iterable) # ['why', 'sometimes', 'I', 'have', 'believed', 'others', 'blindly']
# a = []
# for sub_string in iterable:
# x = len(sub_string)
# a.append(x)
# print(a)
# print([len(sub_string) for sub_string in iterable]) # returns list of lengths of each string in the list
"""below list contains square of all even numbers from 1 to 10"""
# even_square = []
# for number in range(1, 11):
# if number % 2 == 0:
# sqare = number ** 2
# even_square.append(sqare)
# print(even_square)
""" by using list comprehension above generation is same as"""
# print([number ** 2 for number in range(1, 11) if number % 2 == 0])
# print([x ** 2 for x in range(1, 11) if x % 2 == 1])
# Example 2:
# ==========
"""Nested Conditionals With a Python list comprehension,
it doesn’t have to be a single condition; you can nest conditions.
Here’s an example.
"""
"""print the list of numbers b/w 1 to 50, which are divisible by 2 and divisible by 3 """
# output = []
# for number in range(1, 50):
# if number%2 == 0:
# if number%3==0:
# output.append(number)
# print(output)
#
# output = []
# for number in range(1, 50):
# if number%2 == 0 and number%3==0:
# output.append(number)
# print(output)
# print([element for element in range(1, 51) if element%2==0 and element%3==0]) # divisible by both 2 and 3
# print([element for element in range(1, 51) if element%2==0 if element%3==0]) # divisible by both 2 and 3
# print([element for element in range(1, 51) if element%2==0 or element%3==0]) # divisible by either 2 or 3
""" performing squares for which are divisible by 2 and divisible by 3 """
# print([element**2 for element in range(1, 51) if element%2==0 and element%3==0])
"""print the list of numbers b/w 1 to 50, which are not divisible by 2 and not divisible by 3 """
# print([number for number in range(1,51) if number%2!=0 and number%3!=0])
####### set Comprehensions examples ######
"""
Variable = [expression(element) for element in iterable [if else]]
Syntax: Variable = {expression(element) for element in iterable [if else]}
set function eliminates duplicate objects present if any
result set will not store in order because set is an unordered
Difference b/w list and set
list ---> list is a hetrogenious mutalble elements
set --> set is an unordered collection of hetrogenious unique mutalble elements
"""
from math import factorial
# f = [factorial(number) for number in range(0, 10)] # list comprehension
# print(f)
# f = {factorial(number) for number in range(0, 10)} # set comprehension
# print(f) # len of factorial of numbers till 19 set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16,17, 18])
# x = [2,"narendra", 55.6, "kavya"]
# x[2] = 99.6 # based on index i can modify the element in a list
# print(x)
krithika_details = {"name":"Krithika", "age":22, "qualification":"B.Tech"}
# krithika_details["age"] = 24 # modifying value using key
# krithika_details["Designation"] = "Developer" # adding new item by using key
# print(krithika_details)
####### Dictionary Comprehensions examples ######
"""Examples for Dictionary Comprehensions:
syntax: output = {key_expr:value_expr for item in iterable}"""
square_dict = {} # empty dictionary
# square_dict = dict() # empty dictionary
# for num in (2, 4, 6, 8):
# square_dict[num] = num**2 # obj[new_key] = new_value
# print(square_dict)
"""dictionary comprehension example"""
# print({num: num**2 for num in (2, 4, 6, 8, 10, 12, 13)})
# print({num: num**2 for num in range(0, 11) if num%2==0})
# print({num: num**2 for num in range(0, 11, 2)}) # used step element accessing concept
#
# Naren = [1, 2, 3, 4]
# print({i:i+i for i in Naren}) # dictionary comprehension, Performing addition
#
# Naren = [1, 2, 3, 4]
# print({i:i**i for i in Naren}) # dictionary comprehension Performing powers
# # output: {1: 1, 2: 4, 3: 27, 4: 256}
#
# # Naren = [1, 2, 3, 4]
# print({i:i**i for i in Naren if i%2==0}) # dictionary comprehension
"""
Delete recuring(repeated) in string
Take a string as your input then delete any reoccurring(repeated) character, and return the new string.
Take a list as your input then delete any reoccurring element, and return the new list.
"""
# input_data="qqkqqdffbvgnhthfggcgnvghrg"
input_data = "abMeera sindhu"
# input_data = "kruthika Shivakumar"
# output=[]
# for i, char in enumerate(input_data):
# if char not in input_data[ :i] : # input[0:7] "abMeera"
# output.append(char)
# print(output)
# print("".join(output))
""" or """
# output = [char for i, char in enumerate(input_data) if char not in input_data[ :i]]
# print("".join(output))
# print("".join([char for i, char in enumerate(input_data) if char not in input_data[ :i]]))
"""Take a list as your input then delete any reoccurring element, and return the new list.
"""
input_data = ["Nanditha", "Raahi", "venkat reddy", "Raahi", "narendra","venkat", "Nanditha"]
# output=[]
# for i, element in enumerate(input_data):
# if element not in input_data[ :i] : # input[0:4] ==>["Nanditha", "Raahi", "venkat reddy", "Raahi",]
# output.append(element) #["Nanditha", "Raahi", "venkat reddy", "narendra"]
# print(output)
""" or """
# print([element for i, element in enumerate(input_data) if element not in input_data[ :i]])
#
# print(list(set(input_data)))
"""
Usage of help() and dir() function in Python?
Help() and dir() both functions are accessible from the Python interpreter and used for viewing a consolidated dump of built-in functions.
Help() function: The help() function is used to display the documentation
=============== string and also facilitates you to see the help related to modules, keywords, attributes, etc.
Dir() function: The dir() function is used to display the defined variables/methods/inbuilt functions.
===============
"""
# print(help(input))
# print(help(len))
"""
Dir() function: The dir() function is used to display the defined variables/methods/inbuilt functions.
=============== """
x = "narendra"
# print(dir(x))
################################################################
################# Practice programs ###############
###################################################################
# a = [(i**3) for i in range(10)]
# print(a)
#
# A = [3,2,5]
# A = [[i,i**i] for i in A] # list comprehension
# print("A6 value is : ", A)
# f = [len(str((factorial(x)))) for x in range(20)]
# print(f)
# print(type(f))
##{0: 1, 1: 1, 2: 4, 3: 27, 4: 256}
# print({i:i**i for i in range(5)})
# A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5)))
# print("A0 value is : ", A0)
#
# A1 = range(10)
# print("A1 value is : ", A1)
# A2 = sorted([i for i in A1 if i in A0])
# print("A2 value is : ", A2)
#
# # A3 = sorted([A0[i] for i in A0])
# A4 = [i for i in A1 if i in A3]
# print("A4 value is : ", A4)
# A5 = {i:i**i for i in A1} # dictionary comprehension
# print("A5 value is : ", A5)
input_data = ["Nanditha", "Raahi", "venkat reddy", "Raahi", "narendra","venkat", "Nanditha"]
# output=[]
# char_output = []
# for i, element in enumerate(input_data):
# if element not in input_data[ :i] : # input[0:4] ==>["Nanditha", "Raahi", "venkat reddy", "Raahi",]
# output.append(element) #["Nanditha", "Raahi", "venkat reddy", "narendra"]
# for j, char in enumerate(output):
# if char not in output[ :j] : # input[0:4] ==>["Nanditha", "Raahi", "venkat reddy", "Raahi",]
# char_output.append(char) #["Nanditha", "Raahi", "venkat reddy", "narendra"]
# print(char_output)
# print([char for i, element in enumerate(input_data) for i, char in enumerate(element) if char not in element[ :i] ])Author: Boyina Narendra
Supporting Author: M. Meera Sindhu
Request: If you find this information useful, please provide your valuable comments
No comments:
Post a Comment