{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ " # 基本輸入及輸出 - I/O and comment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "本章節內容大綱\n", "* [輸出](#輸出)\n", "* [讀入](#讀入)\n", "* [加入註解](#加入註解)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ## 輸出" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 使用雙引號或單引號印出一行字串,使用三引號可印出一段字串\n", "print('hello world')\n", "print(\"hello world\")\n", "print('''♪ღ♪*•.¸¸.•*¨¨*•.♪ღ♪*•.¸¸.•*¨¨*•.♪ღ♪\n", "░H░A░P░P░Y░♪░B░I░R░T░H░D░A░Y░\n", "♪ღ♪*•.¸¸.•*¨¨*•.♪ღ♪*•.¸¸.•*¨¨*•.♪ღ♪''')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 用逗號分隔字串(或變數),印出效果為空格\n", "print('hello', 'world')\n", "\n", "print('I am', 18, 'years old.')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# print 預設會在結尾換行,若要印出且不換行,在後面加入 end=''\n", "print('hello', end='')\n", "# 則下一次輸出會接在同一行的後面\n", "print('world')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 印出帶有變數字串,在要印變數的地方輸入變數的 format code,並在字串後面輸入 % 後擺放變數\n", "# format code: 整數 %d, 浮點數 %f, 字串 %s\n", "# format code 的詳細部分會在後面章節說明。\n", "\n", "# 第一種方法\n", "aPrice = 40\n", "print(\"An apple cost %d dollars.\" % aPrice)\n", "\n", "# 印出帶有多變數字串,在要印變數的地方依序輸入變數的 format code,並在字串的後面將變數依序擺放至小括號。\n", "bPrice = 15\n", "\n", "print(\"An apple cost %d dollars and a banana cost %d.\" % (aPrice, bPrice))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 第二種方法\n", "# format string,用需要印出變數的地方輸入大括號,並在字串後面以 .format() 填入對應的變數\n", "# 此方法的好處是不用記 format code。\n", "print(\"An apple cost {} dollars and a banana cost {}.\".format(aPrice, bPrice))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 第三種方法\n", "# f-string,直接將變數以大括號包著,置入字串中輸出\n", "# 目前最推薦的印出方式,相較於前兩種既直覺又彈性。\n", "print(f'An apple cost {aPrice} dollars and a banana cost {bPrice}.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ## 讀入" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "name = input()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# input有互動性功能,可在括號內鍵入互動字串與使用者傳達訊息\n", "name = input('Please enter your name:')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 加入註解" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 單行註解在開頭鍵入井字號\n", "\n", "\"\"\"\n", " 多行註解則在前後各加入三個雙引號\n", " 如此一來可以在引號區間內不限制地加入註解唷\n", " /\\︵-︵/\\\n", " |(◉)(◉)|\n", " \\ ︶V︶ /\n", " /↺↺↺↺\\\n", " ↺↺↺↺↺|\n", " \\↺↺↺↺/\n", " ¯¯/\\¯/\\¯\n", "\"\"\"\n", "print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "-----\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Quiz" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# quiz1:\n", "\n", "# 分別使用 input() 輸入你的名字、性別、年齡\n", "# 並使用三種 print() 分別輸出一段包含這些資料的句子。\n", "# print(\"\" %())\n", "# print(\"\").format())\n", "# print(f'')\n", "\n", "# 選一個之後你主要要用來 print 的方式 (推薦 f-string)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }