python學習日誌 2

2021-10-07 06:24:26 字數 2551 閱讀 1133

6月16日

格式化字符號

1格式化輸出字串

2格式化輸出整數

3格式化輸出浮點數

f-字串

f』』轉義字元

\n 換行

\t 製表符 (四個空格)

formatted string 格式化字串

例子first = 『john』

last = 『smith』

message = first + 』 [』 + last + 『] 』 『is a coder』

msg = f』 [ ] is a coder 』 print (message)

print(msg)

執行結果

john [smith] is a coder

john [smith] is a coder

格式化字串可以讓其更清晰易識別

2 輸入功能

程式接收使用者輸入的資料的功能 就是輸入

input特點

1.當程式執行到input, 等待使用者輸入,輸入完成之後才能繼續向下執行

2.在python中,input接收到使用者輸入後,一般儲存到變數,方便實用

3.在python中,input會把接收到的任意使用者輸入的資料都當做字串處理

password = input(『請輸入您的密碼:』)

print(f』您輸入的密碼是:』)

print(type(password))

2.一些基本的函式和方法

函式(function):general purpose function built into python

方法(method):specific functions for one type of var)

len()function: to count the number of characters in the string

.upper():convert a string into uppercase 轉化為大寫

.lower():convert a string into lowercase 轉化為小寫

.title(): convert a string into title 轉化為標準格式

.find():return the index of a characters 返回字串的index,可用於查詢

.replace():replace characters or words in a string 取代字串中的某個字元

in operator

『…』 in course : 判斷字串中是否有某個或某些字元, 返回乙個布林值

3.arithmetic operations 算術運算

enhanced (agumented) assignment operator 強化版賦值運算子

x = 19

x = x - 3

x -= 3 —> augmented assignment operator

print(x)

第二行與第三行意思完全相同

operator precedence 運算優先順序

x= 10+3*2**2

x= 22

優先順序:exponentiation冪運算 > multiplication or division乘除 > addition or subtraction加減

4. math functions

5.在python中 引用數學函式

google 「python 3 math module」

if statement if條件語句

練習:

price of a house is $1m.

if buyer has good credit,

they need to put down 10%

otherwise

they need to put down 20%

print the down payment

buyer_with_good_credit = false

price = 1000000

downpayment = 『your down payment is: 』

if buyer_with_good_credit:

print( downpayment + str(0.1price) + 』 dollars』)

else:

print( downpayment + str(0.2price) + 』 dollars』)

執行結果

your down payment is: 200000.0 dollars

6.logical operators 邏輯運算器

and:both

or:at least one

not:inverse a boolean value

例子:has_good_credit = true

has_criminal_record = true

if not has_criminal_record and has_good_credit:

print(『eligible for loan』)

python 學習日誌(2)

現在繼續總結一下python的資料結構,python的資料結構主要有三種 列表 元組 字典 列表list 1,2,3,其中list裡面可以以儲存任何物件,包括數,列表等其他資料結構 list本身是乙個類,有很多成員函式,如 list.sort del 可以用來刪除list中的元素 len 可以用來求...

學習日誌2

姓名 高越 日期 2018 09 03 今日學習任務 複習棧,陣列結構和演算法,跟著老師敲程式.今日任務完成情況 對棧和陣列有更多了解 今日未解決問題 有些 還是不太懂 自我評價 對棧以及陣列有了更多了解,但還是有很多不懂 作業 1 請編寫乙個c函式,該函式將乙個字串逆序 include inclu...

學習日誌2

姓名 郭學傑 日期 2018 09 04 今日學習任務 陣列結構和演算法。今日任務完成情況 今天的任務完成的很不理想,課上不能跟上老師的程序,基礎太薄弱。今日未解決的問題 大部分知識沒能記住,希望老師能夠在完成程式設計之後解釋並梳理一下各條 的意思便於理解。自我評價 今日的學習效果很差,接受較慢。作...