In [6]:
a = 99
if a % 9 == 0:
if a % 11 == 0:
print '!!!'
elif a % 10 ==0:
print '???'
else:
print 'hahaha'
In [3]:
x = 7
if (5 > x) and (x < 10):
print 'oh!'
In [4]:
a = []
if a:
print 'right'
else:
print "empty"
In [7]:
5 == 6
5 != 6
a = 0
if not a == 0:
print 'haha'
In [8]:
count = 1
while count <=3:
print count
count += 1
print count
print 'a'
In [9]:
count = 1
while True:
print count
count += 1
if count > 3:
break
In [10]:
var = 7
while var > 0:
var -= 1
if var == 5:
continue
print 'Current variable value :', var
print 'Good bye!'
In [11]:
nums = [1, 3, 5]
position = 0
while position < len(nums):
number = nums[position]
if number % 2 == 0:
print 'even number'
break
position += 1
else:
print 'no even number'
In [12]:
nums = [1, 3, 2, 5, 4, 6, 7, 9, 8]
pos = 0
while pos < len(nums):
print nums[pos],
pos += 1
In [13]:
a = [1, 2, 3, 4]
b = a
print a == b
In [14]:
a = [1, 2, 3, 4, 5]
for i in a:
print i
In [17]:
nums = [1, 3, 2, 5, 4, 6, 7, 8, 9]
for val in nums:
print val,
print
for i in nums:
print i,
print
for num in nums:
print num,
In [20]:
str = 'hello world'
for char in str:
print char
In [25]:
capitals = {'korea' : 'seoul', 'japan' : 'tokyo', 'usa' : 'Washington D.C'}
for country in capitals:
print country, capitals[country]
print '-' * 20
for key in capitals.keys():
print key
print '-' * 20
for val in capitals.values():
print val
print '-' * 20
for k, v in capitals.items():
print k, v
In [26]:
nums = [1, 3, 2, 5, 4, 6, 7, 9, 8]
for val in nums:
print val,
for i, _ in enumerate(nums):
print 'value at index', i
In [27]:
a = [1, 2, 3, 4]
b = [2, 3, 4]
for i in a:
for j in b:
print i, j
if & for 연습문제
In [39]:
nums = range(1, 101)
for i in nums:
if i % 2 == 0 or i % 11 ==0:
print i,
In [ ]:
a = [22, 1, 3, 4, 7, 98, 21, 55, 87, 99, 19, 20, 45]
minimum = a[0]
for i in a[1:]:
if minimum > i:
minimum = i
print minimum
In [40]:
a = [22, 1, 3, 4, 7, 98, 21, 55, 87, 99, 19, 20, 45]
sum1 = 0
for i in a:
sum1 = sum1 + i
print float(sum1)/len(a)
In [41]:
nums = [1, 2, 3, 4, 5, 6]
even_nums = []
for i in nums:
if i % 2 == 0:
even_nums.append(i)
print even_nums
In [44]:
nums = [1, 2, 3, 4, 5, 6]
even_nums2 = [i for i in nums if i % 2 == 0]
even_nums3 = [i + 3 for i in nums if i % 2 == 0]
print even_nums2, even_nums3
In [45]:
nums2 = [i for i in nums]
nums3 = [i**2 for i in nums]
print nums2
print nums3
In [46]:
nums4 = [i for i in nums if i < 4]
nums5 = [i**2 if i < 4 else i * 2 for i in nums]
print nums4
print nums5
In [48]:
rows = range(1, 4)
cols = range(1, 3)
for row in rows:
for col in cols:
print row, col
print '-' * 20
cells = [(row, col) for row in rows for col in cols]
for row, col in cells:
print row, col
In [49]:
words = ['apple', 'banana', 'chicago', 'do', 'elephant']
dict1 = {}
for w in words:
dict1[w] = len(w)
print dict1
words = ['apple', 'banana', 'chicago', 'do', 'elephant']
words_dict = {w: len(w) for w in words}
print words_dict
practice
In [50]:
Celsius = [39.2, 36.5, 37.3, 37.8]
F = [1.8 * c + 32 for c in Celsius]
print F
In [51]:
str1 = 'Today is very nice and I want to go out for dinner'
temp = [ch for ch in str1 if ch == ' ']
print len(temp)
In [52]:
str1 = 'Today is very nice and I want to go out for dinner'
vowels = 'aeiou'
temp = [ch for ch in str1 if not ch in vowels]
print ''.join(temp)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: