$ sudo apt-get update
$ sudo apt-get upgrade
python3 --version
$ vi hello.py
# 파일 내용 내부에 작성
print('Hello World')
$ python3 hello.py
# 출력
Hello World
$ python3
>>> print('Hello World')
Hello World
# pip3가 설치되어 있는지 확인, 없다면 설치
$ pip3 --version
$ sudo apt install python3-pip
$ pip3 --version
# math 모둘 사용
$ python3
>> import math
>> math.ceil(10.3)
11
>>> from math import ceil
>>> ceil(10.3)
11
# django.shorcuts는 패키지는 Django에서 제공하는 패키지 중 하나로, 그 중 render, redirect 클래스를 사용할 대 선언
>>> from django.shorcuts import render, redirect
# 사용자 정의 패키지, 클래스
>>> from boardapp.models import *
2) 블록
# Java
public class HelloWorld() {
public static void main(String args[]){
System.out.println("Hello World");
}
}
# Python
>>> def HelloWorld() :
print("Hello World")
>>> a = -1;
>>> if a > 0:
... print("양수")
... else:
... print("0 또는 음수")
...
0또는 음수
3) 주석
>>> print("Hello World") # 출력하는 부분입니다
Hello World
4) 화면 출력(print)
>>> a = 10
>>> b = "test"
>>> print(a)
10
>>> print(b)
test
>>> a = "1"
>>> b = a+1
TypeError ...
>>> a = 10.5
>>> b = 10
>>> int(a)
10
>>> float(b)
10.0
>>> str(a)
10.5'
>>> a = [1, 'test', 2, 3.5]
>>> a[0]
1
>>> a[1]
'test'
>>> a[3]
3.5
>>> a[0] = 2
>>> print(a)
[2, 'test', 2, 3.5]
>>> a[4] = 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a = [1,2,3,4,5]
>>> a.append(6)
>>> print(a)
[1, 2, 3, 4, 5, 6]
>>> a.insert(2, 7)
>>> print(a)
[1, 2, 7, 3, 4, 5, 6]
>>> print(a)
[1, 2, 7, 3, 4, 5, 6]
>>> a.remove(3)
>>> print(a)
[1, 2, 7, 4, 5, 6]
>>> a.pop()
6
>>> print(a)
[1, 2, 7, 4, 5]
>>> print(a)
[1, 2, 7, 4, 5]
>>> a.index(4)
3
>>> a
[1, 2, 7, 4, 5]
>>> len(a)
5
>>> a = [10, 20, 30, 40, 50]
>>> b = a[:2]
>>> c = a[2:]
>>> d = a[1:3]
>>> print(b)
[10, 20]
>>> print(c)
[30, 40, 50]
>>> print(d)
[20, 30]
>>> a = (1, 2.3, "4")
>>> a[0]
1
>>> a[0] = 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> a.index(1)
0
>>> len(a)
3
딕셔너리 사용
>>> a = {"title" : "타이틀", "page" : 1}
>>> print(a)
{'title': '타이틀', 'page': 1}
>>> a["title"]
'타이틀'
딕셔너리 변경, 추가, 삭제
>>> print(a)
{'title': '타이틀', 'page': 1}
>>> a["title"] = "변경된 타이틀"
>>> print(a)
{'title': '변경된 타이틀', 'page': 1}
>>> a = {"title" : "타이틀", "page" : 1}
>>> a['name'] = 'Tom'
>>> print(a)
{'title': '타이틀', 'page': 1, 'name': 'Tom'}
>>> a = {'title' : '타이틀'}
>>> b = {'name' : 'Tom'}
>>> a.update({'page' : 1})
>>> print(a)
{'title': '타이틀', 'page': 1}
>>> a.update(b)
>>> print(a)
{'title': '타이틀', 'page': 1, 'name': 'Tom'}
>>> print(a)
{'title': '타이틀', 'page': 1, 'name': 'Tom'}
>>> del a['page']
>>> print(a)
{'title': '타이틀', 'name': 'Tom'}
>>> a.clear()
>>> print(a)
{}
딕셔너리 요소 개수 및 키(Key) 추출
>>> a = {'title' : '타이틀', 'page' : 1, 'name' : 'Tom'}
>>> len(a)
3
>>> a.keys()
dict_keys(['title', 'page', 'name'])
>>> b = list(a.keys())
>>> print(b)
['title', 'page', 'name']
>>> c = tuple(a.keys())
>>> print(c)
('title', 'page', 'name')
>>> a = [1, 2, (3,4)]
>>> b = (1, 2, [3, 4])
>>> print(a)
[1, 2, (3, 4)]
>>> a[2]
(3, 4)
>>> b[2]
[3, 4]
>>> a[2][0]
3
>>> c = (1, 2, {'key' : 3})
>>> c[2]
{'key': 3}
>>> c[2]['key']
3
# for문
>>> a = ['title', 'page', 'name']
>>> for i in a:
... print(i)
...
title
page
name
>>> a = [1,2,3,4,5]
>>> i = 0
>>> while i < 5:
... print(a[i])
... i += 1
...
1
2
3
4
5
>>> a = 10
>>> if a > 10:
... print('Over 10')
... elif a > 5:
... print('Between 5 and 10')
... elif a > 3:
... print('Between 3 and 5')
... else :
... print('Under 3')
...
Between 5 and 10
if a > b : # a가 b보다 클 경우
if a == b : # a와 b가 같을 경우
if a != b : # a와 b가 다를 경우
if a is False : # a의 값이 False일 경우
if a : # a의 변수의 값이 빈 값이 아닐 경우
if a is None : # a 변수의 ㄱ밧이 존재하지 않는 경우
# None은 None만 None이며 None은 값이 없음(빈값)과 같다
# 값이 없는(빈값) ""은 None이 아니다
>>> a = [1,2,3,4]
>>> try :
... print(a[2])
... print(a[4])
... except :
... print(0)
...
3
0
>>> try :
... print(a[2])
... print(a[4])
... except IndexError :
... print('인덱스 에러')
... except FileNotFoundError :
... print('미존재 파일 에러')
...
3
인덱스 에러
>>> def helloWorld() :
... print('Hello World!')
... print('Welcome to Python')
...
>>> helloWorld()
Hello World!
Welcome to Python
>>> helloWorld()
Hello World!
Welcome to Python
>>> a = 'Apple'
>>> b = 'Orange'
>>> def printFruit(fruit) :
... print(fruit)
...
>>> printFruit(a)
Apple
>>> printFruit(b)
Orange
>>> printFruit('Banana')
Banana
>>> def exp(number) :
... return number*number
...
>>> a = exp(5)
>>> b = exp(100)
>>> print(a)
25
>>> print(b)
10000
>>> print(ceil(10.3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ceil' is not defined
>>> import math
>>> print(math.ceil(10.3))
11
>>> from math import ceil
>>> print(ceil(10.3))
11
1) 클래스의 개념 및 정의
>>> class Apple :
... def set_information(self, location, color) :
... self.location = location
... self.color = color
...
>>> Apple1 = Apple()
>>> Apple1.set_information('seoul', 'red')
>>> Apple1.location
'seoul'
>>> Apple1.color
'red'
2) 생성자 상속, 오버라이딩
클래스에는 다양한 기능이 있지만, 그 중에서도 생성자(Constructor), 상속(Inherit), 오버라이딩(Overriding)은 반드시 알아야 한다
생성자(Constuctor)
>>> class Apple :
... def __init__(self, location, color) :
... self.location = location
... self.color = color
...
>>> Apple1 = Apple('seoul', 'red')
>>> Apple1.location
'seoul'
>>> Apple1.color
'red'
상속(Inherit)
>>> class Human :
... def __init__(self, name):
... self.name = name
... def getName(self):
... return self.name
...
>>> class Female(Human) :
... def getGender(self):
... return "Female"
...
>>> girl = Female('Jame')
>>> girl.getName()
'Jame'
>>> girl.getGender()
'Female'
오버라이팅(Overriding)
>>> class Human :
... def __init__(self, name) :
... self.name = name
... def getName(self):
... return self.name
...
>>> class Female(Human) :
... def getName(self) :
... return "Miss "+self.name
...
>>> girl = Female('Jane')
>>> girl.getName()
'Miss Jane'
>>> human1 = Human('Unknown')
>>> human1.getName()
'Unknown'
1) Python 가상환경의 이해
2) VirtualEnv 설치
$ pip3 install --user virtualenv
$ cat .profile
$ source ~/.profile
$ virtualenv --version
$ virtualenv venv
$ tree venv
# 가상환경 실행
$ source venv/bin/activate
# 가상환경 종료
$ deactivate
$ source venv/bin/activate
(venv) pip install django
(venv) pip freeze
(venv) django-admin startproject test_proj
(venv) cd test_proj
(venv) python manage.py runserver
(venv) python manage.py migrate
(venv) pthon manage.py runserver
$ sudo apt-get install ubuntu-desktop VNC4Server gnome-panel gnome-settings-daemon xfce4 metacity nautilus