Python 문법을 remind하기 위한 코드 (Anaconda)
# 추가해야 할 내용들 더 있음
import numpy as np
import matplotlib.pyplot as plt
#x - np.arange(0, 6, 0.1)
#y1 = np.sin(x)
#y2 = np.cos(x)
#plt.plot(x, y1, label="sin")
#plt.plot(x, y2, linestype="--", label="cos")
#plt.xlabel("x")
#plt.ylabel("y")
#plt.tilte('sin & cos')
#plt.legend()
x = np.arange(0, 10**5)
y = x / np.log(x)
plt.plot(x, y)
#################################################
plt.show()
class myclass:
__num = 0 # private 변수
name = '' # class 변수
def setname(self, n):
self.name = n # class변수를 사용하기 위해서는 이렇게
name = n + n # 이 name은 local 변수dla
def count(self):
myclass.__num += 1
def val(self):
return myclass.__num
@classmethod
def rest(self):
myclass.__num = 0
class SchoolMember:
'''Represents any school member.'''
def __init__(self, name, age):
self.name = name
self.age = age
print ('Initialized SchoolMember: {}'.format(self.name))
def tell(self):
'''Tell my details.'''
print ('Name:"{}" Age:"{}"'.format(self.name, self.age))
class Teacher(SchoolMember):
'''Represents a teacher.'''
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print ('Initialized Teacher: {}'.format(self.name))
def tell(self):
SchoolMember.tell(self)
print ('Salary: "{:d}"'.format(self.salary))
class Student(SchoolMember):
'''Represents a student.'''
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print ('Initialized Student: {}'.format(self.name))
def tell(self):
SchoolMember.tell(self)
print ('Marks: "{:d}"'.format(self.marks))
t = Teacher('Mrs. Shrividya', 40, 30000)
s = Student('Swaroop', 25, 75)
# prints a blank line
print('')
members = [t, s]
for member in members:
# Works for both Teachers and Students
member.tell()
import pickle
f = open('member', 'wb')
pickle.dump(members, f) # 여러자료를 순차적으로 dump한 후 순차적으로 load해서 사용 가능
fclose(f)
# import this
# { 'name' : 'John' } dictionary
# { 1, 2, 3 } Set - & = intersection, | = union
# [] list (indexable, sequential)
# () tuple (static, indexable, sequential)
# string string (static, indexable, sequential) - in, x.find(), x.join(), x.startswith()
# a[-1] : 마지막 항목
# a[:-1] : 마지막 항목 이전까지
# a[:x] : index x 이전까지 (index는 0부터)
# a = b ref를 copy (a는 b의 또다른 ref)
# a = b[:] value를 copy
class myclass:
__i = 0
def func(self):
pass
#exit() # test end
import sys
if __name__ == '__main__':
print('This program is being run by it self')
else:
print('This program is being imported from another module')
print('\n\n\n')
print('The command line arguments are : ')
for i in sys.argv:
print(i)
print('\n\nThe PYTHONPATH is ', sys.path, '\n')
def varargs(init = 5, *num, **key):
'''
This is docstring test
looks good?
very good
'''
print('init = ', init)
print(num)
print(key)
print(varargs.__doc__)
varargs()
varargs(1,'2',2,3,3,a=4,b=5,c=6)
print('\n\n\n')
number = 23
try:
guess = int(input('Enter an integer : '))
if guess == number:
print('Right !!!')
elif guess < number:
print('It is little higer than that')
else:
print('It is little lower than that')
except:
print('Something wrong !!!!!!!!!!')
print('Done')
for i in range(1, 5):
print('in the loop', i)
if(i == 3):
break
else:
print('out of the loop', i)
while i < 10:
print('in the while', i)
i += 1
else:
print('out of the while', i)
def test_global():
global number
number = number + 1
print('hello world')
print('in function', number)
test_global()
print('out of function', number)