Wei-Ting
    • 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
    • Invite by email
      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 Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
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
  • Invite by email
    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 --- # DAY 02 變數和簡單的資料型別 ## 變數的使用 「變數」是指一個包含部分已知或未知數值或資訊(即一個值)之儲存位址 那我們來實際操作一下 : 設定一個變數名稱叫s,s等於一個字串,然後再將s print出來 ```Python= s='hello' print(s) ``` 印出的結果如下,因為s等於hello,所以將s印出就等於是在印hello ```python hello ``` 接下來我們把上述的程式碼擴充成以下形式 : ```Python= s='hello' print(s) s='hahaha' print(s) ``` 印出後我們會發現結果如下: ```python hahaha ``` 為甚麼會從hello變成hahaha呢 ? 因為python會記錄它最新的值,也就是說變數我們能隨時更改,它只會印出最新的那個值 ! <font color=#FF0000> 變數命名的注意事項: * 只能有英文字母,數字和底線 * 變數名稱裡不能有空格,可以用底線代替 ex : student_id </font> --- ## 字串(String)的基本運用 在python3裡面只要是引號括起來的都是字串,所以單引號雙引號是不影響的 ! 現在來說一些字串的常見用法: ```python= student_name="Weiting" school="FJU" student_id=405570147 print(student_name.title()) print(student_name.upper()) print(student_name.lower()) print(len(student_name)) print(school+" "+student_name) print(school+student_name) print(school+" "+str(student_id)+" "student_name) ``` ### 上面程式碼的說明 : * 第一行是在定義一個student_name變數是"Weiting"字串 * 第二行是在定義一個school變數是"FJU"字串 * 第三行是在定義一個student_id變數是405570147,==但資料型態是int(整數)== * 第四行的title()方法是讓第一個字大寫 * 第五行的upper()方法是讓全部變大寫 * 第六行的lower()方法是讓全部變小寫 * 第七行的len()方法是計算長度 * 第八行是兩個字串的相加(中間還有再加一個空格隔開兩個字串) * 第九行是兩個字串相加(可以和第八行比較一下輸出結果)。 * 第十行是字串和int的相加,因為字串只能跟字串相加,所以int要轉成字串的型態,不然會出現TypeError(型別錯誤) ! str()方法是轉成字串的意思 ! 所以印出的東西分別是: >Weiting >WEITING >weiting >7 >FJU Weiting >FJUWeiting >FJU 405570147 Weiting :warning: 印字串的時候也有一點要注意 當你的print是用兩個單引號括起來,但你要印的字串裡也有單引號的時候,你就得用\這個符號隔在內容的單引號前面,不然就是得換成兩個雙引號,如下 : ```python print('This\'s an apple') ``` ```python print("This's an apple") ``` 這樣才能不讓程式碼出錯喔 ~ --- ## 字串的增加空白和刪除空白 ### 1.增加空白 增加空白有兩種方式 ==\n (換行)== 和 ==\t (空格)== * 換行範例 ```python print("hello\nworld") ``` 印出來就是 : ```python hello world ``` * 空格範例 (一個 \t 就是增加一個縮排,預設縮排是四格) ```python print("hello\n\tworld") ``` 印出來就是 : ```python hello world ``` ### 2.刪除空白 刪除空白有三種方法可以使用,==lstrip()刪除左邊空白==、==rstrip()刪除右邊空白==,或是==strip()刪除兩邊空白== * 刪除左邊空白範例 ```python= my_name=" Weiting" print(my_name) #輸出結果為" Weiting" print(my_name.lstrip()) #輸出結果為"Weiting" print(my_name) #輸出結果為" Weiting" ``` 從輸出結果來看,我們可以發現lstrip()方法只是暫時將字串的空白刪除,如果想要永久刪除空白,我們應該要把刪除空白的字串再存回變數中 ! ```python= my_name=" Weiting" print(my_name) #輸出結果為" Weiting" my_name = my_name.lstrip() #將去除左邊空白的字串存回變數中 print(my_name) #輸出結果為"Weiting" ``` ==p.s 順便說一下要寫注釋的方法,在python中是用#這個符號== 學會了刪除左邊空白後,刪除右邊空白和刪除兩邊空白也是同樣的做法 ! >[color=#0000FF]那我們稍微統整一下今天所學的 • title()---讓第一個字大寫 • upper()---讓全部變大寫 • lower()---讓全部變小寫 • len()---計算長度 • \n---換行 • \t ---空格 • lstrip()刪除左邊空白、 • rstrip()刪除右邊空白 • strip()刪除兩邊空白 • #---註釋 #### 好了今天就講到這了,各位明天見 :hand: *** 資料來源:<<python程式設計的樂趣>>-Eric Matthes著/H&C譯

    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