標籤:位元組 str 列表 邏輯 賦值運算 use 專業 編碼 空格
昨日回顧
python的種類
javapyhton
cpython
pypy
編碼
!/usr/bin/env python
-*- coding:utf-8 -*-
print("abc")
name = input("name:")
input互動內容為字串
需要數字需要用int()定義
while else
break 終止整個迴圈
continue 終止當次迴圈進入下一次迴圈
pycharm 安裝專業版
不要漢化
漢化無法使用部分功能
運算子
in not in
name = "Amanda"
if "man" in name:
print("yes")
else:
print("no")
if "man" not in name:
print("yes1")
else:
print("no1")
布爾值 :
真 True
假 False
name = "Amanda"
v = ‘man‘ in name
print(v)
v2 = ‘man‘ not in name
print(v2)
比較運算
==
>
<
>=
<=
!=不等於(推薦)
<>不等於
not
not True = False
not False = True
user = "Amanda"
pwd = "123"
v = user == "Amanda" and pwd == "123" or 1==1 and pwd == "456"
print(v)
沒有優先順序,從左至右按順序運行
v = (user == "Amanda" and pwd == "123") or (1==1 and pwd == "456")
print(v)
推薦使用括弧 括弧先計算
算數運算
+ 加
+ 減
* 乘
/ 除
** 冪
% 取餘
// 取整
賦值運算
count = count = 1
count += 1
count = count - 1
count -= 1
count = count * 1
count *= 1
count = count / 1
count /= 1
count = count ** 1
count **= 1
count = count % 1
count %= 1
count = count // 1
count //= 1
算數運算和賦值運算都是取得真實結果
比較運算 邏輯運算 成員運算 結果是布爾值
#資料類型
數字
a = 123
b = 456
字串
a = "asdas "
b = "assfsd"
布爾值
True 真
False 假
數字
python2 "整形""長整形"
python3 int
int()
u = "123"
v = int(u)
將字串轉換為數字
u2 = "a"
v2 = int(u2, base=16)
將字串按16進位轉換為數字
age = 10
r = age.bit_length()
計算至少用幾位來表示當前數字
字串 str
test = "amAnda"
v = test.capitalize()
print (v)
Amanda首字母大寫
v1 = test.casefold()
print(v1)
amanda轉小寫功能多
v2 = test.lower()
print(v2)
amanda轉小寫功能少
v3 = test.center(20, "*")
print(v3)
*******amAnda*******設定總長度 將內容置中 *一個輸入鍵台空白處 預設空格
v4 = test.count("a")
print(v4)
2 字串中a的個數
v5 = test.count("a",5)
print(v5)
1從第五個位元組開始有幾個a
v6 = test.endswith("da")
print(v6)
True 是否已da結尾
v7 = test.startswith("am")
print(v7)
True 是否以am 開頭
test = "AmandaAmanda"
v = test.find("a")
print(v)
2 重頭開始找an 返回an在第幾位 從0開始 沒有找到傳回值-1
test = "AmandaAmanda"
v = test.index("a")
2 重頭開始找an 返回an在第幾位 從0開始 沒有找到報錯
test = "I am {name}, age{age}"
v = test.format(name="Amanda", age="21")
print(v) 格式化 將字串中的預留位置替換為制定值
還有一種
test = "I am {0}, age{1}"
v = test.format("Amanda", "21")
按順序替換
字典格式化
test = "I am {name}, age{age}"
v = test.format_map({"name":‘Amanda‘, "age":21})
test = "sdf123"
v = test.isalnum()
print(v) 字串中是否只包含數字和字母
# 列表 list
元祖 tuple
字典 dict
布爾值 bool
python全棧之路【二】基礎2