Python 3.4 - List Comprehension Practice

## values = expression for value in collection## squares = x * x for x in range(10)
squares = [x * x for x in range(10)]

print(squares)

##values = []## for value in colleciton##values.append(expression)
squares = []
for x in range(10):
    squares.append(x*x)

print(squares)

print('-------------------------------')

## this will only do the expression if the remainder is True.

even_squares = [x*x for x in range(10) if x % 2 == 0]

print(even_squares)

##this does the same thing as the above, but in a for loop
even_squares = []
for x in range(10):
    if x % 2 == 0:
        even_squares.append(x*x)


print(even_squares)

print('-------------------------------')

employees = ['jeff', 'rick', 'nick', 'matt']

new_employees = []
for x in employees:
    new_employees.append(x)
print(new_employees)

print('-------------------------------')

##notice the difference with the print statement and its position.##If it is inside the for loop it will print each time and each time a new n is added to the list
nums = [1,2,3,4,5,6,7,8,9,10]

my_list = []
for n in nums:
    my_list.append(n)
    print(my_list)

print('-------------------------------')

nums = [1,2,3,4,5,6,7,8,9,10]

my_list = []
for n in nums:
    my_list.append(n)
print(my_list)

## list comprehension
my_list = [n/2 for n in nums]

print(my_list)

## you can do a lot of things to the n of the list you are working with.
print('-------------------------------')

my_list = []
for letter in'abcd':
    for num in range(4):
        my_list.append((letter, num))
print(my_list)

my_list = [(letter, num) for letter in 'abcd' for num in range(4)]

print(my_list)

print('-------------------------------')

my_dict = {}
names = ['Bill Gates', 'Barack Obama', 'Donald Trump', 'Jeff Bezos',]
jobs = ['CEO', 'Pres', 'Pres', 'CEO']
ranks = [1, 2, 3, 4]
salaries = [1222, 12223, 122345, 1223346]

for name, job, rank, salary in zip(names, jobs, ranks, salaries):
    my_dict[name] = job, rank, salary

print(my_dict)

print('-------------------------------')

nums = [1,1,2,2,3,4,45,5,5,5,6,5,5,5,4444,444,33]

my_set = set()
for n in nums:
    my_set.add(n*2)

print(my_set)

print('-------------------------------'
 
/Users/mattfassnacht/PycharmProjects/untitled6/venv/bin/python /Users/mattfassnacht/PycharmProjects/untitled6/program.py
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
-------------------------------
[0, 4, 16, 36, 64]
[0, 4, 16, 36, 64]
-------------------------------
['jeff', 'rick', 'nick', 'matt']
-------------------------------
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
-------------------------------
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]
-------------------------------
[('a', 0), ('a', 1), ('a', 2), ('a', 3), ('b', 0), ('b', 1), ('b', 2), ('b', 3), ('c', 0), ('c', 1), ('c', 2), ('c', 3), ('d', 0), ('d', 1), ('d', 2), ('d', 3)]
[('a', 0), ('a', 1), ('a', 2), ('a', 3), ('b', 0), ('b', 1), ('b', 2), ('b', 3), ('c', 0), ('c', 1), ('c', 2), ('c', 3), ('d', 0), ('d', 1), ('d', 2), ('d', 3)]
-------------------------------
{'Bill Gates': ('CEO', 1, 1222), 'Barack Obama': ('Pres', 2, 12223), 'Donald Trump': ('Pres', 3, 122345), 'Jeff Bezos': ('CEO', 4, 1223346)}
-------------------------------
{2, 66, 4, 6, 8, 10, 12, 888, 8888, 90}
-------------------------------
1
1
1
1
1
1
1
1
1
1

Process finished with exit code 0 

Comments

Popular posts from this blog

Pyhton 3.4 - Continue

Conversations Ongoing: Valuing Modernization in Early European History

Conversations Ongoing: A Bunch of Amateurs