Colab

cheeses = ["adam", "bree", "cheddar", "rockfort"]
prices = [1.9, 2.45, 9.90, 6.60]

range(len(cheeses))

range(0, 4)
{cheeses[i] : prices[i]  for i in range(4)}

{'adam': 1.9, 'bree': 2.45, 'cheddar': 9.9, 'rockfort': 6.6}
nums = [2, -6, 4, 100, -9, 81]

import math
[math.sqrt(x) for x  in nums if x>=0]

[1.4142135623730951, 2.0, 10.0, 9.0]
newlist = []
for i in range(1, 4):
    for j in range(1,4):
        newitem = f"{i} * {j} = {i*j}"
        newlist.append(newitem)
        
print(newlist)

['1 * 1 = 1', '1 * 2 = 2', '1 * 3 = 3', '2 * 1 = 2', '2 * 2 = 4', '2 * 3 = 6', '3 * 1 = 3', '3 * 2 = 6', '3 * 3 = 9']
[f"{i} * {j} = {i*j}" for i in range(1,4) for j in range(1,4)]

['1 * 1 = 1',
 '1 * 2 = 2',
 '1 * 3 = 3',
 '2 * 1 = 2',
 '2 * 2 = 4',
 '2 * 3 = 6',
 '3 * 1 = 3',
 '3 * 2 = 6',
 '3 * 3 = 9']
range(1000 * 1000 * 1000)

range(0, 1000000000)
x = list(range(1000* 1000 * 10))

f = open("myfile.txt")

print(f)

<_io.TextIOWrapper name='myfile.txt' mode='r' encoding='cp1252'>
lines = f.readlines()

type(lines)

list
gen = (2**i for i in range(8))

next(gen)


    ---------------------------------------------------------------------------

    StopIteration                             Traceback (most recent call last)

    <ipython-input-3-6e72e47198db> in <module>
    ----> 1 next(gen)
    

    StopIteration: 


for i in gen:
    print(i)
    
    

1
2
4
8
16
32
64
128
def infinite():
    i=0
    while True:
        #return i
        yield i
        i+=1


x = infinite()
print(x)

<generator object infinite at 0x05E8E7B0>
next(x)

7
def read_file_line_by_line_lazily(f):
    while True:
        line = f.readline().strip()
        yield line


f = open("myfile.txt")
lines = read_file_line_by_line_lazily(f)
non_empty_lines = (line for line in lines if len(line.strip()) > 0)
print(lines)


<generator object read_file_line_by_line_lazily at 0x0623A770>
for line in non_empty_lines:
    print(line)

I am Daniel
I am Sam
Sam I am
That Sam-I-am
That Sam-I-am!
I do not like
That Sam-I-am
Do you like
Green eggs and ham
I do not like them,
Sam-I-am.
I do not like
Green eggs and ham.
Would you like them
Here or there?
I would not like them
Here or there.
I would not like them
Anywhere.
I do not like
Green eggs and ham.
I do not like them,
Sam-I-am
Would you like them
In a house?
Would you like them
With a mouse?
I do not like them
In a house.
I do not like them
With a mouse.
I do not like them
Here or there.
I do not like them
Anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.
Would you eat them
In a box?
Would you eat them
With a fox?
Not in a box.
Not with a fox.
Not in a house.
Not with a mouse.
I would not eat them here or there.
I would not eat them anywhere.
I would not eat green eggs and ham.
I do not like them, Sam-I-am.
Would you? Could you?
In a car?
Eat them! Eat them!
Here they are.
I woould not,
Could not,
In a car
You may like them.
You will see.
You may like them
In a tree?
I would not, could not in a tree.
Not in a car! You let me be.
I do not like them in a box.
I do not like them with a fox
I do not like them in a house
I do mot like them with a mouse
I do not like them here or there.
I do not like them anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.
A train! A train!
A train! A train!
Could you, would you
On a train?
Not on a train! Not in a tree!
Not in a car! Sam! Let me be!
I would not, could not, in a box.
I could not, would not, with a fox.
I will not eat them with a mouse
I will not eat them in a house.
I will not eat them here or there.
I will not eat them anywhere.
I do not like them, Sam-I-am.
Say!
In the dark?
Here in the dark!
Would you, could you, in the dark?
I would not, could not,
In the dark.
Would you, could you,
In the rain?
I would not, could not, in the rain.
Not in the dark. Not on a train,
Not in a car, Not in a tree.
I do not like them, Sam, you see.
Not in a house. Not in a box.
Not with a mouse. Not with a fox.
I will not eat them here or there.
I do not like them anywhere!
You do not like
Green eggs and ham?
I do not
Like them,
Sam-I-am.
Could you, would you,
With a goat?
I would not,
Could not.
With a goat!
Would you, could you,
On a boat?