🐍 파이썬
-
-
[Function] 함수란🐍 파이썬/파이썬 기본 문법 2022. 7. 18. 08:17
•A function is a namedset of instructions that performs a specific task. •A function can take one or more parameters (arguments) and returnsa value. •First mechanism for abstraction and decomposition •Advantage of using function : - Facilitates code reuse - Helps avoiding repetitive code - Hides implementation details - Makes testing easier parameters : - You can specify a default valuefor one o..
-
[file] copy the file🐍 파이썬/파이썬 연습문제 2022. 7. 17. 07:42
문제 : Implement a function that copies the contents of a file to a new file. The function should add a header (one line of arbitrary text) to the top def copy_file(src, dest, header): with open (src, 'r') as f_in , open (dest, 'w') as f_out: f_out.write(header) for line in f_in.readlines: f_out.write(line)
-
working with file🐍 파이썬/파이썬 기본 문법 2022. 7. 17. 07:39
create file # opening a file in write mode will either create a new file (if the file does not exist) # or overwrite an existing one f = open('test.txt', 'w') read file f = open('test.txt', 'r') contents = f.read() # read the entire file content print(contents) contents = f.read(10) # read only the first 10 characters of the file print(contents) line = f.readline() # read first line of the file ..
-
[random/inf loop] guessing game🐍 파이썬/파이썬 연습문제 2022. 7. 17. 07:26
문제 : Assign a random value to a variable and ask the user to guess the value. If the guess is lower than the chosen value, print ‘too low’, if it is greater, print ‘too high’. In case the number is correct, ‘correct’ and terminate the program. take home: random library와 while True / break 구문을 활용한 infinite loop 작성하기 import random rnd = random.randint (0, 100) while True: guess = int input("guess ..
-
[input/string interpolation] 유저에게 인풋을 받아서 프린트하기🐍 파이썬/파이썬 연습문제 2022. 7. 17. 06:58
생각보다 쓰일일이없어서 검색하지않고서는 기억이 안날수있는 문법중에하나인 input function과 헷갈리는 string interpolation ! 문제 : Ask from the user the following information: name, age, occupation and print the following message using the provided values: Hi NAME! I see you are AGE years old and work as a OCCUPATION. # ask and store what the user has typed in a variable name = input('What is your name?') age = input('How old are you?')..