MOOC的Python笔记(二)python数据类型与输入输出

来源:互联网 发布:神通数据库 ceil 编辑:程序博客网 时间:2024/05/16 17:05

python的输入是使用input函数,<变量>=input(<提示性文字>)

例如:val=input ("please input a temperature like 32C :")

界面输出

please input a temperature like 32C :

等待你的输入,你输入的东西会赋值给val,python是脚本语言,所以变量都是弱类型的。


但是弱类型的语言也是有数据类型的,如下

1、字符串
2、布尔类型
3、数字
4、列表
5、元组
6、字典
7、日期

(4~7这篇博文暂时不详细说,后面在用一篇博文说)

1、字符串(默认编码方式为 UTF-8)
如何在Python中使用字符串
a、使用单引号 '
str='this is string'

b、使用双引号 "
双引号中的字符串与单引号中的字符串用法完全相同
str="this is string"

c、使用三引号 '''
利用三引号,表示多行的字符串,可以在三引号中自由的使用单引号和双引号,例如:
str='''this is string
this is pythod string
this is string'''

2、布尔类型
False、True

3、数字
包括整数、浮点数。
a=1
b=2.3


删除一个变量使用 del
例如:
del a

python的输出是使用print格式化输出
格式化输出需要用%来做变量的占位符


字符串 %s
整型 %d
浮点型 %f
浮点型 %.2f 指定保留小数点后2位数


例如:
print("hello world")#输出 hello world
print("%s" %("hello world"))#输出 hello world

h="hello world"

print(h)#输出 hello world


f=2.33
print("Converted temperature is %f F"%f)
print("Converted temperature is %.2f F"%f)
输出
Converted temperature is 2.330000F
Converted temperature is 2.33F

0 0
原创粉丝点击