Table of Contents

python

prepare

on WSL2

  • pip 설치
    $ sudo apt update & sudo apt upgrade -y
    $ sudo apt install python3-pip
    $ sudo apt install python3-venv
    
  • 가상환경 생성
    $ python3 -m venv .venv
    
  • 가상환경 활성화
    $ source .venv/bin/activate
        또는
    $ . .venv/bin/activate
    
  • 가상환경 비활성화
    $ deactivate
    
· 2022/12/29 16:17 · alex

get python

python official site

settings

$ python3 hello_world.py

variables and types

message = "Hello Python"
print(message)

>>> import this

list

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)
 

list handling

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)

if state

cars = ['audi', 'bmw', 'subaru', 'toyota']

for car in cars:
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.title())

if age < 4:  # GOOD
if age<4:  #bad

dictionary

alien_0 = {'color': 'green', 'points': 5}

print(alien_0['color'])
print(alien_0['points'])

alien_0['x_position'] = 0
alien_0['y_position'] = 25

dict_0 = {}  # 빈 값으로 딕셔너리 선언

del alien_0['points']  # points key 제거

favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
    }
    
point_value = alien_0.get('points', 'No point value assigned.')  # key가 없을 경우의 default 값을 지정하여 error 방지

user_0 = {
    'username': 'efermi',
    'first': 'enrico',
    'last': 'fermi',
    }

for key, value in user_0.items():
    print(f"\nKey: {key}")
    print(f"Value: {value}")
    
    
for name in favorite_languages.key():
    print(name.title())
    
for language in favorite_languages.values():
    print(language.title())
    
for language in set(favorite_languages.values()):  # set는 중복을 제거한 고유한 데이터 형식
    print(language.title()) 

user input and while loop

message = input("Tell me something and I will repeat it back to you: ")
print(message)

prompt = "If you tell us who you are we can personalize the messages you see."
prompt += "\nWhat is you first name? "

name = input(prompt)
print(f"\nHello, {name}!")

current_number = 1

while currnet_number <= 5:
    print(current_number)
    current_number += 1

message = ""

while message != 'quit':
  message = input(prompt)
  print(message)

function

def greet_user():  # 함수 정의
    """간단한 환영 인사를 표시합니다"""  # document string 혹은 docstring 따옴표 세개"""로 둘러쌈
    print("Hello")
    
greet_user()

def describe_pet(animal_type, pet_name='dog'):  # 기본 값이 있는 파라메터는 뒤쪽에
    """반려동물에 관한 정보를 출력합니다"""
    print(f"\nI have a {animal_type}.")
    print(f"My {animal_type}'s name is {pet_name.title()}.")
    #return 값
    
describe_pet('hamster', 'harry)
describe_pet(animal_type='hamster', pet_name='harry')
describe_pet(pet_name='harry', animal_type='hamster')
describe_pet('willie')

def make_pizza(*toppings):  # 여러 개의 매개변수 빈 튜플
def build_profile(first, last, **user_info):  # 빈 딕셔너리

import pizza  # pizza.py 파일을 열고 그 파일에 있는 모든 함수 사용

pizza.make_pizza()  # pizza 모듈 안의 make_pizza() 함수 호출

# 모듈에서 일부 함수만 사용
from module_name import function_name  
from module_name import function_0, function_1, function_3

# 별칭; 함수의 별칭으로 호출
from pizza import make_pizza as mp
from module_name import function_name as fn

# 모둘의 별청
import pizza as p
import module_name as mn

# 모든 함수 비추천
from pizza import *
from module_name import *

class

class Dog:  # 대문자로 시작
    """개를 모델화하는 시도"""  # docstring
    
    def __init__(self, name, age):  # 생성자 메서드, 반드시 구현
        ""name과 age 속성 초기화"""
        self.name = name
        self.age = age
        
    def sit(self):
        """명령에 따라 앉는 개"""
        print(f"{self.name} is now sitting.")
        
    def roll_over(self):
        """명령에 따라 구르는 개"""
        print(f"{self.name} rolled over!")
        
        
my_dog = Dog('Willie', 6)
my_dog.sit()
my_dog.roll_over()

from car import Car  # car.py 안에 Car 클래스 하나만 있을 경우
from car import ElectricCar  # car.py 안에 여러 클래스 중 ElectricCar 만 임포트
from car import Car, ElectricCar  # car.py 안에 Car, ElectricCar 클래스 임포트
import car  # car.py 안에 있는 모든 클래스 임포트

from module_name import *

from electric_car import ElectricCar as EC  # electric_car.py의 ElectricCar 클래스를 EC로 임포트

from random import randint
randint(1, 6)

from random import choice
choiced_value = choice(list_values)

file and exception

# 기본적인 읽기
file_path = '/home/..../openfile.txt'
with open(file_path) as file_object:
    contents = file_object.read()
    
print(contents)

# 라인씩 읽기
with open(filename) as file_object:
    for line in file_object:
        print(line)
        
filename = 'pi_digits.txt'
with open(filename) as file_object:
    lines = file_object.readlines()
    
for line in lines:
    print(line.rstrip())
    
print(f"{pi_string[:52]...")  # 52번째 캐릭터까지만

# write a file
filename = 'prg.txt'

with open(filename, 'w') as file_object:  # w: write mode, r: read mode, a: append mode, r+; read and write. default is 'r'
    file_object.write("I love programming.")
    
#open(filename, encoding='utf-8')

try:
    print(5/0)
except ZeroDivisionError:  # FileNotFoundError,
    print("You can't divide by zero!")
    #pass  # 조용히 실패하기, except 블럭 안에 아무것도 하지 않고 pass만.
else:
    print("try block success.")

import json

json.dump(contents, file_object)  # contents를 file_object에 저장, with open(filename, 'w') 사용
loaded_object = json.load(file_object)  # with open(filename) 

code test

import unittest

class NamesTestCase(unittest.TestCase): #unittest.TestCase를 상속 받는 클래스 생성

    def setUp(self):  # 각 메서드를 실행하기 전에 
....
....
    def test_first_last_name(self):
        ....
        ....
        self.assertEqual(formatted_name, 'Janis Joplin')  # 예상한 결과가 일치하는 지 단언assert.
    
    
if __name__ == '__main__':  # 다른 프로그램에서 임포트하지 않고 직접 실행하면 __name__의 값은 '__main__'이 된다.
  unittest.main()

method example
assertEqual(a,b) a == b임을 확인
assertNotEqual(a,b) a != b임을 확인
assertTrue(x) x가 True임을 확인
assertFalse(x) x가 False임을 확인
assertIn(item, list) item이 list 안에 있음을 확인
assertNotIn(item, list) item이 list 안에 있지 않음을 확인

game

$ python3 -m pip install --user pygame

import sys
import pygame
import pygame.font
import pygame.sprite import Sprite
import pygame.sprite import Group


....
        pygame.init()
        
        pygame.display.set_mode((1200,800))  # ((0,0), pygame.FULLSCREEN)
        pygame.display.set_caption("pygame")
        ...
        ...
        
        ...
        pygame.image.load('image/ship.bmp')
        ...
        
        ...
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:  # pygame.KEYUP, pygame.MOUSEBUTTONDOWN,
                if event.key == pygame.K_RIGHT:  # pygame.K_LEFT, pygame.K_q
                    ...
                     
            
        pygame.display.flip()
        
        # pygame.Rect(0, 0, self.settings.bullet_width,
        #     self.settings.bullet_height)
        # pygame.draw.rect(self.screen, self.color, self.rect)
        # pygame.sprite.Group()
        # pygame.sprite.goupcollide(
        #     self.bullets, self.aliens, True, True)
        # if pygame.sprite.spritecollideany(slef.ship, self.aliens):
        #     ...
        # mouse_pos = pygame.mouse.get_pos()
        # pygame.mouse.set_visible(False) / True
        # pygame.font.SysFont(None, 48)
        

data visualization

$ python3 -m pip install --user matplotlib

import matplotlib.pyplot as plt

squares = [1, 4, 9, 16, 25]

plt.style.use('seaborn')  # plt.style.available = ['seaborn-dark', 'seaborn-darkgrid', 'seaborn-ticks', 'fivethirtyeight', .... ]
fig, ax = plt.subplots()  # 그래프 생성
ax.plot(squares)  # ax.plot(squares, linewidth=3), ax.plot(input_values, squares, linewidth=3)

ax.set_title("Square Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)

ax.tick_params(axis='both', labelsize=14)  # 눈금 라벨 크기를 정함

plt.show()

$ python3 -m pip install --user plotly

from plotly.graph_objs import Bar, Layout
from plotly import offline

...
...
data = [Bar(x=x_values, y=frequencies)]
...

x_axis_config = {'title': 'Result'}
y_axis_config = {'title': 'Frequency of Result'}
my_layout = Layout(title='Results of rolling one D6 1000 times',
    xaxis_x_axis_config, yaxis=y_axis_config)
offline.plot({'data':data, 'layout': my_layout}, filename='d6.html')

import csv

filename = 'data/aaa.csv'

with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    print(header_row)
    
    for index, column_header in enumerate(header_row):
        print(index, column_header)
...

import datetime import datetime
first_date = datetime.strptime('2021-08-02', '%Y-%m-%d')
print(first_date)

$ python3 -m pip install --user requests

import requests

# API 호출을 보내고 응답을 저장
url = 'https://api.github.com/search/repositories?q=language:python&amp;sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")

# API 응답을 변수에 저장
response_dict = r.json()

# 결과 처리
print(response_dict.keys())

web application

virtual environment

$ python3 -m venv ll_env  # ll_env란 이름으로 가상 환경 만들기
$ source ll_env/bin/activate  # ll_env의 가상 환경 활성화
(ll_env)$ deactivate  # ll_env 가상 환경 활성화 상태에서 사용 중지

install django

(ll_env)$ pip install django  # ll_env 가상 환경에서 장고 설치
(ll_env)$ django-admin startproject learning_log . # learning_log라는 이름으로 프로젝트 생성 마지막에 . 반드시 입력 -> learning_log 디렉토리 안에 settings.py, urls.py, wsgi.py 생성
(ll_env)$ python manage.py migrate  # 데이터베이스 생성 -> 기본적으로 db.sqlite3 생성
(ll_env)$ python manage.py startapp learning_logs  # learning_logs라는 이름의 앱 생성 -> model.py, admin.py, views.py
(ll_env)$ python manage.py runserver  # 프로젝트 실행

모델 정의

from django.db import models

class Topic(models.Model):
    """....."""
    ...
    def __str__(self):
        ...
        

INSTALLED_APPS = [
    # 내 앱
    'learning_logs',
    
    # 장고 기본 앱
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
...

(ll_env)$ python manage.py makemigrations learning_logs
(ll_env)$ python manage.py migrate
(ll_env)$ python manage.py createsuperuser
(ll_env)$ python manage.py shell

from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('learning_logs.urls')),
]

from django.shortcuts import render

# view here

(ll_env)$ pip install django-bootstrap4  # bootstrap4 설치
(ll_env)$ pip install psycopg2==2.7.*
(ll_env)$ pip install django-heroku
(ll_env)$ pip install gunicorn
(ll_env)$ pip freeze > requirements.txt

python-3.7.2

web: gunicorn learning_log.wsgi --log-file -

(ll_env)$ git --version

ll_env/
__pycache__/
*.sqlite3

Python Keywords and internal functions

keywords

python internal functions

References