Python Introduction
Python is a programming language that lets you work more quickly and integrate your systems more effectively.
calculator (計算機)
2 + 2
4
2 + 2
2 - 2
2 * 3
4 / 2
2.0
Sample Code
The sample code in colab Python Introduction.ipynb
Output (輸出)
print() function (函式)
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
*objects : print content can be nothing or
multiple things seprated by ,
sep='' : seperate charachter default ' ' is space
end='' : end character default \n is new line
print() Example
print(1,2,3,4)
print()
print(1,2,3,4, sep=',')
print(1,2,3,4, end=' -- ')
print('end no')
print(1,2, sep='=', end=' ** \n')
1 2 3 4
1,2,3,4
1 2 3 4 -- end no
1=2 **
print() Example
print('2 + 2 = ', 2 + 2)
print('2 - 2 = ', 2 - 2)
print('2 * 3 = ', 2 * 3)
print('4 / 2 = ', 4 / 2)
print('2 ** 3 = ', 2 ** 3)
print('5 // 2 = ', 5 // 2)
print('5 % 2 = ', 5 % 2)
2 + 2 = 4
2 - 2 = 0
2 * 3 = 6
4 / 2 = 2.0
2 ** 3 = 8
5 // 2 = 2
5 % 2 = 1
Comment (註解)
Code comments explain purpose, aiding understanding. Start with # symbol.
程式註解解釋用途,以 # 開頭。
# 我是單獨一行的註解
print(2+2) # 我是程式之後的註解
4
Adding comments
print('2 + 2 = ', 2 + 2) # 加法
print('2 - 2 = ', 2 - 2) # 減法
print('2 * 3 = ', 2 * 3) # 乘法
print('4 / 2 = ', 4 / 2) # 除法
print('2 ** 3 = ', 2 ** 3) # 次方
print('5 // 2 = ', 5 // 2) # 商數
print('5 % 2 = ', 5 % 2) # 餘數
2 + 2 = 4
2 - 2 = 0
2 * 3 = 6
4 / 2 = 2.0
2 ** 3 = 8
5 // 2 = 2
5 % 2 = 1
Input (輸入)
Input() Function (函式)
input([prompt]) prompt is hint string it is option
If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that
input() Example
# input no prompt
name = input()
print(name)
# input with prompt
name = input("please enter your name : ")
print("hello,", name)
Ted
Ted
please enter your name : Ted
hello,Ted
Formatting Strings
f-string formatting
f' ... {變數名稱}' f" ... {變數名稱}"
用大括號 {變數名稱} 表示直接填入變數的值
name1 = 'Eric'
name2 = 'John'
hello_str = f'Hello {name1}, my name is {name2}'
print(hello_str)
print(f'Hi {name2}, Lovely to meet you.')
Hello Eric, my name is John
Hi John, it is nice to meet you.
f-string express
f' ... {表達式或呼叫函數} ' 用大括號 {表達式或呼叫函數},Python會求出其結果並填入字串
print(f'The Answer of 24 * 8 + 4 is {24 * 8 + 4}')
name = 'ERIC'
print(f'My name is {name} and lowercase is {name.lower()}')
import math # 載入數學模組
print(f'The PI is {math.pi}') # 取出pi
The Answer of 24 * 8 + 4 is 196
My name is ERIC and lowercase is eric
The Pi is 3.141592653589793
f-string floats
{math.pi:.3f} the number 3 of decimal places
import math # 載入數學模組
# Printing the value of Pi with 3 decimal places.
print(f'The PI is {math.pi:.3f}')
# Printing the value of Pi with 5 decimal places.
print(f'The PI is {math.pi:.5f}')
The PI is 3.142
The PI is 3.14159
f-string width and justify
print("{1:5} is -"+f'{1:5}-')
print("{12:05} is -"+f'{12:05}-')
print("{123:*>5} is -"+f'{123:*>5}-')
print("{1234:*<5} is -"+f'{1234:*<5}-')
{1:5} is - 1-
{12:05} is -00012-
{123:>5} is -**123-
{1234:<5} is -1234*-
{2:0>5}
{ 2 : 0 > 5 }
{number : fill_character jusity_to_right width }
Python Introdution
By wschen
Python Introdution
Discover the power of Python! This presentation introduces Python as a programming language that enhances productivity and system integration. Explore topics like calculator functions, print(), comments, user input, and string formatting through engaging examples.
- 429