python学习第一天

python 环境我们将用3.x以上的版本,因为官方说python3.x以上的版本才是未来的需要的语言,过渡版本2.6 、2.7 将不会在更新,也就是说不会有2.8这个版本,如果要继续用python,所有人都要讲业务搬到3.x以上。因为教学所以会混用,(实际上我不喜欢混用,谁让人家是老师呢)。

好了开始学习了:

1.首先用python 打出hello world!

进入python的编辑器

#python

>>> print ("hello world!")
hello world!

2.编写python脚本注意事项

#!/usr/bin/env python  #指定脚本的解释器类似shell 的#!/bin/bash 只有这样电脑才知道你是用什么语言编写的脚本

#缩进   

因为python的控制语句没有类似shell 的 结束语句例如:

#shell for语句

for n in {1..5} 

do

   echo $n

done 

#python for 语句

for n in {1..5}:

    echo $n

python通过严格的缩进来控制一个流程的结束

3.变量

可以重复调用,不用再脚本中写大量的代码,方便,效率快。

(1)变量定义的规则:

一、只能用字母、数字和下划线的任意组合

二、变量名不能用数字开头

三、以下的关键字不能用变量名字

'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if',
'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield'

(2)变量的赋值

>>> name = "zzn"  #如果赋值是字符串要加引号
>>> print name
zzn
>>> age = 21   #如果赋值是数字则不需要加引号
>>> print age
21

ps:我们可以通过id (变量名)可以看到赋值在内存的id

>>> id(name),id(age)

(39424776, 39424776)

4.用户的交互

ps:python2.x 的交互命令是raw_input();而python3.x的交互命令是input() 我们按2.x的说吧,3.x我觉得还有点遥远,外企基本上都有3.x

(1)我们写一个交互语句:你叫什么名字

>>> raw_input("what are you name:")
what are you name:zzn
'zzn'

编写脚本如下:

#vim test.py #!/usr/bin/env python name = raw_input("what are you name:") print name #python test.py what are you name:zzn zzn

这就是用户交互的一个小脚本

5.条件语句

(1)if 判断

例子: 

#vim test.py #!/usr/bin/env python name = raw_input("what are you name:") age = raw_input("what are you age:") if name == "zzn":    print name,age

  else:
     print ("wo cuo le!")

#python test.py what are you name:zzn what are you age:21 zzn 21 

(2)for 语句

例子:

#!/usr/bin/env python
sum = 10
for n in range(3):
    sums = int(input("please you input sum:"))
    if sums > sum:
        print ("you sum da")
    elif sums < sum:
        print ("you sum xiao")  
    else:
        print ("bingo")
        break #跳出整个循环
(3)while 语句
#!/usr/bin/env python
sum = 10
cai = 0
while cai < 3:
    print ("cai",cai)
    sums = int(input("please you input sum:"))
    if sums > sum:
        print ("you sum da")
    elif sums < sum:
        print ("you sum xiao")
    #elif sums == 10:
    else:
        print ("bingo")
        break
    cai += 1
else:
    print("To many option")

6.数据类型

(1)数字

int(整形) 如果你是32位系统 int的取值范围就是-2**31:2**31-1 :-2147483648?2147483647 64位系统也是亦然 

long(长整形) 理论上取值范围是不限制的

float(浮点型)就是小数点

(2)布尔值

真或假 1或0

(3)字符串

”hello world“

一.字符串拼接输出

#vim test.py

#!/usr/bin/env python
name = raw_input("what are you name:")
age = raw_input("what are you age:")
sex = raw_input("what are you sex:")
job = raw_input("what are you job:")

print ("ni yao cha kan: "+ name +" name:"+ name +" age:"+ age +" sex:"+ sex +" job:"+ job +" ")

#python test.py

what are you name:zzn
what are you age:24
what are you sex:n
what are you job:IT
ni yao cha kan: zzn
name:zzn
age:24
sex:n
job:IT

二.格式化输出

#vim test.py

#!/usr/bin/env python
name = raw_input("what are you name:")
age = raw_input("what are you age:")
sex = raw_input("what are you sex:")
job = raw_input("what are you job:")

msg = """
ni yao cha kan:%s
name:%s
age:%s
sex:%s
job:%s
""" %(name,name,age,sex,job)
print msg

#python test.py

ni yao cha kan:zzn
name:zzn
age:21
sex:n
job:IT

ps:字符串常用的功能:

@移除空白或移除指定字符串

#vim test1.py

#!/usr/bin/env python
name = raw_input("what are you name:").strip("z") #移除指定字符串"z"
age = raw_input("what are you age:").strip()
print name,age

#python test1.py

what are you name:zzn 
what are you age: 21 #21前面是有空格 但是输出却没有空格
n 21

7.列表

dir(list)查看列表能用的参数 help(list)查看详细的帮助
'append'(添加), 'count'(统计), 'extend'(迭代器), 'index'(索引), 'insert'(插入), 'pop'(删除), 'remove'(指定删除), 'reverse'(反转), 'sort'(排序)
 
练习:
>>> list = ['zzz','xxx','ccc']
>>> list
['zzz', 'xxx', 'ccc']
通过索引查看值
>>> list[1]
'xxx'
添加值到列表最后
>>> list.append("eee")
>>> list
['zzz', 'xxx', 'ccc', 'eee']
>>> list.append("ccc")
>>> list
['zzz', 'xxx', 'ccc', 'eee', 'ccc']
查看某个值的索引
>>> list.index("ccc")
2
统计某个值有几个
>>> list.count("ccc")
2
插入到指定位置
>>> list.insert(2,"rrr")
>>> list
['zzz', 'xxx', 'rrr', 'ccc', 'eee', 'ccc']
删除列表最后一个值
>>> list.pop()     
'ccc'
>>> list
['zzz', 'xxx', 'rrr', 'ccc', 'eee']
删除指定值
>>> list.remove("rrr")
>>> list
['zzz', 'xxx', 'ccc', 'eee']
把所有的值反转过来
>>> list.reverse()
>>> list
['eee', 'ccc', 'xxx', 'zzz']
>>> list.append("!")
>>> list.append("@")
>>> list.append("#")
>>> list.append("$")
>>> list.append(11) 
>>> list.append(14)
>>> list.append(19)
排序  优先数字 接着通用符  接着按字母顺序排序 python2.x和3.x有不同  3.x  字符和数字不能一起排序
>>> list.sort()
>>> list
[11, 14, 19, '!', '#', '$', '@', 'ccc', 'eee', 'xxx', 'zzz']
 
扩展列表
>>> list.extend("eee")
>>> list
[11, 14, 19, '!', '#', '@', 'ccc', 'eee', 'xxx', 'zzz', 'e', 'e', 'e']
如果超过5000个值甚至更多如何把某个值在列表中全部删除
>>> list.append("$")
>>> list.append("$")
>>> list.append("$")
>>> for n in range(list.count('$')):
...   list.remove("$")
... 
>>> list
[11, 14, 19, '!', '#', '@', 'ccc', 'eee', 'xxx', 'zzz']
 
切片
>>> d = range(10)
>>> d
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
打印出前五个
>>> d[0:5]
[0, 1, 2, 3, 4]
偶数
>>> d[0:5:2]
[0, 2, 4]
切最后两个
>>> d[-1:]   
[49]
>>> d[-2:]
[48, 49]
包含  #可以看这个值是否在这个列表中
>>> 20 in d
True
 
8.元组
定义之后不能修改
元组列表相互转换
>>> a
[1, 2, 3, 4, 5, 'd', 'b']
>>> type(a)
<class 'list'>
>>> b = (1,2,3,4,5,6)
>>> b
(1, 2, 3, 4, 5, 6)
>>> type(b)
<class 'tuple'>
>>> tuple(a)
(1, 2, 3, 4, 5, 'd', 'b')
 
9.运算符
通过取模可以在前端设置颜色 
and  和  
or  或
not  非
成员运算符 in  not in
>>> "zzn" in name 
True
>>> "vvv" in name
False
>>> "vvv" not in name
True
身份运算符 is   is not
>>> x = 1
>>> x is 1
True
>>> 6 is x
False
>>> 6 is not x
True

 10.文件操作

文件的三种模式
file('test.txt', 'r') r 仅读 w 仅写  a 追加  w+  可读可写
 
没有这个文件 用读的模式会报错
>>> file('test.txt', 'r')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'test.txt'
>>> 
写文件
>>> file('test.txt', 'w') 
<open file 'test.txt', mode 'w' at 0x7ff3d27e6d20>
>>> file('test.txt', 'w').write('df')
对文件定义一个变量
>>> f = file('test.txt', 'w')             
>>> f.write('the good is very ')
关闭
>>> f.close()
# cat test
how are you 
读文件
>>> f = file('test', 'r')
>>> f.read()
'how are you '
 
这是我第一次写博客,如果写的不好或是大家有什么建议,可以评论,我会改正的!
原文地址:https://www.cnblogs.com/nansenblog/p/5085440.html