파이썬 - 4일차

2020. 1. 28. 18:24카테고리 없음

remodule

정규화된 표현식 -> .(점) =>

glob.glob('*') -> 현재폴더 밑에 있는 모든파일

 

re.match() 함수의 경우 시작하는 부분을 점검

re.search() 함수는 대상 문자열 전체에 대해서 검색을 수행한다.

 

>>> import re

>>> re.findall('app\w*', "application orange apple banana")
['application', 'apple']

>>> re.search('app', "application orange apple banana")
<re.Match object; span=(0, 3), match='app'>

 

>>> import random
>>> random.random()
0.6134354233690916
>>> random.random()
0.8770024061127576

>>> random.randrange(1, 7)
3
>>> random.randrange(1, 7)
6
>>> random.randrange(1, 7)
2

fd = open('t.txt', 'wt')
fd.close()

 

fd = open('t.txt', 'w')
print(fd.name)
print(fd.mode)

fd.write('hello\n')
fd.close()

 

import os

fd = open('t.txt', 'a')
for i in range(1, 11):
    data = "%d번째 줄입니다.\n" % i
    fd.write(data)
fd.close()
os.system('cat t.txt')

 

content = """Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!"""

f = open('t.txt', 'w')
f.write(content)
f.close()

fd = open('t.txt')
lines = fd.readlines()
print(lines)
for line in lines:
    print(line)
fd.close()

 

content = """Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!"""

f = open('t.txt', 'w')
f.write(content)
f.close()

fd = open('t.txt')
data = fd.read()
print(data)
fd.close()

import os
content = """Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!"""

fd = open('t.txt', 'w')
fd.write(content)
fd.close()

if os.path.exists('t.txt'):
    os.remove('t.txt')
    print('The file removed.')
else:
    print('THe file does not exist.')

 

import os rwㅈ
content = """반갑습니다. python 개발자 여러분
한살 더 드셨죠!
행복한 한해가 되었으면 합니다."""

if not os.path.exists('/test'):
    os.mkdir('/test')

f = open('/test/test.txt', 'w')
f.write(content)
f.close()

fd = open('/test/test.txt')
data = fd.read()
print(data)
fd.close()

if os.path.exists('/test/test.txt'):
    os.remove('/test/test.txt')
    print('The file removed.')
else:
    print('The file does not exist.')

 

class Singer:

    def sing(self):
        return "lalala~"

teaji = Singer()
print(teaji.sing())

 

class MyClass:
    x = 5

p1 = MyClass()
print(p1.x)

 

class Person:
    def __init__(self, name, age): // person이라는 class는 2개의 인자를 갖는구나
        self.name = name // self 뜻 -> 입력받은 값을 self 내에서 사용 
        self.age = age // self 뜻 -> 입력받은 값을 self 내에서 사용

 


    def __init__(self, name, age):
        self.name = name
        self.age = age

    def myfun(self):
        print("Hello my name is " + self.name)
        print("I'm 28 years old.")


p1 = Person('John', 28)
p1.myfun()

 

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def myfun(self):
        print("Hello my name is " + self.name)
        print("I'm %d years old." % self.age)


p1 = Person('John', 28)
p1.age = 29

print(p1.age)
p1.myfun()

 

class Person:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)


class Student(Person):
    pass


x = Person('Seoungchan', 'baik')
x.printname()

 

class Person:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)


class Student(Person):
    def __init__(self, fname, lname):
        super().__init__(fname, lname)
        self.graduationyear = 2019


x = Student('Seoungchan', 'baik')
print(x.graduationyear)

 

class Person:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)


class Student(Person):
    def __init__(self, fname, lname, year):
        super().__init__(fname, lname)
        self.graduationyear = year

    def welcome(self):
        print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)

x = Student('Seoungchan', 'baik', 2019)
x.welcome()

 

Person.py

class Person:
    eyes = 2
    nose = 1
    mouth = 1
    ears = 2
    arms = 2
    legs = 2

    def eat(self):
        print("냠냠")

    def sleep(self):
        print("쿨쿨")

    def talk(self):
        print("주저리 주저리...")

 

Student.py

from Person import Person
#import Person
#from Person import *

class Student(Person):
#class Student(Person.Person):
#class Student(Person):
    def study(self):
        print('열공모드...')

kim = Person()
print(kim.eyes)
kim.eat()

lee = Student()
print(lee.eyes)
lee.eat()
lee.study()