파이썬 문법 - 유용한 연산자

Description

  • range()
  • enumerate()
  • zip()
  • abs() absolute value
  • pow(a,b) power
  • max(a,b) maximum
  • min(a,b) minimum
  • round() 반올림
  • floor() 내림
  • ceil() 올림
  • sqrt() 제곱근

range() 예시

for num in range (11):
    print(num)

result:
0
1
2
3
4
5
6
7
8
9

_____________________________
for num in range (0,11,2):
    print(num)

result:
0
2
4
6
8
10
_____________________________
list(range(0,11,2))

result:
[0,2,4,6,8,10]

enumerate() 예시

word= 'abcde'

for itme in word:
    print(word[index_count])
    index_count += 1

result:
a
b
c
d
e
_____________________________
word = 'abcde'

for item in enumerate(word):
    print(item)

result:
(0, 'a')
(1, 'b')
(2, 'c')
(3, 'd')
(4, 'e')

______________________________
word = 'abcde'

for index,letter in enumerate(word):
    print(index)
    print(letter)
    print(\n)

result:
0
a

1
b

2
c

3
d

4
e

zip() 예시

mylist1 = [1,2,3]
mylist2 = ['a','b','c']

for item in zip(mylist1, mylist2):
    print(item)

result:
(1, 'a')
(2, 'b')
(3, 'c')

Shuffle

from random import shuffle

mylist = [1,2,3,4,5,6,7,8,9,10]

random_list = shuffle(mylist)
print(mylist)

result:
[3,6,5,4,1,2,8,9,7,10]

randint

mynum = randint(0,100)
print(mynum)

result:
39

input

myname = input('my name is )
print(myname)

result:
my name is Junyeong

좋은 웹페이지 즐겨찾기