Python全棧工程師(運算符、if)


 

 

 

ParisGabriel

 
 
 
Python 入門基礎
 

比較運算符:
< 小於
<= 小於等於
> 大於
>= 大於等於
== 等於
!= 不等於

語法:

表達式1>表達式2 返回布爾類型

數值對象的構造函數:

float(obj)

用字符串或數字轉換為浮點數,如果不給出實參,則返回0.0

int(x=0,base=10)

用數字或者字符串轉換為整數,如果不給出實參,則返回0.0 bas表示是進制

complex(r=0.0,i=0.0)

用數字創建一個復數

bool(x)

用x創建一個布爾值(true/false)

bool(obj)返回假值的情況:
None 空值
false 假值
0
0.0
0j
所有的數字零
‘’ 空字符串
[ ] 空列表
{ }空詞典
() 空原組
...

函數調用表達式:
函數名(傳參列表)
說明:函數調用表達式時,此表達式一定是會返回一個對象的引用關系
如果不需要返回值時,通常返回none對象的引用關系

內建數值型函數:
abs(x)

取x的絕對值

round(number,ndigits=0)

對數值進行四舍五入 ,ndigits是小數向右取整的位數,負數表示像左取整

pow(x,y,z=none)

相當於x**y或x**y%z

help()查看幫助

help(函數或對象名)

help(int)

語句:

語句是由一些表達式組成,通常一條一句可以獨立執行來完成一部分事情並形成結果
Python建議一條語句寫在一行內
多條一句子寫在一行內需要用“;”分開

print("hello")
x = 100 + 200
print(x)
print("hello"); x = 100 + 200; print(x)

顯示換行:
折行符“\”
折行符必須一行的末尾,來告訴解釋執行器下一行也是本行的語句

隱式折行符:
所有的括號的內容換行,成為隱式折行符()、{}、[]

函數的使用:
基本輸入函數input
從標准輸入設備上讀取一個字符串(末尾的換行符會被刪除)
返回輸入的字符串(僅Python3)

提示字符可以為空

基本輸出函數:
print
將一系列的值一字符串的形式輸出到標准輸出設備上,默認為終端
選項的關鍵字參數:
sep:兩個值之間的分隔符,默認為一個空格
end:輸出完畢后在末尾自動加一個字符串,默認為換行符(\n)

 

練習:
1.輸入兩個整數,分別用變量x,y綁定
1)打印輸出計算這兩個數的和
2)打印輸出計算這兩個數的積
3)打印輸出計x的y次方

答案:

x = int(input("plwase input integer:"))
y = int(input("plwase input integer:"))
print(x + y, "\n", x * y, "\n", x ** y)

2.今天是小明的20歲生日,假設每年都有365天,計算他過了多少個星期天,剩余多少天

答案:

print((20 * 365) // 7, "星期天   剩余", (20 * 365) % 7, "天")

3.本別輸入當前時間的時、分、秒 在終端打印輸出當前距離0:0:0過了多少少天

答案:

h = int(input("plwase input hour:"))
m = int(input("plwase input minute:"))
s = int(input("plwase input second:"))
print(h * 3600 + m * 60 + s, "second")

 

 

 if語句:

讓程序根據條件選着性的執行某條語句或某些語句

 語法:

a = int(input("plaese input integer:"))
if a > 0:
    print(a, ">0")
elif a > 6:
    print(a, ">6")
elif a > 100:
    print(a, ">100")
else:
    print(a, "<0")

elif可以有0個或多個
else可以有零個或1個

並且所有語句當中只能執行一個

if的嵌套:
if語句本身是由多條子句組成的一條復合語句
if語句可以作為語句嵌套到另一個語句內部

條件表達式:
表達式1 if 真值表達式 else 表達式2
根據真值表達式的取值(true、false)
來決定執行表達式1或2,並返回結果

money = int(input("100"))
pay = money - 20 if money >= 100 else money

pass語句:
通常用來填充語法空白又名空語句

布爾運算符:
not and or
布爾非 not
如果為true返回false否則為反
布爾或 or
優先返回真值
見true得true

 


布爾與 and
優先返回假值對象
見false得false

 

正負號運算符:
+ 表達式
- 表達式
這是一個一元運算符(只有一個數據參加運算)

 

練習:
1.北京出租車價格 3公里以內13元
基本單價:2.3元/公里(超出3公里以外)
回空費:超過15公里 每公里 加收單價的50%的會空費(3.45元/公里)
輸入公里數 打印費用金額

 答案:

kilometre = int(input("plaese input kilometre:"))
if kilometre < 0:
    if kilometre > 15:
        money = (kilometre - 15) * 3.45 + (15 - 3) + 13
        print("money:", money, "$")
    elif 3 < kilometre < 15:
        money = (kilometre - 3) * 2.3 + 13
        print("money:", money, "$")
    else:
        print("money:13$")
else:
    print("not'is kilonetre")

  


2.輸入一個學生的三科成績(3個整數:
打印出最高分、最低分、平局分是多少

答案:

a = int(input("plaese input english mark:"))
b = int(input("plaese input language mark:"))
c = int(input("plaese input mathematisc mark:"))
if 100 > a > 0 and 100 > b > 0 and 100 > c > 0:
    if a > b and a > c:
        print("top score english:", a)
    elif b > c and b > a:
        print("top score language:", b)
    elif c > a and c > b:
        print("top score mathematisc:", c)

    if a < b and a < c:
        print("lowest english:", a)
    elif b < c and b < a:
        print("lowest language:", b)
    elif c < a and c < b:
        print("lowest mathematisc:", c)
    print("mean:", (a + b + c)/3)
else:
    print("not'is mark")

 

3.bmi指數(body、mass、index)以稱身體質量指數
bmi的計算公式: bmi = 體重(公斤)/身高的平方
標准表:

bmi< 18.5 體重過輕
18.5<=bmi<24 體重正常
bmi> 24 體重過重
輸入公斤體重、身高 打印出 bmi的值 並打印體重狀況

答案:

z = int(input("plaese input your weigh:"))
g = float(input("plaese input your height:"))
if z < 0 and g < 0:
    bim = (z / g) ** 2
    if bim < 18.5:
        print("your bim qing")
    elif 24 > bim > 18.5:
        print("your bim normal")
    else:
        print("your bim serious")
else:
    print("your inuput error")

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM