์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

 

<์ด์ „ ๊ธ€>

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๋ฒˆ์งธ ๊ธ€์ž๊นŒ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

 

 

 

 

 

 

 

 

+ Recent posts