Python Programming: Lesson 22 - List Comprehensions and Formatted Strings (format Function)

preview_player
Показать описание
PLEASE SUBSCRIBE!!!

A dictionary is a set of key-value pairs. We map a:b, for a unique set of keys a. The keys have to be unique, but the values b do not have to be unique. Dictionaries also use curly braces { }, like sets. We can write D[K] = V to assign or reassign a key K to value V. We can remove a key-value pair from the other dictionary, among other operations. Learn about it in this video.

A list comprehension is defined as follows:

myList = [ functionOfVariable for variable in otherDataStructureOrRange if someBoolean ]

equivalent to

myList = [ ]

for variable in otherDataStructureOrRange:
if someBoolean:

It is a shorter way of composing a list. Also, we have a way to format a particular string. It can also accept ints, floats, and booleans, unlike the basic string concatenation.

a = 32
b = " dogs "
c = " in the building"

print( str(a) + b + c ), 32 dogs in the building
print( "{0}{1}{2}".format( a , b , c ) ), 32 dogs in the building
print( "{x}{r}{q}".format(x=a,r=b,q=c) ), 32 dogs in the building

0:32 NEW: Comparing and Contrasting Lists, Tuples, Sets, and Dictionaries
12:22 NEW: List Comprehensions
16:13 NEW: Using the format Function to Format Strings

***

Thanks for watching, and PLEASE SUBSCRIBE!!!
Рекомендации по теме
visit shbcf.ru