์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ)
<์ด์ ๊ธ>
https://silvercoding.tistory.com/33
[python ๊ธฐ์ด] 1. print ํจ์
์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ) <์ฐธ๊ณ ๋งํฌ> https://www.python-course.eu/python3_formatted_output.php Python Tutorial: Formatted Output Even though it may look so, the formatting is not..
silvercoding.tistory.com
* ๋ฐ์ดํฐ ํ์ ํ๋์ ๋ณด๊ธฐ
v_str = "Niceman"
v_bool = True
v_str2 = "Goodboy"
v_float = 10.3
v_int = 7
v_dict = {
"name" : "Kim",
"age" : 25
}
v_list = [3, 5, 7]
v_tuple = 3, 5, 7
v_set = {7, 8, 9}
print(type(v_str))
print(type(v_bool))
print(type(v_str2))
print(type(v_float))
print(type(v_int))
print(type(v_dict))
print(type(v_list))
print(type(v_tuple))
print(type(v_set))
<class 'str'>
<class 'bool'>
<class 'str'>
<class 'float'>
<class 'int'>
<class 'dict'>
<class 'list'>
<class 'tuple'>
<class 'set'>
1. ์ซ์ํ
<์ซ์ํ ์ฐ์ฐ์ ์ฐธ๊ณ >
https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex
Built-in Types — Python 3.9.6 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
i1 = 39
i2 = 939
big_int1 = 999999999999999999999999999999999999999
big_int2 = 777777777777777777777777777777777777777
f1 = 1.234
f2 = 3.1415
f3 = .5
f4 = 10.
print(i1 * i2)
print(big_int1 * big_int2)
print(f1 ** f2)
print(f3 + i2)
36621
777777777777777777777777777777777777776222222222222222222222222222222222222223
1.93582713947248
939.5
result = f3 + i2
print(result, type(result))
939.5 <class 'float'>
int + float๋ฅผ ํ๋ฉด ์๋์ ์ผ๋ก float ํ์ ์ด ๋๋ค.
a = 5.
b = 4
c = 10
print(type(a), type(b))
result2 = a + b
print(result2)
<class 'float'> <class 'int'>
9.0
- ์ซ์ํ ํ๋ณํ (์ ์ฐํ ํ๋ณํ ๊ฐ๋ฅ)
* int, float, complex(๋ณต์์ : ์ ์์)
print(int(result2))
print(float(c))
print(complex(3))
print(int(True))
print(int(False))
print(int('3'))
print(complex(False))
9
10.0
(3+0j)
1
0
True, False๋ ๊ฐ๊ฐ ์ ์ํ์ผ๋ก ๋ณํํ๋ฉด 1, 0 ์ด ๋๋ค.
3
0j
์ด์ฒ๋ผ ํ์ด์ฌ์ ์ ์ฐํ ํ๋ณํ์ ์ง์ํ๋ค .
y = 100
y += 100
print(y)
200
<์์น ์ฐ์ฐ ํจ์ ์ฐธ๊ณ >
https://docs.python.org/3/library/math.html
math — Mathematical functions — Python 3.9.6 documentation
math — Mathematical functions This module provides access to the mathematical functions defined by the C standard. These functions cannot be used with complex numbers; use the functions of the same name from the cmath module if you require support for co
docs.python.org
print(abs(-7))
n, m = divmod(100, 8) # ๋ชซ, ๋๋จธ์ง
print(n, m)
7
abs() ํจ์๋ ์ ๋๊ฐ์ ์์์ฃผ๋ ํจ์์ด๋ค.
12 4
divmod()ํจ์๋ ๋ชซ๊ณผ ๋๋จธ์ง๋ฅผ ๋์์ ๋ฐํํด ์ค๋ค.
import math
print(math.ceil(5.1)) # ์ด ์ซ์๋ณด๋ค ํฌ๋ฉด์ ๊ฐ์ฅ ์์ ์ ์
print(math.floor(3.874)) # ์ด ์ซ์๋ณด๋ค ์์ผ๋ฉด์ ๊ฐ์ฅ ํฐ ์ ์
6
3
math.ceil() : ์ธ์์ ๋ค์ด๊ฐ ์ซ์๋ณด๋ค ํฌ๋ฉด์ ๊ฐ์ฅ ์์ ์ ์
math.floor : ์ธ์์ ๋ค์ด๊ฐ ์ซ์๋ณด๋ค ์์ผ๋ฉด์ ๊ฐ์ฅ ํฐ ์ ์
2. ๋ฌธ์์ด
str1 = "I am a geak!"
str2 = "NiceGirl"
str3 = ""
str4 = str()
print(len(str1), len(str2), len(str3), len(str4)) # ๊ณต๋ฐฑ ํฌํจ
12 8 0 0
๊ณต๋ฐฑ์ ํฌํจํ์ฌ ๋ฌธ์์ด์ ๊ฐ์๋ฅผ ์ธ๋ len() ํจ์
escape_str1 = "Do you have a \"big collection\"?"
print(escape_str1)
escape_str2 = "Tab\tTab\tTab"
print(escape_str2)
Do you have a "big collection"?
๊ฐ์ ์ข ๋ฅ ๋ฐ์ดํ๋ฅผ ์์ ๋ฃ๊ธฐ ์ํ์ฌ ์ด์ค์ผ์ดํ ๋ฌธ์๋ฅผ ์ฌ์ฉํ๋ค.
Tab Tab Tab
- Raw String
raw_s1 = r'C:\Programs\Test\Bin'
print(raw_s1)
raw_s2 = r"\\a\\a"
print(raw_s2)
C:\Programs\Test\Bin
\\a\\a
rwa string์ผ๋ก ์ด์ค์ผ์ดํ๋ฌธ ์์ด ๊ทธ๋๋ก ์ถ๋ ฅ์ด ๊ฐ๋ฅํ๋ค.
- ๋ฉํฐ๋ผ์ธ : ๋ค์์ค๊ณผ ์ด์ด์ง๋ค.
multi = \
"""
๋ฌธ์์ด
๋ฉํฐ๋ผ์ธ
ํ
์คํธ
"""
print(multi)
๋ฌธ์์ด
๋ฉํฐ๋ผ์ธ
ํ
์คํธ
- ๋ฌธ์์ด ์ฐ์ฐ
str_o1 = '*'
str_o2 = 'abc'
str_o3 = 'def'
str_o4 = 'Niceman'
print(str_o1 * 100)
print(str_o2 + str_o3)
print(str_o1 * 3)
print('a' in str_o4)
print('f' in str_o4)
print('z' not in str_o4)
****************************************************************************************************
abcdef
***
True
False
True
๋ฌธ์์ด์ ์ซ์๋ฅผ ๊ณฑํ๊ฑฐ๋, ๋ฌธ์์ด๋ผ๋ฆฌ ๋ํ๋ ์ฐ์ฐ์ด ๊ฐ๋ฅํ๋ค.
- ๋ฌธ์์ด ํ๋ณํ
print(str(77) + 'a')
print(str(10.4))
77a
10.4
- ๋ฌธ์์ด ํจ์
<๋ฌธ์์ด ํจ์ ์ฐธ๊ณ >
https://www.w3schools.com/python/python_ref_string.asp
Python String Methods
Python String Methods Python has a set of built-in methods that you can use on strings. Note: All string methods returns new values. They do not change the original string. Method Description capitalize()Converts the first character to upper case casefold(
www.w3schools.com
a = 'niceman'
b = 'orange'
print(a.islower()) # ์๋ฌธ์์ธ์ง : Bool ๋ฐํ
print(b.endswith('e')) # e๋ก ๋๋๋์ง : Bool ๋ฐํ
print(a.capitalize()) # ์ฒซ ๊ธ์๋ฅผ ๋๋ฌธ์๋ก
print(a.replace('nice', 'good')) # nice๋ฅผ good์ผ๋ก ๋์ฒดํด๋ผ
print(list(reversed(b))) # ๋ค์ง์ด์ ๋ฆฌ์คํธ๋ก ๋ฐํํ๋ผ
True
True
Niceman
goodman
['e', 'g', 'n', 'a', 'r', 'o']
- ๋ฌธ์์ด ์ฌ๋ผ์ด์ฑ
a = 'niceman'
b = 'orange'
print(a[0:3])
print(a[0:4])
print(a[0:len(a)])
print(a[:4])
print(b[0:4:2])
print(b[1:-2])
print(b[::-1])
nic
nice
niceman
nice
oa
ran
egnaro
a[0:n] ์ผ ๋ 0๋ฒ์งธ ๊ธ์๋ถํฐ n-1๋ฒ์งธ ๊ธ์๊น์ง๋ฅผ ๋ฐํํ๋ค.
'ํ๋ก๊ทธ๋๋ฐ ์ธ์ด > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python ๊ธฐ์ด] 4. ๋ฐ์ดํฐ ํ์ (2) (์๋ฃํ) - dictionary, set (0) | 2021.07.31 |
---|---|
[python ๊ธฐ์ด] 3. ๋ฐ์ดํฐ ํ์ (1) (์๋ฃํ) - list, tuple (0) | 2021.07.30 |
[python ๊ธฐ์ด] 1. print ํจ์ (0) | 2021.07.30 |
[python ์ฌํ] 15. Asyncio, async await (0) | 2021.07.24 |
[python ์ฌํ] 14. Future ๋์์ฑ/ ๋น๋๊ธฐ ์์ ์คํ/ ThreadPoolExecutor, ProcessPoolExecutor (0) | 2021.07.23 |