Python基础(1)保留字数据类型运算符format

1.保留字与标识符

import keyword
print(keyword.kwlist)
print(len(keyword.kwlist))

输出:

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
35

2.基本数据类型

整型
浮点型
字符串
布尔类型(代表真或假的数据类型,值只有True或False)

3.运算符

1.赋值运算符 =
2.算术运算符 (+,-,,/, %,//,**)
3.复合运算符 (+=,-=,
=,/=,%=,//=,**=)
4.位运算符 (&按位与,|按位或,^按位异或,~按位取反,<<左移 , >>右移)
5.比较运算符 ( >, <, >=, <=, !=, <>(也是不等于) )
6.逻辑运算符 ( and(并且), or(或者), not(不是), is(是) )
7.成员运算符 in/not in 判断给定的元素是否在列表,字典或元组,集合
8.身份运算符 is/is not 判断给定的元素是否是某一个引用的数据

4.输出print与format

(1)print

str = 'hello' 
length = len(str) 
print('字符串%s的长度为%d'% (str, length))

输出:

字符串hello的长度为5

拓展:Python中%(常用的) %s字符串 %e指数 %f浮点数 %%字符%

(2)format

1.字段名字为整数,表示参数的位置

print("my name is {0} and my age is {1} years old".format('sunlin', '100'))

输出:

my name is sunlin and my age is 100 years old

2.字段名字为参数的名字

print("my name is {mingzi} and my age is {nianling} years old".format(mingzi = 'sunlin', nianling = '100'))

输出:

my name is sunlin and my age is 100 years old

3.列表的取值

print("my name is {0[0]} and my age is {0[1]} years old".format(['sunlin', 100]))
print("my name is {[1]} and my age is {[2]}".format(['sunlin', 'dagou', 'ergou'], [21, 34, 47]))

输出:

my name is sunlin and my age is 100 years old my name is dagou and my
age is 47

4.字典的取值

infor = {'ming_zi': 'sunlin', 'nian_ling': '24'}
print("my name is {ming_zi} and my age is {nian_ling}".format(**infor))

输出:

my name is sunlin and my age is 24

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章