Numbers
- Division (/) always returns a float.
- The equal sign (=) is used to assign a value to variable.
- If a variable is not 'defined' (assigned a value), trying to use it will give you an error.
- In interactive mode, the last printed expression is assigned to the variable _.
- supports types of numbers, such as int, float, Decimal and Fraction.
- also has built-in support for complex numbers, and uses the j or J suffix to indicate the imaginary part (e.g. 3+5j).
>>> 8 / 5 # division always returns a floating number 1.6 >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 128
Strings
- can be enclosed in single quotes('…') or double quotes (“…”)
- \ can be used to escape quotes:
- String literals can span multiple lines.
- “”“…”“” or '…'
- it's possible to prevent including end of lines by adding a \ at the end of the line
- Python strings cannot be changed – they are immutable.
>>> 3 * 'a' + 'bcd' 'aaabcd' >>> 'Py' 'thon' 'Python' >>> text = ('Put several strings within parentheses ' ... 'to have them joined together.') >>> text 'Put several strings within parentheses to have them joined together.' >>> prefix = 'Py' >>> prefix 'thon' # can't concatenate a variable and a string literal File "<stdin>", line 1 prefix 'thon' ^ SyntaxError: invalid syntax >>> ('a' * 3) 'bcd' # SyntaxError >>> prefix + 'thon' 'Python' >>> word = 'python' >>> word[0] 'p' >>> word[5] 'o' >>> word[-1] 'n' >>> word[0:2] 'py' >>> word[2:5] 'tho' >>> word[:2] + word[2:] 'python' >>> word[100] # IndexError >>> word[4:100] 'on' >>> word[100:] '' >>> len(word) 6
'Python' 카테고리의 다른 글
설치된 패키지 목록 저장 및 복원 (0) | 2022.08.25 |
---|---|
How To Update All Python Packages (0) | 2021.10.13 |
Anaconda3 (0) | 2021.08.16 |
Fibonacci series (0) | 2021.08.16 |
Array/List (0) | 2021.08.16 |