카테고리 없음

스파르타 AI-8기 TIL(9/12)

kimjunki-8 2024. 9. 12. 19:13

Alright, let's do it

오늘만 외부 강의를 통해 배우도록 하자!(maybe?)

Python D-1 (Starts with Hello World)

Step 1. print (...)

print -> This is display all the texts written inside of (.....)

Remember - Don't forget to use -> ""

I also must consider the types of text or number

1. int(integer) - ex. 1, 2, 3, 4....

2. float - ex. 3.14, 5.16

예를 들어,

x = 10 -> For this, x는 상자라고 치고 이름만 x라고 정의한 것, 그 안에는 타입에 알맞은 것을 넣으면 된다

여기서또 다른 포인트, 정수인지, 텍스트인지, 아니면 또 다른 데이터인지 확인 한 후 만들자.

x라는 상자를 만든다 한들, x = hello world라고 하니 에러가 떴다. 즉, 알맞게 사용해야겠다.

확실히 따로 상자를 만들어 그 안에 넣다보니 나중에 사용할 때 용이했다. 

print (10-5)보다

 

x = 10

y = 5

print (x-y) - much better

tips! -> remember
* -> Multiply

/ -> division

// -> division but in float type

% -> It only gets the remain

** -> Exponent

or

I could see that there are multiple things that I must remember. not like X x Y but X*Y

divmod() -> shows the answer of both / and %

print (divmod(x ,y))

 

문자열을 쓸 때에는 상자를 만들고 상자 안에 넣을 문자를 넣기위해 ""을 쓴다 -> Don't forget

 

Things that are needed when using string

escape

\\ -> \

ex -> print ("hello\\world") -> hello\world

\n -> line break

ex -> print ("hello\nworld") -> hello

                                              world

\b -> backspace

ex -> print ("hello\bworld") -> hellworld - hello -> hell

\t -> tap

ex -> print ("hello\tworld") -> hello      world

\' or \" -> just ' or "

ex -> print("hello\"world) -> hello"world

but isn't it the same as " ' "?

ex->

print ("hello'world'") -> hello'world'

realized that I could not add another ""

print ("hello"world"") -> error

print ("hello\"world\"") -> correct -> hello"world"

 

x = "hello"

y = "world"

print (x + y) -> helloworld

print (x*3 + y*2) -hellohellohelloworldworld

 

len -> shows the length of the text

print(len(x)) -> 5

indexing and slicing -> probably using []

ex-> print (x[0]) -> h ('h'ello) -> means 'please take the 0th text'

remember!!!!!!! -> It always starts with 0. 

h - 0

e - 1

l - 2

l - 3

o - 4

print (x[:2]) -> he -> means 'please takes only 2 texts'

 

.replace -> replacing the texts 

ex -> print (x.replace("hello", "hi")) -> means print x but change it to 'hi' from 'hello' ("hello" -> "hi")

 

text formatting (%s, %(...))

tips

%s- string

%d- int

%f- float

ex.

x = "Kevin"

I must be awarded the types. %s, %d, %f

print ("Hello, my name is %s." %x) -> Hello, my name is Kevin

y = 3

print("I'm %d years old" %y) -> %d = text type, %y = choosing y to insert number or text to %d

ex. 

a = "Nice to "

b = "meet you"

c = "Jenny"

d = 24

print ("%s%s, my name is %s and I'm %d years old." %(a, b, c ,d))

-> must make sure it follows in order 

-> Nice to meet you, my name is Jenny and I'm 24 years old

format

print ("{0}{1}, my name is {2} and I'm {3} years old.".format(a, b, c, d))

-> Nice to meet you, my name is Jenny and I'm 24 years old

It's like the %s thing but it seems that it is much more convenient to recognize the order.

 

f- string

print (f"{a}{b}, my name is {c}" )

-> Nice to meet you, my name is Jenny

-> I need to put the a, b, c into wherever I want -> pretty easy

 

float f{num: rounding)

f = 64.123456789

print (f{f:0.4f) (will be rounded at 5th num) (remember to put f(0.4f))

-> 64.1235

 

finding or counting the texts

counting(.count("text you want to count")

x = "aaaabbbccccddd"

print (x.count("a")) 

-> 4 = means there are four a in the sentence

finding(.find("text you want to find"))

print (x.find("c"))

-> 7 = means the first spell c is at the 7th in the sentence (remember it always starts with 0)

It will show -1 if I search for a number or text that doesn't exist in the sentence

.index = same as .find but it will not return -1 but gives you an error 

 

.join()

print("/".join(x)) -> a/a/a/a/b/b/b/c/c/c/c/d/d/d

It will insert / every side of the texts

 

Upper or lower spelling

print (x.upper()) -> AAAABBBCCCCDDD

print (x.lower()) -> aaaabbbccccddd

 

removing blank (.rstrip(), .lstrip(), strip())-> seems it removes all the blanks in the sentence

x = "   A   "

print(x.rstrip()) ->    A

print(x.lstrip()) ->A

print(x.strip())  ->A

 

split (.split(symbol to split between the texts)

it can be .split() - blank, but if there is a certain symbol, you must add ""

x = "A : B"

print (x.split(" : "))

 

Done for today 'V'b

 

Things that I must remember today

1. How to write and use the factors. (.split, .rstrip etc.)

2. Must know the types of the text.