코딩
-
[Python tutorial] 23. Tuple Methods코딩/Python 2022. 12. 21. 17:28
tuple.count(value) Returns the number of times a specified value occurs in a tuple thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5) x = thistuple.count(5) tuple.index(value) Searches the tuple for a specified value and returns the position of where it was found thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5) x = thistuple.index(8)
-
[Python tutorial] 22. Dictionary methods코딩/Python 2022. 12. 21. 17:27
dictionary.clear() Removes all the elements from the dictionary car = { "brand": "Ford", "model": "Mustang", "year": 1964 } car.clear() dictionary.copy() Returns a copy of the dictionary car = { "brand": "Ford", "model": "Mustang", "year": 1964 } x = car.copy() dict.fromkeys(keys, value) Returns a dictionary with the specified keys and value x = ('key1', 'key2', 'key3') y = 0 thisdict = dict.fro..
-
[Python tutorial] 21. List/Array methods코딩/Python 2022. 12. 21. 17:18
list.append(elmnt) Adds an element at the end of the list fruits = ['apple', 'banana', 'cherry'] fruits.append("orange") list.clear() Removes all the elements from the list fruits = ['apple', 'banana', 'cherry', 'orange'] fruits.clear() list.copy() Returns a copy of the list fruits = ['apple', 'banana', 'cherry', 'orange'] x = fruits.copy() list.count(value) Returns the number of elements with t..
-
[Python tutorial] 20. String methods코딩/Python 2022. 12. 21. 17:18
string.capitalize() Converts the first character to upper case txt = "hello, and welcome to my world." x = txt.capitalize() print (x) string.casefold() Converts string into lower case txt = "Hello, And Welcome To My World!" x = txt.casefold() print(x) string.center(length, character) Returns a centered string txt = "banana" x = txt.center(20) print(x) string.count(value, start, end) Returns the ..
-
[Python tutorial] 19. Built-in Function코딩/Python 2022. 12. 21. 17:18
abs(n) Returns the absolute value of a number x = abs(-7.25) all(iterable) Returns True if all items in an iterable object are true mylist = [True, True, True] x = all(mylist) any(iterable) Returns True if any item in an iterable object is true mylist = [False, True, False] x = any(mylist) ascii(object) Returns a readable version of an object. Replaces none-ascii characters with escape character..
-
[Python tutorial] 18. File handling코딩/Python 2022. 12. 21. 17:17
File Handling File Open: open(filename, mode) "r" - Read, Default value, error if the file does not exist "a" - Append, creates the file if it does not exist "w" - Write, creates the file if it does not exist "x" - Create specified file, returns an error if the file exists "t" - Default value. Text mode "b" - Binary mode (e.g. images) Syntax f = open("demofile.txt", "rt") Read demofile.txt Hello..
-
[Python tutorial] 17. Try...Except코딩/Python 2022. 12. 21. 17:17
Try Except try: 에러 발생 확인을 위한 테스트. 에러가 생겼을 때 'exception' 호출 except: 에러 발생한 경우의 실행문 else: 에러가 없을 경우 실행문 finally: 에러 발생 유무에 관계없이 실행 try: print("Hello") except: print("Something went wrong") else: print("Nothing went wrong") Finally try: f = open("demofile.txt") try: f.write("Lorum Ipsum") except: print("Something went wrong when writing to the file") finally: f.close() except: print("Something went..
-
[Python tutorial] 16. Classes, Objects and Inheritance코딩/Python 2022. 12. 21. 17:16
Classes and Objects OOP(Object Oriented Programming)언어: 속성과 메서드(명령, 함수)를 가지는 객체 클래스: 객체를 생성하기 위한 기본청사진 객체: 실제 사용되는 대상 Create a Class: class class MyClass: # create a class 'MyClass' x = 5 # property p1 = MyClass() # create a object 'p1' print(MyClass) # print(p1.x) # 5 class Person: def __init__(self, name, age): self.name = name self.age = age def __str__(self): return f"{self.name}({self.age})..