카테고리 없음

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

kimjunki-8 2024. 9. 28. 21:19

Text formatting

1. INT, STRING, VARIABLES

1. putting INT data into text

Ex.

"I eat %d apples." % 3

result -> I eat 3 apples

%d -> for INT data type

 

2. putting STRING data into text

"I eat 3 %s." % "apples"

result -> I eat 3 apples

%s -> for STRING data type

BUT, must covered with "" -> because it's the string

3. putting VARIABLES data into text

number = 3
apple = 'I ate %d apples' %number
print(apple)

result = I ate 3 apples

But must consider the type of data too. number = 3 = INT = %d

 

Or 

I can put data of more than 2

number = 2
today = 'today'
day = '%s, I ate %d apples' %(today, number)
print(day)

Result -> today, I ate 2 apples

But -> must covered with () and separated by ,

 

codes

%s -> STRING

%c -> CHARACTER

%d -> INT

%f -> FLOAT

%% -> just % itself (showing % in the text)

AND for %s,

%s can be used for all types of data

"I have %s apples" %3
"rate is %s" %3.234

Result ->

I have 3 apples

rate is 3.234

 

Input

-> makes the input type data and returns the input data

Ex.

name = input('name:')
print(name)

 

then It will show this in the terminal

name:

If I write Kevin Kim here,

name: Kevin Kim

result -> Kevin kim

It will return the value of the input data

 

f-string formatting

f -> bring data 

ex.

name = input('이름을 입력하세요')
age = input('나이를 입력하세요')
print(f'안녕하세요, {name}님! 당신의 나이는 {age}살이군요.')

f-string will bring name and age variables and make them usable in the text using {}.

 

Global Variable -> Can be used in the whole program

Local Variable -> Only available in certain areas (function etc)

 

Operator

산술 연산자 (Arithmetic Operators)

비교 연산자 (Comparison Operators)

논리 연산자 (Logical Operators)

대입 연산자 (Assignment Operators)

비트 연산자 (Bitwise Operators)

맴버십 연산자 (Membership Operators)

식별 연산자 (Identity Operators)

1. Arithmetic Operators

+ -> plus

- -> minus

* -> multiply

/ -> dividing

% -> will only get the remains from dividing

** -> Exponent

// -> will only get the share

2. Comparison Operator

== -> return true if values are same (3 == 3) -> true

!= -> return true if values are different (3 != 4) -> true

> -> return true if the left value is bigger (4 > 3) -> true

< -> return true if the right value is bigger (3 < 4) -> true

>= -> return true if the left value is bigger or the same (3 >= 3) or (4 >= 3) -> true

<= -> return true if the right value is bigger or the same (3 <= 3) or (3 <= 4) -> true

3. Logical Operators

and -> return true if both conditions are satisfied

or -> return true if one of the conditions is satisfied

not -> reverse logical value

4. Assignment Operators

= -> same as -> a= 5

+=  -> same as a = a + 5

-=  -> same as a = a - 5

*= same as a = a * 5

/= same as a = a / 5

%= same as a = a % 5

**= same as a = a ** 5

//= -> same as a = a // 5

5. Bitwise Operators(Binary)

1. Binary to Decimal

2. Decimal to Binary

& -> same as and 

Ex.

a= 101101

b= 010111

----------------

     000101 -> returns 1 or 0 if that line matches correctly

| -> same as or

Ex.

a= 101101

b= 010111

----------------

     111111 -> return 1 or 0 if that line doesn't match correctly

^ -> reverse of or

Ex.

a= 010101

b= 010111

----------------

     101010 -> like 0 = 0 -> 1, 1 = 1 -> 0 1 = 0 -> 1

~ -> same as not(I'm not sure about it but let's remember this as (x+1)*1

Ex.

x = 5

print(~5) -> result is -6

<< -> Just moving the numbers to the left

Ex.

b = 1(01)

b << 1 -> (10) = 2

b << 2 -> (100) = 4

>> -> Just moving the numbers to the right

 

6. Membership Operators

in -> return true if the chosen value is in the given text

Ex.

"d" in "dragon" -> true

not in -> return true if the chosen value is not in the given text

Ex.

"d" not in "apple" -> true

7. Identity Operators

Is -> Check if the two variables have the same object

Is not -> Check if the two variables have some object

 

Good Night