본문 바로가기
파이썬/개념정리

[Python] 개념 정리 # 1 (feat. 나도코딩)

by 로토마 2021. 6. 26.

연산자)

+  더하기

빼기

곱하기

/  나누기

% 나머지

 

복합대입연산자)

+=          -=          *=          /=         %=

 

외장함수)

from math import *

print(floor(4.99))  # 내림 --> 4

print(ceil(3.14))    # 올림 --> 4

print(sqrt(16))      # 루트 --> 4

 

from random import *

print(random())                  # 0.0~1.0 미만의 임의의 값 생성

print(random() * 10)            # 0.0~ 10.0 미만의 임의의 값 생성

print(int(random() * 10)        # 0~10 미만의 임의의 값 생성

print(int(random() * 10) + 1)  # 1~10 이하의 임의의 값 생성

print(randrange(1,46))           # 1~46 미만의 임의의 값 생성

print(randint(1,45))               # 1~45 이하의 임의의 값 생성

 

퀴즈)

이 문제의 핵심 포인트!!

- random 함수 불러오기

- 1~28의 수를 임의로 뽑을 수 있는 방법 구상하기

- 출력하기

 

코드 답) 

from random import *
num =  randint(1,28)
print("오프라인 스터디 모임 날짜는 매월 %d 일로 선정되었습니다." %num)

 

슬라이싱)

rotoma = "012345-6789123"

0  1  2  3  4  5  -  6  7  8   9   1   2   3    변수

0  1  2  3  4  5  6  7  8  9  10  11 12  13   위치 번호 (중요!! 0부터 샌다!!)

                                   . . .  -3  -2  -1   마이너스(-) 로도 표시할 수 있다.

               

print(rotoma[3])        #3자리의 수 -->3

print(rotoma[0:2])      #0~2직전까지 -->01

print(rotoma[4:6])      #4~6직전까지 -->45

print(rotoma[:6])        #처음부터 6직전까지 -->012345

print(rotoma[7:])        #7부터 끝까지 -->6789123

print(rotoam[-7:])       #맨 뒤에서 7번째부터 끝까지 -->6789123

 

문자열 처리 함수)

rotoma = "Rotoma is Amazing"

print(rotoma.lower())         # 문자열 소문자 출력 --> rotoma is amazing

print(rotoma.upper())         # 문자열 대문자 출력 --> ROTOMA IS AMAZING

print(rotoma[0].isupper())    # 0자리의 문자가 대문자인가? -->TRUE

print(len(rotoma))              # rotoma의 길이 --> 17

print(rotoma.replace("Rotoma","Coding"))     #Rotoma를 Coding으로 바꿔라 --> Coding is Amazing

 

index = rotoma.index("a")   # a가 있는 위치의 자리 

print(index)                      #   --> 5

index = rotoma.index("a",index+1)  # 시작위치가 index+1이니까 뒤에 있는 a의 자리를 찾아라               

print(index)                                #  --> 12

 

print(rotoma.count("a"))           # a가 나오는 개수 --> 2

print(rotoma.find("Rotoma"))     # Rotoma의 시작위치를 찾아라 --> 0

 

# 만약 없는 문자를 넣으면??

print(rotoma.find("Coding"))      # -1 출력

print(rotoma.index("Coding"))    # 오류 발생

 

문자열 포멧)

# 방법 1

print(" I am %d years old" %20)             #정수 형태 %d --> I am 20 years old.

print(" my name is %s." %rotoma)           #문자열 형대 %s --> my name is rotoma.

print(" i love %s and %s." %(wind,moon))  # 2개 이상 사용시 --> i love wind and moon.

 

# 방법 2

print(" I am {} years old".format(20))                   #  {}와 .format()사용  --> I am 20 years old.

print(" my name is {}.".format("rotoma"))              # {} 와 .format("") 사용 --> my name is rotoma.

print(" i love {0} and {1}." .format("wind","moon"))  # 2개 이상 사용시  --> i love wind and moon.

 

#방법 3

age = 20

name = rotoma

print(" I am {name} and {age} years old." )   # I am  rotoma and 20 years old. 

 

#방법 4

age = 20

name = rotoma

print(f" I am {name} and {age} years old." )  # I am  rotoma and 20 years old. 

# f 빠뜨리지 말기!!

탈출 문자)

# \

-줄바꿈시 - print("\n")

-따옴표 안의 따옴표를 출력하고 싶을 때

    print(" I love word \"love\".")   # --> I love word "love".

-\를 출력하고 싶을때

    print("C:\\Users\\Rotoma\\Desktop")  # --> C:\Users\Rotoma\Desktop

 

# \r

-커서를 맨 앞으로 이동

print("Red Apple\rPine")   #PineApple

 

#\b 

-백스페이스 (한 글자 삭제)

print("Redd\bApple")     # RedApple

 

# \t

-탭 

print("Red\tApple")    #Red    Apple