ํ์ด์ฌ์ ๋ฐ์ดํฐ ํ์ ์ ํฌ๊ฒ 6๊ฐ์ง ์นดํ ๊ณ ๋ฆฌ๋ก ๋ถ๋ฅ๋ ์ ์๋ค.
- Numeric Types : int, float, complex
- Sequence Types : str, list, tuple
- Mapping Typye : dict
- Set Type : set
- Boolean Type : bool
- Binary Type : bytes, bytearray, memoryview
[Code Examples]๐ :
Numeric Types : int, float, complex
# 1.Numeric Types : int(์ ์), float(์ค์), complex(๋ณต์์)
x=3;
y=3.5;
z=2+2j;
print(type(x)); #<class 'int'>
print(type(y)); #<class 'float'>
print(type(z)) #<class 'complex'>
Sequenc Types : String, List, Tuple
# 2.Sequence Types : str(๋ฌธ์์ด), list(๋ฆฌ์คํธ), tuple(ํํ)
#2-1) str
str="hello everyone";
print(type(str)); #<class 'str'>
name="jeein";
print(f"Hello nice to meet you,{name}"); #Hello nice to meet you,jeein
print("""Hello world,
Welcome to wise coder!"""); #Hello world, # Welcome to wise coder!
print("Python's favorite food is pizza."); #Python's favorite food is pizza.
print("Python\'s favorite food is pizza."); #Python's favorite food is pizza.
#multiline
print("Life is too short\nYou need python.")
#capitalize
print(str.capitalize()); #Hello everyone
#upper
print(str.upper()); #HELLO EVERYONE
#split
print(str.split(" ")); #['hello', 'everyone']
#len
print(len(str));#14
#check
print("hello" in str);#True
print("hello" not in str);#False
#2-2) List
my_list=[1,2,3,4]
my_list2=list(("apple","banana","cherry")); #Tuple->List
#slicing
print(my_list[1:3]); #[2, 3]
#add
my_list.append(5);
print(my_list) #[1, 2, 3, 4, 5]
#insert
my_list.insert(1,6);
print(my_list); #[1, 6, 2, 3, 4, 5]
#pop
my_list.pop();
print(my_list); #[1, 6, 2, 3, 4]
my_list.pop(2); #index=2์ธ ํญ๋ชฉ ์ญ์
print(my_list); #[1, 6, 3, 4]
#remove
my_list.remove(6); #[4, 3, 1]
#reverse
my_list.reverse();
print(my_list); #[4, 3, 1]
#sort
my_list.sort();
print(my_list); #[1, 3, 4]
#len
print(len(my_list)); #3
#change
thislist = ["apple", "banana", "cherry"]
thislist[1:3] = ["watermelon"]
print(thislist) #['apple', 'watermelon']
#extend
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist) #['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
#del
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist) #['banana', 'cherry']
#clear
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist) #[]
#loop
thislist = ["apple", "banana", "cherry"]
for i in range(len(thislist)):
print(thislist[i])
#copy
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist) #['apple', 'banana', 'cherry']
print(thislist is mylist); #False
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist) #['apple', 'banana', 'cherry']
print(thislist is mylist); #False
#join
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
# 2-3) tuple : is similar to list except for the fact that they are immutable.
my_tuple=(1,2,3);
print(type(my_tuple)); #<class 'tuple'>
print(my_tuple[1]); #2
# my_tuple[1]=4; #TypeError: 'tuple' object does not support item assignment
#len
print(len(my_tuple));#3
#add
thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y
print(thistuple) #('apple', 'banana', 'cherry', 'orange')
#unpack
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green) #apple
print(yellow) #banana
print(red) #cherry
#If the number of variables is less than the number of values, you can add an * to the variable name and the values will be assigned to the variable as a list
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
(green, yellow, *red) = fruits
print(green) #apple
print(yellow) #banana
print(red) #[cherry, strawberry, raspberry]
#join
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3) #('a', 'b', 'c', 1, 2, 3)
#count
my_tuple=(1,2,3,3);
print(my_tuple.count(3)); #2
#index
print(my_tuple.index(3)); #2
Mapping Types : dict
# 3.Mapping Type : dict(๋์
๋๋ฆฌ)
#{"key":"value"}
my_dict={
"chef":"Amy",
"ceo":"Jason"
}
print(type(my_dict)) #<class 'dict'>
print(my_dict.get("chef")) #Amy
print(my_dict["chef"]) #Amy
print(my_dict.keys()) #dict_keys(['chef', 'ceo'])
print(my_dict.values()) #dict_values(['Amy', 'Jason'])
print(my_dict.items()) #dict_items([('chef', 'Amy'), ('ceo', 'Jason')])
#Adding in a new key-value pair
my_dict["cfo"]="Tomson";
print(my_dict); #{'chef': 'Amy', 'ceo': 'Jason', 'cfo': 'Tomson'}
#Update to k-v
my_dict["ceo"]="James";
print(my_dict); #{'chef': 'Amy', 'ceo': 'James', 'cfo': 'Tomson'}
#dict inside list
stock_prices={'google':[200,210,220],'gme':[20,100,300]}
print(stock_prices['google']); #[200, 210, 220]
print(stock_prices["google"][0]); #200
#dict inside dict
mid_grade={"CS":{"assign1":100,"assign2":200}};
print(mid_grade["CS"]["assign1"]); #100
#check
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
#update
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"year": 2020})
print(thisdict) #{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}
#remove
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict) #{'brand': 'Ford', 'year': 1964}
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.popitem()
print(thisdict) #{'brand': 'Ford', 'model': 'Mustang'}
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict) #{'brand': 'Ford', 'year': 1964}
#clear
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict) #{}
#loop
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x, y in thisdict.items():
print(x, y)
#copy
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict) #{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = dict(thisdict)
print(mydict) #{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
Set Type : set
# 4.Set Type : set(์งํฉ)
# ์ค๋ณต ์ ๊ฑฐ, ์์๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ธ๋ฑ์ฑ์ผ๋ก ์ ๊ทผ ๋ถ๊ฐ(์๋ฃํ์ ๋ฆฌ์คํธ๋ ํํ๋ก ๋ณํ ํ ์ ์ดํด์ผ)
my_set1=set([1,2,4,5]);
my_set2=set("Hello");
print(my_set1) #{1, 2, 4, 5}
print(my_set2) #{'o', 'H', 'e', 'l'} ์ค๋ณต ์ ๊ฑฐ๋จ.
list_s1=list(my_set1)
list_s2=list(my_set2)
print(list_s1); #[1, 2, 4, 5]
print(list_s2); #['e', 'o', 'l', 'H'] -> ๊ณ์ ๊ฐ์ด ๋ณ๊ฒฝ๋จ. ๋ฆฌ์คํธ๋ก ๋ฐ๋๋ ๊ณผ์ ์์ ์์๊ฐ ๋๋ค์ผ๋ก ์์น.
print(list_s1[1]); #2
print(list_s2[2]); #l
set1=set([1,2,3,4,5]);
set2=set([3,4,5,6,7]);
#Intersection ๊ต์งํฉ
print(set1&set2); #{3, 4, 5}
print(set1.intersection(set2)); #{3, 4, 5}
#Union ํฉ์งํฉ
print(set1|set2); #{1, 2, 3, 4, 5, 6, 7}
print(set1.union(set2)); #{1, 2, 3, 4, 5, 6, 7}
#Difference ์ฐจ์งํฉ
print(set1-set2); #{1, 2}
print(set1.difference(set2)); #{1, 2}
#ํฉ์งํฉ - ๊ต์งํฉ
print(set1^set2); #{1, 2, 6, 7}
#add
set1.add(6);
print(set1); #{1, 2, 3, 4, 5, 6}
set2.update([8,9])
print(set2); #{3, 4, 5, 6, 7, 8, 9}
#remove or discard pop()๋ ๊ฐ๋ฅ
set1.remove(3);
print(set1); #{1, 2, 4, 5, 6}
#list์ ์ค๋ณต ์์ ๊ธฐ
my_list=[1,2,3,4,2,2,3];
my_set=set(my_list);
print(my_set); #{1, 2, 3, 4}
Boolean Type : bool
# 5.Boolean Type : bool(๋ถ๋ฆฌ์ธ)
# ๋น ์๋ฃ๊ฐ("",(),{},[]), ์ซ์ 0, None => False
print(bool(False));#False
print(bool(None))
print(bool(0))
print(bool(""))
print(bool(()))
print(bool([]))
print(bool({}))
print(bool("hi")); #True
Binary Type : bytes, bytearray, memoryview
# 6.Binary Type : bytes, bytearray, memoryview
print(bytes(range(10)));
print(bytearray(10));
'Python๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Python Lambda๐ (0) | 2022.06.27 |
---|---|
Python Functionโจ (0) | 2022.06.27 |
Python ๋ฐ๋ณต๋ฌธ while, forโก (0) | 2022.06.27 |
Python ์กฐ๊ฑด๋ฌธ If๐ (0) | 2022.06.27 |
Python Operators๐ (0) | 2022.06.25 |