카테고리 없음

스파르타 AI-8기 TIL(10/1)

kimjunki-8 2024. 10. 1. 22:40

Review and make all the examples 

1. Function

import random as r
numb = r.randrange(1,100)
def num(numb):
    if numb == range(0, 50):
        print(f"뽑은 숫자는 50이하의 숫자인 {numb}입니다")
    elif numb == range(50, 100):
        print(f"뽑은 숫자는 50 이상의 숫자인 {numb}입니다")
    return numb
print(num(numb))

but

import random as r
numb = r.randrange(1,100)
def num(numb):
    if numb in range(0, 50):
        print(f"뽑은 숫자는 50 이하의 숫자인 {numb}입니다")
    elif numb in range(50, 100):
        print(f"뽑은 숫자는 50 이상의 숫자인 {numb}입니다")
    return numb
print(num(numb))

 

== -> in       = since == is only used when both sides are equal, but in will show if the number are included in variable

But I couldn't see the statements

It only showed the numbers 

print()

len()

type()

input()

a = input('who?')
print(a)
print(len(a))
print(type(a))
print(type([1,2,3]))
print(type(print))

result

who?me
me
2
<class 'str'>
<class 'list'>
<class 'builtin_function_or_method'>

As well as sum, max, min

c = [1,2,3,4,5]
print(sum(c))
print(max(c))
print(min(c))

15
5
1

round ->

c = 6.3423442
print(round(c, 4))

6.3423

 

setting default in function? 

def im(name = 'Kevin'):
    print(f"hi, {name}")
print(im())

hi, Kevin
None
hi, why
None

no problem

*args and **kwargs

def im(*args):
    sum1 = sum(args)
    return sum1
print(im(2,32,3,2,2,4))

 

remember ','

Ex.

import math  
result = math.sqrt(16)
print(result)

. means that the data in the back is fetched from the module in front.  

-> math.sqrt ->sqrt is placed inside of the math module

bringing sqrt from(.) math = same as -> from math import sqrt

. This means that the data in the back is fetched from the module in front.

 

self = to store its own data when the __init__ function is called. That's why it stores data called names such as self, name, etc.

class Noon:
    def __init__(self, *args):
        self.args = args
    def sum1(self):
        print(sum(self.args))

num2 = Noon([1, 2, 3, 4, 5])
print(num2.sum1())

-> error.

Good night(I had to suffer because of some error!!!!!! in Anaconda-> had to spend more than 5 hours to fix it....)