linghaiyan 发表于 2018-8-16 10:04:56

从零开始学Python-day2

  Python--Day2
  今日一句:距2017年仅剩三月不足,整理思路。希望来年按计划一步一步走下去!
  学习要有定位,明确目标地去学习。---leaves
  python01---基础语法
  运维开发:
  这个岗位最近已经越来越火,作为一个刚毕业没两年的小青年,职位规划与目标都是迷茫的。仅此记录一下日常点滴。
  编程:这本身就很难 ===>需要用心学习的一个技能。
  学习编程的最佳方法【最佳姿势】:
  一、事前准备
  1.准备电脑
  2.找些简单的教程(如:如何用Flask开发网站,如何制作网站爬虫,如何打开文件)
  =====>开始敲代码
  3.学会Python后,才需要结合书(买书建议豆瓣8.0以上)
  4.多练习,熟能生巧。==>书读百遍其义自见。
  二、工具准备
  python编程编辑器vim    sublime    pycharm
  三、练习代码
  目标:把python当作工具应用到运维工作中去,成为运维界高手。
  四、Python的优势
  python语言的优势:易学、脚本,功能强大。缺点就是运行速度比java慢
  Python语言:
  1.上手简单;
  2.功能健全;
  3.语言生态系统完善,第三方库多;
  4.有众多大公司成功的案例(豆瓣)。可以应用到脚本、游戏、图形化、Web爬虫等方方面面。
  Python学习之路
  一、(Linux系统下)输入python命令进入交互界面
# python  
Python 2.7.9 (default, Oct 19 2016, 13:38:05)
  
on linux2
  
Type "help", "copyright", "credits" or "license" for more information.
  

  
练习1   输出hello world
  
>>>print "hello world"
  
hello world
  

  
练习2   四则运算
  
In : 2 + 4
  
Out: 6
  
In : 4*2
  
Out: 8
  
In : print "hello" + "world"
  
helloworld
  二、Python的文件执行方法
  (vim 编辑后使用python +文件名运行,但是文件名要以.py后缀结尾)
  # vim 01.py
  print "hello world"
  # python 01.py
  hello world
  #
  三、Python基础知识
  3.1.变量用来存放数据
  In : x = "hello world "
  In : print x
  hello world
  3.2 语句    ===>一行代码
  在首行加_*_coding:utf-8 _*_ 后在.py程序中可以输入保存中文,防止不识别中文报错。
  与用户交互的函数raw_input()和input()
raw_input()==>获取用户输入(主要用来测试,实际生产中更多的是从数据库中查询数据。)默认是字符串  

  
input()         ===>需要带原始的数据结构,即输入字符串要带引号。
  

  
eg:
  
In : x = raw_input("a digit: ")
  
a digit: 2
  

  
In : type(x)
  
Out: str
  

  
In : a = input("a digit: ")
  
a digit 2
  

  
In : type(a)
  
Out: int
  3.3 python的数据类型
  数字、字符串、布尔、列表(list)、字典(dict)、元组(tuple)
  逻辑控制    +++>根据情况不同执行不同代码
  循环   ====>一直不停执行 whileif   until
  3.4 四则运算
    /===>整除  
    %===>取余
  
eg:
  
   In : 8/3
  
   Out: 2
  

  
   In : 7%3
  
   Out: 1
  字符串的运算:==>字符串可以相乘和相加
  单双引号没有区别,注意"\"为转义符
  '''三重引号'''===>三重引号忽略所有格式,里边可以使用所有符号
eg:  
###转义符的使用
  
In : print 'I\'m Bob'
  
I'm Bob
  
###字符串相加
  
In :
  
In : print "hello" + "world"
  
helloworld
  
###字符串相乘
  
In : print "hello " * 4
  
hello hello hello hello
  
###三重引号用法
  
In : print '''
  
    ...: HIBob!
  
    ...: I'm Yaso.
  
    ...: say"Bay0"
  
    ...: '''
  

  
HIBob!
  
I'm Yaso.
  
say"Bay0"
  3.5 字符串的格式化
  print "hello " +x+ "I am " + str(y) + "years old"。字符串的格式化主要为了解决这种输出繁琐且丑陋的方式
  有两种方式格式化:    (%)和(format)
eg:  
###不使用字符串格式化的丑陋拼接。
  
In : x = "Leaves"
  
In : y = 3
  
In : print "hello " +x+ "I am " + str(y) + "years old"
  
hello LeavesI am 3years old
  
###使用%来格式化
  
In : print "hello %s ,I am %s years old " %(x,y)
  
hello Leaves ,I am 3 years old
  
###使用format来格式化
  
In : print "hello {} ,I am {} years old ".format(x,y)
  
hello Leaves ,I am 3 years old
  
##字符串转换为数字类型
  
In : a = '13'
  
In : print int(a)
  
13
  
In : print int(a) + 4
  
17

小练习:让用户输入两个数求平均值  
# cat 02.py
  
x = raw_input("first digist: ")
  
y = raw_input("second digist: "
  
print (int(x)+int(y))/2.0
  
#
  
# python 02.py
  
first digist: 4
  
second digist: 5
  
4.5
  3.6 流程控制 : 布尔值 True False
  与and
  或or
  非not
  注意:ifelse 可嵌套(嵌套不要超过三层,以保证代码可读性)
  伪代码流程控制
  iftrue还是False:
  如果是True(False)执行这段代码
  else :
  执行False(True)这些行代码
eg:流程控制  
In : if 2 > 3 :
  
    ...:   print "condition isTrue"
  
    ...: else :
  
    ...:   print "condition is false"
  
    ...:
  
condition is false
  

  
###具体流程控制例子
  
In : name = "wd"
  
In : age = 22
  
In : if name == "wd":
  
...:   if age > 14:
  
...:         print "you are %s years old" % age
  
...:   else :
  
...:         print "Too young"
  
...: else :
  
...:   print "You are {}".format(name)
  3.7 循环while   for
  while、for用来实现循环
  跳出循环break && continue 。
  break : 中断终止循环
  continue :跳过当前循环,循环没有终止
###break举例(跳出循环,中断并终止循环)  
In : i = 0
  
In : while True:
  
    ...:   if i > 5:
  
    ...:         break
  
    ...:   print i
  
    ...:   i += 1
  
    ...:
  
0
  
1
  
2
  
3
  
4
  
5
  

  
###continue举例(跳过当前循环,循环没有终止)
  
In : arr = ['C','js','python','js','css','js','html', 'node']
  
In : for i in arr:
  
    ...:   if i == "js":
  
    ...:         continue
  
    ...:   print i
  
    ...:
  
C
  
python
  
css
  
html
  
node
  

  
##小练习:用户输入数字,判断是不是闰年
  
·如果是100倍数,要被400整除
  
·被4整除
  
·比如1900年不是闰年,2000、2004是闰年
  
·如果不是闰年,提示信息,并继续输入
  
# cat 07.py
  
while True:
  if (int(year) % 400 == 0):
  print "run nian "
  break
  elif (int(year) % 100 != 0) and (int(year) % 4 == 0) :
  print "run nian "
  break
  else :
  year = raw_input("pleaseinput year: ")
  
#
  
优化后代码如下:
  
while True:
  if ((int(year) % 100 != 0) and (int(year) % 4 == 0) ) or (int(year) % 400 == 0):
  print "run nian "
  break
  year = raw_input("pleaseinput year: ")
  
#
  while循环伪代码
  while 情况1:
  代码会一直被执行,直到情况1为False
eg:  
In : i = 1
  
In : while i 专门针对list列表dict字典等复杂数据结构的
小练习:遍历列表  
In : arr = ['one',2,'333','four']
  
In : for i in arr:
  
...:         print i
  
one
  
2
  
333
  
four
  3.8 初识列表和字典
  list   列表 [] ====>有顺序的
  dict字典 {} ===> 没有顺序的 ,结构为key -value形式(所有数据统计的思路都是这样)
d = {'name' :"black"}  
n : d = {'name':"black",'age': 12}
  
In : print d['name']
  
black
  
In : d['age'] = 50 #修改值
  
In : d
  
Out: {'age': 50, 'name': 'black'}
  
In : d['newKey'] = "newValue"#新增值
  
In : d
  
Out: {'age': 50, 'name': 'black', 'newKey': 'newValue'}
  

  

  

  
###小练习:所有数据统计的思路都是这样
  
统计出列表中arr =['js','C','js','python','js','C','js','python','C','js','python','css',\
  
'js','html','node' ]各个字符出现的次数。
  

  

  
代码如下:
  
In : arr = ['js','C','js','python','js','C','js','python','C','js','python','css','js','html','node' ]
  
In : count_d ={}
  
In : for i in arr:
  
   ...:   if i not in count_d:
  
   ...:         count_d = 1
  
   ...:   else :
  
   ...:         count_d+= 1
  
In : print count_d
  
{'node': 1, 'C': 3, 'python': 3, 'js': 6, 'html': 1, 'css': 1}
  

  
##小练习:作业
  
作业:求一个序列最大的两个值。arr =
  
#!/usr/bin/python
  
arr =
  
max1 = 0
  
max2 = 0
  

  
for i in arr:
  if i >= max1:
  max2 = max1
  max1 = i
  elif (i < max1) and (i >= max2):
  max2 = i
  
print max1, max2
  

  

  
# python zuoye.py
  
10002 4444
  
#


页: [1]
查看完整版本: 从零开始学Python-day2