name="john"
name="mary"
age=18
sex= "F"
# case 1, placeholder code, %s string, %d integer, %f float, %x hex
print("name: %s, age: %d" %(name, age))
# case 2, convert data to str
#everything is string, so can connect with "+"
print("Name: "+ name +", Age: "+ str(age))
# case 3, str.format(), comparing with case 1, use {} instead of placeholder
print("Name: {}, Age: {}, Sex: {}".format (name,age,sex))
# case 4, named arguments
# easier to relocate variables
print("Name: {n}, Age: {a}, Name again:{n}, Sex: {s}, Sex again: {s}".format(n=name,a=age,s=sex))
# eg.
# 見括弧 先運算
print("Double {x}: {x}+{x}={sum_x}".format(x=5, sum_x=5+5))
# case 5: f-string python > 3.6
# f=format, more convenient version of line 18, case 4
print(f"Name: {name}, Age: {age}, Name again:{name}, Sex: {sex}, Sex again: {sex}")
x=5
sum_x=5+5
print(f"Double {5}: {5}+{5}={5+5}")
print(f"Double {x}: {x}+{x}={sum_x}")
# format number
price=49.9
tax=0.08
total=price * (1+tax)
print(f"Price: ${price:.2f}|Tax: {tax:.1%}|Total: ${total:.2f}")
## f"{variable:[fill][align][width][.precision][type]}
print('1---5----01---5----0')
print(f"{age:5d}") #{variable:[width][type]}
#5=alignment, d=integer
print(f"{'Name':<10}{'Age':>5}")
print(f"{name:<10}{age:>5.2f}")
print(f"{'Apple':^10}")
print(f"{name:<<10}{age:->5d}")
print(f"{42:05}")
print(f"{1234567:,}")
print(f"{42:5b}") # binary value
print(f"{42:5o}") # oct value
print(f"{42:5x}") # hex value
# sci format
print(f"{42:.5e}") #value in e format
print(f"{42:.5E}") #e=10^5
print(f"{13.14564567:.5g}") #significant figure
print(f"{111113.1234567888:.5G}")
print(f"{0.14123456:.5%}") # % made this become a percent num, hence 14.12346%
# Boolean
print(True, False, type(True)) #True internally = 1, False = 2
print(True==1) # == means compare
x = True and 1 and [1] and {0} and 1.0 and "hello"
print(x)
y = False or 0 or [] or {} or None or 0.0 or ''
print(y)
# and 找第一個假:沒找到假,就回最後一個。or 找第一個真:沒找到真,就回最後一個。
default_city='kw'
city=''
city=city or print("please input city") or default_city
print(city)
# [] list, everything included in the bracket will have assigned numbers accordingly, collectable
name=["john","ann","mary","peter",["ryan",1],2,3,4,[True,0.0,9,"hello"]]
print(name, id(name),type(name))
print(name[0],name[4])
name[0]="david"
name[4][0]="goodman"
print(name)
del name[1]
print(name)
# format number
price=49.9
tax=0.08
total=price*(1+tax)
print(f"Price: ${price:.2f}|Tax: {tax:.1%}|Total: ${.2f}")