changed 10 months ago
Linked with GitHub

9/13 第一堂社課

今日講師:R緯

(Python班)


今日課程主題:


基本輸入輸出

Basic input & output


資料型別(型態)

Type


基本輸入輸出

Basic input & output


輸出

output


「Hello, World!」程式通常指輸出或顯示「Hello, World!」字串的電腦程式。可以用來展示該程式語言的基本語法。
「Hello, World!」是初學者學習某種程式語言所接觸的第一個程式內容,。


第一個程式:

print("Hello, World!")

output:

Hello, World!

print()簡單直觀

其他程式語言:


JavaScript

console.log('Hello World');
(這幾頁絕對不是水時間用的)

Java

class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
(這幾頁絕對不是水時間用的)

C++

#include <iostream> using namespace std; int main() { cout << "Hello! World!"; return 0; }
(這幾頁絕對不是水時間用的)

Assembly(組合語言)

global _start ; This exports _start label and ; makes entry point to the programing ; ########################## ; code ; ########################## section .text _start: ; ########################## ; write(1, message, length) ; ########################## mov rax, 1 ; system call for write mov rdi, 1 ; making file handle stdout mov rsi, message ; passing adress of string to output mov rdx, length ; number of bytes syscall ; invoking os to write ; ########################## ; exit(0) ; ########################## mov rax, 60 ; sys call for exit xor rdi, rdi ; exit code 0 syscall ; invoke os to exit ; ########################## ; Variables ; ########################## section .data message: db "Hello, World", 0xa ; const char * message = "Hello, World"oxa is "\n" length: equ $-message; int length= len(message) =12;
(這幾頁絕對不是水時間用的)

例題一
image

word = input() print(----, word)

print()的參數

print([object], separator, end, file, flush)

其中只有sep和end常用


sep: 分開不同資料

預設為 sep = " " (space)

print("hello", "world") print("hello", "world", sep = " ") print("hello", "world", sep = "") print("hello", "world", sep = "啟動") print("hello", "world", "python", sep = "啟動")

output:

hello world
hello world
helloworld
hello啟動world
hello啟動world啟動python

end: 資料結尾

預設為 end = "\n" (換行)

print("Hello, World!1") print("Hello, World!2", end = "\n") print("Hello, World!3", end = "") print("Hello, World!4") print("Hello, World!5", end = "啟動") print("Hello, World!6")

output:

Hello, World!1
Hello, World!2
Hello, World!3Hello, World!4
Hello, World!5啟動Hello, World!6

例題二
image


year = input() month = input() day = input() print(----)

輸入

input


input()

name = input() print("hello", name)

output:

{User input}
hello {User input}

()內為等待輸入時,系統輸出的東西

name = input("what is your name? :") print("hello", name)

output:

what is your name? :{User input}
hello {User input}

輸入後的所有東西,

預設皆為字串(str)


資料型別(型態)

Type


型別:一個資料的型態、類別


Python中內建了很多型別

我們只要會用其中四個

詳細:Python官方


表示方式 中文 英文
int 整數 integer
float 浮點數 floating point
bool 布林值 boolean
str 字串 string

我們能用type()查看型別

num1 = 120 num2 = 3.14159 num3 = 5 print(type(num1)) print(type(num2)) print(type(num3 == 5))

output:

<class 'int'>
<class 'float'>
<class 'bool'>

其中int, float, bool皆可拿來運算

(bool的運算比較特別)


float能轉int,int也能轉float

num1 = 120 num2 = 3.14159 print(type(num1)) print(type(num2)) num3 = float(num1) #將num3定為num1轉float的值 num4 = int(num2) #將num4定為num2轉int的值 print(num3) print(num4) print(type(num3)) print(type(num4))

output:

<class 'int'>
<class 'float'>
120.0   #多了小數點
3       #捨去小數部分(不是四捨五入,是無條件捨去)
<class 'float'>
<class 'int'>

str的使用


所有型別都能轉成str

num1 = 120 num2 = 3.14159 num3 = 5 print(type(num1), type(num2), type(num3 == 5), sep="/") str1 = str(num1) str2 = str(num2) str3 = str(num3 == 5) print(str1, str2, str3, sep = "/") print(type(str1), type(str2), type(str3), sep="/")

output:

<class 'int'>/<class 'float'>/<class 'bool'>
120/3.14159/True
<class 'str'>/<class 'str'>/<class 'str'>

str是純數字時可轉成int, float

str1 = "120" str2 = "3.14159" num1 = int(str1) num2 = float(str2) print(num1) print(type(num1)) print(num2) print(type(num2))

output:

120
<class 'int'>
3.14159
<class 'float'>

str長度、取出字元

str1 = "HSNUCRC" print(len(str1)) print(str1[3]) print(str1[1:5])
7
U
SNUC

END

(總計:34面簡報、377行Markdown、5191個字元)
Select a repo