티스토리 뷰


Numbers

12/4 #= 3.0 ,실수형으로 반환한다.
18//4 #= 4 #몫
5 ** 3 #= (5 * 5 * 5) (지수)

변수설정은 변수명 = 값으로 한다.
tuna = 5
20 + tuna #=25


String


변수설정


String 을 위해서는 single quote ( ' ' ) 또는
double quote ( " " ) 를 사용한다.

string 에 quote 가 있을경우,
"I don't think she is 20"
'She said, "What part of the cow is the meatloaf from?"'
으로 타입이 다른 것으로 감싸주거나 
일반적으로 사용하는 escape (\) 를 사용하면된다.

string = 'string'
string = "I don't think she is 20"
string = 'I don\'t think she is 20'
string = 'She said, "What part of the cow is the meatloaf from?"'


print

print('hello world')
print('C:\Desktop\nature')
print(r'C:\Desktop\nature')
print('a' + 'b') # ab (2개의 string 함께 print 하기)
print('a' * 10) # aaaaaaaaaa (여러번 반복하고 싶을 때 * 사용가능)


print ('hello world') 

print ('C:\Desktop\nature') 하게되면 \n 때문에 개행되는데,

개행을 원치 않으면 r 을 써주면된다.

r 의 의미는 'raw' 로써 특별한 의미를 지니는 것이 없다는 것이다.



Substring

user = 'Barack Obama'
user[0] # -> 'B'
user[1] # -> 'a'
user[-1] # -> 'a' 가장 끝 글자
user[-2] # -> 'm' 뒤에서 2번째
user[2:9] # -> 'rack Ob' , 2 이상 9 미만
user[:9] # -> 'Barack Ob', 0 이상 9 미만
user[2:] # -> 'rack Obama', 2 이상부터 끝까지
user[:] # -> 'Barack Obama' , 전체!
len(user) # -> 12, string 길이를 알려준다

음수를 붙이게 되면 오른쪽부터 사용된다.

일반적으로 -0 은 null or \n 이 존재한다.


범위로 설정하기 위해선 : (콜론) 을 사용한다.



List

players = [29, 55, 52, 61, 78, 87]
players[2] = 68 # -> [29, 55, 68, 61, 78, 87]
players + [90, 91, 98] # -> [29, 55, 68, 61, 78, 87, 90, 91, 98], 실제로 list 가 변경되지는 않는다.
players.append(120) # -> [29, 55, 68, 61, 78, 87, 120], 실제 변경을 위해서는 append 를 사용한다.
players[:2] # -> [29, 55] , index 2미만의 배열 반환
players[:2] = [0, 0] # -> [0, 0, 68, 61, 78, 87, 120], index 2 미만의 배열 설정
players[:2] = [] # -> [68, 61, 78, 87, 120], index 2 미만의 배열 삭제
players[:] = [] # -> [] , 초기화

리스트는 하나의 스택이라고 생각해도 된다.
students = [54, 79, 93, 100]
students.pop() # -> 100, student = [54, 79, 93]
students.pop(1) # -> 79, student = [54, 93]
students.append(88) # -> student = [54, 93, 88]
students[3:6] = [77, 80, 63] # -> student = [54, 93, 88, 77, 80, 63]
score = students.pop(1) # -> score = 93



조건문 (If - Else)

name = "Scarlett Johansson"

if name is "Jason":
print("Hey there Jason!")
elif name is "Lucy":
print("What's up Lucy?")
else:
print("Plase sing up for the site!")
if 문 시작은 콜론으로 하고,
반드시 Indentation (들여쓰기) 가 되어야한다.

else if 는 elif 로 사용하고 else 는 else 이다.
마찬가지고 시작은 콜론이고 if 문 내부는 들여쓰기가 되어야한다.

String 비교는 "==" 등호 2개로도 가능하다.


반복문 (For, Range, While)

For 문

foods = ['bacon', 'beef', 'spam']

for f in foods:
print(f) # -> bacon, beef, spam

for f in foods[:2]:
print(f) # -> bacon, beef

range 문


range() 는 괄호안의 범위까지를 의미한다. 
3개를 사용하면, range(시작값, 끝값, 간격) 을 의미한다.

for number in range(10):
print(number) # -> 0, 1, 2, ..., 9

for number in range(1, 10):
print(number) # -> 1, 2, 3, 4, ..., 9

for number in range(1, 10, 2)
print(number) # -> 1, 3, 5, 7, 9

for number in range(10)
print(str(number) + " Hello world") # -> 0 Hello world, 1 Hello world, ..., 9 Hello world

while

times = 5

while times < 10:
print(times) # -> 5, 6, 7, 8, 9
times += 1


모든 반복문에서 break, continue 를 사용할 수 있다.


Comment

한줄 주석은 #
여러줄 주석은 ''' (3 single quote) 를 사용하면 된다.
# This program finds a magic number.
'''
Assignment.
'''




출처

http://creativeworks.tistory.com/m/category/Programming/Python%20Tutorials 의 1~10 장







공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함