1.3. Python Built-In Methods#

from itertools import chain
# concatenate ranges
new_range = chain(range(5), range(5, 10))
for num in new_range:
    print(num, end=" ")
0 1 2 3 4 5 6 7 8 9 
# some consonants
consonants = ["d", "f", "k", "l", "n", "p"]
# some vowels
vowels = ["a", "e", "i", "o", "u"]
# resultatnt list
res = list(chain(consonants, vowels))

# sorting the list
res.sort()
print(res)
['a', 'd', 'e', 'f', 'i', 'k', 'l', 'n', 'o', 'p', 'u']
li = ["123", "456", "789"]

res = list(map(int, list(chain.from_iterable(li))))

sum_of_li = sum(res)

print("res =", res, end="\n\n")
print("sum =", sum_of_li)
res = [1, 2, 3, 4, 5, 6, 7, 8, 9]

sum = 45
a = {"a", "b"}
set(["a", "b"])

b = {"b", "c"}
b
set(["c", "b"])

a |= b
a
{'a', 'b', 'c'}

Working with text

In Python, you can create file objects with the built-in function open. When you call open, you need to specify the file name and how to open the file:

  • f = open (filename,method)
  • A filename is the name of a file that exists on disk. Common ways to open a file are:
  • "r"   # to read an existing file
  • “w” # create a new file and write
  • “a” # if the file exists, write to the end of the file. If the file does not exist, a new file is created and written”
  • import os
    
    f = open("harrypotter.txt", "w")
    f.write("This is hogwarts")
    f.write(" This is the chamber of secrets \n")
    f.close()
    
    f = open("harrypotter.txt", "r")
    content = f.read()
    print(content)
    f.close()
    
    This is hogwarts This is the chamber of secrets 
    
    f = open("Ron.txt", "w")
    print(f.closed)
    f.write("I love Quidditch")
    f.close()
    print(f.closed)
    
    False
    True
    
    # delete contents of a file
    open("Ron.txt", "w").close()
    open("harrypotter.txt", "w").close()
    
    os.remove("Ron.txt")
    os.remove("harrypotter.txt")
    

    Using context manager

    with open("readme.txt", "w") as f:
        f.write("Create a new text file!")
    
    # delete file
    open("readme.txt", "w").close()
    
    with open("new.txt", "w") as f:
        f.write("Hello World!")
    print(f.closed)
    
    True
    
    with open("new.txt", "r") as f:
        print(f.read())
    
    Hello World!
    
    # delete file
    open("new.txt", "w").close()
    
    os.remove("readme.txt")
    os.remove("new.txt")
    

    Pickle

    The first step is to grab the object’s data out of memory and convert it into an ordered text, called serialization.

    The second step is to save the text to a file.

    import pickle
    
    class Animal(object):
        have_trunk = True
        howtheyreproduce = "zygote"
    
    
    # step 1: converting to string
    winter = Animal()
    pickle_string = pickle.dumps(winter)
    
    # step 2: saving as a text
    with open("winter.pkl", "wb") as f:
        f.write(pickle_string)
    
    # step 1: converting to string
    winter = Animal()
    pickle_string = pickle.dumps(winter)
    
    # all at once
    with open("winter.pkl", "wb") as f:
        pickle.dump(winter, f)
    
    # reading a file
    with open("winter.pkl", "rb") as f:
        winter = pickle.load(f)
    print(winter.have_trunk)
    
    True
    
    os.remove("winter.pkl")