初次见面 ,承蒙遇见 !

人生苦短,我用Python!

1.关键字

 

"""

查看python中的关键字:
    1.需要先导入python的工具包:keyword
    2.通过工具包查看关键字
"""

# 1.需要先导入python的工具包:keyword
import keyword

# 2.通过工具包得到关键字

# ['False', 'None', 'True', 'and', 'as', 'assert', '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']
print(keyword.kwlist)

 

 

2.不同数据类型之间的计算规则

 

"""
/ 除号
% 取余,取模
"""

# 两个整数相除
print(10 / 2) # 5.0
print(10 / 3) # 3.3333333333333335
# 复数整除 【整除的结果向左取整】
print(10 // -3)# -4 
print(10 //3 ) # 3
# 取余: 如果这个数据余上2 结果为0,那么这个数一定是一个偶数
print(3 % 2)
print(10 % 2)

# 字符串与整数不能相加:TypeError: must be str, not int
# print("hello"+2)

# 字符串与整数相乘:会把字符串拼接n次,这个n是整数
print("hello" * 3) # hellohellohello

# 字符串与字符串相加:这里的+号,是字符串连接符
print("hello" + "world") # helloworld
print("hello" + "world" + "python") # helloworldpython

# bool类型的数据可以与数字相加
print(1 + True) # True默认 为 1
print(1 + False) # False为0

 

 

3.输出

 

"""
输出:
    把数据打印到控制台.

    在python中通过调用print()函数打印数据,达到向控制台输出数据的目的.


格式化操作符:占位符

    字符串: %s
    整数:%d
    小数:%f
"""
name = "非池中"
print("姓名是" + name) # 姓名是非池中

#使用字符串的格式化操作符
print("姓名是%s" % name) # 姓名是非池中

# 使用整数的格式化操作符
age = 18
print("年龄是%d" % age) # 年龄是18

# 使用小数的格式化操作符
pi = 3.14733
# %.2f 表示保留两位小数
print("圆周率是%.2f" % pi) # 圆周率是3.14

n = 5
print("三位的整数:%3d" % n) # 三位的整数:005

# 如果输出的内容里面包含了多个变量,字符串中需要使用格式化操作符,
# 在% 的后面 使用小括号括住多个变量,变量之间使用逗号隔开,注意变量的顺序与格式化操作符一一对应
print("姓名是%s,年龄是%d" % (name,age)) # 姓名是李浩宇,年龄是20


scale = 50
# 百分比是%50: 【要想输出一个%,需要使用%%来表示】
print("百分比是%% %d" % scale)

 

 

 

"""
 ==========我的名片==========
    姓名: 非池中
    QQ:xxxxxxx
    手机号:185xxxxxx
    公司地址:北京市xxxx
 ===========================

"""
name = "非池中"
qq = 888888
phone = "1999999999"
address = "北京市长安街1号院"

print("==========我的名片==========")
print("  姓名: %s" % name)
print("  QQ:%d" % qq)
print("  手机号: %s" % phone)
print("  公司地址:%s " % address)
print("==========我的名片==========")

 

 

 

原文地址:https://www.cnblogs.com/Lee1010/p/9801378.html