target = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = []
def is_even(n):
return True if n % 2 == 0 else False
for value in target:
if is_even(value):
result.append(value)
print(result)
# ๋ฐฉ๋ฒ 1
c.execute("DELETE FROM users WHERE id = ?", (2,))
# ๋ฐฉ๋ฒ 2
c.execute("DELETE FROM users WHERE id = :id", {'id' : 5})
# ๋ฐฉ๋ฒ 3
c.execute("DELETE FROM users WHERE id = '%s'" % 4)
with open('./resource/sample2.csv', 'r') as f:
reader = csv.reader(f, delimiter='|')
# next(reader) # Header ์คํต
# ํ์ธ
print(reader)
print(type(reader))
print(dir(reader))
print()
for c in reader:
print(c)
with open('./resource/sample1.csv', 'r') as f:
reader = csv.DictReader(f)
for c in reader:
for k, v in c.items():
print(k, v)
print("------------------")
import time
print(time.time())
print(time.month())
1627887313.5666506 Traceback (most recent call last): File "c:/Users/./Desktop/python_study/python_easy/section10.py", line 45, in <module> print(time.month()) AttributeError: module 'time' has no attribute 'month'
(7) ValueError
x = [1, 5, 9]
x.remove(10)
ValueError: list.remove(x): x not in list
x.index(10)
ValueError: 10 is not in list
(8) FileNotFoundError
f = open('test.txt', 'r') # ์์ธ ๋ฐ์
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
(9) TypeError
x = [1, 2]
y = (1, 2)
z = 'test'
print(x + y)
TypeError: can only concatenate list (not "tuple") to list
print(x + z)
TypeError: can only concatenate list (not "str") to list
name = ['Kim', 'Lee', 'Park']
try:
z = 'Kim' # Cho : ์์ธ ๋ฐ์
x = name.index(z)
print('{} Found it! in name'.format(z, x + 1))
except ValueError:
print('Not found it! - Occured ValueError!')
else:
print('OK! else!')
Kim Found it! in name
OK! else!
name = ['Kim', 'Lee', 'Park']
try:
z = 'Cho'
x = name.index(z)
print('{} Found it! in name'.format(z, x + 1))
except ValueError:
print('Not found it! - Occured ValueError!')
else:
print('OK! else!')
try:
z = 'jim'
x = name.index(z)
print('{} Found it! in name'.format(z, x + 1))
except:
print('Not found it! - Occured Error!')
else:
print('OK! else!')
Not found it! - Occured Error!
- ์์ 3
try:
z = 'Kim'
x = name.index(z)
print('{} Found it! in name'.format(z, x + 1))
except:
print('Not found it! - Occured Error!')
else:
print('OK! else!')
finally:
print("finally ok !")
f = open('./resource/review.txt', 'r')
content = f.read()
print(content)
print(dir(f))
# ๋ฐ๋์ close ๋ฆฌ์์ค ๋ฐํ
f.close()
The film, projected in the form of animation, imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, which eventually paves the path for gaining a fresh perspective on an age-old problem. The story also happens to centre around two parallel characters, Shundi King and Hundi King, who are twins, but they constantly fight over unresolved issues planted in their minds by external forces from within their very own units.
with open('./resource/review.txt', 'r') as f:
for c in f:
print(c.strip())
The film, projected in the form of animation, imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, which eventually paves the path for gaining a fresh perspective on an age-old problem. The story also happens to centre around two parallel characters, Shundi King and Hundi King, who are twins, but they constantly fight over unresolved issues planted in their minds by external forces from within their very own units.
with open('./resource/review.txt', 'r') as f:
content = f.read()
print('>>>', content)
content = f.read() # ๋ด์ฉ ์์
print('>>>', content)
>>> The film, projected in the form of animation, imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, which eventually paves the path for gaining a fresh perspective on an age-old problem. The story also happens to centre around two parallel characters, Shundi King and Hundi King, who are twins, but they constantly fight over unresolved issues planted in their minds by external forces from within their very own units. >>>
with open('./resource/review.txt', 'r') as f:
line = f.readline()
# print(line)
while line:
print(line, end='####')
line = f.readline()
The film, projected in the form of animation, ####imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, ####which eventually paves the path for gaining a fresh perspective on an age-old problem. ####The story also happens to centre around two parallel characters, Shundi King and Hundi King, ####who are twins, but they constantly fight over unresolved issues planted in their minds ####by external forces from within their very own units.####
readline()์ ํ์ค์ฉ ๋ฐํ ํด ์ค๋ค.
- ์์ 6 : readlines()
with open('./resource/review.txt', 'r') as f:
contents = f.readlines()
print(contents)
for c in contents:
print(c, end=" ****** ")
['The film, projected in the form of animation,\n', 'imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues,\n', 'which eventually paves the path for gaining a fresh perspective on an age-old problem.\n', 'The story also happens to centre around two parallel characters, Shundi King and Hundi King,\n', 'who are twins, but they constantly fight over unresolved issues planted in their minds\n', 'by external forces from within their very own units.'] The film, projected in the form of animation, ****** imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, ****** which eventually paves the path for gaining a fresh perspective on an age-old problem. ****** The story also happens to centre around two parallel characters, Shundi King and Hundi King, ****** who are twins, but they constantly fight over unresolved issues planted in their minds ****** by external forces from within their very own units. ******
score = []
with open('./resource/score.txt', 'r') as f:
for line in f:
score.append(int(line))
print(score)
print('Average : {:6.3}'.format(sum(score)/len(score)))
class Fibonacci:
def __init__(self, title='fibonacci'):
self.title = title
def fib(n): # ํผ๋ณด๋์น ์ถ๋ ฅ
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a + b
print()
def fib2(n): # ํผ๋ณด๋์น ๋ฆฌ์คํธ ๋ฆฌํด
result = []
a, b = 0, 1
while a < n:
result.append(a)
a, b = b, a + b
return result