2、变量和数据类型

一、变量

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
####################

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值