[ROS2] 1주차_(2)

존진·2023년 10월 20일

📌 식별자

  • _*: 대화식 인터프리터가 마지막 평가 결과를 저장하는 데 사용
  • __*__: 시스템 정의 이름
  • __*: 클래스의 비공개 이름

📌 문자열 리터럴 연결

>>> "hello"'world'
>>> "hello" "world"
>>> "hello" + "world"

결과: 'helloworld'

📌 문자열 변환 필드

"이름은 %s 입니다" % "철수"
"이름은 %s, 나이는 %d살 입니다." % ("철수", 20)


📌 int/float 리터럴

count = 0


def int_literal():
	print('\n>>>int_literal():')
    n1 = 1_234_567
    display_info("n1", n1)
    n1 = 1234567
    display_info("n1", n1)
    n1 = n1+1
    display_info("n1", n1)
    
    #print('n1 = ' + n1)
    print('n1 =' + str(n1))
    print('n1 =' + format(n1, ',d'))
    print('n1 =' + format(n1, '12,d'))
    print('n1 =' + format(n1, '012,d'))
    print('n1 = %d' % n1)
    print(f'n1 = {n1:,}')
    print(f'f\'n1 = {n1:15,d}')
    
    n2 = int()
    display_info("n2", n2)
    n3 = int('0'
    display_info("n3", n3)
    
    n3 = 31
    #이진수, 팔진수, 십육진수 변환 함수
    print(n3, bin(n3), oct(n3), hex(n3))
    
    # 이진수 0b, 팔진수 0o, 십육진수 0x 표기법
    result = (n3 == 0b1_1111 == 0o37 == 0x1F)
    print('n3 == 0b1_1111 == 0o37 == 0x1F -->', result)
    
    
    def float_literal():
    	print('\n>>>float_literal():')
        f1 = 1_234.567
        display_info("f1", f1)
        
        print(format(f1, 'f'))
        print(format(f1, ',.2f'))
        print('f1 = %10.2f' % f1)
        print(f'f1 = {f1:,.4f}')
        
        f2 = float()
        display_info("f2", f2)
        
        result = 3.14 == 0.314e1 ==0.0314E2
        print('3.14 == 0.314e1 == 0.0314E2 -->', result)
        
        
        def bool_literal():
        print('\n>>>bool_literal():')
        b1 = True
        display_info("True", b1)
        
        b2 = bool()
        display_info("bool()", b2)
        
        b3 = bool('False')
        display_info("bool('False')", b3)
        
        b4 = bool('test')
        display_info("bool('test')", b4)
        
        b5 = True and False
        display_info("True and False", b5)
        
        
        def display_info(name, value):
        	global count
            print("(%s) %s: id = %s, type = %s, value = %s" %
            	(count, name, id(value), type(value), value))
                count+=1
                
                
        if __name__ == "__main__":
        	int_literal()
            float_literal()
            bool_literal()
    

0개의 댓글