변수의 시작은 영문자나 언더스코어(_)로만 시작해야 한다.:
변수의 시작 다음에는 숫자나 문자나 언더스코어등 아무거나 와도 상관없다. :
변수명은, 같은 문자라도 대문자와소문자를 구별하므로, 다른변수로 취급한다.
변수의 시작은 절대 숫자로 시작할수 없다.
letters = 'abcdefghijklmnopqrstuvwxyz'
letters[0] # "a"
letter[-1] # "z"
letters = 'abcdefghijklmnopqrstuvwxyz'
new_letter = letters.replace("abc","Hello")
print(new_letter) # Hellodefghijklmnopqrstuvwxyz
letters = 'abcdefghijklmnopqrstuvwxyz'
letters[0:3] # 'abc'
letters[-3:] # 'xyz'
letters[ : : -1] # 'zyxwvutsrqponmlkjihgfedcba'
ex_str = " hello world "
len(ex_str) # 13
파이썬 리스트에서 비어있는 리스트, 즉 아무것도 없는 리스트는 아래처럼 두가지 방법으로 생성할 수 있습니다.
list_one = []
print(list_one) # []
list_two = list()
print(list_one) # []
list_three = [1,"2","hello",[1,2,3]]
list_three[0] # 1
list_three[3] # [1, 2, 3]
list_three[3][1] # 2
list_three[2] = "13"
list_three # [1, '2', '13', [1, 2, 3]]
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
week.insert(5,"Saturday")
week # ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
del week[1]
week # ['Monday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
week.remove('Monday')
week # [ 'Wednesday', 'Thursday', 'Friday', 'Saturday']
value = week.pop()
value # 'Saturday'
week # [ 'Wednesday', 'Thursday', 'Friday']
두개의 리스트를 + (더하기 연산) 을 이용해서, 두개 이상의 리스트를 하나의 리스트로 만들 수 있다.
fruits = ["apple","banana","pineapple"]
numbers = [1,2,3]
print(fruits+numbers) # ['apple', 'banana', 'pineapple', 1, 2, 3]
"찾고자하는 값" in "자료구조(리스트)"
True 이면 리스트에 해당 값이 존재하는 것이고, 존재하지 않으면 False 가 리턴된다.
"apple" in fruits # true
fruits = ['Apple', 'Banana', 'Mango', 'Pineapple',
'Coconut', 'Orange', 'Strawberry', 'Lemon', 'Mango']
fruits.count('Mango') # 2
len(fruits)# 9
fruits = ['Apple', 'Banana', 'Mango', 'Pineapple',
'Coconut', 'Orange', 'Strawberry', 'Lemon', 'Mango']
new_fruits = sorted(fruits)
print(fruits)
# ['Apple', 'Banana', 'Mango', 'Pineapple', 'Coconut', 'Orange', 'Strawberry', 'Lemon', 'Mango']
print(new_fruits)
#['Apple', 'Banana', 'Coconut', 'Lemon', 'Mango', 'Mango', 'Orange', 'Pineapple', 'Strawberry']
fruits.sort()
print(fruits)
#['Apple', 'Banana', 'Coconut', 'Lemon', 'Mango', 'Mango', 'Orange', 'Pineapple', 'Strawberry']
num_list = [ 44, 5, 12, 100, 32]
new_num_list = sorted(num_list)
print(num_list) # [44, 5, 12, 100, 32]
print(new_num_list) # [5, 12, 32, 44, 100]
new_num_list = sorted(num_list, reverse=True)
print(new_num_list) # [100, 44, 32, 12, 5]
fruits = ['Apple', 'Banana', 'Mango', 'Pineapple',
'Coconut', 'Orange', 'Strawberry', 'Lemon', 'Mango']
my_fruits = fruits # 얕은 복사
my_fruits[0] = 'Guava'
print(my_fruits)
print(fruits)
둘의 결과는 모두 ['Guava', 'Banana', 'Mango', 'Pineapple', 'Coconut', 'Orange', 'Strawberry', 'Lemon', 'Mango'] 가 나온다.
그걸 막기 위해서는 copy() 함수를 사용 해야한다.
my_fruits = fruits.copy()
my_fruits[0] = 'Apple'
print(my_fruits)
print(fruits)
결과는
['Apple', 'Banana', 'Mango', 'Pineapple', 'Coconut', 'Orange', 'Strawberry', 'Lemon', 'Mango']['Guava', 'Banana', 'Mango', 'Pineapple', 'Coconut', 'Orange', 'Strawberry', 'Lemon', 'Mango']