2024-07-16

우진·2024년 7월 16일

python study

목록 보기
1/1

help() :

-m = —module : start module.
module = py file.
ex) -m unittest ⇒ -m unittest.py ⇒ -m {absolute path of unittest.py}
pip install pandas ⇒ python pip install {absolute path of pandas.py}
we usually use abbreviation command, there always has absolute path

chapter3

pycharm multi cursor = alt + j

pycharm shift file = ctrl + shift + n

indent

  • 4 space = 1 tab
  • in python, indent = grammer. if we don’t indent, it cause “grammer” error.
  • To change indent size,

version notation

  1. major

    python 3.x.x

    major = sys.version_info.major
  2. minor

    python 3.11.x

    minor = sys.version_info.minor

pass ⇒ ellipsis( . . . )

  • In function(or Class) if there has no content, it cause grammer error. So we use pass first.

variable

  • memory space.

function() → meaning call function

print(function)

in function or method if print it removing (), print start position by memory adress

print(id(function))

if print it having (), print uniq id that assign element in ()

Q. why thaere has no type declaration in python ?

A. python interpreter guess the type of variable itself.
But we can ‘type hint’ (inform explicitly to interpreter) in function or method.

python is dynamic type langauge.

type casting : python intermreter change type appropriately.

💡 make python package named ‘sec03’

llama3, ollama

💡 1. install ollama 2. window trminal : ollama pull llama3 ollama run llama3

branch statement

  • condition statement
    • if

loop statement

  • for
    fruits = ["apple", "pair"]
    
    for fruit in fruits:
    	print(f'fruit name is {fruit}')
    	
    for i in range(0, len(fruits)):
    	print(f'{i} fruit name is {fruits[i]}')
    	
    # most pythonic code. enumerate makes tuple data.
    for idx, fruit in enumerate(fruits):
    	print(f'{idx} fruit name is {fruit}')
    	
    chars = 'word'
    for count, char in enumerate(chars):
    	print(f'{count} character is {char}')
    • enumerate
      • class. return enumerate object
      • return (idx, tuple)
      • change list object to enumerate object. And push index to each element.
    • range
      • range( initial value, last value, step )
      • initial value, step can be skipped.
      • if range ( 0, 3 ), starting at 0, ending at 2. totaly 3 repeat.
  • foreach
    foreach plural as singular {
    	singular ~~~ 
    }
  • while
    n = 0
    while n < 3:
    	print(f'variable n is (n)')
    	n += 1 

jump statement

  • continue : pass the next content. go next step.
  • break : end present loop.

get grade function

def get_grade(grade):
    if grade >= 90:
        return "A"
    elif grade >= 70:
        return "B"
    elif grade >= 50:
        return "C"
    else:
        return "D"
  • main.py
import exam.exam
from exam.exam import *

# 파이썬은 진입 점이 따로 없기 때문에
# 파이썬 인터프리터가 이 파일을 직접 실행시키면 이라는 뜻. 여기 부터 프로그램이 시작.
if __name__ == "__main__":
    print("Input your grade (0-100): ")
    grade = float(input())
    result = exam.exam.get_grade(grade)
    print(result)

True = on = electrified

False = off = not electrified

len() : input countable object, retrurn number people count. ( 1 ~ )

  • computer count ( 0 ~ )

countable object, container object

docker, django, rest + react, pitcher(in model)

input must be equal to model’s pitch ? training pitch ?

None type : meaning empty

not True == False

1-term operator

  • not

binary operator

  • in
  • is

ternary operator

  • ? : contraction if-statement
    ex) a == b ? True( statement ) : False( statement )

data structure

  • array
    • fixed length
  • list
    • items = [item, …]
    • redundant, variable length
  • dictionary
    • dictionary = { ”key”, “value” }
    • item = call key and value both
  • tuple
  • set

pythonic = readable, sppedy

scope

  • global scope
  • local scope
    ex) variables generate in for-statement are removed when the loop ends.

formatting

  • f - string
    for i in range(1, 10):
        print(f"Multiplication table of {i}:")
        for j in range(1, 10):
            result = i * j
            print(f"{i} x {j} = {result}")
        print()
  • { }
    for i in range(1, 10):
        print(f"Multiplication table of {i}:")
        for j in range(1, 10):
            result = i * j
            print("{} * {} = {}".format(i, j, result))
        print()
  • zfill
    for i in range(1, 10):
        print(f"Multiplication table of {i}:")
        for j in range(1, 10):
            result = i * j
            print("{} * {} = {}".format(i, j, str(result).zfill(2)))
        print()

destructive method vs non-destructive method

  • destructive
    • pop() : delete last element
  • non - destructive
    • copy()

shallow copy vs deep copy

  • shallow copy : refer to the original address. if change copied one, also change original one.
  • deep copy : create all new one
    • copy()

signiture

expression

statement

exception handling

try:
	import akfnsdf # there has no module named like that
except Exception as e:
	print(e)
finally:
	# What to do regardless of exceptions
	print(e)

preprocessing vs post processing

Exception hierarchy

from io import UnSupportedOperation

get UnSupportedOperation class in io.py file

raise() : make error on purpose. then go to sys.exit().

# with : auto close
with open('some.txt', 'w') as f:
	print(f.read)
	print(f.readlines)
	print(f.readline)
	
	f.write('some.txt')
	f.write('ssajfbs')

0개의 댓글