python了解一下_Python基础语法了解一下(一)

数据类型

容器类型

强制类型转换

标识符

变量赋值

数据类型 Base Types: integer, float, bollean, string, bytes

数据类型

1、整型 int: 123 , 0 , -121

2、浮点型 float: 2.5 , 0.0 , -1.2

3、布尔类型 bool: False True

4、字符串 str: "ab" , "a\nb" , "I'm" "a\tb\tc"

5、字节类型 bytes: b"toto\xfe\775"

容器类型:list[], tuple(), str bytes(), dict{:}, set()

容器类型

#list数组

[1,3,5,7,"9"][:2]

[1, 3]

#数组函数

list((1,3,5,7,"9"))[:2]

[1, 3]

#tuple元组

(1,2,"3",)[1]

2

#元组函数

tuple((1,2,"3",))[1]

2

#dict字典

{"apple":1,"pear":2,"orange":3}

{'apple': 1, 'pear': 2, 'orange': 3}

#字典函数

dict(apple=1,pear=2,orange=3)

{'apple': 1, 'pear': 2, 'orange': 3}

#set集

{"a","b",1,2}

{1, 2, 'a', 'b'}

#集函数

set({"a","b",1,2})

{1, 2, 'a', 'b'}

强制类型转换 Conversions

强制类型转换

int("123")

123

#二进制转换成十进制

int("0101",2)

5

#六进制转换成十进制

int("12",16)

18

int(12.3)

12

float("12345")

12345.0

round(12.345,1)

12.3

#bool(x):

#False for null x, empty container x, None or False x;

#True for other x

bool(0)

False

bool(None)

False

bool([])

False

str("abc")

'abc'

#code ↔ char

chr(35)

'#'

ord("@")

64

ord("%")

37

repr("one")

"'one'"

bytes([72,9,64])

b'H\t@'

list("abcde")

['a', 'b', 'c', 'd', 'e']

list((1,2,3,4,5))

[1, 2, 3, 4, 5]

dict([("apple",1),("pear",2)])

{'apple': 1, 'pear': 2}

dict(apple=1,pear=2)

{'apple': 1, 'pear': 2}

set(["apple","pear"])

{'apple', 'pear'}

#separator str and sequence of str → assembled str

"@".join(["name","net.com"])

'name@net.com'

#str splitted on whitespaces → list of str

"this is an apple".split()

['this', 'is', 'an', 'apple']

#str splitted on separator str → list of str

"this is an apple".split("a")

['this is ', 'n ', 'pple']

#str splitted on separator str → list of str

[int(x) for x in ('1','2',3,3.3)]

[1, 2, 3, 3]

[int(x) for x in ['1','2',3,3.3]]

[1, 2, 3, 3]

标识符 Identifiers 用作变量,函数,模块,类......名称

标识符

标识符必须以字母(大小写均可)或者" _ "开头,接下来可以重复0到多次(字母|数字|" _ ") a…zA…Z_ followed by a…zA…Z_0…9

diacritics allowed but should be avoided

禁止使用关键字(python内部已经使用了的标识符)(language keywords forbidden)

区分大小写 (lower/UPPER case discrimination)

没有长度限制

变量赋值 Variables assignment =

变量赋值

#"变量名称"="赋值"

a=120

a=b=50

x,y,z=12,11,10

x+=3

y/=2

z%=3

n=None

#remove name x

del x

#values swap

y,z=z,y

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值