1. Variable assignment
spam_amount = 0
변수 할당은 왼쪽에 값을 넣고 싶은 변수의 이름을 적고, 오른쪽에 넣고 싶은 값을 적는다. 변수에 값을 할당한다는 행위는 assignment 연산자인 '='에 의해 가능하다.
2. Function calls
print(spam_amount)
print()는 입력값을 스크린에 보여주는 함수이다. 함수는 함수명 뒤에 괄호() 를 붙임으로써 정의할 수 있으며, 입력값을 넣어 원하는 연산을 수행하도록 지시한다.
3. Comment, Reassigning
# Ordering Spam, egg, Spam, Spam, bacon and Spam (4 more servings of Spam)
spam_amount = spam_amount + 4
가장 첫줄은 주석이라고 불리며 '#'를 사용해 코드가 어떤 행위를 하는지 설명해주는 용도로 사용한다. 개발자들 간의 소통을 위해 사용하며 다른 사람의 코드를 이해하는데 도움을 준다. Reassigning은 기존에 존재하는 변수의 값을 변경하고자 할때 assigning과 같은 연산자 '='를 사용하여 기존 변수에 값을 넣어준다.
개발자의 주석
- 개발자의 의도를 알려주는데 중요하다. 무엇을 작성한 코드인지, 언제 만든건지, 누가 만든건지 등등.
- 협업 과정에서 함수 제목, 변수명 등으로 표시하지 못하는 부분에 대해서 가독성을 확보하는데 도움을 준다.
- 과도한 주석은 오히려 가독성을 해치며 필수적인 정보들로 최소화하는 노력을 해보자.
4. Code block
if spam_amount > 0:
print("But I don't want ANY spam!")
viking_song = "Spam Spam Spam"
print(viking_song)
코드 한줄의 끝에 붙어있는 ':' 콜론은 새로운 코드 블럭(Code block)이 시작되고 있음을 말한다. 하위 라인은 코드 블럭안에서 실행되는 코드들로 if 문안의 조건이 만족될 시 실행되는 코드이다. viking_song은 4칸이 띄어져서 작성되지 않았으므로 코드 블럭에 속하지 않게 되며 독립적으로 시행되는 코드이다.
5. Numbers and arithmetic in Python
Python에는 여러가지 데이터 타입(int, float, string, ...)이 존재한다.
spam_amount = 0
type(spam_amount)
Operator | Name | Description |
---|---|---|
a + b | Addition | Sum of a and b |
a - b | Substraction | Difference of a and b |
a * b | Multiplication | Product of a and b |
a / b | True division | Quotient of a and b |
a // b | Floor division | Quotient of a and b, removing fractional parts |
a % b | Modulus | Integer remainder after division of a by b |
a ** b | Exponentiation | a raised to the power of b |
-a | Negation | The negative of a |
단어 : Quotient(몫), Modulus(계수)
6. Order of operations
연산 순서는 우리가 상식적으로 알고있는 괄호 - 지수 - 곱/나누기 - 더하기/빼기 순으로 계산된다.
-3 + 4 * 2
# This would be 5
hat_height_cm = 25
my_height_cm = 190
total_height_meters = hat_height_cm + my_height_cm / 100
print(f"Height in meters = {total_height_meters}")
total_height_meters = (hat_height_cm + my_height_cm) / 100
print(f"Height in meters = {total_height_meters}")
# Result of two codes will be different as 26.9 and 2.15
7. Builtin function for working with numbers
# return maximum of their arguments
print(max(1,2,3))
# return minimum of their arguments
print(min(1,2,3))
# return absolute value of an argument
print(abs(-32))
# Change type as float
print(float(10))
# Change type as int
print(int(10.5))
Built-in function은 Python에 기본적으로 내장되어 있는 함수로 다른 라이브러리를 import하는 것과 관계없이 바로 사용가능한 함수들이다.
Source of the course : Kaggle Course _ Hello, Python
Hello, Python
Explore and run machine learning code with Kaggle Notebooks | Using data from No attached data sources
www.kaggle.com
'Course > [Kaggle] Data Science' 카테고리의 다른 글
[Python] Loops and List Comprehensions (0) | 2022.02.11 |
---|---|
[Python] Lists (0) | 2022.02.11 |
[Python] Booleans and Conditionals (0) | 2022.02.11 |
[Python] Functions and Getting help (0) | 2022.02.11 |
Kaggle Course Introduction (0) | 2022.02.10 |