转载请标明出处:
原文发布于:浅尝辄止,未尝不可的博客
https://blog.csdn.net/qq_31019565
Python教程(四)–变量以及类型、打印名片
变量以及类型
是用来存东西的,要用需先定义。
#定义了一个变量num ,num存储了数值100
num = 100
#长度 宽度
length_ = 60 #单位cm
width_ = 40 #单位cm
#第一次计算area 是定义,第二次计算则是修改area的值
area = length_ * width_
area = area - 10
打印名片
关于print 函数中%s 和%d的使用。可以简单记为,如果是整数就用%d,如果带小数点就用%s。
这里还用到了 input 函数。使用input来获取必要的信息。
#使用input获取必要的信息
name = input("please input your name:")
#打印名字
print("===============")
print("name:%s"%name)
print("===============")
以上为python3的用法。如果python2按照以上方式创建.py文件,则运行则会出现问题。python2中的raw_input 和 python3中的input的用法是一样的。
#使用input获取必要的信息
name = raw_input("please input your name:")
#打印名字
print("===============")
print("name:%s"%name)
print("===============")
ipython模式下,对比python2和python3中input的区别。
首先是python2的交互模式,输入1+4后,会自动计算结果 输出5。
[python@ubuntu Python]$ ipython
Python 2.7 (r27:82500, Feb 9 2012, 13:27:42)
Type "copyright", "credits" or "license" for more information.
IPython 2.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: a = input("please input:")
please input:1+4
In [2]: a
Out[2]: 5
其次是在python3的交互模式下的input,输入1+4 ,输出是‘1+4’,不会自动计算。
[python@ubuntu Python Python]$ ipython3
Python 3.3.2 (default, Sep 19 2013, 11:15:15)
Type "copyright", "credits" or "license" for more information.
IPython 4.2.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: a = input("please input:")
please input:1+4
In [2]: a
Out[2]: '1+4'
小结:以后以python3为主,直接使用input。