一、变量
1.定义变量的格式
变量名 = 变量值
定义变量的注意事项: 变量名称不能以数字或是Python的关键字开头。
#!/usr/bin/python3
x = 10
y = 'hello world'
print(x)
print(y)
执行结果
[root@node5 data]# ./test.py
10
hello world
二、数据类型
1.数字
(1)整数(int)
>>> x = 10
>>> type(x)
<class 'int'>
(2)浮点型(float)
>>> x = 3.14
>>> type(x)
<class 'float'>
2.字符串
字符串定义的时候要用单引号或者双引号引用
>>> x = 'hello world'
>>> type(x)
<class 'str'>
3.布尔型
在使用时,首字母必须大写
(1)True
(2)Flase
#!/usr/bin/python3
print(3 > 2)
print(2 > 3)
[root@node5 data]# ./test.py
True
False
三、print用法
#!/usr/bin/python3
print('hello','world','zhangsan')
print('lisi','wangwu')
[root@node5 data]# ./test.py
hello world zhangsan
lisi wangwu
print的sep参数: 是将多个参数用空格分割 改变为用-分割
print的end参数: 是将print换行符换位制表符\t
#!/usr/bin/python3
print('hello','world','zhangsan',sep='-',end='\t')
print('lisi','wangwu')
[root@node5 data]# ./test.py
hello-world-zhangsan lisi wangwu
四、进制的表示方法
1.二进制(0b)
#!/usr/bin/python3
x = 0b0101
print(x)
2.八进制(0o)
#!/usr/bin/python3
x = 0o34
print(x)
3.十六进制(0x)
#!/usr/bin/python3
x = 0x46f
print(x)
五、运算符
1.常规用法
在python3中 整数除以整数 得到的是浮点数,这点要注意
>>> 9 / 2
4.5
整除
>>> 10 // 3
3
幂运算
>>> 2 ** 10
1024
2.运算符号的特殊用法:
字符串 + 字符串
#!/usr/bin/python3
print('hello' + 'world')
[root@node5 data]# ./test.py
helloworld
字符串 *
[root@node5 data]# cat test.py
#!/usr/bin/python3
print('hello' * 10)
print('#' * 20)
[root@node5 data]# ./test.py
hellohellohellohellohellohellohellohellohellohello
####################