상세 컨텐츠

본문 제목

파이썬 기초 헷갈리는 부분

공부/Python

by E루기 2021. 1. 11. 18:07

본문

def say_hello(name, age):

return f"Hello {name} I'm {age} years old" -> string formatting

string formatting : ""앞에 f 붙이고 해당하는 변수를 {} 감싸면 해당 변수가 출력됨

"Hello ", name, … 방식으로도 가능(번거롭다.)

 

hello = say_hello(age = "23", name = "lalala") -> keyword argument

함수를 호출 할 때 각 argument의 이름을 써넣으면 순서에 상관 없이 해당 argument로 값이 들어간다.

 

 

list선언 : s_list = [1, 2, 3]

tuple선언 : s_tuple = (1, 2, 3) -> cannot fix

dict선언 : s_dict = {1, 2, 3}

list c언어의 배열과 비슷함. [1, "hi", True]식의 list 가능

tuple list지만 지정해놓으면 수정할 없음.

dict 요소에 index 없음.

for문으로 요소를 불러오면 무작위로 나옴

dict 대한 부가적인 설명

https://docs.python.org/3/library/stdtypes.html#typesmapping

 

Built-in Types — Python 3.9.1 documentation

The following sections describe the standard types that are built into the interpreter. The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions. Some collection classes are mutable. The methods that add, subtract,

docs.python.org

# 1만 출력한다.
for x in s_list: # s_tuple을 해도 같은 값
    if(x == 2):
    	break
    print(x)
    
# 무작위로 출력된다.
for x in s_dict:
    if(x == 2):
    	break
    print(x)

is == 차이점

https://medium.com/peloton-engineering/the-dangers-of-using-is-in-python-f42941124027

 

The dangers of using ‘is’ in Python

This blog post covers a strange integer bug that I encountered, and it corresponds to a talk I gave at PyCon Canada. In this post you’ll…

medium.com

 

==, != (value) 비교하는 .

is, is not 가리키는 object 같은 검사

 

Module import하기

import math(기본 파이썬 module)

from math import ceil as c -> math 불러오면 math안의 모든 것을 불러오기 때문에 비효율적이라서 사용하고자 하는 함수만 불러온다

다른 파이썬 파일에서도 불러올 있음

ex) from my_py import plus

댓글 영역