Mes
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
###### tags: `Python` # 變數與型態 (Variables) **<a href="https://hackmd.io/@Mes/python_note" class="redlink">點此回到 Python筆記 目錄</a>** ## 基本型態 | 關鍵字 | 型態 | | ------ | --------------------- | | int | 整數 | | float | 小數 | | string | 字串 | | bool | 布林值ex : true/false | ## 命名變數 我們需要給每個變數一個名字,這樣之後要使用這個變數的時候我們才能呼叫他,而在命名時有些規定要遵守。 + 硬性規定 : + 需以字母,數字和底線命名 + 不能以數字開頭 + 大小寫不同 + 不要跟關鍵字和系統保留字(像是函式的名字)一樣 + 常用習慣: + 用小寫字母來命名,如果包含多個單字,用底線連接 ## 使用變數 下面我們這個例子我們用變數儲存了整數,並以變數來進行運算: ```python= # 使用變數保存整數並進行運算 a = 321 b = 12 print (a + b) # 333 print (a - b) # 309 print (a * b) # 3852 print (a / b) # 26.75 ``` print是一個函式,可以用來印出括號內的東西,函式的概念後面會提到,妳可以先把它當作語法。 <br> 我們可以使用type函式來檢視變數的型態: ```python= a = 100 b = 12.345 c = 1 + 5j d = 'hello, world' e = True print (type(a)) # <class 'int'> print (type(b)) # <class 'float'> print (type(c)) # <class 'complex'> print (type(d)) # <class 'str'> print (type(e)) # <class 'bool'> ``` <br> 另外,python中可以使用函式來轉換型態: + `int()` 將一個數值或字串轉成整數,可以指定幾進位 + `float()` 將一個字串轉為浮點數 + `str()` 將指定的對象轉成字串,可以指定編碼 + `chr()` 將整數轉換成這個編碼對應到的字元 + `ord()` 將字元轉換成對應的編碼 字元可以想像為只有一個字的字串,我們電腦在存字母時都是以數字編碼來存取的,而這些數字編碼的方法有很多種,如ASCII、BIG5、UTF-8、UTF-16等等,有興趣的話可以看看這篇:**<a href="https://www.itread01.com/content/1546343830.html" class="redlink">連結</a>** 轉換型態的使用可以看下面這個示範: ```python = a = 100 b = 12.345 c = 1 + 5j d = "hello, world" e = "100" f = "7.5" print( int(e) + a ) #200 print( float(f) + b ) #19.845 print( str(a) + " " + str(b) ) #100 12.345 print( ord('a') ) #97 print( chr(97) ) #a ``` # 運算符 Python支持多種運算符,下表大略地按照優先度從高到低的順序列出了所有的運算符,運算符的優先度指的是多個運算符同時出現時,先做什麼運算然後再做什麼運算。除了我們之前已經用過的賦值運算符(=) 和算術運算符(加減乘除) ,我們稍後會挑一些其他運算符的使用來講解。 | 運算符 | 描述 | | ---------------------------------------------------------- | ------------------------------------ | | `[]` `[:]` | 下標,切片 | | `**` | 指數 | | `~` `+` `-` | 位元取反、正和負號 | | `*` `/` `%` `//` | 乘、除、取餘數、整除 | | `+` `-` | 加、減 | | `<<` `>>` | 左移、右移 | | `&` `^` `|` | 位元運算的and、xor、or | | `<=` `<` `>` `>=` | 小於等於、小於、大於、大於等於 | | `==` `!=` | 等於、不等於 | | `is` `is not` | 身分運算符 | | `in` `not in` | 成員運算符 | | `not` `or` `and` | 邏輯運算符 | | `=` `+=` `-=` `*=` `/=` `%=` `//=` `**=` `&=` `>>=` `<<=` | 復合賦值運算符,a = a+1 可以寫成a += 1 | ## 賦值運算符 賦值運算符應該算是最常見的運算符了,它的作用是將右邊的值賦給左邊的變量。下面的例子演示了賦值運算符和復合賦值運算符的使用。 ```python= a = 10 b = 3 a += b #相當於:a = a + b a *= a + 2 #相當於:a = a * (a + 2) print ( a ) # 輸出: 195 ``` ## 比較運算符和邏輯運算符 **比較運算符** 就是數學中常用的大於、小於等等,需要注意的是等於是兩個等號 `==` ,一個等號 `=` 會是前面的賦值運算符。 比較運算符會回傳布林值,也就是說會回傳 `true` 或 `false` 。 <br> **邏輯運算符** 有三個,分別是 `and` 、 `or` 、和 `not` 。 `and`翻譯成中文就是`而且` ,會連接兩個以上的判斷式,如果所有判斷式回傳的值都是 `true` ,那麼運算得結果就會是 `true` ,否則為 `false`,可以看下面這個例子: ```python= print( 1>2 and 2>1) print( 2>1 and 1>2) print( 1>2 and 1>2) print( 2>1 and 2>1) 輸出: False False False True ``` 由於程式是由左至右執行的,所以在執行 `and` 判斷時,只要一看見 `false` , and便會直接回傳false並停止繼續判斷後面的判斷式(短路處理)。 因此,在撰寫的時候把較有可能為 `false` 的判斷式寫在左邊可以稍微提高程式執行的效率。 `or` 翻譯成中文為 `或`, 會連接兩個以上的判斷式, 如果所有判斷式回傳的值都是 `false`,那麼運算得結果就會是 `false` ,否則為 `true`,可以看下面這個例子: ```python= print( 1>2 or 2>1) print( 2>1 or 1>2) print( 1>2 or 1>2) print( 2>1 or 2>1) 輸出: True True False True ``` 當然, `or` 也是有短路功能的, 在執行 `or` 判斷時,只要一看見 `true`, or便會直接回傳true並停止繼續判斷後面的判斷式。 與前面同樣的原因,在撰寫的時候把較有可能為 `true` 的判斷式寫在左邊可以稍微提高程式執行的效率。 `not` 運算符的後面會跟上一個布林值(判斷式), 它的作用是得到與該布林值相反的值,也就是說,如果是 `true` ,運算結果便會是 `false` ,反之亦然,可以看下面的例子: ```python = print( not (1>2 and 2>1) ) print( not (2>1 and 1>2) ) print( not (1>2 and 1>2) ) print( not (2>1 and 2>1) ) 輸出: True True True False ``` 可以看見輸出的結果與 `and` 完全相反。 # 練習 ### 練習1: 將華氏溫度轉換為攝氏溫度。 > 提示:$攝氏溫度(℃) = \frac{華氏溫度(°F) - 32}{1.8}$ 參考解答: ```python= f = float(input("請輸入華氏溫度 = ")) c = (f-32)/1.8 print("攝氏溫度 =", c) 輸入: 123 輸出: 攝氏溫度 = 50.55555555555556 ``` ### 練習2: 輸入年份判斷是不是閏年。 > 提示:年份能被 4 整除且不被 100 整除, 或年份能 400 整除,則該年為閏年 參考解答: ```python= year = int(input( "請輸入年份: " )) det = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0 ) print ( det ) 輸入: 100 輸出: False 輸入: 400 輸出: True ``` <style> .green { color:#29E5A9; } .brown { color:#990000; } .pink { color:#C18387; } .red { color:#E71B18 ; } .blue { color:#0b5394; } .purple { color:#AC9FDD; } @-webkit-keyframes A { 0% { color:#C10066;} 10% { color: #CC0000;} 20% { color: #E63F00; } 30% { color:#EE7700; } 40% { color: #DDAA00; } 50% { color:#EEEE00;} 60% { color: #99DD00;} 70% { color:#66DD00;} 80% { color: #00DDDD;} 90% { color: #0044BB;} 100% { color: #A500CC;} } #animation_title{ animation: A 3s ease 0s infinite alternate; -webkit-animation: A 3s ease 0s infinite alternate; } </style> <style> a.redlink { color:#DF2F6A; } a.redlink:link { color:#DF2F6A; text-decoration:none; } a.redlink:visiteid { color:#DF2F6A; text-decoration:none; } a.redlink:hover { color:#19CABC; text-decoration:none; } a.redlink:active { color:#000000; text-decoration:underline; background:#FFFFFF; } </style> <style type="text/css"> h1 { font-size:; color:#0b5394; } h2 { font-size:; color:#0b5394; } p { font-size:; color:; } h3 { font-size: ; color:#990000; } h4 { font-size:; color:#990000; } </style>

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully