0% found this document useful (0 votes)
4 views6 pages

Python Built-in Functions and Data Structures

The document explains the built-in Python functions 'any()' and 'all()', which evaluate iterables for truth values. It also covers the 'pass' statement for creating placeholders in functions and control structures, and discusses Python's data structures, including mutable and immutable types. Additionally, it introduces comprehensions for lists, dictionaries, and sets, providing examples of their usage for creating and filtering data efficiently.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Python Built-in Functions and Data Structures

The document explains the built-in Python functions 'any()' and 'all()', which evaluate iterables for truth values. It also covers the 'pass' statement for creating placeholders in functions and control structures, and discusses Python's data structures, including mutable and immutable types. Additionally, it introduces comprehensions for lists, dictionaries, and sets, providing examples of their usage for creating and filtering data efficiently.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Any All in Python

Any and All are two built-in functions provided in Python used for successive
And/Or.
What is Any () in Python
Any Returns true if any of the items is True and returns False if empty or all are
false. Any can be thought of as a sequence of OR operations on the provided
iterables.
Python any() Function Syntax
Syntax: any(list of iterables)
Example:-
# Check if any element in the list is greater than 5
my_list = [1, 3, 7, 2]
result = any(x > 5 for x in my_list)
print(result) # Output: True

What is All () in Python


The all () function in Python is a built-in function that returns True if all elements
of an iterable (like a list, tuple, set, or dictionary) are true or if the iterable is
empty. Otherwise, it returns False.
# Check if all elements in the list are even numbers
my_list = [2, 4, 6, 8]
result = all (x % 2 == 0 for x in my_list)
print (result) # Output: True

pass statement
Pass keyword in a function is used when we define a function but don’t want to
implement its logic immediately. It allows the function to be syntactically valid,
even though it doesn’t perform any actions yet.
# Example of using pass in an empty function
def fun ():
pass # Placeholder, no functionality yet
# Call the function
Fun ()
Explanation:
• function fun () is defined but contains the pass statement, meaning it does
nothing when called.
• program continues execution without any errors and the message is
printed after calling the function.

Using pass in Conditional Statements


In a conditional statement (like an if, elif or else block), the pass statement is
used when we don’t want to execute any action for a particular condition.
x = 10

if x > 5:
pass # Placeholder for future logic
else:
print("x is 5 or less")
No Output
Using pass in Loops
In loops (such as for or while), pass can be used to indicate that no action is
required during iterations.
for i in range(5):ss
Output
if i == 3:
0
pass # Do nothing when i is 3
1
else:
2
print(i)
4
Python Data Structures: Lists, Dictionaries, Sets, Tuples
What Is a Data Structure?
A data structure is a way of organizing data in computer memory, implemented in
a programming language. This organization is required for efficient storage,
retrieval, and modification of data. It is a fundamental concept as data structures
are one of the main building blocks of any modern software. Learning what data
structures exist and how to use them efficiently in different situations is one of
the first steps toward learning any programming language.
Data Structures in Python
Built-in data structures in Python can be divided into two broad
categories: mutable and immutable. Mutable data structures are those which
we can modify -- for example, by adding, removing, or changing their elements.
Python has three mutable data structures: lists, dictionaries, and sets.
Immutable data structures, on the other hand, are those that we cannot modify
after their creation. The only basic built-in immutable data structure in Python is
a tuple.

Comprehensions on List, Set, and dictionary


Comprehensions in Python are a concise way to perform operations on data sets
like lists, dictionaries, and sets. They can make code more readable and compact.
Types of comprehensions
List comprehension: Creates a new list from an existing list.
Dictionary comprehension: Creates a dictionary from an iterable object, like a list.
How they work
Comprehensions apply an expression to each item in an existing iterable.
They can be used for mapping, filtering, and standard list creation.
They can be written in a few lines of code, or even a single line.
Benefits of comprehensions
They can help write cleaner and more readable code.
They can shorten lines of code while keeping the logic intact.
They can be combined with slicing to create efficient ways to handle and
manipulate lists.
List Comprehensions
List comprehensions in Python are a concise way to create lists. They provide a
syntactic way of generating lists from iterables, and they're often more readable
and faster than traditional for-loop based list creation.
Here's the basic syntax:
[expression for item in iterable if condition]
examples:
1. Generating an Even list WITHOUT using List comprehensions

input_list = [1, 2, 3, 4, 4, 5, 6, 7, 7]
output_list = []

for var in input_list:


if var % 2 == 0:
output_list.append(var)

print("Output List using for loop:", output_list)

Output:
Output List using for loop: [2, 4, 4, 6]

Example 2: Generating Even list using List comprehensions

input_list = [1, 2, 3, 4, 4, 5, 6, 7, 7]

list_using_comp = [var for var in input_list if var % 2 == 0]

print("Output List using list comprehensions:",


list_using_comp)

Output:
Output List using list comprehensions: [2, 4, 4, 6]

Example [Link] a list of squares:


squares = [x**2 for x in range(10)]
print(squares) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Example [Link] even numbers:
evens = [x for x in range(20) if x % 2 == 0]
print(evens) # Output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Dictionary Comprehensions
The basic structure of a dictionary comprehension looks like below.
output_dict = {key: value for (key, value) in iterable if (key, value satisfy this
condition)}
# Example: Filtering even numbers and creating a dictionary with their cubes
cubes = {x: x**3 for x in range (10) if x % 2 == 0}
print(cubes)

Python Dictionary Comprehension Example


Here we have two lists named keys and value and we are iterating over
them with the help of zip () function.
The zip function in python combines elements from multiple lists, tuples, or
dictionaries into a single iterable. It’s a built-in function that’s useful for
simplifying code and making it more efficient.

# Python code to demonstrate dictionary


# comprehension

# Lists to represent keys and values


keys = ['a','b','c','d','e']
values = [1,2,3,4,5]

# But this line shows dict comprehension here


myDict = { k:v for (k,v) in zip(keys, values)}

# We can use below too


# myDict = dict(zip(keys, values))

print (myDict)
Output:
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

Set Comprehensions
Set comprehensions are pretty similar to list comprehensions. The only
difference between them is that set comprehensions use curly brackets { }
Example 1: Checking Even number using set comprehension

input_list = [1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 7]

set_using_comp = {var for var in input_list if var % 2 == 0}

print("Output Set using set comprehensions:",


set_using_comp)

Output:
Output Set using set comprehensions: {2, 4, 6}

You might also like