public:computer:python

This is an old revision of the document!


python

  • Editors
    • Visual Studio Code
    • Vim
    • Sublimetext
    • pyCharm

$ python3 hello_world.py

  • 변수 이름은 snake_case로
  • 문자열은 “This is a string”, 'This is also a string.' 모두 가능
  • {string_variable}.title() → 첫 글자 대문자로
  • {string_variable}.upeer()
  • {string_variable}.lower()
  • f-string
    • full_name = f“{first_name} {last_name}”
    • full_name = “{} {}”.format(fist_name, last_name)
  • escape characters; \', \\, \n, \r, \t, \b, \f, \ooo, \xhh
  • {string_variable}.rstrip() → 오른쪽 공백 삭제, .strip() → 양쪽 공백 제거, .lstrip() → 왼쪽 공백 제거
  • numeric; integer, floating point number, underscored number → 15_000_000_000
  • 상수는 대문자로; MAX_CONNECTIONS = 5000
  • comments; #

message = "Hello Python"
print(message)

>>> import this

  • 대괄호 []와 콤마 ,
  • 이름을 복수형으로
  • 인덱스는 0부터, [-1]은 끝에서부터의 인덱스

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)

print(bicycles[0])
print(bicycles[0].title())

bicycles[0] = 'mini bell'  # change value
bicycles.append('trek')  # append value
bicycles.insert(0, 'strida')  # insert value

del bicycles[0]  # delete
popped_bicycle = bicycles.pop()  # pop value (stack)
#popped_bicycle = bicycles.pop(0)  # pop value by index
bicycles.remove('redline')  # remove by value

bicycles.sort()  # sort permanently by alphabet
bicycles.sort(reverse=True) # reverse sort
print(sorted(bicycles))  # sort temporarily
bicycles.reverse()  #

len(bicycles)
 

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician)
    
for value in range(1, 5):  # 1 ~ 4
    print(value)
    
numbers = list(range(1, 6))  # numbers = [1, 2, 3, 4, 5]
even_numbers = list(range(2, 11, 2))  # even_numbers = [2, 4, 6, 8, 10]

squares = []
for value in range(1, 11):
    square = value ** 2
    squares.appen(square)
# squares = [value ** 2 for value in range(1,11)]
print(squares)

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
min(digits)
max(digits)
sum(digits)

  • slice; 원래 리스트 유지
    • players[0:3] # index 0에서부터 3개까지 자름 [:3]
    • players[2:] # index 2부터 끝까지
    • players[-3:] # 끝에서 셋
  • 복사
    • my_friends = your_friends[:]
    • my_friends = your_friends는 포인터 같은 역할
  • tuple(immutable)
    • 대괄호 대신 소괄호 ()와 콤마 ,
    • 한 개 항목의 튜플이더라도 콤마 필요; my_t = (3,)
  • public/computer/python.1627893079.txt.gz
  • Last modified: 2021/08/02 17:31
  • by alex