0% found this document useful (0 votes)
6 views1 page

Extended Unpacking in Python

The document discusses various features of Python, including extended unpacking with examples of how to assign values to variables while ignoring some. It also demonstrates creating a list of lists and shows how to simplify conditional checks using a lambda function. Additionally, it illustrates the use of underscores in numeric literals for better readability.

Uploaded by

happynextgen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Extended Unpacking in Python

The document discusses various features of Python, including extended unpacking with examples of how to assign values to variables while ignoring some. It also demonstrates creating a list of lists and shows how to simplify conditional checks using a lambda function. Additionally, it illustrates the use of underscores in numeric literals for better readability.

Uploaded by

happynextgen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Spicy Python

a, *rest = [1, 2, 3]
# a = 1, rest = [2, 3]

a, *middle, c = [1, 2, 3, 4]
# a = 1, middle = [2, 3], c = 4

## ignoring a value
a, _, b = (1, 2, 3) # a = 1, b = 3
print(a, b)

## ignoring multiple values


## *(variable) used to assign multiple value to a variable
as list while unpacking
## it's called "Extended Unpacking", only available in
Python 3.x
a, *_, b = (7, 6, 5, 4, 3, 2, 1)
print(a, b)

Create a length-N list of lists


four_lists = [[] for __ in range(4)]

different number systems


million = 100_0_000
print(million)

roles = ["DistributorAdmin", "DistributorUser", "ClientAdmin", "ClientUser"]


simplify_if_else_elif = lambda role: role in roles
role = "ClientUser"
print(simplify_if_else_elif(role))

You might also like