初学者遇到的问题以及解决方法
编辑以.py结尾的文件并用解释其python 解释该程序运行
1.代码书写错误
File "python.py", line 1
printt 'hello python'
^
SyntaxError: invalid syntax
2.写在同一行
File "python.py", line 1
print 'hello python'print 'hello python'
^
SyntaxError: invalid syntax
3.缩进错误
File "python.py", line 2
print 'hello python'
^
IndentationError: unexpected indent
4.加入中文
File "python.py", line 1
SyntaxError: Non-ASCII character '\xe7' in file python.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
[root@foundation19 python]# vim python.py
解决方法
#_*_coding:utf-8_*_
#的萨回到萨
#sassdsa的萨大赛的岁的的撒旦阿萨德萨撒旦阿斯
print 'hello python'
print 'hello python'
2.python的安装
ipython安装
安装ipython,需自行寻找ipython的rpm包以及它的相关包,安装其全部。
[root@station ipython]# ls
openpgm-5.2.122-2.el7.x86_64.rpm
python-ipython-3.2.1-1.el7.noarch.rpm
python-ipython-console-3.2.1-1.el7.noarch.rpm
python-ipython-gui-3.2.1-1.el7.noarch.rpm
python-jsonschema-2.3.0-1.el7.noarch.rpm
python-mistune-0.5.1-1.el7.x86_64.rpm
python-path-5.2-1.el7.noarch.rpm
python-pip-7.1.0-1.el7.noarch.rpm
python-pygments-1.4-9.el7.noarch.rpm
python-simplegeneric-0.8-7.el7.noarch.rpm
python-zmq-14.3.1-1.el7.x86_64.rpm
zeromq3-3.2.5-1.el7.x86_64.rpm
[root@station ipython]# yum install -y * ##安装
Pycharm的安装
从官网下载安装包
解压到/opt目录;
cd{dir/home}/bin;./pycharm.sh;
pycharm的设置信息保存在~/.PycharmCE2017.1;
charm &
3.数值类型
int,long,float,bool(True,False),complex
变量以及变量的赋值
# 凡购买减掉3元
# 每斤2.5元
price = 2.5
# 购买4.7斤
weight = 4.7
# much
money = price * weight
money = money - 3
print money
# str字符串
name = 'wangpeihua'
print name
age = 20 # int表示整形
print age
weight = 63.2 # 表示浮点数
print weight
gender = True # bool 表示布尔类型
print gender
# 输入水果的单价
price = float(raw_input('please input the price of fruit:'))
# 输入你需要的水果量
much = float(raw_input('please input how many the fruit you need:'))
# 输出总价格
money = price * much
print money
格式化输出
# 我的学号是000032
num = '00003123'
print '我的学号是%s'%num
# 输出水果的单价
price = 5.3
print '水果的单价是%.2f'%price
# 输出楼的高度
A = 2000
print '这座楼高达%05dm' %A
# 输出数据比例为10.00%
Bii = 0.1
print '输出%.2f%%' % (Bii * 100)