从零开始学Python-day2,,Python--Da


Python--Day2

今日一句:距2017年仅剩三月不足,整理思路。希望来年按计划一步一步走下去!


学习要有定位,明确目标地去学习。---leaves


python01---基础语法


运维开发:

这个岗位最近已经越来越火,作为一个刚毕业没两年的小青年,职位规划与目标都是迷茫的。仅此记录一下日常点滴。


编程:这本身就很难 ===>需要用心学习的一个技能。


学习编程的最佳方法【最佳姿势】:

一、事前准备

1.准备电脑

2.找些简单的教程(如:如何用Flask开发网站,如何制作网站爬虫,如何打开文件)

=====>开始敲代码

3.学会Python后,才需要结合书(买书建议豆瓣8.0以上)

4.多练习,熟能生巧。==>书读百遍其义自见。


二、工具准备

python编程编辑器 vimsublimepycharm


三、练习代码

目标:把python当作工具应用到运维工作中去,成为运维界高手。


四、Python的优势

python语言的优势:易学、脚本,功能强大。缺点就是运行速度比java慢


Python语言:

1.上手简单;

2.功能健全;

3.语言生态系统完善,第三方库多;

4.有众多大公司成功的案例(豆瓣)。可以应用到脚本、游戏、图形化、Web爬虫等方方面面。


Python学习之路

一、(Linux系统下)输入python命令进入交互界面

[root@xiaowei~]#pythonPython2.7.9(default,Oct192016,13:38:05)[GCC4.4.720120313(RedHat4.4.7-17)]onlinux2Type"help","copyright","credits"or"license"formoreinformation.练习1输出helloworld>>>print"helloworld"helloworld练习2四则运算In[2]:2+4Out[2]:6In[3]:4*2Out[3]:8In[4]:print"hello"+"world"helloworld



二、Python的文件执行方法

(vim 编辑后使用python +文件名运行,但是文件名要以.py后缀结尾)

[root@xiaowei 01]# vim 01.py

print "hello world"

[root@xiaowei 01]# python 01.py

hello world

[root@xiaowei 01]#



三、Python基础知识

3.1.变量 用来存放数据

In [8]: x = "hello world "

In [9]: print x

hello world


3.2 语句===>一行代码

在首行加_*_ coding:utf-8 _*_ 后在.py程序中可以输入保存中文,防止不识别中文报错。

与用户交互的函数raw_input()和input()

raw_input()==>获取用户输入(主要用来测试,实际生产中更多的是从数据库中查询数据。)默认是字符串input()===>需要带原始的数据结构,即输入字符串要带引号。eg:In[2]:x=raw_input("adigit:")adigit:2In[3]:type(x)Out[3]:strIn[4]:a=input("adigit:")adigit2In[5]:type(a)Out[5]:int


3.3 python的数据类型

数字、字符串、布尔、列表(list)、字典(dict)、元组(tuple)

逻辑控制+++>根据情况不同执行不同代码

循环====>一直不停执行 while if until


3.4 四则运算

/===>整除%===>取余eg:In[6]:8/3Out[6]:2In[7]:7%3Out[7]:1

字符串的运算:==>字符串可以相乘和相加

单双引号没有区别,注意"\"为转义符

‘‘‘三重引号‘‘‘===>三重引号忽略所有格式,里边可以使用所有符号

eg:###转义符的使用In[10]:print‘I\‘mBob‘I‘mBob###字符串相加In[11]:In[8]:print"hello"+"world"helloworld###字符串相乘In[9]:print"hello"*4hellohellohellohello###三重引号用法In[11]:print‘‘‘...:HIBob!...:I‘mYaso....:say"Bay0"...:‘‘‘HIBob!I‘mYaso.say"Bay0"


3.5 字符串的格式化

print "hello " +x + "I am " + str(y) + "years old"。字符串的格式化主要为了解决这种输出繁琐且丑陋的方式

有两种方式格式化:(%)和(format)

eg:###不使用字符串格式化的丑陋拼接。In[23]:x="Leaves"In[24]:y=3In[26]:print"hello"+x+"Iam"+str(y)+"yearsold"helloLeavesIam3yearsold###使用%来格式化In[27]:print"hello%s,Iam%syearsold"%(x,y)helloLeaves,Iam3yearsold###使用format来格式化In[29]:print"hello{},Iam{}yearsold".format(x,y)helloLeaves,Iam3yearsold##字符串转换为数字类型In[31]:a=‘13‘In[32]:printint(a)13In[34]:printint(a)+417

技术分享


小练习:让用户输入两个数求平均值[root@xiaowei01]#cat02.pyx=raw_input("firstdigist:")y=raw_input("seconddigist:"print(int(x)+int(y))/2.0[root@xiaowei01]#[root@xiaowei01]#python02.pyfirstdigist:4seconddigist:54.5


3.6 流程控制 : 布尔值 True False

与 and

或 or

非 not


注意:if else 可嵌套(嵌套不要超过三层,以保证代码可读性)

伪代码流程控制

if true 还是False:

如果是True(False)执行这段代码

else :

执行False(True)这些行代码

eg:流程控制In[36]:if2>3:...:print"conditionisTrue"...:else:...:print"conditionisfalse"...:conditionisfalse###具体流程控制例子In[37]:name="wd"In[38]:age=22In[39]:ifname=="wd":...:ifage>14:...:print"youare%syearsold"%age...:else:...:print"Tooyoung"...:else:...:print"Youare{}".format(name)

3.7 循环 while for

while、for用来实现循环

跳出循环 break && continue 。

break : 中断终止循环

continue :跳过当前循环,循环没有终止

###break举例(跳出循环,中断并终止循环)In[57]:i=0In[58]:whileTrue:...:ifi>5:...:break...:printi...:i+=1...:012345###continue举例(跳过当前循环,循环没有终止)In[59]:arr=[‘C‘,‘js‘,‘python‘,‘js‘,‘css‘,‘js‘,‘html‘,‘node‘]In[61]:foriinarr:...:ifi=="js":...:continue...:printi...:Cpythoncsshtmlnode##小练习:用户输入数字,判断是不是闰年·如果是100倍数,要被400整除·被4整除·比如1900年不是闰年,2000、2004是闰年·如果不是闰年,提示信息,并继续输入[root@xiaowei01]#cat07.pywhileTrue:if(int(year)%400==0):print"runnian"breakelif(int(year)%100!=0)and(int(year)%4==0):print"runnian"breakelse:year=raw_input("pleaseinputyear:")[root@xiaowei01]#优化后代码如下:whileTrue:if((int(year)%100!=0)and(int(year)%4==0))or(int(year)%400==0):print"runnian"breakyear=raw_input("pleaseinputyear:")[root@xiaowei01]#



while循环伪代码

while 情况1:

代码会一直被执行,直到情况1为False

eg:In[40]:i=1In[41]:whilei<=20:...:printi...:i+=1...:1234567891011121314151617181920####让用户一直输入数字,如果输入的是0,终止程序,打印所有数字的和并求出平均值[root@xiaowei01]#cat03.pyx=raw_input("shurushuzi:")sum=0num=0.0whilex:print"x:"+xsum+=int(x)x=raw_input("shurushuzi:")num=num+1.0#printnumprintsum/numprintsum[root@xiaowei01]#[root@xiaowei01]#python03.pyshurushuzi:1x:1shurushuzi:2x:2shurushuzi:3x:3shurushuzi:2.06###小练习4:存10000块钱,年利率是3.25%。求多少年后存款可以翻番[root@xiaowei01]#cat05.pyn=10000time=1whilen<20000:n=(1+0.0325)*ntime+=1printtime,n[root@xiaowei01]#python05.py2320210.6986788


for循环 ==>专门针对list 列表 dict字典等复杂数据结构的

小练习:遍历列表In[46]:arr=[‘one‘,2,‘333‘,‘four‘]In[47]:foriinarr:...:printione2333four


3.8 初识列表和字典

list 列表 [] ====>有顺序的

dict字典 {} ===> 没有顺序的 ,结构为key -value 形式(所有数据统计的思路都是这样)

d={‘name‘:"black"}n[1]:d={‘name‘:"black",‘age‘:12}In[2]:printd[‘name‘]blackIn[3]:d[‘age‘]=50#修改值In[4]:dOut[4]:{‘age‘:50,‘name‘:‘black‘}In[5]:d[‘newKey‘]="newValue"#新增值In[6]:dOut[6]:{‘age‘:50,‘name‘:‘black‘,‘newKey‘:‘newValue‘}###小练习:所有数据统计的思路都是这样统计出列表中arr=[‘js‘,‘C‘,‘js‘,‘python‘,‘js‘,‘C‘,‘js‘,‘python‘,‘C‘,‘js‘,‘python‘,‘css‘,‘js‘,‘html‘,‘node‘]各个字符出现的次数。代码如下:In[7]:arr=[‘js‘,‘C‘,‘js‘,‘python‘,‘js‘,‘C‘,‘js‘,‘python‘,‘C‘,‘js‘,‘python‘,‘css‘,‘js‘,‘html‘,‘node‘]In[8]:count_d={}In[9]:foriinarr:...:ifinotincount_d:...:count_d[i]=1...:else:...:count_d[i]+=1In[10]:printcount_d{‘node‘:1,‘C‘:3,‘python‘:3,‘js‘:6,‘html‘:1,‘css‘:1}##小练习:作业作业:求一个序列最大的两个值。arr=[1,2,3,6,111,32,433,211,10002,4444,222]#!/usr/bin/pythonarr=[1,2,3,6,111,32,433,211,10002,4444,222]max1=0max2=0foriinarr:ifi>=max1:max2=max1max1=ielif(i<max1)and(i>=max2):max2=iprintmax1,max2[root@www~]#pythonzuoye.py100024444[root@www~]#



从零开始学Python-day2

评论关闭