IT/Python3
파이썬 기본문법
어센트
2021. 12. 10. 14:41
숫자 자료형
print('ss')
animal = "강아지"
age = 4
is_adult = age >= 3
print("우리집"+animal+"는"+str(age)+"살")
print(animal, "는", age, "살") # ,를 이용하면 빈칸이 들어간다.
print("우리집 강아지는 어른인가요? " + str(is_adult))
# station = input()
# print(station, "행 열차가 들어오고 있습니다.")
연산
print(1+1)
print(3-2)
print(5*2)
print(6/3)
print(2**3) # 2^3
print(5 % 3)
print(10 % 3)
print(10//3)
print(5//3)
print(3 == 3)
print(3+4 == 7)
print(3 != 4)
print((3 > 0) and (3 < 5))
print((3 > 0) & (3 < 5))
print((3 > 0) or (3 > 5))
print((3 > 0) | (3 < 5))
print(5 > 4 > 3)
print(5 > 4 > 7)
수식 연산
print(2+3*4)
print((2+3) *4)
number = 2+3*4
print(abs(-5))
print(pow(4,2))
print(max(5,12))
print(min(5,12))
print(round(3.14))#반올림
print(round(4.99))
from math import *
print(floor(4.99))
print(ceil(3.14))
print(sqrt(16))
랜덤함수
from random import *
# print(random()) #0.0~1.0 미만의 임의의 값 생성
# print(random()*10) #0.0 부터 10.0 미만의 임의의 값 생성
# print(int(random()*10))
# print(int(random()*10))
# print(int(random()*10))
# print(int(random()*10)+1)#1부터 10이하의 임의의 값
# print(int(random()*45)+1)#1-45 이하의 임의의 값 생성
# print(int(random()*45)+1)#1-45 이하의 임의의 값 생성
# print(int(random()*45)+1)#1-45 이하의 임의의 값 생성
# print(randrange(1,46)) # 1-45까지 임의의 값 생성
# print(randint(1,45)) # 1-45까지의 임의의 값 생성
print("오프라인 스터디 모임 날짜는 매월 "+str(randint(4,28))+"일로 선정되었습니다.")
lst = range(1,21) #1부터 20까지 숫자를 생성
lst = list(lst) #리스트로 타입 변환
shuffle(lst)
print(lst)
winner = sample(lst,4)
print(type(winner))
print('치킨 당첨자:{0}'.format(winner[0]))
print('커피 당첨자:{0}'.format(winner[1:]))
슬라이싱
탈출문자
print("백문이 불여일견 \\n 백견이 불여일타")
print('저는 "나도코딩" 입니다.')
print("저는 \\"나도코딩\\" 입니다.")
# \\\\ :문장내에서 \\
# \\r :커서를 맨 앞으로 이동
print("Red Apple\\rPine")
#\\b :백스페이스 (한글자 삭제)
print("Redd\\bApple")
# \\t : 탭
print("Red\\tApple")
If문
weather = "미세먼지"
if weather == "비":
print('우산을 챙기세요')
elif weather == "미세먼지":
print('마스크를 챙기세요')
else:
print('준비물 필요없어요')
temp = int(input("기온은 어때요?"))
if 30<=temp:
print('너무더워요. 나가지 마세요')
elif 10 <= temp and temp <30:
print('괜찮은 날씨에요')
elif 0<=temp<10: # and
print('외투를 챙기세요')
else:
print('추워요. 나가지 마세요')
For문
# print("대기번호 :1")
# print("대기번호 :2")
# print("대기번호 :3")
# print("대기번호 :4")
for wating_no in range(1,6): # 1 2 3 4 5
print("대기번호 :{0}".format(wating_no))
starbucks =["아이언맨","그루트","토르"]
for customer in starbucks:
print('{0}, 커피가 준비되었습니다.'.format(customer))
While문
#While
customer ="토르"
index = 5
while index >=1:
print("{0}, 커피가 준비되었습니다. {1}번 남았어요.".format(customer,index))
index -= 1
if index == 0:
print('커피는 폐기처분되었습니다.')
customer = "아이언맨"
# index = 1
# while True:
# print("{0}, 커피가 준비 되었습니다.".format(customer))
# index +=1
customer = "토르"
person = 'Unknown'
while person != customer:
print("{0}, 커피가 준비되었습니다.".format(customer))
person = input("이름이 어떻게 되세요?")
Continue 와 Break
absent = [2 , 5] #결석
no_book = [7] #책을 안들고옴
for student in range(1,11):
if student in absent:
continue
elif student in no_book:
print('오늘 수업 여기까지 {0}은 교무실로 따라와'.format(student))
break
print("{0}, 책을 읽어봐".format(student))