Python—01初始Python

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36282409/article/details/83930653

python语法

不必使用;作为结尾

print(“hello”);

print (“world”)

name=True

if name:

print(“hello”)

else:

print (“world”)

特点:强类型动态的脚本语言,效率高、包多.

Python数据类型

两大类:不可变的(str—字符串 number—数值 boolean—布尔)和可变的(list—列表;dict 字典 set——集合 元祖)。

id() 查看对象在内存中的地址

a = 12
b = 0.5
c = 1.2e5

十六进制

d = 0x45A

八进制

e = 0o112
print©

print(id(a))
a = “hello”
print(id(a))

字符串

al = “zhongguo”
print(id(a1)

  # n 换行
  print(zhhon)

t tab 键

r 换行

给转义符合转义

print(“zhong\\ruo”)

三引号 按格式输出注释中的内容

print (’’’

    hello
    world

    呵呵呵

‘’’)

按字符串原文输出

print(r"zhong\\ruo")

print("----------------------------------")
print(True)
print(False)

检查数据类型的方法

print(type(“zhongguo”))
print(type(14))
print(type(0.25))
print(type(1.2e35))

类型的强制转换

b = str(12)
print(type(b))
int
c = (“45”)
print(type©)

可以转成假的Boolean类型,其余都是真 0 “” None [] {}

print (bool(“zhangsan”))

print (bool(""))

print (bool({}))

print (bool([]))

aa = (1, 2, 3)
bb[4, 5]
print()

字典的key必须是不可变的类型。

cc = {“zhangsan”;
“14”, 14:“lisi”, (1, 5):[5, 5, 5], [4, 5]:“zhongguo”}
print(type(cc))

集合的写法 不可包含可变的类型,集合可以看成把字典的key提取出来,组合在一起,利用集合这个特点可以去掉重复的元素。

dd = {4, 5, 7, 8, 10, }
print(dd)

字符串的拼接

name = “zhangsan”
age = 14
print(“我的名字叫%s,我的年龄是%i” % name, age)

猜你喜欢

转载自blog.csdn.net/weixin_36282409/article/details/83930653