text1 = 'In this world nothing can be said to be certain except death and taxes'
len(text1)
text2 = text1.split(' ')
len(text2)
text2
[w for w in text2 if len(w) > 5]
[w for w in text2 if w.endswith('d')]
[w for w in text2 if w.startswith('d')]
[w for w in text2 if w.startswith(('d','n'))]
text3 = 'to be or not to be'
text4 = text3.split(' ')
text4
text5 = set(text4)
len(text5)
text5
import time
import random
NUMBER_OF_ELEMENTS = 10000
# Create a list
lst = list(range(NUMBER_OF_ELEMENTS))
random.shuffle(lst)
# Crt=eate a set from the list
s = set(lst)
# Test if an element is in the set
start = time.time()
for i in range(NUMBER_OF_ELEMENTS):
i in s
end = time.time()
print(f'To test if {NUMBER_OF_ELEMENTS} elements are in the set, runtime is {end-start} seconds')
# Test if an element is in the list
start = time.time()
for i in range(NUMBER_OF_ELEMENTS):
i in lst
end = time.time()
print(f'To test if {NUMBER_OF_ELEMENTS} elements are in the list, runtime is {end-start} seconds')
'Python' in 'Python is good'
'PYTHON IS GOOD'.isupper()
'Python is good'.isupper()
'python is good'.islower()
'Python is good'.islower()
'Python Is Good'.istitle()
'Python is good'.istitle()
'000002'.isdigit()
'SZ000002'.isdigit()
'Textmining'.isalpha()
'Text mining1'.isalpha()
'SZ000002'.isalnum()
'SZ_000002#'.isalnum()
s1 = 'python is good'
s2 = s1.upper()
s2
s2.lower()
s1.capitalize()
s1.title()
s3 = 'cattcatt'
s4 = s3.split('a')
s4
'a'.join(s4)
list(s3)
[c for c in s3]
'{} {}'.format('hello', 'world')
'{} {}'.format(24, 'seconds')
24 + 'seconds'
name = 'Eric'
age = 74
f'Hello, {name}. You are {age}.'
s5 = ' a quick brown fox jumped over the lazy dog '
s6 = s5.strip()
s6
s6.replace('o', 'O')
s6.replace('o', 'O', 2)
intab = 'aeiou'
outtab = '12345'
table = str.maketrans(intab, outtab)
s7 = 'aeiou-xmppp'
s7.translate(table)
table_1 = str.maketrans(intab, outtab, 'xm')
s7.translate(table_1)
s = 'Hello World!'
s[4]
s[-3]
s[1:5]
s[:5]
s[-4:-1]
s[-2:]
s[:5]
s[:5:1]
s[:5:2]
s[::-1]
s[-1:-7:-2]
s.find('o')
s.find('or')
import csv
with open('test.csv', 'w', encoding='utf8', newline='') as wf:
writer = csv.writer(wf)
writer.writerow(('张三','北京'))
with open('test.csv', 'r', encoding='utf8') as rf:
r = csv.reader(rf)
for row in r:
print(f'姓名:{row[0]}, 住址:{row[1]}')
def change_char(str1):
### START CODE HERE ###
### END CODE HERE ###
return str1
# Check your function
print(change_char('restart'))
print(change_char('text'))
resta@t
tex@
def arrange_chars(str1):
### START CODE HERE ###
### END CODE HERE ###
return str1
# Check your function
print(arrange_chars('PyNaTive'))
print(arrange_chars('OpTYabi'))
yaivePNT
pabiOTY
def find_longest_word(words_list):
longest_word = ''
max_len = 0
### START CODE HERE ###
### END CODE HERE ###
return longest_word
# Check your function
print(find_longest_word(["Python", "Text", "Analysis"]))
Analysis