본문 바로가기
카테고리 없음

파이썬 프로그래밍을 향한 첫 단계

by EasyGPT 2023. 12. 17.
반응형

파이썬 프로그래밍을 향한 첫 단계

 

물론 2와 2를 합치는 것보다 더 복잡한 작업에 Python을 사용할 수 있습니다.

예를 들어, Fibonacci series의 초기 하위-수열을 다음과 같이 작성할 수 있습니다:

Of course, we can use Python for more complicated tasks than adding two and two together. For instance, we can write an initial sub-sequence of the Fibonacci series as follows:

>>> # Fibonacci series:

... # the sum of two elements defines the next

... a, b = 0, 1

>>> while a < 10:

... print(a)

... a, b = b, a+b

...

0

1

1

2

3

5

8

이 예에서는 몇 가지 새로운 특징을 소개합니다.

This example introduces several new features.

첫째 줄에는 multiple assignment가 포함되어 있습니다:

변수 a와 b는 동시에 새 값 0과 1을 얻습니다.

마지막 줄에서, 이것이 다시 사용되어, 할당 전에 오른쪽 표현식이 모두 먼저 평가된다는 것을 보여줍니다.

오른쪽 표현식은 왼쪽에서 오른쪽으로 평가됩니다.

The first line contains a multiple assignment: the variables a and b simultaneously get the new values 0 and 1. On the last line this is used again, demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.

while loop는 조건(a < 10)이 true인 동안 실행됩니다.

C와 마찬가지로 Python에서는 0이 아닌 모든 정수 값은 true;

0은 false.

조건은 문자열이나 목록 값일 수도 있고 실제로는 임의의 시퀀스일 수도 있습니다;

길이가 0이 아닌 것은 모두 참이고, 빈 시퀀스는 거짓입니다.

예제에 사용된 테스트는 단순 비교입니다.

표준 비교 연산자는 C와 동일하게 작성: <(보다 작음), >(보다 큼), ==(같음), <=(작거나 같음), >=(크거나 같음) 및 !=(같지 않음).

The while loop executes as long as the condition (here: a < 10) remains true. In Python, like in C, any non-zero integer value is true; zero is false. The condition may also be a string or list value, in fact any sequence; anything with a non-zero length is true, empty sequences are false. The test used in the example is a simple comparison. The standard comparison operators are written the same as in C: < (less than), > (greater than), == (equal to), <= (less than or equal to), >= (greater than or equal to) and != (not equal to).

루프의 본문 body 들여쓰기 indented되어 있습니다: 들여쓰기는 명령문을 그룹화하는 Python의 방식.

대화형 프롬프트에서는 들여쓰기된 각 줄에 탭이나 공백을 입력해야 합니다.

실제로는 텍스트편집기를 사용하여 Python에 대한 더 복잡한 입력을 준비합니다.

모든 괜찮은 텍스트편집기에는 자동 들여쓰기 기능이 있습니다.

복합 명령문이 대화형으로 입력되면 완료를 나타내기 위해 빈 줄이 와야 합니다(파서는 사용자가 마지막 줄을 입력한 시기를 추측할 수 없기 때문입니다).

기본 블록 내의 각 줄은 동일한 양만큼 들여쓰기되어야 합니다.

The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount.

print() 함수는 주어진 인수의 값을 씁니다.

이 함수는 여러 인수, 부동 소수점 수량 및 문자열을 처리하는 방식에서 작성하려는 표현식 작성하는 것과는 다릅니다(앞의 계산기 예제에서 했던 것처럼).

문자열은 따옴표 없이 인쇄되고 항목 사이에 공백이 삽입되므로 다음과 같이 형식을 멋지게 지정할 수 있습니다:

The print() function writes the value of the argument(s) it is given. It differs from just writing the expression you want to write (as we did earlier in the calculator examples) in the way it handles multiple arguments, floating point quantities, and strings. Strings are printed without quotes, and a space is inserted between items, so you can format things nicely, like this:

 
>>> i = 256*256
>>> print('The value of i is', i)
The value of i is 65536

키워드 인수 end를 사용하면 출력 다음에 줄 바꿈을 피하거나 다른 문자열로 출력을 끝낼 수 있습니다:

The keyword argument end can be used to avoid the newline after the output, or end the output with a different string:

>>> a, b = 0, 1
>>> while a < 1000:
... print(a, end=',')
... a, b = b, a+b
...
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,

**는 -보다 우선순위가 높으므로 -3**2는 -(3**2)로 해석되어 -9가 됩니다.

이를 방지하고 9를 얻으려면 (-3)**2를 사용할 수 있습니다.

Since ** has higher precedence than -, -3**2 will be interpreted as -(3**2) and thus result in -9. To avoid this and get 9, you can use (-3)**2.

다른 언어와 달리 \n 같은 특수문자는 작은따옴표('...')와 큰따옴표("...") 모두와 동일한 의미를 갖습니다.

둘 사이의 유일한 차이점은 작은따옴표 내에서는 "를 이스케이프할 필요가 없고(그러나 \'를 이스케이프해야 함) 그 반대의 경우도 마찬가지라는 것입니다.

Unlike other languages, special characters such as \n have the same meaning with both single ('...') and double ("...") quotes. The only difference between the two is that within single quotes you don’t need to escape " (but you have to escape \') and vice versa.

https://amzn.to/41nHjKZ

 
 
반응형

댓글