파이썬-py

제어문 <if문 while문 for문>

it 킹왕짱 2022. 7. 21. 21:31
728x90

if

>>> money=2000
>>> if money >= 3000:
	print("택시타고사라")
else:
	print("걸어가라")

	
걸어가라
>>> card=True
>>> if money >= 3000 or card:
	print("택시타고")
else:
	print("걸어라")

	
택시타고
>>> 1 in [1,2,3]
True
>>> 1 not in [1,2,3]
False
>>> 'a' in ['a','b','c']
True
>>> 'j' not in 'pyhthon'
True
>>> pocket=['paper','cellphone','money']
>>> if 'money' in pocket:
	print('taxi')
elif 'money' not in pocket:
	print('bus')
else:
	print('work')

	
taxi
>>> if 'money' in pocket:	pass
else:	print('카드를 꺼내라')

 

 

while

>>> tree=0
>>> while tree<10:
	print('나무를 %d번 찍었다'%tree)
	tree+=1
	if tree==10:
		print('나무가 넘어가는 중,,')

		
나무를 0번 찍었다
나무를 1번 찍었다
나무를 2번 찍었다
나무를 3번 찍었다
나무를 4번 찍었다
나무를 5번 찍었다
나무를 6번 찍었다
나무를 7번 찍었다
나무를 8번 찍었다
나무를 9번 찍었다
나무가 넘어가는 중,,
>>> promt='''
1.add
2.del
3.list
4.quit

enter number: '''
>>> number=0
>>> while number != 4:
	print(promt)
	number=int(input())

	

1.add
2.del
3.list
4.quit

enter number: 
1

1.add
2.del
3.list
4.quit

enter number: 
2

1.add
2.del
3.list
4.quit

enter number: 
4

## while 강제로 빠져나오기 break ##
>>> coffee=10
>>> while True:
	money=int(input("돈을 넣어주세요 : "))
	if money==300:
		print("커피를 준다")
		coffee=-1
	elif money > 300:
		print("거스름돈을 %d 주고 커피를 준다"%(money-300))
		coffee=-1
	else:
		print("돈을 다시 돌려주고 커피는 주지 않음")
		print("남은 커피의 양은 %d개 입니다. " %coffee)
	if coffee == 0:
		print("커피가 다 떨어짐")
		break

	
돈을 넣어주세요 : 100
돈을 다시 돌려주고 커피는 주지 않음
남은 커피의 양은 10개 입니다. 
돈을 넣어주세요 : 400
거스름돈을 100 주고 커피를 준다
돈을 넣어주세요 : 300
커피를 준다
돈을 넣어주세요 : 

## while 맨 처음으로 돌아가기 ##
>>> a = 0
>>> while a < 10:
...     a = a + 1
...     if a % 2 == 0: continue
...     print(a)
...
1
3
5
7
9

 

for문

>>> test_list=['one','two','three']
>>> for i in test_list:
	print(i)

	
one
two
three

>>> a=[(1,2),(3,4),(5,6)]
>>> for (F,L) in a:
	print(F+L,' ',F-L)

	
3   -1
7   -1
11   -1

>>> marks = [90, 25, 67, 45, 80]
>>> n=0
>>> for mark in marks:
	n+=1
	if mark>=60:
		print("%d fail" %n)
	else:
		print("%d success" %n)

		
1 fail
2 success
3 fail
4 success
5 fail

>>> n=0
>>> for mark in marks:
	n+=1
	if mark < 60:
		continue
	print("%d success" %n)

	
1 success
3 success
5 success

>>> add=0
>>> for i in range(1,11):
	add+=i

>>> print(add)
55

>>> marks=[90,25,67,45,80]
>>> for n in range(len(marks)):
	if marks[n]<60:
		continue
	print("%d success")

	
%d success
%d success
%d success

>>> a=[1,2,3,4]
>>> result=[]
>>> for n in a:
	result.append(n*3)
>>> print(result)
[3, 6, 9, 12]

 

 

[표현식 for 항목 in 반복가능객체 if 조건문]

변수=[표현식 for 항목1 in 반복가능객체1 if 조건문1

                      for 항목2 in 반복가능객체2 if 조건문2

                                             ....

                      for 항목n in 반복가능객체n if 조건문n]

 

>>> a=[1,2,3,4]
>>> result=[n*3 for n in a if n%2==0]
>>> print(result)
[6, 12]

result = [x*y for x in range(2,10)
                for y in range(1,10)]
print(result)
[2, 4, 6, 8, 10, 12, 14, 16, 18, 3, 6, 9, 12, 15, 18, 21, 24, 27, 4, 8, 12, 16, 20, 24, 28, 32, 36, 5, 10, 15, 20, 25, 30, 35, 40, 45, 6, 12, 18, 24, 30, 36, 42, 48, 54, 7, 14, 21, 28, 35, 42, 49, 56, 63, 8, 16, 24, 32, 40, 48, 56, 64, 72, 9, 18, 27, 36, 45, 54, 63, 72, 81]

 

 

 

출처: https://wikidocs.net/22

728x90
728x90

'파이썬-py' 카테고리의 다른 글

클래스, 모듈.py  (0) 2022.07.25
사용자 입력과 출력.py  (0) 2022.07.25
함수.py  (0) 2022.07.25
파이썬 자료형&변수  (0) 2022.07.21
두근두근 파이썬 CH 1 <예제, 연습문제>  (0) 2022.05.23