---
title: HW2
tags: dataframe
---
<style>
.green {
padding: 0 2px;
white-space: pre-line;
border-radius: 2px;
background-color: #CCFF99;}
.red {
padding: 0 2px;
white-space: pre-line;
border-radius: 2px;
background-color: #FFA488;}
.blue {
padding: 0 2px;
white-space: pre-line;
border-radius: 2px;
background-color: #77DDFF;}
.purple {
padding: 0 2px;
white-space: pre-line;
border-radius: 2px;
background-color: #D1BBFF;}
</style>
# `HW2重點小整理:`
> format( ): 對字串進⾏格式化
> ex:
> ```python
> print ('{0} study in {1}'.format("John","FCU"))
> ```
### **寫下第⼀隻Python程式**
```python
print ("This is my first Python program")
```
### **假設你⽤每股80元買進⼀張(1000股)台灣的上市股票,⼀個⽉以後以87賣出,請問獲利是多少?投報率是多少?**
```python
nBuy=80
nSell=87
nProfit=(87-80)*1000
nRate=nProfit/(nBuy*1000)
print ('獲利 :{0}元'.format(nProfit))
print ('投報率 :{0:.2f}%'.format(nRate*100))
```
> {0:.2f}第0個變數,⼩數點後第2位
> <span class="green">查找 % 的意思</span>
> -> 百分比的意思
### **假設有⼀字串(string)如下:s ='I am a fourth grade student at FCU’印出這個字串的長度為何**
```python
s ='I am a fourth grade student at FCU'
print('字串的長度為 {0}'.format(len(s)))
```
> 使⽤==len( )函數==,可以知道字串的長度
> 使⽤==lower( )函數==,可以將字串中所有字元符號變成⼩寫
> ex:
> ```python
> strA = "Hello, World!"
> print(strA.lower())
### **假設有⼀串列(list)如下:list_fruit= ['banana','orange', 'melon', ‘apple', ‘mango',‘cherry', 'kiwi']印出這個串列中的項⽬ 'melon', 'apple', 'mango'**
```python
list_fruit= ['banana','orange', 'melon', 'apple','mango','cherry','kiwi']
print (list_fruit[2:5])
```
> list (串列)
> ```python
> lst=[1,2,3,4,5,6]
> lst #[1,2,3,4,5,6]
> lst[:] #[1,2,3,4,5,6]
> lst[0] #the first item 1
> lst[-1] #the last item 6
> lst[0:] #[1,2,3,4,5,6]
> lst[0:-1] #[1,2,3,4,5]
> lst[1:-2] #[2,3,4]
### **假設某學⽣的課程分數使⽤字典(dict)表⽰如下:score = {'homework':92,'mid-term':85,'final-term':92},⽽作業分數佔50%, 期中考30%, 期末考20%, 請印出這位學⽣學期末成績**
```python
score = {'homework':92,'mid-term':85,'final-term':87}
final=score['homework']*0.5 + score['mid-term']*0.3 +
score['final-term']*0.2
print('期末成績:{0:.1f}分'.format(final))
```
> 使⽤key['model']取得字典的value
> {0:.1f}第0個變數,⼩數點後第1位