Variables
Follow along: Variables
The program launching process along with parameter settings are all simplified and set up on the Jupyter Notebook Environment.
- Create a new *.ipynb file Jupyter Notebook
- Fill in the content below in the newly created file
- Follow and Execute the example codes
(The Jetson Board used for these examples are => Jetson Nano)
Let’s write the following examples.
Numeric Variables
# 1
a = 10
b = 20
c = a + b
print(c)
print(type(c))
# 2
x = 3.3764
y = 6/2
print(x, y)
print(type(x), type(y))
# 3
a = 10
b = 20
c = a + b * 10 -5 / 5
print(c)
# 4
x = 10
x += 20 # x = x + 20 과 동일
print(x)
# 5
a = input('첫번째 숫자를 입력하세요: ')
b = input('두번째 숫자를 입력하세요: ')
c = int(a) + int(b)
print(c)
# 6
x = 3
y = 5
x *= x + y # x = x * (x + y) 과 동일
print(x)
String Variables
# 1
a = 'x'
b = 'I am ok.'
c = "안녕하세요."
print(a)
print(b)
print(c)
print(type(c))
# 2
x = 'I am happy!'
print(x)
print(x[0])
print(x[0:3])
print(x[5:])
print(x[-1])
print(x[-3:])
print(x[-4:-2])
# 3
a = True
b = False
print(a)
print(b)
c = 10 > 20
print(c)
print(type(a))
# 4
name = '홍지수'
greet = name + '님 안녕하세요!'
print(greet)
# TypeError
eng = 100
result = '파이썬 점수: ' + eng + '점'
print(result)
# 5
hello = '안녕' * 5
print(hello)
# 6
a = '쥐 구멍에 볕 들 날 있다.'
b = len(a)
print(b)
# 7
name = '김수영'
a = '나는%s입니다.' % name
print(a)
# 8
name = '홍길동'
age = 30
height = 173.7
print(name, age, height)
# 9
year = 2020
month = 3
day = 5
print(year, month, day, sep='/')
# 10
x = 10
y = 20
print('x = ' + str(x) + ', y = ' + str(y))
# 11
life = 'too\nshort'
print(life)
List Data Type
# 1
a = [1, 2, 3]
print(a)
print(a[0])
print(a[0] + a[2])
print(a[0] + a[-1])
print(a[1] * 2)
# 2
b = [1, 2, 3, ['a', 'b', 'c']]
print(b)
print(b[0])
print(b[3])
print(b[3][0])
print(b[3][0:2])
# 3
c = [1, 2, 3, 4, 5]
print(c)
print(c[0])
print(c[0:2])
print(c[2:4])
print(c[4:5])
print(c[0:5])
# 4
d = [1, 2, 3, 4, 5, 6]
d1 = d[:3]
print("d1 :", d1)
d2 = d[3:]
print("d2 :", d2)
d3 = d1 + d2
print("d3 :", d3)
# 1
a = ['b', 'y', 'e']
print(a)
print(a * 3)
# 2
b = ['b', 'y', 'e']
print(b)
print('b[0]:', b[0])
b[0] = 'B'
print(b)
# 3
c = ['h', 'e', 'o']
print(c)
print('c[1:2]:', c[1:2])
c[1:2] = ['e', 'l', 'l']
print(c)
# 4
d = [1, 2, 3, ['a', 'b', 'c'], 4, 5]
print(d)
print(d[2:5])
print([d[3][:2]])
# 1
a = [82, 43, 92, 66, 1]
a.sort()
print(a)
a.reverse()
print(a)
# 2
b = ['nice', 'to', 'meet']
print(b)
b.append('you')
print(b)
# 3
c = ["see", "again"]
print(c)
c.insert(1, "you")
print(c)
# 4
d = ["time", "to", "say", "goodbye"]
print(d)
del d[3]
print(d)
Tuple Data Type
# 1
a = ('R', 'E', 'D', 'R', 'U', 'M')
print(a)
# TypeError
b = ('R', 'E', 'D', 'R', 'U', 'M')
b[0] = 'B'
print(b)
# TypeError
c = ('R', 'E', 'D', 'R', 'U', 'M')
del c[0]
print(c)
# TypeError
d1 = ('R', 'E', 'D', 'R', 'U', 'M')
d2 = ['r', 'e', 'd', 'r', 'u', 'm']
d = d1 + d2
print(d)
Dictionary Data Type
a = {'name': 'pey', 'phone': '010-9999-1234', 'birth': '1118'}
print(a.keys())
for k in a.keys():
print(k)
list(a.keys())
print(a)
print(a.values())
print(a.items())
a.clear()
print(a)
a = {'name': 'pey', 'phone': '010-9999-1234', 'birth': '1118'}
print(a.get('name'))
print(a.get('phone'))
a = {'name':'pey', 'phone':'010-9999-1234', 'birth': '1118'}
print('name' in a)
Set Data Type
s1 = set([1, 2, 3])
print(s1)
s2 = set("Hello")
print(s2)
s1 = set([1, 2, 3, 4, 5, 6])
s2 = set([4, 5, 6, 7, 8, 9])
print(s1 & s2)
print(s1.intersection(s2))
print(s1 - s2)
print(s2-s1)
print(s1.difference(s2))
print(s2.difference(s1))
Bool Data Type
a = [1, 2, 3, 4]
while a:
print(a.pop())