데이터 구조 및 분석 ch_1_4 Variable Statements and Operators
Jul 3, 2021
»
writing
KAIST 산업및시스템공학과 문일철_ 데이터 구조 및 분석 수업을 참고하여 작성하였습니다
ch_1_4 Variable Statements and Operators
Variable Statements and Operators
1. Data Types 1) Numeric Data Type - integer : signed interger, 32bit - float : 64 bit double precision. like 1.23 - long integer : Large integer like 1.2333 L - octal integer : base-8 integer - hexadecimal integer : base-16 integer 2) String Data Type - character string : ordered collection of characters 3) Collection Data Type - list : ordered collection of object - dictionary : collection of associated key data pairs - tuple : similar to list 2. Variable Statements
def main() :
numYearBase10 = 2021
numYearBase8 = 0o3745
numYearBase16 = 0x7e5
# -> integer in decimal, octal, hexadecimal
print("Year by base 10 : %d, by base 8: %d, by base 16 : %d"%(numYearBase10, numYearBase8, numYearBase16))
numComplex1 = complex(3, 4) # -> complex number
numComplex2 = 4+3j
print("Complex value : ", numComplex1)
print("Absolute value : ", abs(numComplex2))
print("Real value : ", numComplex2.real) # -> 실수
print("Image value : ", numComplex2.imag) # -> 허수
strDeptName = ("Industrial & System Engineering")
strUnivName = ("KAIST")
print("Department : ", strDeptName)
print("Full name of dept : ", strDeptName, ", ", strUnivName)
main()
# Year by base 10 : 2021, by base 8: 2021, by base 16 : 2021
# Complex value : (3+4j)
# Absolute value : 5.0
# Real value : 4.0
# Image value : 3.0
# Department : Industrial & System Engineering
# Full name of dept : Industrial & System Engineering , KAIST
3. Operator
def main() :
numTest1 = 10
numTest2 = 3.0
numPlus = numTest1 + numTest2
numMinus = numTest1 - numTest2
numMultiply = numTest1 * numTest2
numDivide = numTest1 / numTest2
numModula = numTest1 % numTest2
print("%d, %d, %d, %f, %d" %(numPlus, numMinus, numMultiply, numDivide, numModula))
# -> +, -, *, /, % (modulo operator)
numDivideInt = numTest1 /int(numTest2) # -> how to int, type casting
print(numDivide, numDivideInt)
numTest2, numTest1 = numTest1, numTest2 # -> swapping statement
print(numTest1, numTest2)
print(numTest1 == numTest2) # == equivalence
print(numTest1 != numTest2) # != in-equivalence
print(type(numTest1))
numTest1 = str(numTest1) # -> how to str, type casting
print(type(numTest1), numTest1)
strFormula = "2021 / 100"
print(eval(strFormula))
main()
# 13, 7, 30, 3.333333, 1
# 3.3333333333333335 3.3333333333333335
# 3.0 10
# False
# True
# <class 'float'>
# <class 'str'> 3.0
# 20.21