【第一篇】Python基础,第一篇python,Python学习学习


Python学习

学习站点:https://www.shiyanlou.com/
1hello worldcode如下:
$ python [15:50:40]Python2.7.6(default,Mar222014,22:59:56)[GCC 4.8.2] on linux2Type"help","copyright","credits"or"license"for more information.>>>print(‘hello world‘);hello world>>>

运行文本编辑器中的python:首先建立个py结尾的文件,里面代码如下:
1 print(‘hello world‘);

1 shiyanlou:~/ $ python hello.py [15:55:05]2 python: can‘t open file ‘hello.py‘: [Errno 2] No such file or directory3 shiyanlou:~/ $ cd desktop [15:55:15]4 cd:cd:13: \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55: desktop5 shiyanlou:~/ $ cd Desktop [15:55:30]6 shiyanlou:Desktop/ $ python hello.py [15:55:42]7 hello world8 shiyanlou:Desktop/ $

脚本:修改hello.py中的文件:
1 #!/usr/bin/env python2 print(‘hello world‘);

运行:chmod 755 hello.py 为修改权限为可执行脚本
1 shiyanlou:Desktop/ $ chmod 755 hello.py [16:02:17]2 shiyanlou:Desktop/ $ ./hello.py [16:02:40]3 hello world4 shiyanlou:Desktop/ $ [16:02:53]

基本数据类型python的变量不需要声明:变量a ,值为10,类型integer.数据类型由python决定。
>>> a =100>>>print a>>>print type(a)

type为查询变量的类型。python的常见数据类型:整形 a =100浮点型 a = 1.3真假值 a = True字符串 a = ‘hello world’序列:一组有顺序的元素集合序列包含0到多个元素。基本数据类型壳子作为序列的元素。元素还可以是另外一个序列。序列的分类:tuple 定值表list 表
s1 =(2,1.3,‘love‘,5.6,9,12,False)# s1是一个tuples2 =[True,5,‘smile‘]# s2是一个listprint s1,type(s1)print s2,type(s2)

tuple的各个元素不可再变更,而list的各个元素可以再变更。一个序列作为另一个序列的元素:
1 s3 =[1,[3,4,5]]

空序列:
1 s4 =[]

元素的引用:
序列元素的下标从0开始:
1 print s1[0]2 print s2[2]3 print s3[1][2]

由于list的元素可变更,你可以对list的某个元素赋值:
1 s2[1]=3.02 print s2

如果你对tuple做这样的操作,会得到错误提示。所以,可以看到,序列的引用通过s[int]实现,(int为下标)。其他方式的引用:基本样式 [下限:上限:步长]
print s1[:5]# 从开始到下标4 (下标5的元素 不包括在内)print s1[2:]# 从下标2到最后print s1[0:5:2]# 从下标0到下标4 (下标5不包括在内),每隔2取一个元素 (下标为0,2,4的元素)print s1[2:0:-1]# 从下标2到下标1

尾部元素引用:
print s1[-1]# 序列最后一个元素print s1[-3]# 序列倒数第三个元素

字符串是元祖:
1 str =‘abcdef‘2 print str[2:4]

结果为:
shiyanlou:~/ $ cd Desktop[19:12:53]shiyanlou:Desktop/ $ python hello.py [19:12:59]cd

运算:包括加减乘除,乘方,求余
1 print1+92 print1.3-43 print3*44 print4.5/1.55 print3**26 print10%3

结果为:
shiyanlou:Desktop/ $ python hello.py [19:19:43]10-2.7123.091

判断 :
1 print5==62 print5!=63 print3<3,3<=34 print4>5,4>=05 print5in[1,2,3]

结果为:
shiyanlou:Desktop/ $ python hello.py [19:20:26]FalseTrueFalseTrueFalseTrueFalse

逻辑运算:包括,and,or ,not(注意python 里面 true和false第一个字母必须大写)
1 print True and False,True and True2 print True or False3 print not True

结果为:
shiyanlou:Desktop/ $ python hello.py [20:59:38]FalseTrueTrueFalse

if语句:
1 i =12 if i >0:3 print‘x>0‘

结果为:
shiyanlou:Desktop/ $ python hello.py [21:05:24]x>0

复杂的if语句:elif 相当于java中的else if另外逻辑判断没有花括号
 1 i =1 2 if i >0: 3 print‘positive i‘ 4 i = i+1 5 elif i ==0: 6 print‘i is 0‘ 7 i = i*10 8 else: 9 print‘negative i‘10 i = i-111 print‘new i : ‘,i

结果:
shiyanlou:Desktop/ $ python hello.py [21:07:41]positive inew i :2



【第一篇】Python基础

相关内容

    暂无相关文章

评论关闭