Variables Assignment, Math Ops and Data Types是變數賦值、數學運算與資料類型。
學習4種定義東西跟他的屬性的基礎語法。
- String(str):字串
- 用單引號(’ ’)或是雙引號(” ”)包起來的
- 如果要連結英文字,用底線(_)
- 用途:顯示公司名稱
- Floating Point(float):浮點數
- 帶有小數點的實數除以整數部分和小數
- integer(int):整數
- 顯示整數,不包含小數點
- boolean(bool):布林值,代表有兩種可能(true or false)
- type:種類
- 用來檢查屬性,會把某個資料的屬性顯示出來
練習
Assume that you have $5000 in your brokerage account and you decided to invest the entire amount in Procter & Gamble (P&G) stock which is trading at $142.5 per share. Complete the following tasks:
- 1. Write a Python code that defines two variables for the account balance and P&G stock price. Feel free to select any valid variable names
- 2. Confirm variables datatypes
- 3. Calculate number of shares that you can buy with the available balance in your account
# Define the account balance
balance = 5000
balance
# Confirm the datatype which is integer
type(balance)
# Define the price per share
price = 142.5
price
# Confirm the datatype which is float
type(price)
# Calculate the number of shares by dividing the brokerage balance by P&G stock price
# Place the answer in num_shares
num_shares = balance/price
num_shares
# Calculate the number of shares by dividing the brokerage balance by P&G stock price
# Place the answer in num_shares
num_shares = balance/price
num_shares
# Note that you can round your answer to the nearest integer by using the round function
round(num_shares)
你好,我是蔡至誠PG,任職於《阿爾發證券投顧》,《我畢業五年,用ETF賺到400萬》作者,《提早五年退休:PG 財經個人財務調配術》講師。