Python The Hard Way (3rd ed.)

Learn Python
The Hard Way. 3rd Edition.
http://learnpythonthehardway.org/book

Exercise 32: Loops and Lists
Exercise 33: While Loops

def is_prime(n):
if n < 2:
prime_status = False
else:
prime_status = True
for x in range(2, n):
if n % x == 0:
prime_status = False
break
return prime_status

break statements on loops
https://docs.python.org/3.6/tutorial/controlflow.html

==========================

def rev erse(a_string):
    new_list = “”
    for i in range(len(a_string)):
        new_list = new_list + a_string[len(a_string) – i – 1]
    return new_list

=========================

def anti_vowel(a_string):
    new_list = “”
    for i in range(len(a_string)):
        if not (a_string[i] in “aeiouAEIOU”):
            new_list = new_list + a_string[i]
    return new_list

========================

def scrabble_score(word):
    puntos = 0
    word = word.lower()
    for i in range(len(word)):
        puntos += score[word[i]]
    return puntos

=========================

def censor(text, word):
while (word in text):
lista = text.partition(word)
text = lista[0] + “*” * len(word) + lista[2]
return text

def censor2(text, word):
while (word in text):
lista = text.partition(word)
text = (“*” * len(word)).join([lista[0], lista[2]])
return text

==========================

def count(sequence, item):
    contador = 0
    for i in sequence:
        if item == i:
            contador +=1
    return contador

==========================

def purify(list_of_numbers):
    new_list = []
    for elemento in list_of_numbers:
        if (elemento % 2) == 0:
            new_list.append(elemento)
    return new_list

===========================

def product(list_of_integers):
    temporal = 1
    for i in list_of_integers:
        temporal = temporal * i
    return temporal

===========================

def remove_duplicates(lista):
    new_list = []
    for i in lista:
        if not (i in new_list):
            new_list.append(i)
    return new_list

===========================

def median(sequence):
if len(sequence) == 1:
return sequence[0]
sequence = sorted(sequence)
if (len(sequence) % 2) == 0:
n1 = sequence[(len(sequence) // 2) – 1]
n2 = sequence[len(sequence) // 2]
return (n1 + n2)/2
else:
return sequence[(len(sequence) // 2)]

badge_practice_makes

==========================

>>> listaA = [[]] * 3
>>> listaA
[[], [], []]
>>> listaB = [[] for i in range(3)]
>>> listaB
[[], [], []]
>>> listaA == listaB
True
>>> listaA[0].append(3)
>>> listaA
[[3], [3], [3]]
>>> listaB[0].append(3)
>>> listaB
[[3], [], []]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s