本文簡單地介紹了python的一些基本入門知識,通過對這些知識的了解,大家可以寫一些簡單的**,同時也為後面深入理解打下基礎。本文的主要內容如下:
**值**,即value,通常有:1,2,3.1415,'bright','rose'
**型別**,不同的值有不同的型別。值型別
1int型=整型
'bright'
str字串型
3.1415
float浮點型
type()
是判斷值的型別的函式
---------code start------------
print(type(1))
print(type('bright'))
print(type([1, 2, 3, 4]))
print(type((1, 2, 3, 4)))
print(type())
print(type())
---------code end------------
# 結果
在python中**變數**是指向**物件**的,**變數**的型別和**賦值物件**的型別一致。name = 'bright' #name為變數指向字串'bright'
age = 22 #age為變數指向整數22
gender = 'male'
print(type(name)) # print(type(age)) #
比較
含義操作符
+ - * /
等符號
操作物件
操作符作用的物件
$$表示式=值、變數和操作符的組合$$
eg: 22; x; x+22
$$語句=python直譯器能執行的乙個**單元$$
eg:print(type('name')); gender = 'male'
簡單一點理解,就是直接使用`()`對其施加操作順序。同級,從左向右執行。通常如下:
- parentheses括號
- exponentiation乘方
- multiplication乘法
- division除法
- addition加法
- substraction減法
字串的乘法操作相當於字串重複操作
字串的加法操作相當於字串組合
```python
str1 = 'bright'
str2 = 'rose'
str3 = 'like'
print(str1 + str3 + str2) # brightlikerose
print(str1*2 + str3*2 + str2*2) # brightbrightlikelikeroserose
```
strx = 'bright'
print(strx) #列印字串
for item in range(len(strx)):
print(str0[item])
**函式(function)**,一些語句的組合並給個名字,函式的作用是對**的封裝。
函式需要接收引數,返回結果,一般有輸入有輸出,輸出叫做返回值return value
常用的函式:
- type()
- int()
- float()
- str()
- 列表是乙個值得序列
- 列表是乙個物件
- 列表是乙個可以容納萬物的容器
- 列表可以巢狀
print([1,2,3,4,5])
# 結果:[1, 2, 3, 4, 5]
print(['bright','23','rose',21,3.1415926])
# 結果:['bright', '23', 'rose', 21, 3.1415926]
print([[1,2,3],['a','b','c'],[1.1,1.2,1.3]])
# 結果:[[1, 2, 3], ['a', 'b', 'c'], [1.1, 1.2, 1.3]]
- 可以理解為帶下表的列表,下標為鍵,可以是大部分物件
- 鍵:值
- dict()定義字典
- 鍵值對映
- 可以巢狀
dicttem = dict()
print(dicttem) # {}
dicttem['1'] = 'one'
dicttem['2'] = 'two'
dicttem['3'] = 'three'
print(dicttem) #
print(len(dicttem)) #列印鍵的長度 # 3
- 不可變
- 是乙個序列
- 是乙個物件
- tuple()
- ,逗號也可定義
t = 1,2,3,4,5
print(t) # (1, 2, 3, 4, 5)
print(t[0]) # 1
python3 入門基礎
1 檢視python版本 python v 2 指定python檔案的編碼格式 coding utf 8 或 coding utf 8 3 變數命名以字母和下劃線 開始 num num 4 注釋 開頭表示注釋該行 這裡是注釋部分 5 多行語句用反斜槓 實現 toatl num1 num2 num3 ...
Python3入門系列之 字典
字典是一種可變容器模型,且存放任何型別對像 如 字串,數字,或者列表甚至字典 每個字典有鍵名 key 和鍵值 value 且用冒號 隔開 多個字典用逗號 隔開整個字典包括在花括號中 示例 注 key為鍵名,name為鍵值 dict dict 執行結果 name print dict key dict...
Python3入門學習之 基礎語法
目錄 編碼 識別符號 python保留字 數字 number 型別 字串 string 語句構成 組 import 與 from.import 使用者輸入 print 輸出 同一行顯示多條語句 預設情況下,python 3 原始碼檔案為utf 8編碼,所有字串都是 unicode 字串 即關鍵字,我...