mylist = [1, 2, 3]
len(mylist)
mylist.append(4)
print(mylist)
Compare with modules:
import math
math.pi
math.sin(2)
dir(math)
help(math)
print(mylist)
dir(mylist)
help(mylist)
mylist.index(3)
dir(2)
(1000).bit_length()
2 + 3
(2).__add__(3)
mylist2 = [5, 6, 7]
mylist
mylist + mylist2
mylist.__add__(mylist2)
def f(n, m=5):
return n*m
f(10)
dir(f)
f.__defaults__
f.__defaults__ = (2, 7)
f(10)
f()
The open()
function opens a file. The argument "w" opens the file for writing. If a file with a specified name does not exist it will be created. If it exists its content will be overwritten.
myfile = open('test_file.txt', 'w')
s = "This is a test"
myfile.write(s)
myfile.close()
\n
is the new line character:
s = "hello\nthere"
print(s)
myfile = open('test_file.txt', 'w')
s = "Twinkle, twinkle little star\nHow I wonder what you are."
myfile.write(s)
myfile.close()
Open a file with the argument "a" to append to it:
myfile = open('test_file.txt', 'a')
myfile.write("Up above the world so high\nLike a diamond in the sky.")
myfile.close()
myfile = open('test_file.txt', 'r')
myfile.read(10)
A file object remembers its current position. The tell()
function can be used to get the current position in the file:
myfile.tell()
myfile.read(15)
myfile.tell()
The seek()
function can be used to change the current position in the file:
myfile.seek(0)
myfile.read(10)
read()
without argument reads the whole file starting at the current position:
myfile.seek(0)
myfile.read()
myfile.seek(0)
print(myfile.read())
readlines()
function returns a list of all lines of the file (starting from the current position):
print(myfile.read())
myfile.seek(0)
print(myfile.readlines())
myfile.seek(0)
lines = myfile.readlines()
lines[1]
myfile.close()
requests
is a module from communicationg with webpages:
import requests
ubmath = requests.get('http://www.math.buffalo.edu')
dir(ubmath)
print(ubmath.headers)
print(ubmath.text[:1000])
verne = requests.get('http://www.gutenberg.org/cache/epub/103/pg103.txt').text
verne[:1000]
Note: Microsoft Windows computers use \r\n
to encode the new line in text.
s = 'beginning\rEND'
print(s)
print(verne[:1000])
replace()
¶The replace function replaces a substring in a string with another substring:
poem = "Twinkle, twinkle, little star\nHow I wonder what you are"
print(poem)
new_poem = poem.replace('\n', ' ***NEW_LINE*** ')
print(new_poem)
twinkle = poem.replace('\n', ' ')
print(twinkle)
split()
¶The split()
function splits a string along all occurences of specified substring and returns a list of the strings obtained in this way:
print(twinkle)
words = twinkle.split(' ')
print(words)
parts = twinkle.split('tt')
print(parts)
s = "This is a test"
print(s.split(' '))
print(s.split())
join()
¶The join()
function joins a list of strings:
print(words)
'__'.join(words)
' '.join(words)
''.join(words)
find()
¶print(twinkle)
'little star' in twinkle
'little duck' in twinkle
The find()
function is searching for a substring in a string. It the substring is found it returns the index where the substring starts. If it is not found it returns -1.
found = twinkle.find('little star')
print(found)
twinkle.find('t')
verne.find('ocean')
print(verne[40489: 40489+300])
verne_lines = verne.split('\r\n')
verne_lines[300]
for line in verne_lines:
if line.find('ocean') != -1:
print(line + '\n')
for line in verne_lines:
if line.find('Ocean') != -1:
print(line + '\n')
The lower()
function returns a string converted to lowercase. The upper()
returns a string converted to upper case:
s = 'This is Bob.'
print(s.lower())
print(s.upper())
for line in verne_lines:
lowline = line.lower()
if lowline.find('ocean') != -1:
print(line + '\n')
The ord()
function returns the ascii code of a character:
msg = 'Secret!!!'
for c in msg:
print(ord(c))
The chr()
function converts ascii codes into corresponding characters:
mylist = [83, 101, 99, 114, 101, 116, 33, 33, 33]
for n in mylist:
print(chr(n))
charlist = [chr(n) for n in mylist]
print(charlist)
''.join(charlist)