python基础和数据类型(01,02)

##1.www.tiobe.org        ##计算机开发年度大奖

##python的版本特性:
#python2.7最新,2版本将不在作后续开发
#python3推出    与2特性一样,但是版本代码不兼容

##python应用的领域:
#youtube google NASA 豆瓣

#python流行原因
##python之所以流行,是因为他可以利用很多第三方库
##python的移植性很强,兼容性很强

##python的优点
#简单
#优雅
#明确


##python的缺点
#C是描述型,编译型,速度快
#python是解释型语言,速度比C慢


##python应用的方向
#自动化运维---python运维工程师
#数据分析--Hadoop(>TB) Map-Reduce, 10G(matlab,R)
#AI,机器人
#
#
#
#
################################################
########    python        ################
################################################
#1.安装
在配置好yum源的情况下执行
yum install python -y

#2.python的基本操作
python -V        ##查看python版本
python --version    ##查看python版本

[root@python ~]# python    ##进入python交互界面
Python 2.7.5 (default, Feb 11 2014, 07:46:25)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello world"    ##执行动作
hello world
>>> exit ()        ##退出,也可以安CTRL+D


[root@python ~]# python    
Python 2.7.5 (default, Feb 11 2014, 07:46:25)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1        ##定义变量
>>> print a        ##显示变量
1
>>> exit ()        ##退出

[root@python ~]# python    ##再次进入
Python 2.7.5 (default, Feb 11 2014, 07:46:25)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print a        ##可以但到上次交互界面定义的变量值是没有被保存的
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> exit ()


###脚本的方式执行动作
vim hello.py
#!/usr/bin/python        ##指定脚本解释器
a = 1
print a

chmod +x hello.py
[root@python ~]# ./hello.py
1


#版本问题的解决

vim hello.py
#!/usr/bin/env python        ##当我们在拥有不同版本python中执行python时这样的定义解释器可以解决版本问题
a = 1
print a

chmod +x hello.py
[root@python ~]# ./hello.py
1


#字符问题
vim hello.py
#!/usr/bin/env python
a = "你好"        ##当脚本中出现中文时是无法执行的
print a


ot@python ~]# ./hello.py ##脚本执行报错
  File "./hello.py", line 2
SyntaxError: Non-ASCII character '\xe4' in file ./hello.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details


vim hello.py
#!/usr/bin/env python
#指定编码,以下方式任选一
#coding:utf-8
#coding=utf-8
#encoding:utf-8
#encoding=utf-8
a = "你好"
print a

chmod +x hello.py
[root@python ~]# ./hello.py
你好


##字符编码问题
#定义:
#GB2312 中文-----地址------图片
#UTF-8:可变长 ,如果英文 a----->1byte,中文 你 ----->3byte
#ASCII:1个字节=8,中文问题(佛略伊曼)
#Unicode:中文出现  2字节=16 2**16=65536 ,a ----> 2byte

#在内存读取:Unicode
#硬盘存储:utf-8
#字符编码:encode :Unicod ---->  utf-8
#字符解码:decode :utf-8 ----> Unicod


####示例:
#指定字符编码

[root@python ~]# python
Python 2.7.5 (default, Feb 11 2014, 07:46:25)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = '你好'
>>> type(a)    ##查看字符类型
<type 'str'>    ##字符串(utf-8)
>>> a = u'你好' ##定义a的字符串为unicode
>>> type(a)    
<type 'unicode'>
>>> exit()

##字符编码与解码
#当在内存中处理数据是,用Unicode在内存中处理方便,但是Unicode在编码在存储到硬盘中时会比UTF-8占用磁盘空间多
#所以在存储到硬盘时把Unicode编码成UTF-8,在从硬盘把代码调回内存中使用时需要把UTF-8解码成Unicode

[root@python ~]# python
Python 2.7.5 (default, Feb 11 2014, 07:46:25)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> TYPE_UNICODE = u'你好'    ##定义变量为unicode,内存中存储方便
>>> type(TYPE_UNICODE)        ##查看字符类型
<type 'unicode'>
>>> print TYPE_UNICODE
你好
>>> TYPE_UTF8 = TYPE_UNICODE.encode('utf-8')    ##在存储到硬盘时需要编码为utf8为节省磁盘空间
>>> type(TYPE_UNF8)
<type 'str'>                    
>>> print TYPE_UTF8
你好
>>> TYPE_UNICODE1 = TYPE_UTF8.decode('utf-8')    ##从磁盘中调出代码需要解码成Unicode到内存中调用
>>> type(TYPE_UNICODE1)
<type 'unicode'>
>>> print TYPE_UNICODE1
你好

>>> exit()

下载ipython包

cd ipython/
yum install * -y
ipython   ##一次定义后下次使用可以自动补齐



##python解释器种类
#cpython
#ipython:cpython高配
#Jpython
#IronPython
#PYPY



##编辑器
#vim
#atom------>github
#pycharm
#markdown
#git    svn
#记事本:0xefbbbf<16>
#
#

############pycharm#####
#添加文件头

!/usr/bin/env python
#coding:utf-8
_author_ = "timinglee"
'''    
#author:timinglee
#file:lll.py
#mail:timinglee@gmail.com
#DATE:6/25/1712:27 PM
#desc
'''

#######python的编程代码风格###########
#C的代码风格
#!include <stdio.h>
void hello(){
    print ("hello!\n");
    }
void main(){
        hello();
}
##我们可以但到,在C中写代码缩进是不做要求的,




##1.python在定义函数的时候时必须保持缩进格式的
##代码示例:
##代码一使用了正确缩进格式
#!/usr/bin/python
def hello():        ##def定义函数
        print "hello"
def main():
        hello()
        print "world"
main()

[root@python ~]# ./hello.py
hello
world

##代码二缩进格式不正确
#!/usr/bin/python
def hello():
        print "hello"
def main():
        hello()
print "world"        ##此行应该缩进但是没有
main()

[root@python ~]# ./hello.py
world        ##先执行了print "world",main中没有包含到
hello

##2.在python中"#"表示注释单行,'''...'''三个单引号表示注释多行
##脚本示例:
#示例一
#!/usr/bin/python
print "hello"
print "world"

[root@python ~]# ./hello.py
hello
world

#示例二
#!/usr/bin/python
#print "hello"
print "world"

[root@python ~]# ./hello.py
world

#示例三
#!/usr/bin/python
'''    ##注释整段
print "hello"
print "world"
'''

[root@python ~]# ./hello.py
##没有任何输出


###if else 语句##

x=1
def CHECK_NUM():
    if x>0:
        print "x 是正数"
    print x-10
    elif x<0:
        print "x负数"
    print x-10
    else:
        print "x是0"
CHECK_NUM()

##注意缩进格式

####交互试定义变量并判断###
#
#注意此脚本一定注意缩进!!!!
#
#!/usr/bin/env python
'''
#author:timinglee
#file:osmessage.py
#mail:timinglee@gmail.com
#DATE:6/25/173:19 PM
#desc
'''
NAME = raw_input('Please input your name: ')    ##raw_input定义字符变量
IP = raw_input('Please input your ipaddress: ')
USED_YEAR = input('How many year in used by you: ')    ## input定义数字变量
CPU = raw_input('Please input your CPU type: ')
MEM = raw_input('Please input your MEM size: ')
if USED_YEAR < 10:

    MESS = '''                    ##'''指定格式输出
                主机信息
        用户名称: %s                ##%s为字符占位符号
        主机ip  : %s
        使用期限: %d                ##%d为数字占位符号
        cpu信息 : %s
        内存信息: %s
        ''' %(NAME, IP, USED_YEAR, CPU, MEM)    ##让占位符号取值

    print MESS

else:
    print '您的计算机使用时间太长了!!!!'





####python变量的定义####
[root@python mnt]# python
Python 2.7.5 (default, Feb 11 2014, 07:46:25)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1
>>> id(a)
36293736
>>> b = 1
>>> id(b)
36293736
>>> name-lee = 1
  File "<stdin>", line 1
SyntaxError: can't assign to operator
>>> 1name = 1
  File "<stdin>", line 1
    1name = 1
        ^
SyntaxError: invalid syntax
>>> _name = 1



###PYTHON运算符##
#赋值运算
=
+=
-=
/=
%=
*=
**=
#算术运算
1+1
2-1
2*3
6/2
5/2
5.0/2
##关系运算
<
>
>=
<=
==
!=
##逻辑运算
and #逻辑与
or  #逻辑或
not #逻辑非
1   #本身代表true
0   #代表False

#脚本示例


#!/usr/bin/env python
# coding:utf-8
from __future__ import division
_author_ = "timinglee"
'''
#author:timinglee
#file:later.py
#mail:timinglee@gmail.com
#DATE:6/25/173:49 PM
#desc
'''
NUM1 = input('Please input first num: ')
NUM2 = input('please input secend num: ')
ACTION = raw_input('please input acton: ')
if ACTION == '+':
    print NUM1 + NUM2
elif ACTION == '-':
    print NUM1 - NUM2
elif ACTION == '*':
    print NUM1 * NUM2
elif ACTION == '/':
    print NUM1 / NUM2
else:
    print '请在动作提示后输入:+|-|*|/'


#####字符整形
#int      整形
#long    长整形
#float    浮点型
#
#\t    tab
#\n    换行
#索引
#切片 a[从那里:到那里:步长] a[0:4:1]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值