-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonGrammar.txt
More file actions
96 lines (81 loc) · 1.75 KB
/
pythonGrammar.txt
File metadata and controls
96 lines (81 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
[list]
list = [1,2,3]
list.count(3)-1
list.index(3)-2
list.reverse()
list.clear()
list.extend([1,2]) - [1,2,3,1,2]
list.insert(1,5) - [1,1,2,3]
del list[1]-[1,3]
list.remove(1)-[2,3]
[1,2]+[3,4]-[1,2,3,4]
[1]*4-[1,1,1,1]
["hello","world].join(' ')-"hello world"
[tuple]
a=(1,2) a[0]=2 불가
[str]
str ="hello,world"
str.split(',')-['hello','world']
str.count('l')-3
str.capitalize()-"Hello,world"
"hello world".title()-"Hello World"
"hi ".rstrip()-"hi"
"{0:5s}".format(str)
"{0:>5,.2%}".format
print("3".rjust(3))
[bool]
"hi".isdigit()
"hi".isalpha()
"hi".isalnum()
"hi".islower()
"hi".isupper()
"hi".isspace()
[set]
s1={"egg","hi"}, s2={"egg","bye"}
ss3 =set()
ss1.add('egg')-{"egg", "hi"}
ss1.discard('egg')-{"hi"}
ss1.clear()-{}
set([1,2])-{1,2}
s1.union(s2)-{"egg","hi","bye"}
s1.intersection(s2)-{"egg"}
s1.difference(s2)-{s1-s2}
[dict]
di = {1:'a' , 2:'b'}
len(di) -2
1 in di - true
di[1]='z' - {1:'z', 2:'b'}
di.get(3,'h')-'h'
di.keys() - [1,2]
di.items() - [(1,'a'), (2,'b')]
di.values() - ['a','b']
del di[1] - {2:'b'}
set(di) - key의 셋
tuple(di)- key의 튜플
list(di) - key의 리스트
di1.update(di2) di1우선
comprehension
file = [1,1,2,3]
[i*2 for i in file] - [2, 2, 4, 9]
{i*2 for i in file} - {2, 3, 9}
{i:i**2 for i in file} - {1:1, 2:4, 3:9}
[class]
class Rect:
def __init__(self, height=1, width=2):
self.height = height
self.width = width
def setWidth(self, width):
self.width = width
def __str__(self):
return("width: "+str(self.width)+ "\nheight: ")
class Square(Rect):
def __init__(self, height=2):
self.height=height
self.width=width
[pickle library]
file = open("input.dat","rb")
a = pickle.load(file)
file = open("result.dat","wb")
pickle.dump(a,file)
[random library]
random.choice([1,2,3,4,5]) - 1~5값 랜덤하게 리턴