Enclose in quotation marks or single quotes
hi = "hello there"
concatenate strings
: put the strings together
hi = "hello there"
name = "ana"
greet = hi + name
print(greet) # hello thereana
greeting = hi + " " + name
print(greeting) # hello there ana
silly = hi + " " + name * 3
print(silly) # hello there anaanaana
x = 1
print(x)
x_str = str(x)
print("my fav num is", x, ".", "x =", x) # my fav num is 1 . x = 1
# 두 object 사이에는 자동으로 공백이 생성. 즉, 콤마 자리는 하나의 공백으로 처리.
# object가 반드시 string일 필요가 없다.
print("my fav num is " + x_str + ". " + "x = " + x_str)
# concatenate : 모든 object가 반드시 string이어야 한다.
text = input("Type anything... ")
print(5*text)
# Type anything... string
# stringstringstringstringstring
num = int(input("Type a number... "))
print(5*num)
# Type a number... 5
# 25
x = float(input("Enter a number for x : "))
y = float(input("Enter a number for y : "))
if x == y :
print("x and y are equal")
if y != 0 :
print("therefore, x / y is", x/y)
elif x < y :
print("x is smaller")
else :
print("y is smaller")
print("thanks")
i and j are var. names
comparisons below evaluate to a Boolean
: i > j
: i >= j
: i < j
: i <= j
: i == j (equality)
: i != j (inequality)
string vs num(int, float)
: not allowed
string vs string
: 오름차순
a and b are boolean var.
not a
: True if a is False
: False if a is True
a and b
: True if both are True
a or b
: True if either or both are True
pset_time = 15
sleep_time = 8
print(sleep_time > pset_time) # False
derive = True
drink = False
both = drink and derive
print(both) # False
if <condition>:
<expression>
<expression>
...
if <condition> :
<expression>
<expression>
...
else :
<expression>
<expression>
...
if <condition> :
<expression>
<expression>
...
elif <condition> :
<expression>
<expression>
...
else :
<expression>
<expression>
...
while <condition> :
<expression>
<expression>
...
n = 0
while n < 5 :
print(n)
n = n+1
# 0
# 1
# 2
# 3
# 4
while <condition_1> :
while <condition_2> :
<expression_a>
break
<expression_b>
<expression_c>
mysum = 0
for i in range(5, 11, 2) :
mysum += i
if mysum == 5 :
break
mysum += 1
print(mysum) # 5
for <variable> in range(<some_num>) :
<expression>
<expression>
...
for n in range(5) :
print(n)
# 0
# 1
# 2
# 3
# 4
mysum = 0
for i in range(7, 10) :
mysum += i
print(mysum) # 24 = 7 + 8 + 9
mysum = 0
for i in range(5, 11, 2) :
mysum += i
print(mysum) # 21 = 5 + 7 + 9