(32) python基础知识

ctrl + r  ##查找命令
一、python安装
 1.下载安装包Python-3.6.4.tgz

   官网www.python.org


 
 2.解压安装包

  tar xf Python-3.6.4.tgz -C /opt/



 3.编译  ##注意目录/opt/Python-3.6.4
  ./configure --prefix=/usr/local/python3  ##指定目录

  注意:编译会报错,缺少编译器C compials,安装gcc即可



 4.检测并安装
  ./configure --prefix=/usr/local/python3
  make && make install

  注意:报错缺少zlib,安装zlib-devel.x86_64开发包即可

(注:此图未显示安装zlib,自行安装)



 5.使用python(建立软链接)  ##即可使用python3

  ln -s /usr/local/python3/bin/python3 /usr/bin/python3

(建立软链接)



二、python编码格式
  ASCII:1字节=8bit,2**8-1=255
  Unicode:2字节=16bit,2**16-1=65535

  utf-8:可变字节编码,英文:1字节,中文3字节



 1.python3
[root@localhost mnt]# cat hello.py
#!/usr/bin/python3
print("hello world")

print("hello python")

[root@localhost mnt]# python3 hello.py

hello world

hello 朋友们


 ##注意使用当使用python2时,需要添加编码格式(coding:utf-8),否则报错!!!
[root@localhost mnt]# cat hello-python2.py
#!/usr/bin/python
print("hello")
print("hello 编程")
[root@localhost mnt]# python hello-python2.py
  File "hello-python2.py", line 3

SyntaxError(语法错误): Non-ASCII character '\xe6' in file hello-python2.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details



[root@localhost mnt]# cat hello-python2.py
#!/usr/bin/python
#coding:utf-8   ##编码格式
print("hello ")

print("hello 编程")


[root@localhost mnt]# python hello-python2.py
hello world

hello 朋友们

(添加编码格式后运行正常)


 2.python环境实现自动补全
  安装ipython

  进入ipython模式

cd /home/kiosk/Desktop/python/ipython(绝对路径)


In [3]: welcomepython=520

In [4]: print w
%%writefile    %who_ls        welcomepython  with           

%who           %whos          while




 3.pycharm

  下载 pycharm-community-2017.1.4.tar.gz


  解压(指定目录) tar xf pycharm-community-2017.1.4.tar.gz -C /opt/


  切换目录运行即可
    cd /opt/pycharm-community-2017.1.4/bin/

    ./pycharm.sh


  配置文件 ~/.PyCharmCE2017.1


4.配置块注释  Python Script

#!/usr/bin/python3
"""
Name=${NAME}.py
Auther=Rick

Date=${YEAR}-${MONTH}-${DAY}-${HOUR}-${MINUTE}

Desc=

"""


 快捷键
  Alt + Insert:新建文件/目录
  ctrl + alt + s:python配置
  ctrl + shift + F10:执行
  ctrl + d :快速复制
  ctrl + alt + l :调整格式

三、输入与输出
 1.常用格式
 %s:字符串
 %d:整型数
 %.3d:3位整型数,不够位数用0代替
 %f:浮点数
 %.2f%%:显示2位小数
 %%:转译%

 ctrl + / :批量注释



 2.数据类型

 

(1) int:整型    

  aInt = 100

  print(aInt,type(aInt))

 


(2)float:浮点型 

abc=1.4e3
print(abc,type(abc))

 


(3)complex:复数型

abc=4j+1
print(abc,type(abc))


(4)bool:布尔型 

 bool = True

 print(bool,type(bool))



 bool = bool(1)

 print(bool,type(bool))



 注意:强制转换

 str():字符串

 int() :整型     

 float() :浮点型            

 bool() :布尔型    除0之外,全是true

 


 3.python2.7与3.6差异  
 python3
   1.没有长整型
   2.input():只识别字符串
   3. 5/2=2.5
 python2.7
   1.input():只识别整型数
   2.raw_input():转换为字符串
In [3]: a=input("Name:")
Name:hello
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-8edc87a57f4b> in <module>()
----> 1 a=input("Name:")

<string> in <module>()

NameError: name 'hello' is not defined

   3. 5/2=2需要i加载division函数
     from __future__ import division  ##注意双下划线

      5/2=2.5



四、if语句

 1.打印学生成绩
name = input("学生名字:")
math = float(input("数学:"))
eng = float(input("英语:"))
test = float(input("综合:"))
ave = float((math+eng+test)/3)
if 90<ave<=100:
    level='A'
elif ave>=80:
    level='B'
elif ave>=70:
    level='C'
else:
    level='D'

print("学生%s,平均分为%.2f,等级为%s" %(name,ave,level))



 2.三元运算符
a = 100
b = 89

print(a if a > b else b)



 3.




猜你喜欢

转载自blog.csdn.net/qq_41869566/article/details/80505436