카테고리 없음

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

kimjunki-8 2024. 9. 29. 19:42

1. List

frame

variable = [element 1, element 2, element 3,..... ]

1.1 changing the element

variable[number] = 'element'

Ex.

a = [1,2,3]

a[1] = 4

result a = [1,4,3]

1.2 adding the element

variable.append('element')

1.3 removing the element

variable.remove('element')

1.4 checking the length of the list

len(variable)

Ex.

print(len(variable)) 

1.5 sorting the list

variable.sort()

Ex.

a.sort()

print(a)

result -> [1,3,4]

2. Tuple -> similar to list but cannot be edited once it is created

Ex.

a = (1,2,3) -> similar to list (a = [1,2,3]) -> but covered by ()

3. Dictionary

frame

variable = {"key" : "value", "key" : "value", "key" : "value",.....}

Ex.

a = {'first' : 1, 'second' : 2, 'third' : 3}
print(a['first'])

result -> 1

3.1 Adding the dictionary

variable['key'] = value -> just put the key which doesn't exist in the dict data as well as the value

Ex.

a = {'first' : 1, 'second' : 2, 'third' : 3}
a['fourth'] = 4
print(a)

result -> {'first': 1, 'second': 2, 'third': 3, 'fourth': 4}

Additionally,

If I put the existing key, the value will be changed.

variable['key(already exist)'] = value(changed)

 

3.2 Removing dict

del variable['key(already exist)']

 

3.3 finding key and value

variable.keys() -> will show all the existing keys 

variable.values() -> will show all the existing values

 

4. Set -> represents a set of non-duplicated elements. (must covered by {})

What if there is duplicated data? 

Ex. 

a = {1,2,3,3,4,5}

print (a)

result -> {1,2,3,4,5} 

4.1 adding element

variable.add(element)

4.2 removing element

variable.remove(element)

What if I want to add between two sets?

variable1.union(variable2)

What if I want to get the duplicated data?

variable1.intersection(variable2)

 

Changing the type of data

1. Explicit Conversion

1.1 int()

1.2 float()

1.3 str()

1.4 bool()

1.5 list(), tuple(), set()

Ex.

a = 6.4343 -> float

b = '5944'

c = int(6)

d = int(b)

result

6

5944

BUT must be aware of the types of data

'3.14'(str type) -> int('3.14') X , float('3.14') O

 

bool1 = 0

bool2 = ""

 

bool(bool1) 

bool(bool2) 

result

False -> 0 is the common data type of false
False -> Empty value, empty list, empty dict, etc(mostly empty data) are considered as false data type

For list(), tuple(), set()

Ex.

a = 1

list(a) -> X 

a = [1,2,3,4]

list (a) -> [1,2,3,4]

tuple (a) -> (1,2,3,4)

set (a) -> {1,2,3,4}

 

str and list can be worked together

a = "KEVIN"
b = list(a)
print(b)

result -> ['K', 'E', 'V', 'I', 'N']

So, I can work like this

a = "KEVIN"
b = list(a)
b[1] = 'I'
print(b)

result -> ['K', 'I', 'V', 'I', 'N']

 

2. Implicit Conversion -> automatically change the type of data

Ex.

a = 1

b = 1.5

print (a + b)

result -> 2.5

 

Control statement, loop statement

1. if

frame

if condition:

       codes

Ex.

a = 2

if a == 2: -> should use ==, <, >, etc

      print(a)

2. else -> used when the condidtion are not satisfied

Ex.

a = int(input('Input value'))
if a > 20:
    print('bigger than 20')
else:
    print('whatever')

If -> input 40 -> result: bigger than 20

If -> input 15 -> result: whatever

3. elif -> since it is not useful to keep write if, so elif is used when to put mutiple condition

Ex.

a = int(input('Input value'))
if a > 20 and a <39:
    print('bigger than 20')
elif a > 40:
    print('bigger than 40')
else:
    print('whatever')

If -> input 40 -> result: bigger than 20

If -> input 50 -> result: bigger than 40

If -> input 15 -> result: whatever

 

Nested Conditions (if:if)

Ex.

a = int(input('number: '))
if a == 5:
    b = int(input('password'))
    if b == 10:
        print('passed: ')
    else:
        print('denied')
else:
    print('nah')

If I input 13, then It will show 'nah' but, If I input 5, then it will show 'password'. If I input 10 again, It will show passed or else denied

Tip. -> .lower() -> willt turn the input value into lowercase

 

Loop statement

1. for -> reiterate based on the collection(list, tuple, string text) 

frame

for variable in collection:
          codes

2. while -> will continuesly reiterate codes till the condition is satisfited

frame

while condition:

          codes

3. break -> stop the codes when certain condition are met

4. continue -> will skip next code and continue the next loop

for i in range(1, 6): -> 1,2,3,4,5
    if i % 2 == 0: -> 2,4
        continue -> skip
    print(i) -> 1,3,5

Tips

range() -> same as like ~

range(1~11) -> will get 1~10(except the last one)

range(number) -> will get 0~number

range(start, stop) -> will get start to start -1

range(start, stop, step) -> will get number base on the stop

ex. (0, 21, 2) -> 2, 4, 6, 8, etc...

 

enumerate() -> shows the order of the value

ex.

a = [1,2,3,4,5]
for b, c in enumerate(a):
    print(b ,c)

result ->

0 1
1 2
2 3
3 4
4 5

Good night!