본문 바로가기

Python 배우기50

list_items.py animals = ['elephant', 'lion', 'tiger', "giraffe", "monkey", 'dog'] # create new list print(animals) animals[1:3] = ['cat'] # replace 2 items -- 'lion' and 'tiger' with one item -- 'cat' print(animals) animals[1:3] = [] # remove 2 items -- 'cat' and 'giraffe' from the list print(animals) animals[0:4] = [] print(animals) 산출물 (Output)['elephant', 'lion', 'tiger', 'giraffe', 'monkey', 'dog'] ['elep.. 2016. 3. 7.
list_operations.py animals = ['elephant', 'lion', 'tiger', "giraffe"] # create new list print(animals) animals += ["monkey", 'dog'] # add two items to the list print(animals) animals.append("dino") # add one more item to the list using append() method print(animals) animals[6] = 'dinosaur' print(animals) 산출물 (Output) ['elephant', 'lion', 'tiger', 'giraffe'] ['elephant', 'lion', 'tiger', 'giraffe', 'monkey', 'dog'].. 2016. 3. 7.
lists.py squares = [1, 4, 9, 16, 25] # create new list print(squares) print(squares[1:4]) 산출물 (Output)[1, 4, 9, 16, 25] [4, 9, 16] * A list is a data structure you can use to store a collection of different pieces of information under a single variable name. A list can be written as an array of comma-separated values (items) between square brackets, e.g. lst = [item1, item2]. Lists might contain items of.. 2016. 3. 6.
string_methods.py monty_python = "Monty Python" print(monty_python) print(monty_python.lower()) # print lower-cased version of the string print(monty_python.upper()) 산출물 (Output) Monty Pythonmonty python MONTY PYTHON * 유용한 string methods가 많이 있습니다. 문자열을 전부 소문자로 만들려면 'lower()' method를 사용합니다. 'upper()' method는 문자열을 대문자로 만들 때 사용합니다. To call any string method, type a dot after the string (or a variable containing the .. 2016. 3. 6.
character_escaping.py dont_worry = "Don't worry about apostrophes" print(dont_worry) print("The name of this ice-cream is \"Sweeet\"") print('text') 산출물 (Output)Don't worry about apostrophesThe name of this ice-cream is "Sweeet"text * Backslash is used to escape single or double quotation marks, for example 'It\'s me' or "She said \"Hello\"".The special symbol '\n' is used to add a line break to a string. 2016. 3. 6.
len_function.py phrase = """ It is a really long string triple-quoted strings are used to define multi-line strings """ first_half = phrase[0:int(len(phrase)/2)+1] # 변수 ‘phrase’에 포함된 문자열의 앞 절반을 얻기 print(first_half) 산출물 (Output)It is a really long string triple-quoted str * len() function은 string 이 몇 개의 글자를 포함하고 있는지 계산하는데 사용됩니다. 참고: type conversion 사용하기. 2016. 3. 6.