martes, 27 de mayo de 2025

iterating

 >>> i=0

>>> while i<len(a.keys()):

...    print(a[list(a.keys())[i]])

...    i=i+1

... 

Ambiorix

43

195

---------

for key in a:
    print(a[key])

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

for value in a.values():
    print(value)


domingo, 6 de abril de 2025

Pyhon Exception

 try:

    result = 10 / a

except Exception as e:

    print(f"An unexpected error occurred: {e}")

else:

    print("Codigo funciona")

finally:

    print("Siempre corre")

domingo, 16 de marzo de 2025

python generators

# Online Python compiler (interpreter) to run Python online.

# Write Python 3 code in this online editor and run it.



def  test(i):

    n=0

    while n<i:

        if n==3:

            yield 'We reached the limit'

        else:

            

          yield n

        n=n+1


m=test(24)

print(next(m))

print(next(m))

print(next(m))

print(next(m))


///////

0

1

2

We reached the limit

jueves, 13 de febrero de 2025

while and if

 i=0

m=True

while m:

    print(i)

    i+=1

    if i==10:

        m=False


0

1

2

3

4

5

6

7

8

9


find missing number from a list range

 l1=[1,2,5,6]

l2=[7, 1, 3, 5, 6]

def find_missing_number(l1):

    max_n=max(l1)+1

    min_n=min(l1) 

    c=[a for a in range(min_n,max_n) ]

    missing_n=[a for a in c if a not in l1]

    return missing_n



print(find_missing_number(l1))

print(find_missing_number(l2))


[3, 4]

[2, 4]


miércoles, 22 de enero de 2025

using cases with dict

def mycase(v):

    d={"A":13,"B":14,"C":22}

    if v in d.keys():

        return d[v]

    else:

        return 0

print(mycase("C"))

lunes, 20 de enero de 2025

sábado, 4 de enero de 2025

Python Range

 >>> 

>>> [a for a in range(2,11,2) ]

[2, 4, 6, 8, 10]

>>> [a for a in range(2,11,2) if a>4]

[6, 8, 10]

>>> [a**2 for a in range(2,11,2) if a>4]

[36, 64, 100]

>>> 


filtering a list with other list

>>> l=[a for a in l if a not in ['Casa',0]]

>>> l

['Television', 10, 'Pistola']

 

>>> l=[a for a in l if a not in ['Casa',0]]

>>> l

['Television', 10, 'Pistola']